Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created March 23, 2012 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gozala/2175647 to your computer and use it in GitHub Desktop.
Save Gozala/2175647 to your computer and use it in GitHub Desktop.
// module: plugin-manager.js
var installable = require('./installable')
var events = require('./event-protocol')
function getPlugin() { /* ... */ }
function getDependents() { /* ... */ }
exports.start = function(plugin) {
// start all dependencies
plugin.dependencies.forEach(function(id) {
exports.start(getPlugin(id))
})
// notify that plugin is going to be started.
events.on(plugin, 'before:start', { ... })
// start an actual plugin!
installable.on(plugin)
// notify that plugin was started.
events.emit(plugin, 'after:start', { ... })
// track plugin stops so we can unload dependents
events.once(plugin, 'before:stop', function onstop() {
// stop all the plugins that depend on this one.
getDependents(plugin).forEach(exports.stop)
})
}
exports.stop = function(plugin) {
events.emit(plugin, 'before:stop', {... })
installable.off(plugin)
events.emit(plugin, 'after:stop', { ... })
}
@Gozala
Copy link
Author

Gozala commented Mar 23, 2012

BTW to be completely fare in reality I would have defined Plugin type where I would have implemented custom on / off that would have emitted events, but anyway I think this illustrates how two protocols may be used with a same type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment