-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileStorage.js
343 lines (318 loc) · 12.8 KB
/
fileStorage.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"use strict";
// Will initialize File API functions in your namespace
var FS = {};
(function ($, DX, ns) {
ns.trivialPromise = function (_) {
var d = $.Deferred();
return d.resolve.apply(d, arguments).promise()
};
ns.rejectedPromise = function (_) {
var d = $.Deferred();
return d.reject.apply(d, arguments).promise()
};
ns.date = function (date) {
date = date || new Date();
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
};
ns.initFileAPI = function (bytesNeeded, persistent) {
if (ns.fs)
return ns.trivialPromise(true);
if (persistent === undefined)
persistent = true;
var d = $.Deferred();
function initFS(fs) {
fs.root.getDirectory('data', { create: true }, function (dirEntry) {
ns.fs = fs;
ns.dataRoot = dirEntry;
d.resolve();
}, errorHandler);
}
function errorHandler(e) {
ns.fs = null;
ns.dataRoot = null;
d.reject(e);
}
function requestFS() {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var deviceInfo = DevExpress.devices.current();
window.requestFileSystem(fsType, deviceInfo.android ? 0 : bytesNeeded,
initFS, errorHandler);
}
var fsType;
if (typeof LocalFileSystem !== "undefined")
fsType = persistent ? LocalFileSystem.PERSISTENT : LocalFileSystem.TEMPORARY;
else
fsType = persistent ? window.PERSISTENT : window.TEMPORARY;
var storageInfo = persistent ? navigator.webkitPersistentStorage : navigator.webkitTemporaryStorage;
if (storageInfo) {
storageInfo.requestQuota(bytesNeeded, function (grantedBytes) {
requestFS();
}, function (e) {
d.reject(e);
});
} else
requestFS();
return d.promise();
};
ns.writeFile = function (fileName, content) {
if (!ns.fs)
ns.rejectedPromise("This method requires initialized persistent File API");
var d = $.Deferred();
ns.dataRoot.getFile(fileName, { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
var blob;
try {
blob = new Blob([content], { type: 'text/plain' });
} catch (e) {
blob = content;
}
fileWriter.onwriteend = function () {
if (fileWriter.length == 0)
fileWriter.write(blob);
if (fileWriter.length === content.length)
d.resolve(true);
};
fileWriter.truncate(0);
}, function (e) {
d.reject(e)
});
}, function (e) {
d.reject(e)
});
return d;
};
ns.readFile = function (fileName) {
if (!ns.fs)
ns.rejectedPromise("This method requires initialized persistent File API");
var d = $.Deferred();
ns.dataRoot.getFile(fileName, { create: false }, function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function () {
if (d.state() === "resolved")
d.reject("double resolve");
else
d.resolve(this.result);
};
reader.readAsText(file);
}, function (e) {
d.reject(e)
});
}, function () {
d.resolve(null);
});
return d;
};
ns.removeFile = function (fileName) {
if (!ns.fs)
ns.rejectedPromise("This method requires initialized persistent File API");
var d = $.Deferred();
ns.dataRoot.getFile(fileName, { create: false }, function (fileEntry) {
fileEntry.remove(function () {
d.resolve(true);
}, function () {
d.resolve(false);
});
}, function () {
d.resolve(null);
});
return d;
}
ns.FileArrayStore = DX.data.Store.inherit({
ctor: function (options) {
if ($.isArray(options))
options = { data: options };
else
options = options || {};
this.callBase(options);
if (!this.key())
throw Error("requires keyExpr");
this._array = options.data || [];
this._fileName = options.fileName || null;
this._loadPromise = null;
this._reviver = options.reviver || null;
this._flushScheduled = false;
this._jsonifiedArray = [];
this._keyMap = {};
this.generator = ns.createKeyGenerator(0);
for (var i = 0; i < this._array.length; i++) {
var key = this.keyOf(this._array[i]);
this._keyMap[key] = i;
this.generator.fix(key);
this._jsonifiedArray.push(null);
}
},
createQuery: function () {
return DX.data.query(this._array, { errorHandler: this._errorHandler });
},
_totalCountImpl: function (options) {
var base = $.proxy(this.callBase, this);
return this._load().then(function () {
return base(options);
});
},
_byKeyImpl: function (key) {
var indexByKey = this._indexByKey(key);
if (indexByKey < 0)
return ns.trivialPromise(undefined);
var item = this._array[indexByKey];
if (item !== undefined && this.keyOf(item) != key)
return ns.rejectedPromise("_indexByKey works incorrectly");
return ns.trivialPromise(item);
},
_load: function () {
if (!this._loadPromise) {
if (this._fileName) {
this._loadPromise = ns.readFile(this._fileName)
.then(function (jsonString) {
this._erase();
if (!jsonString)
return ns.trivialPromise(false);
var i = 0;
while (i < jsonString.length) {
var separatorIndex = jsonString.indexOf(":", i);
if (separatorIndex < 0)
return ns.rejectedPromise("malformed json string");
var length = parseInt(jsonString.substr(i, separatorIndex - i));
var jsonifiedItem = jsonString.substr(separatorIndex + 1, length);
this._jsonifiedArray.push(jsonifiedItem);
var obj;
if (this._reviver)
obj = this._reviver(jsonifiedItem);
else
obj = JSON.parse(jsonifiedItem);
this._array.push(obj);
var objKey = this.keyOf(obj);
this._keyMap[objKey] = this._array.length - 1;
this.generator.fix(objKey);
i = separatorIndex + 1 + length;
}
return ns.trivialPromise(true);
}.bind(this));
} else
this._loadPromise = ns.trivialPromise();
}
return this._loadPromise;
},
_loadImpl: function (options) {
var callBase = this.callBase.bind(this);
return this._load().then(function () {
return callBase(options);
}.bind(this));
},
_insertImpl: function (values) {
return this._load()
.then(function () {
if (!$.isArray(values))
values = [values];
var keyExpr = this.key(),
keyValues = [],
obj;
for (var i = 0; i < values.length; i++) {
obj = $.extend({}, values[i]);
var keyValue = this.keyOf(obj);
if (keyValue === undefined || typeof keyValue === "object" && $.isEmptyObject(keyValue)) {
keyValue = obj[keyExpr] = this.generator.next();
} else {
if (this._keyMap[keyValue] !== undefined)
return ns.rejectedPromise("Attempt to insert an item with the duplicate key (" + keyValue + ", " + this._keyMap[keyValue] + ")");
this.generator.fix(keyValue);
}
keyValues.push(keyValue);
this._array.push(obj);
this._jsonifiedArray.push(null);
this._keyMap[keyValue] = this._array.length - 1;
}
this._scheduleFlush();
if (values.length == 1)
return ns.trivialPromise(values[0], keyValues[0]);
else
return ns.trivialPromise(values, keyValues);
}.bind(this));
},
_updateImpl: function (key, values) {
return this._load().then(function () {
var index = this._indexByKey(key);
if (index < 0)
return ns.rejectedPromise("Data item not found");
var target = this._array[index];
DX.utils.deepExtendArraySafe(target, values);
if (this.keyOf(target) !== key)
ns.rejectedPromise("An attempt to change the key");
this._jsonifiedArray[index] = null;
this._scheduleFlush();
return ns.trivialPromise(key, values);
}.bind(this));
},
_removeImpl: function (key) {
return this._load().then(function () {
var index = this._indexByKey(key);
if (index > -1) {
this._array.splice(index, 1);
this._jsonifiedArray.splice(index, 1);
delete this._keyMap[key];
for (var k in this._keyMap) {
if (this._keyMap[k] > index)
this._keyMap[k]--;
}
this._scheduleFlush();
}
return ns.trivialPromise(key);
}.bind(this));
},
_scheduleFlush: function () {
if (!this._fileName || this._flushScheduled)
return;
this._flushScheduled = true;
setTimeout(function () {
if (this._flushScheduled)
this.flush();
}.bind(this), 3000);
},
flush: function () {
var jsonString = "";
for (var i = 0; i < this._jsonifiedArray.length; i++) {
if (this._jsonifiedArray[i] == null)
this._jsonifiedArray[i] = JSON.stringify(this._array[i]);
jsonString += this._jsonifiedArray[i].length;
jsonString += ":";
jsonString += this._jsonifiedArray[i];
}
return ns.writeFile(this._fileName, jsonString).done(function () {
this._flushScheduled = false;
console.log("file '" + this._fileName + "' written");
}.bind(this));
},
erase: function () {
this._erase();
if (!this._fileName)
return ns.trivialPromise();
return ns.removeFile(this._fileName);
},
_erase: function () {
this._flushScheduled = false;
this._jsonifiedArray.length = 0;
this._array.length = 0;
this._keyMap = {};
},
_indexByKey: function (key) {
var index = this._keyMap[key];
return index !== undefined ? index : -1;
}
});
ns.createKeyGenerator = function (startValue) {
var currentValue = startValue || 0;
return {
next: function () {
return ++currentValue;
},
fix: function (loadedKey) {
if (currentValue < loadedKey)
currentValue = loadedKey + 1;
},
reset: function () {
currentValue = 0;
}
};
};
})(jQuery, DevExpress, FS);