-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.js
39 lines (38 loc) · 995 Bytes
/
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
import { Meteor } from "meteor/meteor";
import { Mongo } from "meteor/mongo";
import { proj, omit } from "./utils.js";
export default class State {
constructor(tree){
this._store = new Mongo.Collection(null);
this._store.insert({_id:"state"});
this.memory = {};
if (tree){
this.memorize(tree);
}
}
memorize(tree){
const p = proj(tree,null,true);
for (let k in p)
this.memory[k] = p[k];
this._store.update("state",{$set:tree});
}
revert(tree){
const reset = {};
for (let k in proj(tree,1))
reset[k] = this.memory[k];
this._store.update('state',{$set:reset});
}
clear(tree){
this._store.update('state',{$unset:proj(tree,"")})
}
get(tree){
return omit(this._store.findOne("state", {fields:proj(tree,1)}),"_id")
}
set(tree){
try {
this._store.update("state", {$set:proj(tree)})
} catch(e){
throw new Meteor.Error('Invalid Tree',"You may not set keys for non-object values.")
}
}
}