Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit 80e018a

Browse files
committed
feat: add Api#deregister
1 parent d683a43 commit 80e018a

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

lib/api/deregister.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* eq8-api
3+
* Copyright(c) 2016 Benjamin Bartolome
4+
* Apache 2.0 Licensed
5+
*/
6+
7+
'use strict';
8+
9+
module.exports = function exported() {
10+
return function deregister(type, id) {
11+
var api = this;
12+
var registry = api.registry[type];
13+
if (registry && registry[id]) {
14+
api[type].remove(registry[id].pattern, registry[id].handler);
15+
delete registry[id];
16+
}
17+
};
18+
};

lib/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ function Api(options) {
3232
// INITIALIZATION
3333
_.assign(api, {
3434
actions: actions,
35-
views: views
35+
views: views,
36+
registry: {
37+
actions: {},
38+
views: {}
39+
}
3640
});
3741

3842
Core.call(api, options);
@@ -43,6 +47,7 @@ util.inherits(Api, Core);
4347

4448
Api.prototype.state = require('./api/state')(_, async);
4549
Api.prototype.express = require('./api/express')(_);
50+
Api.prototype.deregister = require('./api/deregister')();
4651

4752
function newApi(options) {
4853
return new Api(options);

lib/registrars/actions.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = function exported(_, Joi, Rx) {
1111
var api = this;
1212

1313
var actionSchema = Joi.object().keys({
14+
id: Joi.string(),
1415
name: Joi.string().required(),
1516
pattern: Joi.object().required(),
1617
handler: Joi.func().minArity(2).maxArity(3)
@@ -33,7 +34,17 @@ module.exports = function exported(_, Joi, Rx) {
3334
if (action.handler.length < 3) {
3435
handler = addCallback(handler);
3536
}
37+
38+
if (action.id) {
39+
api.deregister('actions', action.id);
40+
41+
api.registry.actions[action.id] = {
42+
pattern: pattern,
43+
handler: handler
44+
};
45+
}
3646
api.actions.add(pattern, handler);
47+
3748
},
3849
function(err) {
3950
api.logger.error(err);

lib/registrars/views.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = function exported(_, Joi, Rx) {
1111
var api = this;
1212

1313
var viewSchema = Joi.object().keys({
14+
id: Joi.string(),
1415
name: Joi.string().required(),
1516
pattern: Joi.object(),
1617
handler: Joi.func().minArity(2).maxArity(3)
@@ -31,6 +32,15 @@ module.exports = function exported(_, Joi, Rx) {
3132
function(view) {
3233
var pattern = _.defaults({}, view.pattern);
3334

35+
if (view.id) {
36+
api.deregister('views', view.id);
37+
38+
api.registry.views[view.id] = {
39+
pattern: pattern,
40+
handler: view.handler
41+
};
42+
}
43+
3444
api.views.add(pattern, view.handler);
3545
},
3646
function(err) {

0 commit comments

Comments
 (0)