-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plugin system #142
Plugin system #142
Changes from 3 commits
3ffc3a1
ce3134c
3602893
7bbdd2d
9e88f5f
7fdd5dc
3ea21e0
cca75e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export default class Hooks { | ||
/** | ||
* @callback HookCallback | ||
* @this {*|Jsep} this | ||
* @param {Jsep} env | ||
* @returns: void | ||
*/ | ||
/** | ||
* Adds the given callback to the list of callbacks for the given hook. | ||
* | ||
* The callback will be invoked when the hook it is registered for is run. | ||
* | ||
* One callback function can be registered to multiple hooks and the same hook multiple times. | ||
* | ||
* @param {string|object} name The name of the hook, or an object of callbacks keyed by name | ||
* @param {HookCallback|boolean} callback The callback function which is given environment variables. | ||
* @param {?boolean} [first=false] Will add the hook to the top of the list (defaults to the bottom) | ||
* @public | ||
*/ | ||
add(name, callback, first) { | ||
if (typeof arguments[0] != 'string') { | ||
// Multiple hook callbacks, keyed by name | ||
for (let name in arguments[0]) { | ||
this.add(name, arguments[0][name], arguments[1]); | ||
} | ||
} | ||
else { | ||
(Array.isArray(name) ? name : [name]).forEach(function(name) { | ||
this[name] = this[name] || []; | ||
|
||
if (callback) { | ||
this[name][first ? 'unshift' : 'push'](callback); | ||
} | ||
}, this); | ||
} | ||
} | ||
|
||
/** | ||
* Runs a hook invoking all registered callbacks with the given environment variables. | ||
* | ||
* Callbacks will be invoked synchronously and in the order in which they were registered. | ||
* | ||
* @param {string} name The name of the hook. | ||
* @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered. | ||
* @public | ||
*/ | ||
run(name, env) { | ||
this[name] = this[name] || []; | ||
this[name].forEach(function (callback) { | ||
callback.call(env && env.context ? env.context : env, env); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with JSEP, we kind of need access to the Jsep instance, so I think it should always be bound to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The usual pattern is that the hook creation code just passes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see. Yeah, in that case the typical pattern is that you pass in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion! I'll push a change that uses env.context and env.node. See if it's more what you had in mind? |
||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Add default plugins: | ||
import jsep from './jsep.js'; | ||
import './plugins/ternary/ternary.js'; | ||
|
||
export * from './jsep.js'; | ||
export default jsep; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: spaces for indentation in this entire file (I assume it's the default setting in your editor, so it happened when you copy/pasted).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So sorry! I added
.editorconfig
and added the tab as a rule to eslint so it should be fixed :-)