-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
713 lines (629 loc) · 18 KB
/
store.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/* jshint browser: true */
/* jshint devel: true */
/* jshint jquery: true */
/* jshint strict: false */
//so that the test code runs
if( typeof app === "undefined" ){
var app = {};
app.depends = function( a, b ){
b();
};
}
app.depends([], function(){
'use strict';
//These classes are defined within this file:
//app.structure.store.Store
if( typeof module !== "undefined" ){
/* jshint ignore:start */
module.exports = Store;
/* jshint ignore:end */
} else {
app.store = {};
app.store.Store = Store;
}
/*------------------------- polyfills ------------------------------*/
if (!String.prototype.isSubpathOf) {
Object.defineProperty(String.prototype, 'isSubpathOf', {
enumerable: false,
configurable: false,
writable: false,
value: function(searchString, position) {
position = position || 0;
var matchPosition = this.lastIndexOf(searchString, position);
if(matchPosition === position){
if(matchPosition+searchString.length === this.length){
return true;
} else {
var nextChar = this.substring(searchString.length, searchString.length+1);
return nextChar === '.';
}
} else {
return false;
}
}
});
}
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
enumerable: false,
configurable: false,
writable: false,
value: function(searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
}
});
}
/* actual Store function */
function Store(name, contents) {
var targetCount =0;
this.contents = contents || undefined;
var _storeName = name;
var subscriptions = {};
var subscriptionsNextTick = {};
var store = {};
var dataStore = {};
var fufillTargetTracker = {};
//this.subscriptions = subscriptions;
/*------------------------Store Helper Compontents ----------------*/
function removeChildern(path) {
for(var key in dataStore) {
if(key.isSubpathOf(path)) {
delete dataStore[key];
}
}
for(var keys in store) {
if(keys.isSubpathOf(path)) {
delete store[keys];
if(keys != path) {
fufillTargetTracker[keys] = true;
targetCount++;
}
}
}
}
function getSplits(path) {
var splits = [];
var match;
var regex = /\./g;
while (match = regex.exec(path)) {// jshint ignore:line
splits.push(match.index);
}
return splits;
}
function fufillTargets() {
/*
console.log('----------------------Fufill Targets--------------------------------');
console.log(fufillTargetTracker);
console.log('--------------------------------------------------------------------');*/
function fufillSubscriptionsNextTick( subs, value ){
subs.map(function( sub ){
setTimeout(function() {
sub(value);
}, 0);
});
}
function fufillSubscriptions( subs, value ){
subs.map(function( sub ){sub(value);});
}
// avoid long loops;
if(targetCount > 30) {
for(var key in subscriptionsNextTick) {
if(fufillTargetTracker[key]) {
fufillSubscriptionsNextTick(subscriptionsNextTick[key], dataStore[key]);
}
}
for(var key in subscriptions) {
if(fufillTargetTracker[key]) {
fufillSubscriptions(subscriptions[key], dataStore[key]);
}
}
}
else {
for(var key in fufillTargetTracker) {
if(subscriptionsNextTick[key]) {
fufillSubscriptionsNextTick(subscriptionsNextTick[key], dataStore[key]);
}
if(subscriptions[key]) {
fufillSubscriptions(subscriptions[key], dataStore[key]);
}
}
}
}
function fufill(path) {
/*console.log('----------------fufill ---------------');
console.log(path);
console.log(subscriptions);
console.log(subscriptionsNextTick);
console.log(dataStore[path]);
console.log('--------------------------------------');*/
if(subscriptionsNextTick[path]) {
subscriptionsNextTick[path].map(function(sub){
setTimeout(function() {
if(typeof sub != 'function') {
throw new Error('Unable to fufill subscription ' + _storeName + ' at ' + path);
}
else {
sub(dataStore[path]);
}
}, 0);
});
}
if(subscriptions[path]) {
subscriptions[path].map(function(sub){
if(typeof sub != 'function') {
throw new Error('Unable to fufill subscription ' + _storeName + ' at ' + path);
}
else {
sub(dataStore[path]);
}
});
}
if(path.indexOf('.') !== -1) {
fufill(path.slice(0, path.lastIndexOf('.')));
}
}
function parseData(path, data) {
/*console.log('---------------------------Parse Data----------------------------');
console.log('PATH: ' + path);
console.log('DATA:');
console.log(data);
console.log('------------------------------------------------------------------');*/
function makeObject(key, data) {
var varPath = path + '.' + key;
Object.defineProperty(store[path], key, {set:new StoreSetter(varPath), get:new StoreGetter(varPath), enumerable:true, configurable:true});
parseData(varPath, data[key]);
}
if(!store[path]) {
if(data instanceof Array) {
store[path] = new StorableArray(path);
}
else {
store[path] = {};
}
}
fufillTargetTracker[path] = true;
targetCount++;
dataStore[path] = data;
if(typeof data === 'object' && data !== null) {
for(var key in data) {
makeObject(key, data);
}
}
else {
Object.defineProperty(store, path, {set:new StoreSetter(path), get:new DataGetter(data), enumerable:true, configurable:true});
}
}
/*------------------------Store Getters and Setters ---------------------*/
function StoreSetter(path) {
return function StoreSetter(data) {
//nuke the data underneath
fufillTargetTracker = {};
targetCount = 0;
removeChildern(path);
//Fix parent data
var splits = getSplits(path);
for(var i = 0; i < splits.length; i++) {
var parent = dataStore[path.substring(0,splits[i])];
var child = path.substring(splits[i]+1);
var childArray = child.split('.') || [child];
var lastEle = childArray.length -1;
for(var j = 0; j < lastEle; j++) {
parent = parent[childArray[j]];
}
parent[childArray[lastEle]] = data;
}
parseData(path, data);
fufill(path);
delete fufillTargetTracker[path];
targetCount--;
fufillTargets();
};
}
function StoreGetter(path) {
return function StoreGetter() {
return store[path];
};
}
function DataGetter(value) {
return function DataGetter() {
return value;
};
}
function ArrayPush(path) {
return function() {
fufillTargetTracker = {};
targetCount = 0;
var arrayStore = new StorableArray(path);
for(var k =0 ; k < (store[path].length + arguments.length); k++) {
Object.defineProperty(arrayStore, k, {set:new StoreSetter(path + '.' + k), get:new StoreGetter(path + '.' + k), enumerable:true, configurable:true});
}
store[path] = arrayStore;
var pathArray = path.split('.');
var parent = dataStore[pathArray[0]];
for(var i = 1; i < pathArray.length; i++) {
parent = parent[pathArray[i]];
}
for( var i = 0; i < arguments.length; i++) {
parent.push(arguments[i]);
var toPath = dataStore[path].length -1;
parseData(path + '.' + toPath, arguments[i]);
}
fufill(path);
fufillTargets();
return dataStore[path].length;
};
}
function ArrayPop(path) {
return function() {
var popPath = path + '.' + (dataStore[path].length - 1);
fufillTargetTracker = {};
targetCount = 0;
var popValue = dataStore[popPath];
fufillTargetTracker[popPath] = true;
targetCount++;
removeChildern(popPath);
var pathArray = path.split('.');
var parent = dataStore[pathArray[0]];
for(var i = 1; i < pathArray.length; i++) {
parent = parent[pathArray[i]];
}
parent.pop();
delete store[popPath];
var arrayStore = new StorableArray(path);
for(var k =0 ; k < (store[path].length -1); k++) {
Object.defineProperty(arrayStore, k, {set:new StoreSetter(path + '.' + k), get:new StoreGetter(path + '.' + k), enumerable:true, configurable:true});
}
store[path] = arrayStore;
fufill(path);
fufillTargets();
return popValue;
};
}
function ArrayShift(path) {
return function() {
var shiftPath = path + '.0';
fufillTargetTracker = {};
targetCount = 0;
var shiftValue = dataStore[shiftPath];
var pathArray = path.split('.');
var parent = dataStore[pathArray[0]];
for(var i = 1; i < pathArray.length; i++) {
parent = parent[pathArray[i]];
}
parent.shift();
var remainingArray = dataStore[path];
removeChildern(path);
parseData(path, remainingArray);
fufill(path);
fufillTargets();
return shiftValue;
};
}
function ArrayUnshift(path) {
return function() {
fufillTargetTracker = {};
targetCount = 0;
var splits = getSplits(path);
var parent;
if(splits.length === 0) {
parent = dataStore.contents;
}
else {
for(var j = 0; j < splits.length; j++) {
parent = dataStore[path.substring(0,splits[j])];
var child = path.substring(splits[j]+1);
var childArray = child.split('.');
for(var l = 0; l < childArray.length; l++) {
if(childArray[l]) {
parent = parent[childArray[l]];
}
}
}
}
for(var i = arguments.length-1; i >=0 ; i--) {
parent.unshift(arguments[i]);
}
var updatedArray = dataStore[path];
removeChildern(path);
parseData(path, updatedArray);
fufill(path);
fufillTargets();
return updatedArray.length;
};
}
function ArraySplice(path) {
return function() {
//Argument 1 is index,
//Argument 2 is count,
//Remaing arguments are to be inserted;
var spliceAt = parseInt(arguments[0]);
var spliceCount = arguments[1];
//get the elememts that are to be removed;
var workingArray = dataStore[path];
for(var m = spliceAt; m < workingArray.length; m++) {
removeChildern(path + '.' + m);
}
//= dataStore[path].splice(spliceAt, spliceCount);
fufillTargetTracker = {};
targetCount = 0;
//up to index nothing should change
var arrayStore = new StorableArray(path);
for(var k = 0; k < spliceAt; k++) {
Object.defineProperty(arrayStore, k, {set:new StoreSetter(path + '.' + k), get:new StoreGetter(path + '.' + k), enumerable:true, configurable:true});
}
store[path] = arrayStore;
var pathArray = path.split('.');
var parent = dataStore[pathArray[0]];
for(var i = 1; i < pathArray.length; i++) {
parent = parent[pathArray[i]];
}
var removedElements = parent.splice(spliceAt, spliceCount);
for( var i = 2; i < arguments.length; i++) {
var index = spliceAt + i-2;
parent.splice(index,0,arguments[i]);
parseData(path + '.' + index, arguments[i]);
Object.defineProperty(arrayStore, index, {
set: new StoreSetter(path + '.' + index),
get: new StoreGetter(path + '.' + index),
enumerable: true,
configurable: true
});
}
for(var o = (spliceAt+arguments.length-2); o < dataStore[path].length; o++) {
parseData(path + '.' + o, dataStore[path][o]);
Object.defineProperty(arrayStore, o, {
set: new StoreSetter(path + '.' + o),
get: new StoreGetter(path + '.' + o),
enumerable: true,
configurable: true
});
}
fufill(path);
fufillTargets();
return removedElements;
};
}
function ArrayReverse(path) {
return function() {
fufillTargetTracker = {};
targetCount = 0;
var pathArray = path.split('.');
var parent = dataStore[pathArray[0]];
for(var i = 1; i < pathArray.length; i++) {
parent = parent[pathArray[i]];
}
parent.reverse();
var updatedArray = dataStore[path];
removeChildern(path);
parseData(path, updatedArray);
fufill(path);
fufillTargets();
};
}
function StorableArray(path) {
var array = [];
Object.defineProperty(array, 'push', {value:new ArrayPush(path), enumerable:false});
Object.defineProperty(array, 'pop', {value:new ArrayPop(path), enumerable:false} );
Object.defineProperty(array, 'shift', {value: new ArrayShift(path), enumerable:false});
Object.defineProperty(array, 'unshift', {value:new ArrayUnshift(path), enumerable:false});
Object.defineProperty(array, 'splice', {value: new ArraySplice(path), enumerable:false});
Object.defineProperty(array, 'reverse', {value: new ArrayReverse(path), enumerable:false});
return array;
}
this.add = function( path, data ){
fufillTargetTracker = {};
targetCount = 0;
var parent = path.substring(0, path.lastIndexOf('.'));
var key = path.substring(path.lastIndexOf('.') + 1);
if(!(path in store)) {
Object.defineProperty(store[parent], key, {set:new StoreSetter(path), get:new StoreGetter(path), enumerable:true, configurable: true});
}
else {
removeChildern(path);
}
dataStore[parent][key] = data;
parseData(path, data);
fufill(path);
fufillTargets();
};
this.modify = function(obj){
if( typeof obj !== "object" || obj === null ){
console.error("You must specify an enumerable object to modify the content of a store");
return;
}
for( var i in obj ){
this.add('contents.' + i, obj[i]);
}
};
this.del = function(path){
fufillTargetTracker = {};
targetCount = 0;
if(path.indexOf('contents') !== 0){
path = 'contents.'+path;
}
//Remove the underlying data of the path
removeChildern(path);
var storeToDeleteFrom = store;
var dataStoreToDeleteFrom = dataStore;
//Do length-1 as the last part of the path
//is the key to be deleted, start at 0 as first part is this
path = path.split('.');
for(var i = 0; i<path.length-1; i++){
storeToDeleteFrom = storeToDeleteFrom[path[i]];
dataStoreToDeleteFrom = dataStoreToDeleteFrom[path[i]];
}
//Delete from the dataStore
delete dataStoreToDeleteFrom[path[path.length-1]];
//Delete the getters and setters from contents
delete storeToDeleteFrom[path[path.length-1]];
fufillTargets();
fufill(path.join('.'));
};
this.subscribe = function(){
var subFor = 'contents';
if(arguments.length > 1) {
subFor = arguments[0];
}
var sub = arguments[arguments.length -1];
if(! subscriptions[subFor]) {
subscriptions[subFor] = [];
}
subscriptions[subFor].push(sub);
};
this.subscribeNextTick = function() {
var subFor = 'contents';
if(arguments.length > 1) {
subFor = arguments[0];
}
var sub = arguments[arguments.length -1];
if(! subscriptionsNextTick[subFor]) {
subscriptionsNextTick[subFor] = [];
}
subscriptionsNextTick[subFor].push(sub);
};
this.unsubscribe = function() {
var subFor = 'contents';
if(arguments.length > 1) {
subFor = arguments[0];
}
var unsub = arguments[arguments.length -1];
if(subscriptions[subFor]) {
for(var i = 0; i < subscriptions[subFor].length; i++) {
if(unsub == subscriptions[subFor][i]) {
subscriptions[subFor].splice(i,1);
}
}
}
};
this.unsubscribeNextTick = function() {
var subFor = 'contents';
if(arguments.length > 1) {
subFor = arguments[0];
}
var unsub = arguments[arguments.length -1];
if(subscriptionsNextTick[subFor]) {
for(var i = 0; i < subscriptionsNextTick[subFor].length; i++) {
if(unsub == subscriptionsNextTick[subFor][i]) {
subscriptionsNextTick[subFor].splice(i,1);
}
}
}
};
this.once = function() {
var self = this;
var subFor = 'contents';
if(arguments.length > 1) {
subFor = arguments[0];
}
var sub = arguments[arguments.length -1];
var onceSub = function() {
self.unsubscribe(subFor, onceSub);
sub.apply(this, arguments);
};
self.subscribe(subFor, onceSub);
};
this.onceNextTick = function() {
var self = this;
var subFor = 'contents';
if(arguments.length > 1) {
subFor = arguments[0];
}
var sub = arguments[arguments.length -1];
var onceSub = function() {
self.unsubscribeNextTick(subFor, onceSub);
sub.apply(this, arguments);
};
self.subscribeNextTick(subFor, onceSub);
};
this.printSubscriptions = function(path){
console.log('Subscriptions for path:',path);
console.log(subscriptions,subscriptions[path]);
console.log('SubscriptionsNextTick for path:',path);
console.log(subscriptionsNextTick,subscriptionsNextTick[path]);
};
this.printStores = function() {
console.log('====================Store Contents=========================');
console.log('-------------------- Data Store ---------------------------');
console.log(dataStore);
console.log('-------------------- Comp Store ---------------------------');
console.log(store);
console.log('===========================================================');
};
this.getObjPath = function(subobj, root, acc){
if( root === undefined ){
root = this.contents;
acc = "contents";
if( this.contents === undefined ){
return null;
}
}
if( typeof root !== "object" || root === null ){
return null;
}
for( var i in root ){
if( typeof root[i] === "object" && root[i] !== null ){
if( root[i] === subobj ){
return acc+"."+i;
} else {
var res = this.getObjPath( subobj, root[i], acc+"."+i );
if( res !== null ){
return res;
}
}
}
}
return null;
};
this.getCleanElements = function(item){
console.warn('Store: get clean elements has been depricated, use getCopyOF instead.');
var ret = JSON.parse(JSON.stringify( item ));
for( var i in ret ){
ret[i] = item[i];
}
return ret;
};
this.forceUpdate = function(path){
path = path === undefined ? 'contents' : path;
fufill(path);
};
this.getCopyOf = function(id) {
return dataStore[id];
};
this.getStoreAt = function(location) {
return store[location];
};
Object.defineProperty(this, 'contents', {
set: new StoreSetter('contents'),
get: function() {
return store.contents;
}
});
}
Store.prototype.makePathReadable = function ( url, store, ignore ){
var elems = url.split(".").slice(1);
//elems.splice( elems.length-1, 1); //this will remove the last url element
var parent = store.contents[ elems[0] ];
ignore = ignore || [];
var res = elems.reduce( function( prev, elem ){
var item = parent[ elem ];
if( Array.isArray(item) ){
parent = item;
return prev;
} else {
parent = parent[ elem ];
if( ignore.indexOf( parent.name ) > -1 ){
return prev;
} else {
return prev + "/" + parent.name;
}
}
});
var check = res.split("/");
if( ignore.indexOf( check[0] ) > -1 ){
return (check.slice(1).join("/"));
} else {
return res;
}
};
});