HookableMixin

Hierarchy

  • HookableMixin

Implements

Index

Methods

Methods

getActions

getActions(): Actions

Implementation of Hookable

Returns a collection of all available actions with matching registered hooks as nested collection.

Returns: Actions

Collection of actions(key) with matching registered hooks as nested collection(value).


getHook

getHook(action: string, id: string): Hook | undefined

Implementation of Hookable

Returns hook for action and id.

example

class MyClass extends HookableMixin {}
const hook = sinon.spy();
MyClass.prototype.registerHook('onConstruction', 'my-hook', hook);
expect(MyClass.prototype.getHook('onConstruction', 'my-hook')).to.be.equal(hook);

Parameters:

NameTypeDescription
actionstringAction for which hook is resolved.
idstringIdentifier under which hook was was registered.

Returns: Hook | undefined

Hook as a function matching declaration, else undefined.


getHookOrThrow

getHookOrThrow(action: string, id: string): Hook

Implementation of Hookable

Returns hook for action and id or throws.

throws {HandlerNotFoundError} Thrown if there is no hook registered for action with id.

Parameters:

NameTypeDescription
actionstringAction for which hook is resolved.
idstringIdentifier under which hook was was registered.

Returns: Hook

Hook as a function matching declaration, else throws.


getHooks

getHooks(action: string): Mappings

Implementation of Hookable

Returns a collection of all available hooks registered for action.

Parameters:

NameTypeDescription
actionstringAction for which hooks are resolved.

Returns: Mappings

Collection of hooks.


hasAction

hasAction(action: string): boolean

Implementation of Hookable

Evaluates if hooks for action are registered.

Parameters:

NameTypeDescription
actionstringAction for which hook is existence is evaluated.

Returns: boolean

Returns true if hooks for action exists, else false.


hasHook

hasHook(action: string, id: string): boolean

Implementation of Hookable

Evaluates if hook for action with id is registered.

Parameters:

NameTypeDescription
actionstringAction for which hook is existence is evaluated.
idstringIdentifier under which hook was was registered.

Returns: boolean

Returns true if hook exists, else false.


overrideHook

overrideHook(action: string, id: string, hook: Hook): void

Implementation of Hookable

Overrides registered hook by action and id or registers a new one.

throws {InvalidHookActionError} Thrown if the the action argument is not a string.

throws {InvalidHookIdError} Thrown if the the id argument is not a string.

Parameters:

NameTypeDescription
actionstringAction for which hook will be registered(like onConstruction, onSend, onPublish etc.)
idstringIdentifier under which hook will be registered for further reference.
hookHookHook as a function matching declaration for required action that will be invoked upon action.

Returns: void


registerHook

registerHook(action: string, id: string, hook: Hook, shouldOverride?: boolean): void

Implementation of Hookable

Registers hook by action type and id.

throws {InvalidHookActionError} Thrown if the the action argument is not a string.

throws {InvalidHookIdError} Thrown if the the id argument is not a string.

throws {HookAlreadyExistsError} Thrown if the existing hook with id would be overridden.

example

import {expect} from 'chai';
import {HookableMixin} from 'eveble'
class Document extends HookableMixin {
content: string;
version: number;
constructor(props: Record<keyof any, any>) {
super();
const processedProps = { ...props };
const hooks = this.getHooks('onConstruction');
for (const hook of Object.values(hooks)) {
hook.bind(this)(processedProps);
}
Object.assign(this, processedProps);
}
}
const versionable = (props: Record<keyof any, any>) => {
if (props.version === undefined) {
props.version = 0;
}
return props;
};
Document.prototype.registerHook('onConstruction', 'versionable', versionable);
const newDoc = new Document({ content: 'My document content' });
expect(newDoc.version).to.be.equal(0);

Parameters:

NameTypeDescription
actionstringAction for which hook will be registered(like onConstruction, onSend, onPublish etc.)
idstringIdentifier under which hook will be registered for further reference.
hookHookHook as a function matching declaration for required action that will be invoked upon action.
shouldOverride?booleanFlag indicating that hook should be overridden if exist.

Returns: void


removeHook

removeHook(action: string, id: string): void

Implementation of Hookable

Removes a hook by action and id.

example

class MyClass extends HookableMixin {}
const hook = sinon.spy();
MyClass.prototype.registerHook('onConstruction', 'my-hook', hook);
MyClass.prototype.removeHook('onConstruction', 'my-hook')
expect(MyClass.prototype.getHook('onConstruction', 'my-hook')).to.be.undefined;

Parameters:

NameTypeDescription
actionstringAction for which hook is removed.
idstringIdentifier under which hook was was registered.

Returns: void