4.0.1 • Published 9 days ago

hookpoint v4.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

hookPoint

hooks system for custom plugins

Installation

npm install hookpoint

Hook types

Each hook triggers every added function in a loop.

  • SeriesHook. This hook simply calls every function added in a row.

  • LastOutHook. A bail hook allows exiting early. When any of the tapped function returns anything, the bail hook will stop executing the remaining ones.

  • Waterfall. A waterfall hook also calls each added function in a row. It passes a return value from each function to the next function.

All hooks allow exiting early. To exit early a method must be passed to the constructor for detecting this case.

Usage

All Hook constructors take one optional argument, which is a list of argument names as strings.

const hook = new SeriesHook<[string, number], boolean>();

The best practice is to expose all hooks of a class in a hooks property:

class HttpFile {
	constructor() {
		this.hooks = {
			parse: new SeriesHook<[String, ParseContext], ParseResult>(),
			replaceVariables: new WaterfallHook<[string, ReplaceVariableContext], string>()
		};
	}
}

Other people can now use these hooks:

const httpFile = new HttpFile();
httpFile.hooks.parse.addHook("javascript", (text, context) => addExecuteAction(text));

It's required to pass a id to identify the plugin/reason.

Interception

All Hooks offer an additional interception API:

httpFile.hooks.parse.addInterceptor({
	 beforeLoop: async function checkUserCancellation(
      hookContext: models.HookTriggerContext<[models.ProcessorContext], boolean>
    ) {
      const context = hookContext.args[0];
      if (context.progress?.isCanceled?.()) {
        log.trace('process canceled by user');
        return false;
      }
      return true;
    },
})

License

MIT License

Change Log

CHANGELOG

4.0.1

9 days ago

4.0.0

6 months ago

3.2.1

10 months ago

3.2.0

10 months ago

3.1.0

10 months ago

3.0.0

11 months ago

2.3.0

11 months ago

2.3.2

11 months ago

2.3.1

11 months ago

2.2.1

1 year ago

2.2.0

1 year ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago