Skip to content

Commit

Permalink
Delete action internally on DELETE. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Stegeman committed Mar 28, 2018
1 parent 013c131 commit a463143
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -16,5 +16,6 @@ module.exports = {
quotes: ['error', 'single', {
allowTemplateLiterals: true,
}],
semi: ['error', 'always'],
}
};
4 changes: 2 additions & 2 deletions lib/property.js
Expand Up @@ -61,8 +61,8 @@ class Property {
this.value = value;
}

this.thing.propertyNotify(this)
return this.value
this.thing.propertyNotify(this);
return this.value;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions lib/server.js
Expand Up @@ -391,14 +391,11 @@ class ActionIDHandler extends BaseHandler {
const actionName = req.params.actionName;
const actionId = req.params.actionId;

const action = thing.getAction(actionName, actionId);
if (action === null) {
if (thing.removeAction(actionName, actionId)) {
res.status(204).end();
} else {
res.status(404).end();
return;
}

action.cancel()
res.status(204).end();
}
}

Expand Down
32 changes: 28 additions & 4 deletions lib/thing.js
Expand Up @@ -47,7 +47,7 @@ class Thing {
}

for (const property of Object.values(this.properties)) {
property.setHrefPrefix(prefix)
property.setHrefPrefix(prefix);
}

for (const actionName in this.actions) {
Expand Down Expand Up @@ -327,7 +327,7 @@ class Thing {
this.availableEvents[name] = {
metadata: metadata,
subscribers: new Set(),
}
};
}

/**
Expand All @@ -351,6 +351,30 @@ class Thing {
return action;
}

/**
* Remove an existing action.
*
* @param {String} actionName Name of the action
* @param {String} actionId ID of the action
* @returns boolean indicating the presence of the action.
*/
removeAction(actionName, actionId) {
const action = this.getAction(actionName, actionId);
if (action === null) {
return false;
}

action.cancel();
for (let i = 0; i < this.actions[actionName].length; ++i) {
if (this.actions[actionName][i].id === actionId) {
this.actions[actionName].splice(i, 1);
break;
}
}

return true;
}

/**
* Add an available action.
*
Expand All @@ -369,7 +393,7 @@ class Thing {
this.availableActions[name] = {
metadata: metadata,
class: cls,
}
};
this.actions[name] = [];
}

Expand All @@ -379,7 +403,7 @@ class Thing {
* @param {Object} ws The websocket
*/
addSubscriber(ws) {
this.subscribers.add(ws)
this.subscribers.add(ws);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "webthing",
"version": "0.4.1",
"version": "0.4.2",
"description": "HTTP Web Thing implementation",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit a463143

Please sign in to comment.