-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate.js
43 lines (35 loc) · 1.07 KB
/
state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
state.js
*/
var _ = require('underscore'),
util = require('util'),
logger = require('./logger');
(function(context) {
var things = {};
function saveState(thingId, stateId, data) {
things[thingId] = things[thingId] || {};
var thingStates = things[thingId];
thingStates[stateId] = data;
}
function saveTime(thingId, stateId) {
var time = process.hrtime()[0]; // seconds
saveState(thingId, stateId, time);
}
function getState(thingId, stateId) {
stateId = stateId || "simpleState";
var thing = things[thingId];
var state = thing != null ? thing[stateId] : null;
return state;
}
function getComputedState(thingId) {
var stateId = "computedState";
var thing = things[thingId];
var state = thing != null ? thing[stateId] : null;
return state;
}
context.saveState = saveState;
context.saveTime = saveTime;
context.getState = getState;
context.getComputedState = getComputedState;
context.things = things;
})(exports);