Skip to content

Commit

Permalink
refactor: use method signature for life cycle methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue committed Jul 26, 2020
1 parent 5446100 commit 7e864d0
Show file tree
Hide file tree
Showing 20 changed files with 76 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .changeset/cool-countries-pay.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
---

Switch to using method signatures for extension class methods as discussed in #360.

Remove the first parameter `extensions` from method `onCreate`, `onView` and `onDestroy`.
6 changes: 3 additions & 3 deletions packages/@remirror/core/src/builtins/attributes-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExtensionPriority } from '@remirror/core-constants';
import { bool, object } from '@remirror/core-helpers';
import { AttributesWithClass } from '@remirror/core-types';

import { CreateLifecycleMethod, PlainExtension } from '../extension';
import { PlainExtension } from '../extension';
import { AnyCombinedUnion } from '../preset';

/**
Expand Down Expand Up @@ -32,10 +32,10 @@ export class AttributesExtension extends PlainExtension {
*
* @internal
*/
onCreate: CreateLifecycleMethod = () => {
onCreate() {
this.transformAttributes();
this.store.setExtensionStore('updateAttributes', this.updateAttributes);
};
}

private readonly updateAttributes = (triggerUpdate = true) => {
this.transformAttributes();
Expand Down
20 changes: 10 additions & 10 deletions packages/@remirror/core/src/builtins/commands-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import {
AnyFunction,
CommandFunction,
DispatchFunction,
EditorSchema,
EmptyShape,
FromToParameter,
ProsemirrorAttributes,
Transaction,
} from '@remirror/core-types';
import { EditorView } from '@remirror/pm/view';

import {
AnyExtension,
ChainedCommandRunParameter,
ChainedFromExtensions,
CommandsFromExtensions,
CreateLifecycleMethod,
PlainExtension,
ViewLifecycleMethod,
} from '../extension';
import { throwIfNameNotUnique } from '../helpers';
import { AnyCombinedUnion, ChainedFromCombined, CommandsFromCombined } from '../preset';
Expand All @@ -26,7 +26,7 @@ import {
CreatePluginReturn,
ExtensionCommandFunction,
ExtensionCommandReturn,
StateUpdateLifecycleMethod,
StateUpdateLifecycleParameter,
} from '../types';

/**
Expand Down Expand Up @@ -61,7 +61,7 @@ export class CommandsExtension extends PlainExtension {

#transaction?: Transaction;

onCreate: CreateLifecycleMethod = () => {
onCreate() {
const { setExtensionStore, setStoreKey } = this.store;

// Add the commands to the extension store
Expand All @@ -74,9 +74,9 @@ export class CommandsExtension extends PlainExtension {

// Enable retrieval of the current transaction.
setExtensionStore('getTransaction', () => this.transaction);
};
}

onView: ViewLifecycleMethod = (extensions, view) => {
onView(view: EditorView<EditorSchema>) {
const commands: Record<string, CommandShape> = object();
const names = new Set<string>();
const chained: Record<string, any> & ChainedCommandRunParameter = object();
Expand All @@ -85,7 +85,7 @@ export class CommandsExtension extends PlainExtension {
{ command: AnyFunction; isEnabled: AnyFunction; name: string }
> = object();

for (const extension of extensions) {
for (const extension of this.store.extensions) {
if (!extension.createCommands) {
continue;
}
Expand Down Expand Up @@ -120,14 +120,14 @@ export class CommandsExtension extends PlainExtension {

setStoreKey('commands', commands);
setStoreKey('chain', chained as any);
};
}

/**
* Update the cached transaction whenever the state is updated.
*/
onStateUpdate: StateUpdateLifecycleMethod = ({ state }) => {
onStateUpdate({ state }: StateUpdateLifecycleParameter) {
this.#transaction = state.tr;
};
}

/**
* Create the default commands available to all extensions.
Expand Down
12 changes: 5 additions & 7 deletions packages/@remirror/core/src/builtins/helpers-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import { isMarkActive, isNodeActive } from '@remirror/core-utils';

import {
AnyExtension,
CreateLifecycleMethod,
HelpersFromExtensions,
isMarkExtension,
isNodeExtension,
PlainExtension,
ViewLifecycleMethod,
} from '../extension';
import { throwIfNameNotUnique } from '../helpers';
import { ActiveFromCombined, AnyCombinedUnion, HelpersFromCombined } from '../preset';
Expand Down Expand Up @@ -40,25 +38,25 @@ export class HelpersExtension extends PlainExtension {
* Provide a method with access to the helpers for use in commands and
* helpers.
*/
onCreate: CreateLifecycleMethod = () => {
onCreate() {
this.store.setExtensionStore('getHelpers', () => {
const helpers = this.store.getStoreKey('helpers');
invariant(helpers, { code: ErrorConstant.HELPERS_CALLED_IN_OUTER_SCOPE });

return helpers as any;
});
};
}

/**
* Helpers are only available once the view has been added to
* `RemirrorManager`.
*/
onView: ViewLifecycleMethod = (extensions) => {
onView() {
const helpers: Record<string, AnyFunction> = object();
const active: Record<string, AnyFunction> = object();
const names = new Set<string>();

for (const extension of extensions) {
for (const extension of this.store.extensions) {
if (isNodeExtension(extension)) {
active[extension.name] = (attrs?: ProsemirrorAttributes) => {
return isNodeActive({ state: this.store.getState(), type: extension.type, attrs });
Expand All @@ -85,7 +83,7 @@ export class HelpersExtension extends PlainExtension {

this.store.setStoreKey('active', active);
this.store.setStoreKey('helpers', helpers);
};
}

createHelpers = () => {
return {};
Expand Down
6 changes: 3 additions & 3 deletions packages/@remirror/core/src/builtins/input-rules-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExtensionPriority } from '@remirror/core-constants';
import { InputRule, inputRules } from '@remirror/pm/inputrules';
import { Plugin } from '@remirror/pm/state';

import { CreateLifecycleMethod, PlainExtension } from '../extension';
import { PlainExtension } from '../extension';

/**
* This extension allows others extension to add the `createInputRules` method
Expand All @@ -27,12 +27,12 @@ export class InputRulesExtension extends PlainExtension {
/**
* Ensure that all ssr transformers are run.
*/
onCreate: CreateLifecycleMethod = () => {
onCreate() {
this.store.setExtensionStore('rebuildInputRules', this.rebuildInputRules);
this.loopExtensions();

this.store.addPlugins(this.inputRulesPlugin);
};
}

private loopExtensions() {
const rules: InputRule[] = [];
Expand Down
6 changes: 3 additions & 3 deletions packages/@remirror/core/src/builtins/keymap-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { KeyBindings, ProsemirrorPlugin } from '@remirror/core-types';
import { mergeProsemirrorKeyBindings } from '@remirror/core-utils';
import { keymap } from '@remirror/pm/keymap';

import { CreateLifecycleMethod, PlainExtension } from '../extension';
import { PlainExtension } from '../extension';

/**
* This extension allows others extension to use the `createKeymaps` method.
Expand All @@ -27,11 +27,11 @@ export class KeymapExtension extends PlainExtension {
/**
* This adds the `createKeymap` method functionality to all extensions.
*/
onCreate: CreateLifecycleMethod = () => {
onCreate() {
this.store.setExtensionStore('rebuildKeymap', this.rebuildKeymap);
this.loopExtensions();
this.store.addPlugins(this.keymap);
};
}

/**
* Updates the stored keymap plugin on this extension.
Expand Down
6 changes: 3 additions & 3 deletions packages/@remirror/core/src/builtins/node-views-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExtensionPriority } from '@remirror/core-constants';
import { isFunction, object } from '@remirror/core-helpers';
import { NodeViewMethod } from '@remirror/core-types';

import { CreateLifecycleMethod, PlainExtension } from '../extension';
import { PlainExtension } from '../extension';
import { AnyCombinedUnion } from '../preset';

/**
Expand All @@ -26,7 +26,7 @@ export class NodeViewsExtension extends PlainExtension {
/**
* Ensure that all SSR transformers are run.
*/
onCreate: CreateLifecycleMethod = () => {
onCreate() {
const nodeViewList: Array<Record<string, NodeViewMethod>> = [];
const nodeViews: Record<string, NodeViewMethod> = object();

Expand Down Expand Up @@ -54,7 +54,7 @@ export class NodeViewsExtension extends PlainExtension {
}

this.store.setStoreKey('nodeViews', nodeViews);
};
}
}

declare global {
Expand Down
8 changes: 4 additions & 4 deletions packages/@remirror/core/src/builtins/paste-rules-extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExtensionPriority } from '@remirror/core-constants';
import { ProsemirrorPlugin } from '@remirror/core-types';

import { CreateLifecycleMethod, PlainExtension } from '../extension';
import { PlainExtension } from '../extension';

/**
* This extension allows others extension to add the `createPasteRules` method
Expand All @@ -20,10 +20,10 @@ export class PasteRulesExtension extends PlainExtension {
/**
* Ensure that all ssr transformers are run.
*/
onCreate: CreateLifecycleMethod = (extensions) => {
onCreate() {
const pasteRules: ProsemirrorPlugin[] = [];

for (const extension of extensions) {
for (const extension of this.store.extensions) {
if (
// managerSettings excluded this from running
this.store.managerSettings.exclude?.pasteRules ||
Expand All @@ -40,7 +40,7 @@ export class PasteRulesExtension extends PlainExtension {

// TODO rewrite so this is all one plugin
this.store.addPlugins(...pasteRules);
};
}
}

declare global {
Expand Down
13 changes: 4 additions & 9 deletions packages/@remirror/core/src/builtins/plugins-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import { ProsemirrorPlugin } from '@remirror/core-types';
import { getPluginState } from '@remirror/core-utils';
import { EditorState, Plugin, PluginKey } from '@remirror/pm/state';

import {
AnyExtension,
AnyExtensionConstructor,
CreateLifecycleMethod,
PlainExtension,
} from '../extension';
import { AnyExtension, AnyExtensionConstructor, PlainExtension } from '../extension';
import { AnyCombinedUnion, InferCombinedExtensions } from '../preset';
import { CreatePluginReturn, GetNameUnion } from '../types';

Expand Down Expand Up @@ -45,18 +40,18 @@ export class PluginsExtension extends PlainExtension {

// Here set the plugins keys and state getters for retrieving plugin state.
// These methods are later used.
onCreate: CreateLifecycleMethod = (extensions) => {
onCreate() {
const { setStoreKey, setExtensionStore } = this.store;
this.updateExtensionStore();

for (const extension of extensions) {
for (const extension of this.store.extensions) {
this.extractExtensionPlugins(extension);
}

setStoreKey('pluginKeys', this.pluginKeys);
setStoreKey('getPluginState', this.getStateByName);
setExtensionStore('getPluginState', this.getStateByName);
};
}

/**
* Get all the plugins from the extension.
Expand Down
9 changes: 4 additions & 5 deletions packages/@remirror/core/src/builtins/schema-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { Schema } from '@remirror/pm/model';

import {
AnyExtension,
CreateLifecycleMethod,
GetMarkNameUnion,
GetNodeNameUnion,
isMarkExtension,
Expand All @@ -52,20 +51,20 @@ export class SchemaExtension extends PlainExtension {
return 'schema' as const;
}

onCreate: CreateLifecycleMethod = (extensions) => {
onCreate() {
const { managerSettings } = this.store;
const nodes: Record<string, NodeExtensionSpec> = object();
const marks: Record<string, MarkExtensionSpec> = object();
const namedExtraAttributes = transformExtraAttributes({
settings: managerSettings,
gatheredSchemaAttributes: this.gatherExtraAttributes(extensions),
gatheredSchemaAttributes: this.gatherExtraAttributes(this.store.extensions),
nodeNames: this.store.nodeNames,
markNames: this.store.markNames,
});

// Skip the for loop by setting the list to empty when extra attributes are disabled

for (const extension of extensions) {
for (const extension of this.store.extensions) {
const currentAttributes = namedExtraAttributes[extension.name] ?? object();

namedExtraAttributes[extension.name] = {
Expand Down Expand Up @@ -109,7 +108,7 @@ export class SchemaExtension extends PlainExtension {
this.store.setStoreKey('marks', marks);
this.store.setStoreKey('schema', schema);
this.store.setExtensionStore('schema', schema);
};
}

/**
* Gather all the extra attributes that have been added by extensions.
Expand Down
7 changes: 3 additions & 4 deletions packages/@remirror/core/src/builtins/tags-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isUndefined, object } from '@remirror/core-helpers';

import {
AnyExtension,
CreateLifecycleMethod,
ExtensionTags,
isMarkExtension,
isNodeExtension,
Expand Down Expand Up @@ -37,16 +36,16 @@ export class TagsExtension extends PlainExtension {
};
}

onCreate: CreateLifecycleMethod = (extensions) => {
onCreate() {
this.resetTags();

for (const extension of extensions) {
for (const extension of this.store.extensions) {
this.updateTagForExtension(extension);
}

this.store.setStoreKey('tags', this.combinedTags);
this.store.setExtensionStore('tags', this.combinedTags);
};
}

private resetTags() {
this.#generalTags = {
Expand Down

0 comments on commit 7e864d0

Please sign in to comment.