Comparing version 0.9.6 to 0.9.7
{ | ||
"name": "latching", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"description": "Run-Time Hook Latching", | ||
@@ -5,0 +5,0 @@ "main": "lib/latching.js", |
/* | ||
** Latching -- Run-Time Hook Latching | ||
** Copyright (c) 2012-2016 Ralf S. Engelschall <rse@engelschall.com> | ||
** Copyright (c) 2012-2017 Ralf S. Engelschall <rse@engelschall.com> | ||
** | ||
@@ -5,0 +5,0 @@ ** Permission is hereby granted, free of charge, to any person obtaining |
/* | ||
** Latching -- Run-Time Hook Latching | ||
** Copyright (c) 2012-2016 Ralf S. Engelschall <rse@engelschall.com> | ||
** Copyright (c) 2012-2017 Ralf S. Engelschall <rse@engelschall.com> | ||
** | ||
@@ -5,0 +5,0 @@ ** Permission is hereby granted, free of charge, to any person obtaining |
/* | ||
** Latching -- Run-Time Hook Latching | ||
** Copyright (c) 2012-2016 Ralf S. Engelschall <rse@engelschall.com> | ||
** Copyright (c) 2012-2017 Ralf S. Engelschall <rse@engelschall.com> | ||
** | ||
@@ -25,7 +25,21 @@ ** Permission is hereby granted, free of charge, to any person obtaining | ||
/* helper function for non-enumerable properties */ | ||
var mkProperty = function (ctx, name, value, writable) { | ||
if (writable === undefined) | ||
writable = true | ||
Object.defineProperty(ctx, name, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: writable, | ||
value: value | ||
}) | ||
} | ||
/* the API class */ | ||
var Latching = function () { | ||
this._reg = {} | ||
this._cnt = 0 | ||
this._proc = {} | ||
/* the internal information store */ | ||
mkProperty(this, "_latching_reg", {}) | ||
mkProperty(this, "_latching_cnt", 0 ) | ||
mkProperty(this, "_latching_proc", {}) | ||
mkProperty(this, "_latching_plugin", {}) | ||
@@ -57,3 +71,3 @@ /* pre-define some essential result processings */ | ||
throw new Error("proc: invalid step argument (has to be function)") | ||
this._proc[name] = { init: init, step: step } | ||
this._latching_proc[name] = { init: init, step: step } | ||
return this | ||
@@ -74,12 +88,12 @@ }, | ||
/* on-the-fly create hook callback registry slot */ | ||
if (this._reg[name] === undefined) | ||
this._reg[name] = [] | ||
if (this._latching_reg[name] === undefined) | ||
this._latching_reg[name] = [] | ||
/* store callback into hook callback registry slot */ | ||
var id = this._cnt++ | ||
var id = this._latching_cnt++ | ||
var rec = { id: id, cb: cb, ctx: ctx } | ||
if (prepend) | ||
this._reg[name].unshift(rec) | ||
this._latching_reg[name].unshift(rec) | ||
else | ||
this._reg[name].push(rec) | ||
this._latching_reg[name].push(rec) | ||
return id | ||
@@ -93,3 +107,3 @@ }, | ||
throw new Error("unlatch: invalid number of arguments") | ||
if (this._reg[name] === undefined) | ||
if (this._latching_reg[name] === undefined) | ||
throw new Error("unlatch: no such hook \"" + name + "\"") | ||
@@ -99,4 +113,4 @@ | ||
var k = -1 | ||
for (var i = 0; i < this._reg[name].length; i++) { | ||
if (this._reg[name][i].id === id) { | ||
for (var i = 0; i < this._latching_reg[name].length; i++) { | ||
if (this._latching_reg[name][i].id === id) { | ||
k = i | ||
@@ -110,3 +124,3 @@ break | ||
/* remove callback from hook callback registry slot */ | ||
this._reg[name].splice(k, 1) | ||
this._latching_reg[name].splice(k, 1) | ||
@@ -121,3 +135,3 @@ return this | ||
throw new Error("hook: invalid number of arguments") | ||
if (this._proc[proc] === undefined) | ||
if (this._latching_proc[proc] === undefined) | ||
throw new Error("hook: no such result processing defined") | ||
@@ -127,9 +141,9 @@ var params = Array.prototype.slice.call(arguments, 2) | ||
/* start result with the initial value */ | ||
var result = this._proc[proc].init.call(null, params) | ||
var result = this._latching_proc[proc].init.call(null, params) | ||
/* give all registered callbacks a chance to | ||
execute and modify the current result */ | ||
if (this._reg[name] !== undefined) { | ||
for (var i = 0; i < this._reg[name].length; i++) { | ||
var latched = this._reg[name][i] | ||
if (this._latching_reg[name] !== undefined) { | ||
for (var i = 0; i < this._latching_reg[name].length; i++) { | ||
var latched = this._latching_reg[name][i] | ||
@@ -144,3 +158,3 @@ /* support cancellation */ | ||
/* process/merge results */ | ||
result = this._proc[proc].step.call(null, result, resultNew) | ||
result = this._latching_proc[proc].step.call(null, result, resultNew) | ||
@@ -155,2 +169,47 @@ /* optionally cancel/short-circuit processing */ | ||
return result | ||
}, | ||
/* add a plugin */ | ||
use: function (plugin, options) { | ||
/* sanity check arguments */ | ||
if (typeof plugin !== "object" && typeof plugin !== "function") | ||
throw new Error("use: invalid plugin argument (object or function/class expected)") | ||
/* optionally instanciate plugin */ | ||
if (typeof plugin === "function") | ||
plugin = new plugin() | ||
/* attach plugin */ | ||
var id = this._latching_cnt++ | ||
var rec = { id: id, plugin: plugin } | ||
this._latching_plugin[id] = rec | ||
/* give plugin a chance to react */ | ||
if (typeof plugin.use !== "function") | ||
throw new Error("use: invalid plugin (method \"use\" expected)") | ||
if (typeof options === "undefined") | ||
options = {} | ||
plugin.use(this, options) | ||
return id | ||
}, | ||
/* remove a plugin */ | ||
unuse: function (id) { | ||
/* sanity check arguments */ | ||
if (arguments.length !== 1) | ||
throw new Error("unuse: invalid number of arguments") | ||
if (this._latching_plugins[id] === undefined) | ||
throw new Error("unuse: no such plugin found") | ||
/* give plugin a chance to react */ | ||
var plugin = this._latching_plugins[id].plugin | ||
if (typeof plugin.unuse === "function") | ||
plugin.unuse(this) | ||
/* detach plugin */ | ||
this._latching_plugins[id].plugin = null | ||
delete this._latching_plugins[id] | ||
return this | ||
} | ||
@@ -157,0 +216,0 @@ } |
{ | ||
"name": "latching", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"description": "Run-Time Hook Latching", | ||
@@ -22,8 +22,8 @@ "keywords": [ "hook", "latch" ], | ||
"grunt-cli": "~1.2.0", | ||
"grunt-contrib-jshint": "~1.0.0", | ||
"grunt-contrib-clean": "~1.0.0", | ||
"grunt-mocha-test": "~0.12.7", | ||
"mocha": "~3.0.2", | ||
"chai": "~3.5.0" | ||
"grunt-contrib-jshint": "~1.1.0", | ||
"grunt-contrib-clean": "~1.1.0", | ||
"grunt-mocha-test": "~0.13.3", | ||
"mocha": "~4.0.1", | ||
"chai": "~4.1.2" | ||
} | ||
} |
@@ -94,3 +94,3 @@ | ||
app.latch("access-allowed", (username, password) => { | ||
return db.user.findBy(username).sha1 === sha1(password) | ||
return db.user.findById(username).sha1 === sha1(password) | ||
}) | ||
@@ -188,4 +188,4 @@ ``` | ||
```js | ||
var id = latching.latch("access-allowed", function (user, password, resultPrev, cancel) { | ||
return db.user.findBy(user).sha1 === sha1(password) | ||
let id = latching.latch("access-allowed", (user, password, resultPrev, cancel) => { | ||
return db.user.findById(user).sha1 === sha1(password) | ||
}) | ||
@@ -211,5 +211,36 @@ ``` | ||
```js | ||
var allowed = latching.hook("access-allowed", "and", user, password) | ||
let allowed = latching.hook("access-allowed", "and", user, password) | ||
``` | ||
- `Latching#use(plugin: (function|class|object), options: object = {}): number`<br/> | ||
First, if `plugin` is a function or class, this instanciates it with | ||
`new`. Second, this attaches the plugin to the Latching. Third, this | ||
calls the method `plugin#use(latching: Latching, options: object)` | ||
on the plugin itself to give the plugin a chance to react after it | ||
was attached to the latching. The `Latching#use()` returns a unique | ||
identifier for use by `Latching#unuse()`. | ||
**Example**: | ||
```js | ||
class Plugin { | ||
use (latching, options) { | ||
this.id = latching.latch("access-allowed", (user, password, resultPrev, cancel) => { | ||
return db.user.findById(user).sha1 === sha1(password) | ||
}) | ||
} | ||
unuse (latching) { | ||
latching.unlatch("access-allowed", this.id) | ||
} | ||
} | ||
[...] | ||
latching.use(Plugin) | ||
``` | ||
- `Latching#unuse(id: number): Latching`<br/> | ||
First, this optionally calls the method `plugin#unuse(latching: | ||
Latching)` on the plugin to give the plugin a chance to react before | ||
it detached from the latching. Second, it detaches the plugin from the | ||
latching. | ||
History | ||
@@ -227,3 +258,3 @@ ------- | ||
Copyright (c) 2012-2016 Ralf S. Engelschall (http://engelschall.com/) | ||
Copyright (c) 2012-2017 Ralf S. Engelschall (http://engelschall.com/) | ||
@@ -230,0 +261,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
Sorry, the diff of this file is not supported yet
25888
339
276
8