/
storage.js
138 lines (130 loc) · 3.64 KB
/
storage.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var falcor = require('falcor');
var classify = require('protean/function/classify');
var defaults = require('lodash/object/defaults');
/**
* **File:** [falcor/data-source/storage.js](falcor/data-source/storage.js)
*
* @class StorageSource
* @extends ProteanClass
* @implements DataSource
* @param {Object} opts
* @param {Storage} opts.storage
* @param {String} opts.storageKey
* @param {Object} [opts.cache]
* @param {Function} [opts.serializer]
* @param {Function} [opts.deserializer]
*/
function StorageSource (opts) {
opts = this.options = defaults({}, opts || {}, this.options);
this.model = new falcor.Model({ cache: opts.cache });
this.source = this.model.asDataSource();
if (opts.cache) {
this.serialize();
}
this.deserialize();
}
module.exports = classify(StorageSource,/** @lends StorageSource# */{
/**
* @property {FalcorFModel}
*/
model: null,
/**
* @property {DataSource}
*/
source: null,
/**
* @property {Object}
*/
options: {
/**
* The key to use when accessing the Storage object to serialize the
* graph to.
* @property {String}
*/
storageKey: null,
/**
* The storage source
* @property {Storage}
*/
storage: null,
/**
* A function to convert a JSONGraph to a String.
* @param {JSONGraph} graph
* @returns {String}
*/
serializer: function (graph) { return JSON.stringify(graph); },
/**
* A function to convert a String to a JSONGraph.
* @param {String} src
* @returns {JSONGraph}
*/
deserializer: function (src) { return JSON.parse(src); }
},
/**
* @param {PathSets[]} paths
* @returns {Observable<JSONGraphEnvelope>}
*/
get: function (paths) { return this.source.get(paths); },
/**
* @param {JSONGraphEnvelope} envelope
* @returns {Observable<JSONGraphEnvelope>}
*/
set: function (envelope) {
return this.
source.
set(envelope).
do(this.serialize.bind(this));
},
/**
* @param {PathSet} path
* @param {Array<Mixed>} args
* @param {PathSet[]} refSuffixes
* @param {PathSet[]} thisPaths
* @returns {Observable<JSONGraphEnvelope>}
*/
call: function (paths, args, refSuffixes, thisPaths) {
return this.
source.
call(paths, args, refSuffixes, thisPaths).
do(this.serialize.bind(this));
},
/**
* Clear our storage item
*/
clear: function () {
var opts = this.options;
var storage = opts.storage;
var key = opts.storageKey;
if (storage) {
storage.removeItem(key);
}
},
/**
* Write our cache to storage
*/
serialize: function () {
var opts = this.options;
var storage = opts.storage;
var key = opts.storageKey;
var serializer = opts.serializer;
var model = this.model;
var graph = model && model.getCache();
if (storage && graph) {
storage.setItem(key, serializer(graph));
}
},
/**
* Get our cache from storage
*/
deserialize: function () {
var opts = this.options;
var storage = opts.storage;
var key = opts.storageKey;
var deserializer = opts.deserializer;
var json = storage && storage.getItem(key);
var model = this.model;
if (model && json) {
model.setCache(deserializer(json));
}
}
});