-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawext.js
More file actions
834 lines (779 loc) · 28.1 KB
/
awext.js
File metadata and controls
834 lines (779 loc) · 28.1 KB
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/**
* @author MrZenW
* @email MrZenW@gmail.com, https://MrZenW.com
*/
(function moduleify(moduleFactory) {
'use strict';
var theLib = null;
if (typeof define === 'function' && define.amd) {
function moduleFactoryWrapper() {
theLib = theLib || moduleFactory.apply(this, arguments);
return theLib;
}
define('awext', ['statecore'], moduleFactoryWrapper);
define('Awext', ['statecore'], moduleFactoryWrapper);
} else if (typeof module === 'object' && typeof exports === 'object') {
theLib = theLib || moduleFactory(require('statecore'));
module.exports = theLib;
}
var root = (typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : typeof global !== 'undefined' ? global : this);
if (root && typeof root === 'object') {
theLib = theLib || moduleFactory(root['statecore']);
root['awext'] = theLib;
root['Awext'] = theLib;
}
}(function moduleFactory(deps_Statecore) {
'use strict';
if (!deps_Statecore) throw new Error('Awext depends on Statecore! - https://github.com/statecore/statecore');
var _AwextLib_ = {};
var prototypeHasOwn = Object.prototype.hasOwnProperty;
function _hasOwn(obj, key) {
return obj !== undefined && obj !== null && prototypeHasOwn.call(obj, key);
}
function _hasKey(obj, key) {
return obj !== undefined && obj !== null && (key in Object(obj));
}
_AwextLib_.hasKey = _hasKey;
var prototypeToString = Object.prototype.toString;
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Polyfill
var ARRAY_TYPE_STRINGIFY = Object.prototype.toString.call([]);
function _isArray(value) {
return prototypeToString.call(value) === ARRAY_TYPE_STRINGIFY;
}
var Counter__UUID = 0;
function _genUUID() {
Counter__UUID += 1;
return Counter__UUID + '__' + Date.now() + '__' + Math.random();
}
_AwextLib_.genUUID = _genUUID;
function _blankFunction() { }
_AwextLib_.blankFunction = _blankFunction;
function _isNone(value) {
if (value === undefined || null === value) return true;
if ('number' === typeof value && isNaN(value)) return true;
return false;
}
_AwextLib_.isNone = _isNone;
function _isNumber(value) {
return !isNaN(value);
}
_AwextLib_.isNumber = _isNumber;
function _isKeyValueObject(value) {
return value !== null && 'object' === typeof value && prototypeToString.call(value) !== ARRAY_TYPE_STRINGIFY;
}
_AwextLib_.isKeyValueObject = _isKeyValueObject;
function _isBoolean(value) {
return typeof value === 'boolean';
}
function _isFunction(value) {
return 'function' === typeof value;
}
_AwextLib_.isFunction = _isFunction;
function _isString(value) {
return 'string' === typeof value;
}
function _isRegExp(value) {
return value instanceof RegExp;
}
_AwextLib_.isRegExp = _isRegExp;
function _isPromise(obj) {
return !!obj && ('object' === typeof obj || 'function' === typeof obj) && 'function' === typeof obj.then;
}
_AwextLib_.isPromise = _isPromise;
function _objectAssign(target) {
if (_isNone(target)) throw new TypeError('Cannot convert undefined or null to object');
var isTargetArray = _isArray(target);
var to = Object(target);
if (isTargetArray) {
for (var iArray = 1; iArray < arguments.length; iArray += 1) {
var nextArray = arguments[iArray];
if (_isArray(nextArray)) {
to.push.apply(to, nextArray);
} else {
to.push.call(to, nextArray);
}
}
} else {
for (var iObject = 1; iObject < arguments.length; iObject += 1) {
var nextObj = arguments[iObject];
if (!_isNone(nextObj)) {
for (var nextKey in nextObj) {
if (_hasOwn(nextObj, nextKey)) {
to[nextKey] = nextObj[nextKey];
}
}
}
}
}
return to;
}
_AwextLib_.objectAssign = _objectAssign;
function _objectKeys(obj) {
var keys = [];
for (var key in obj) {
keys.push(key);
}
return keys;
}
function _objectOwnKeysForEach(obj, cb) {
var keys = _objectKeys(obj); // get all keys before operate it.
for (var idx in keys) {
var key = keys[idx];
if (_hasOwn(obj, key)) {
if (cb(key, obj) === false) {
return;
}
}
}
}
function _bind(func, bindThis) {
if (func.bind) return func.bind(bindThis);
return function () {
return func.apply(bindThis, arguments);
};
}
function _objectBindProps(target) {
if (_isNone(target)) throw new TypeError('Cannot convert undefined or null to object');
var to = Object(target);
var argsLength = arguments.length;
for (var iObject = 1; iObject < argsLength; iObject += 1) {
var nextSource = arguments[iObject];
if (!_isNone(nextSource)) {
_objectOwnKeysForEach(nextSource, function (key) {
if (Object.getOwnPropertyDescriptor) {
var desc = Object.getOwnPropertyDescriptor(nextSource, key);
if (desc.get || desc.set) {
if (desc.get) {
desc.get = _bind(desc.get, to);
}
if (desc.set) {
desc.set = _bind(desc.set, to);
}
} else if (_isFunction(desc.value)) {
desc.value = _bind(desc.value, to);
}
Object.defineProperty(to, key, _objectAssign({}, desc));
} else if (_isFunction(nextSource[key])) {
to[key] = _bind(nextSource[key], to);
} else {
to[key] = nextSource[key];
}
});
}
}
return to;
}
_AwextLib_.objectBindProps = _objectBindProps;
function _parsePath(path, separator) {
if (!_isString(separator)) throw new Error('The second parameter \'separator\' must be specified as a string!');
if (_isArray(path)) return path;
if (_isNone(path)) return [];
if (_isRegExp(path)) return [path];
path += '';
if (!path) return [];
if (path[0] === '[' && path[1] === '"') {
var pathStrLen = path.length;
if (path[pathStrLen - 2] === '"' && path[pathStrLen - 1] === ']') {
try {
return JSON.parse(path);
} catch (error) {
// oops, no json
}
}
}
return path.split(separator);
}
_AwextLib_.parsePath = _parsePath;
function _pathStatus(argVar, path) {
var currentPathObject = argVar || {};
for (var idx = 0; idx < path.length; idx += 1) {
var key = path[idx];
if (_hasKey(currentPathObject, key)) {
currentPathObject = currentPathObject[key];
} else {
return { exists: false };
}
}
return { exists: true, value: currentPathObject };
}
_AwextLib_.pathStatus = _pathStatus;
function _pathGetter(argVar, path) {
return _pathStatus(argVar, path).value;
}
_AwextLib_.pathGetter = _pathGetter;
function _pathSetter(rootVar, path, value, onlyWhenParentExists) {
rootVar = rootVar || {};
var currentVar = rootVar;
var parentVar = rootVar;
var key;
for (var idx = 0; idx < path.length; idx += 1) {
key = path[idx];
if (!_hasKey(currentVar, key) || _isNone(currentVar[key])) {
if (onlyWhenParentExists) {
throw new Error('Parent node ' + key + ' does not exist');
} else {
currentVar[key] = {};
}
}
parentVar = currentVar;
currentVar = currentVar[key];
}
parentVar[key] = value;
return rootVar;
}
_AwextLib_.pathSetter = _pathSetter;
function _pathDeleter(argVar, path) {
var rootVar = argVar || {};
var currentVar = rootVar;
var parentVar = currentVar;
var key;
for (var idx = 0; idx < path.length; idx += 1) {
key = path[idx];
if (!_hasOwn(currentVar, key)) {
return rootVar;
}
parentVar = currentVar;
currentVar = currentVar[key];
}
delete parentVar[key];
return rootVar;
}
_AwextLib_.pathDeleter = _pathDeleter;
function _isSameArray(pattern, input) {
if (pattern.length !== input.length) return false;
for (var idx = 0; idx < pattern.length; idx += 1) {
var patternCurr = pattern[idx];
var inputCurr = input[idx];
if (_isRegExp(patternCurr)) {
if (!patternCurr.test(inputCurr)) {
return false;
}
} else if (patternCurr + '' !== inputCurr + '') {
return false;
}
}
return true;
}
function _isBeforeOrSameArray(pattern, input) {
if (pattern.length < input.length) return false;
for (var idx = 0; idx < input.length; idx += 1) {
var patternCurr = pattern[idx];
var inputCurr = input[idx];
if (_isRegExp(patternCurr)) {
if (!patternCurr.test(inputCurr)) {
return false;
}
} else if (patternCurr + '' !== inputCurr + '') {
return false;
}
}
return true;
}
function _isBeforeArray(pattern, input) {
if (pattern.length <= input.length) return false;
for (var idx = 0; idx < input.length; idx += 1) {
var patternCurr = pattern[idx];
var inputCurr = input[idx];
if (_isRegExp(patternCurr)) {
if (!patternCurr.test(inputCurr)) {
return false;
}
} else if (patternCurr + '' !== inputCurr + '') {
return false;
}
}
return true;
}
function _isAfterOrSameArray(pattern, input) {
if (pattern.length > input.length) return false;
for (var idx = 0; idx < pattern.length; idx += 1) {
var patternCurr = pattern[idx];
var inputCurr = input[idx];
if (_isRegExp(patternCurr)) {
if (!patternCurr.test(inputCurr)) {
return false;
}
} else if (patternCurr + '' !== inputCurr + '') {
return false;
}
}
return true;
}
function _isAfterArray(pattern, input) {
if (pattern.length >= input.length) return false;
for (var idx = 0; idx < pattern.length; idx += 1) {
var patternCurr = pattern[idx];
var inputCurr = input[idx];
if (_isRegExp(patternCurr)) {
if (!patternCurr.test(inputCurr)) {
return false;
}
} else if (patternCurr + '' !== inputCurr + '') {
return false;
}
}
return true;
}
function _isAwextPackage(awextStore) {
return awextStore
&& awextStore.awextFunctions
&& awextStore.awextFunctions.awextGetName
&& awextStore.baseObject;
}
// awext definition begin
var Store__AwextInstance = {};
var Store__UnwatchId = {};
var Counter__UnwatchId = 0;
function _getAwextPackage(awextName) {
if (!awextName) throw new Error('Awext name must be provided');
awextName = _parsePath(awextName, '/');
var existingAwext = _pathGetter(Store__AwextInstance, awextName);
if (_isAwextPackage(existingAwext)) {
return existingAwext;
}
return null;
}
deps_Statecore = _objectAssign({}, deps_Statecore);
// lib funcions start
var Counter__AwextName = 0;
function createAwext(createAwextOpt, initArg) {
createAwextOpt = createAwextOpt || {};
if (_isString(createAwextOpt) || _isNumber(createAwextOpt) || _isArray(createAwextOpt)) {
createAwextOpt = { awextName: createAwextOpt };
}
var awextName = _parsePath(createAwextOpt.awextName, '/');
if (awextName.length > 0) {
var existingAwext = _getAwextPackage(awextName);
if (existingAwext) {
return existingAwext.baseObject;
}
} else {
Counter__AwextName += 1;
awextName = ['__unnamed_awext__', Counter__AwextName + '__' + _genUUID()];
}
var __pathSeperator = createAwextOpt.pathSeperator || '.';
var __destroyFunc = null;
var __INNER_EVENT_NAMESPACE = _genUUID();
var __statecoreInstance = deps_Statecore.createStatecore({});
var __initFunction = _isFunction(initArg) ? initArg : null;
var __initObject = _isKeyValueObject(initArg) ? _objectAssign({}, initArg) : null;
var __unwatcherDefaultNS = 'default_ns__' + _genUUID();
var __unwatchers = {};
var __awextThis = {};
_objectAssign(__awextThis, {
// statecoreNotifyAllObservers: __statecoreInstance.statecoreNotifyAllObservers,
// statecoreAddObserver: __statecoreInstance.statecoreAddObserver,
// statecoreDiscard: __statecoreInstance.statecoreDiscard,
// statecoreIsDiscarded: __statecoreInstance.statecoreIsDiscarded,
// statecoreGetState: __statecoreInstance.statecoreGetState,
// statecoreSetState: __statecoreInstance.statecoreSetState,
awextName: _objectAssign([], awextName),
awextGetName: function () {
return _objectAssign([], awextName);
},
awextClone: function () {
return _objectAssign(
{},
__awextThis,
{ awextName: __awextThis.awextGetName() },
);
},
awextIsDiscarded: __statecoreInstance.statecoreIsDiscarded,
awextDiscard: function () {
if (__awextThis.awextIsDiscarded()) {
console.warn('Attempt to discard a discarded awext instance under path: ' + __awextThis.awextName.join('/'));
return;
}
if (_isFunction(__destroyFunc)) {
__destroyFunc();
}
if (__initObject) {
_objectOwnKeysForEach(__initObject, function (key) {
delete __initObject[key];
});
}
_pathDeleter(Store__AwextInstance, __awextThis.awextGetName());
__awextThis.awextEmit(__INNER_EVENT_NAMESPACE, {
action: 'discard',
method: 'awextDiscard',
});
__awextThis.awextUnwatchAll();
__statecoreInstance.statecoreDiscard();
},
awextRegisterUnwatch: function (argUnwatchFunctions) {
if (arguments.length > 1) throw new Error('The second argument are not allowed to be specified!');
return __awextThis.awextRegisterUnwatchWithNS(__unwatcherDefaultNS, argUnwatchFunctions);
},
awextRegisterUnwatchWithNS: function (ns, argUnwatchFunctions) {
if (!_isString(ns) || !ns) throw new Error('The first argument: namespace must be specified in a String!');
__unwatchers[ns] = __unwatchers[ns] || [];
if (!_isArray(argUnwatchFunctions)) {
argUnwatchFunctions = [argUnwatchFunctions];
}
var allUnwatchFunctions = [];
_objectOwnKeysForEach(argUnwatchFunctions, function (key) {
var _currentFunc = argUnwatchFunctions[key];
if (!_isFunction(_currentFunc)) {
throw new Error('Only a function can be registered into the unwatch manager!');
}
allUnwatchFunctions.push(_currentFunc);
});
var unwatchFunction = function () {
if (!allUnwatchFunctions) return;
while (allUnwatchFunctions.length > 0) allUnwatchFunctions.pop()();
allUnwatchFunctions = null;
};
__unwatchers[ns].push(unwatchFunction);
return unwatchFunction;
},
awextEmit: __statecoreInstance.statecoreNotifyAllObservers,
awextOn: function (eventName, func) {
if (!_isFunction(func)) throw new Error('Listener must be a function!');
return __awextThis.awextRegisterUnwatch(
__statecoreInstance.statecoreAddObserver(function (emitEventName) {
if (emitEventName === eventName) {
func.apply(__awextThis, arguments);
}
}),
);
},
awextGet: __statecoreInstance.statecoreGetState,
awextSet: function (value) {
return __awextThis.awextUpdate(function () {
return { value: value };
});
},
awextGetPath: function (path) {
if (arguments.length === 0) {
return __awextThis.awextGet();
}
path = _parsePath(path, __pathSeperator);
return _pathGetter(__awextThis.awextGet(), path);
},
awextSetPath: function (path, value, _opts) {
return __awextThis.awextUpdatePath(_parsePath(path, __pathSeperator), function () {
return { value: value };
}, _opts);
},
awextSetManyPaths: function (values, basePath) {
basePath = _parsePath(basePath, __pathSeperator);
if (_isArray(values)) {
// [{key1: value1, key2: value2}]
_objectOwnKeysForEach(values, function (idx) {
__awextThis.awextSetManyPaths(values[idx], basePath);
});
} else {
// {key1: value1, key2: value2}
_objectOwnKeysForEach(values, function (key) {
var subPath = _parsePath(key, __pathSeperator);
__awextThis.awextSetPath([].concat(basePath, subPath), values[key]);
});
}
},
awextReset: function () {
if (_isFunction(__destroyFunc)) {
__destroyFunc();
}
if (__initFunction) {
__destroyFunc = __initFunction(__awextThis.awextClone());
return null;
} else if (__initObject) {
return __awextThis.awextReplace(_objectAssign({}, __initObject));
} else {
throw new Error('No init argument is provided, cannot reset it.');
}
},
awextResetPath: function (path, _opts) {
if (__initFunction) throw new Error('Init argument is not an object, could not find a path in it.');
_opts = Object(_opts || {});
path = _parsePath(path, __pathSeperator);
var pathInfo = _pathStatus(__initObject, path);
if (pathInfo.exists) {
return __awextThis.awextSetPath(path, pathInfo.value, _opts);
} else {
return __awextThis.awextRemovePath(path, _opts);
}
},
awextReplace: function (newState, _opts) {
_opts = Object(_opts || {});
var currentState = __awextThis.awextGet();
_objectOwnKeysForEach(currentState, function (path) {
if (!_hasOwn(newState, path)) {
__awextThis.awextRemovePath(path, _opts);
}
});
return __awextThis.awextSetManyPaths(_objectAssign({}, newState), _opts);
},
awextHasPath: function (path) {
path = _parsePath(path, __pathSeperator);
return !!_pathStatus(__awextThis.awextGet(), path).exists;
},
awextUpdate: function (updater, _opts) {
_opts = Object(_opts || {});
var oldState = __statecoreInstance.statecoreGetState();
var newStateInfo = updater({ value: _objectAssign({}, oldState), exists: true });
if (!_isKeyValueObject(newStateInfo)) newStateInfo = {};
var silent = _opts.silent;
if (_isBoolean(newStateInfo.silent)) {
silent = newStateInfo.silent;
}
var newStateValue = __statecoreInstance.statecoreSetState(newStateInfo.value);
if (!silent) {
__awextThis.awextEmit(__INNER_EVENT_NAMESPACE, {
oldState: oldState,
newState: newStateValue,
action: 'update',
method: 'awextUpdate',
});
}
return newStateValue;
},
awextUpdatePath: function (path, updater, _opts) {
_opts = Object(_opts || {});
path = _parsePath(path, __pathSeperator);
var oldRootState = __statecoreInstance.statecoreGetState();
var oldRathStateInfo = _pathStatus(oldRootState, path);
var oldPathState = oldRathStateInfo.value;
var newPathStateInfo = updater(oldRathStateInfo);
if (!_isKeyValueObject(newPathStateInfo)) newPathStateInfo = {};
var silent = newPathStateInfo.silent || _opts.silent;
var newPathState = newPathStateInfo.value;
var eventAction;
var newRootState;
// delete
if (newPathState === undefined) {
newRootState = _pathDeleter(_objectAssign({}, oldRootState), path);
eventAction = 'delete';
} else { // update
newRootState = _pathSetter(_objectAssign({}, oldRootState), path, newPathState, _opts.onlyWhenParentExists);
eventAction = 'update';
}
__statecoreInstance.statecoreSetState(newRootState);
if (!silent) {
__awextThis.awextEmit(__INNER_EVENT_NAMESPACE, {
path: path,
oldPathState: oldPathState,
oldPathStateExists: oldRathStateInfo.exists,
newPathState: newPathState,
action: eventAction,
method: 'awextUpdatePath',
});
}
return newPathState;
},
awextRemovePath: function (path, _opts) {
_opts = Object(_opts || {});
path = _parsePath(path, __pathSeperator);
var oldPathState;
var newRootState = __awextThis.awextUpdatePath(path, function (oldPathInfo) {
oldPathState = oldPathInfo.value;
return { exists: false };
}, { silent: true });
if (!_opts.silent) {
__awextThis.awextEmit(__INNER_EVENT_NAMESPACE, {
path: path,
oldPathState: oldPathState,
action: 'delete',
method: 'awextRemovePath',
});
}
return newRootState;
},
awextCreateUnwatchId: function (cb) {
if (!_isFunction(cb)) {
throw new Error('Only a function can be used for creating a watch id!');
}
Counter__UnwatchId += 1;
Store__UnwatchId[Counter__UnwatchId] = cb;
return Counter__UnwatchId;
},
awextUnwatchById: function (id) {
var unwatchFunc = Store__UnwatchId[id];
if (_isFunction(unwatchFunc)) {
delete Store__UnwatchId[id];
unwatchFunc();
}
},
awextWatch: function (cb) {
return __awextThis.awextOn(__INNER_EVENT_NAMESPACE, function (eventName, eventObject) {
cb(undefined, eventObject);
});
},
awextWatchPath: function (watchPath, cb) {
watchPath = _parsePath(watchPath, __pathSeperator);
return __awextThis.awextWatch(function (eventType, awextEvent) {
var path = _parsePath(awextEvent.path, __pathSeperator);
if (_isSameArray(watchPath, path)) {
cb.apply(__awextThis, arguments);
}
});
},
awextWatchPathAfter: function (matchPath, cb) {
matchPath = _parsePath(matchPath, __pathSeperator);
return __awextThis.awextWatch(function (eventType, awextEvent) {
var path = _parsePath(awextEvent.path, __pathSeperator);
if (_isAfterArray(matchPath, path)) {
cb.apply(__awextThis, arguments);
}
});
},
awextWatchPathSameOrAfter: function (matchPath, cb) {
matchPath = _parsePath(matchPath, __pathSeperator);
return __awextThis.awextWatch(function (eventType, awextEvent) {
var path = _parsePath(awextEvent.path, __pathSeperator);
if (_isAfterOrSameArray(matchPath, path)) {
cb.apply(__awextThis, arguments);
}
});
},
awextWatchPathBefore: function (matchPath, cb) {
matchPath = _parsePath(matchPath, __pathSeperator);
return __awextThis.awextWatch(function (eventType, awextEvent) {
var path = _parsePath(awextEvent.path, __pathSeperator);
if (_isBeforeArray(matchPath, path)) {
cb.apply(__awextThis, arguments);
}
});
},
awextWatchPathSameOrBefore: function (matchPath, cb) {
matchPath = _parsePath(matchPath, __pathSeperator);
return __awextThis.awextWatch(function (eventType, awextEvent) {
var path = _parsePath(awextEvent.path, __pathSeperator);
if (_isBeforeOrSameArray(matchPath, path)) {
cb.apply(__awextThis, arguments);
}
});
},
awextUnwatchAll: function () {
if (arguments.length > 0) throw new Error('awextUnwatchAll() does not accept any argument, instead, you can use awextUnwatchWithNS(namespace)');
_objectOwnKeysForEach(__unwatchers, function (ns) {
__awextThis.awextUnwatchWithNS(ns);
});
},
awextUnwatchWithNS: function (ns) {
ns = ns || __unwatcherDefaultNS;
var unwatchersNS = __unwatchers[ns] || [];
while (unwatchersNS.length > 0) unwatchersNS.shift()();
},
awextUnwatchWithNamespace: function (ns) {
return __awextThis.awextUnwatchWithNS(ns);
},
});
if (createAwextOpt.unmanageable !== true) { // it means that it is a manageable awext
_pathSetter(Store__AwextInstance, __awextThis.awextGetName(), {
awextFunctions: __awextThis.awextClone(),
baseObject: __awextThis.awextClone(),
});
}
// init at the last step
if (__initFunction) {
__destroyFunc = __initFunction(__awextThis.awextClone());
} else if (__initObject) {
__awextThis.awextSetManyPaths(_objectAssign({}, __initObject));
}
return __awextThis.awextClone();
}
_AwextLib_.createAwext = createAwext;
function hasAwext(awextName) {
awextName = _parsePath(awextName, '/');
return _isAwextPackage(_pathGetter(Store__AwextInstance, awextName));
}
_AwextLib_.hasAwext = hasAwext;
function getAwextFunctions(awextName) {
if (!awextName) throw new Error('Awext name must be provided');
awextName = _parsePath(awextName, '/');
var existingAwext = _pathGetter(Store__AwextInstance, awextName);
if (_isAwextPackage(existingAwext)) {
return existingAwext.awextFunctions.awextClone();
}
return null;
}
_AwextLib_.getAwextFunctions = getAwextFunctions;
function getAllAwexts() {
return _objectAssign({}, Store__AwextInstance);
}
_AwextLib_.getAllAwexts = getAllAwexts;
function getAwext(awextName) {
if (!awextName) throw new Error('Awext name must be provided');
awextName = _parsePath(awextName, '/');
var existingAwext = _pathGetter(Store__AwextInstance, awextName);
if (_isAwextPackage(existingAwext)) {
return existingAwext.baseObject;
}
return null;
}
_AwextLib_.getAwext = getAwext;
function rebaseAwext(awextName, base) {
awextName = _parsePath(awextName, '/');
var existingAwext = _pathGetter(Store__AwextInstance, awextName);
if (existingAwext) {
_objectBindProps(
base,
existingAwext.awextFunctions.awextClone(),
);
existingAwext.baseObject = base;
_pathSetter(Store__AwextInstance, awextName, existingAwext);
} else {
throw new Error('awext ' + JSON.stringify(awextName) + ' Not found');
}
return existingAwext.baseObject;
}
_AwextLib_.rebaseAwext = rebaseAwext;
function discardAwext(awextName) {
awextName = _parsePath(awextName, '/');
var instance = _pathGetter(Store__AwextInstance, awextName);
if (instance) {
instance.awextFunctions.awextDiscard();
_pathDeleter(Store__AwextInstance, awextName);
} else {
throw new Error('awext ' + JSON.stringify(awextName) + ' Not found');
}
}
_AwextLib_.discardAwext = discardAwext;
function awextify(awextifyOpt, baseObject, initArg) {
awextifyOpt = awextifyOpt || {};
if (_isString(awextifyOpt)) awextifyOpt = { awextName: awextifyOpt };
awextifyOpt.awextName = _parsePath(awextifyOpt.awextName, '/');
baseObject = baseObject || {};
if (awextifyOpt.awextName.length > 0 && hasAwext(awextifyOpt.awextName)) {
throw new Error('Awext ' + JSON.stringify(awextifyOpt.awextName) + ' has existed!');
}
if (awextifyOpt.bindThisToBase) {
_objectBindProps(baseObject, baseObject); // bind the functions from base to base itself
}
var initFuncWhenAwextify = null;
var initFuncWhenCreateAwext = null;
var envRef = { initFunc: null, discardFunc: null };
if (_isFunction(initArg)) {
initFuncWhenAwextify = function (_thisAwext) {
envRef.discardFunc = initArg(_thisAwext, baseObject);
};
initFuncWhenCreateAwext = function (_thisAwext) {
if (_isFunction(envRef.initFunc)) {
envRef.initFunc(_thisAwext);
}
return function () {
if (_isFunction(envRef.discardFunc)) {
envRef.discardFunc();
}
};
};
}
var _awext = createAwext(awextifyOpt, initFuncWhenCreateAwext || initArg);
_objectBindProps(baseObject, _awext);
if (initFuncWhenAwextify) {
initFuncWhenAwextify(_awext);
envRef.initFunc = initFuncWhenAwextify;
}
if (awextifyOpt.awextName.length > 0) {
// rebase the awext
return rebaseAwext(
awextifyOpt.awextName,
baseObject,
);
}
return baseObject;
}
_AwextLib_.awextify = awextify;
return _objectAssign({}, _AwextLib_);
}));