native-document 1.0.21 → 1.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -111,6 +111,8 @@ var NativeDocument = (function (exports) {
111
111
  this.unSubscriptions = [];
112
112
  }
113
113
 
114
+ ObservableChecker.prototype.__$isObservableChecker = true;
115
+
114
116
  ObservableChecker.prototype.subscribe = function(callback) {
115
117
  const unSubscribe = this.observable.subscribe((value) => {
116
118
  callback && callback(this.checker(value));
@@ -145,13 +147,6 @@ var NativeDocument = (function (exports) {
145
147
  * @class ObservableItem
146
148
  */
147
149
  function ObservableItem(value) {
148
- if (value === undefined) {
149
- throw new NativeDocumentError('ObservableItem requires an initial value');
150
- }
151
- if(value instanceof ObservableItem) {
152
- throw new NativeDocumentError('ObservableItem cannot be an Observable');
153
- }
154
-
155
150
  this.$previousValue = value;
156
151
  this.$currentValue = value;
157
152
  this.$isCleanedUp = false;
@@ -159,7 +154,7 @@ var NativeDocument = (function (exports) {
159
154
  this.$listeners = null;
160
155
  this.$watchers = null;
161
156
 
162
- this.$memoryId = MemoryManager.register(this);
157
+ this.$memoryId = null;
163
158
  }
164
159
 
165
160
  Object.defineProperty(ObservableItem.prototype, '$value', {
@@ -172,6 +167,9 @@ var NativeDocument = (function (exports) {
172
167
  configurable: true,
173
168
  });
174
169
 
170
+ ObservableItem.prototype.__$isObservable = true;
171
+
172
+ const noneTrigger = function() {};
175
173
  ObservableItem.prototype.triggerListeners = function(operations) {
176
174
  const $listeners = this.$listeners;
177
175
  const $previousValue = this.$previousValue;
@@ -195,32 +193,39 @@ var NativeDocument = (function (exports) {
195
193
  const $currentValue = this.$currentValue;
196
194
 
197
195
  if($watchers.has($currentValue)) {
198
- const watchValueList = $watchers.get($currentValue);
199
- watchValueList.forEach(itemValue => {
200
- if(itemValue.ifTrue.called) {
201
- return;
202
- }
203
- itemValue.ifTrue.callback();
204
- itemValue.else.called = false;
196
+ $watchers.get($currentValue).forEach(callback => {
197
+ callback.set ? callback.set(true) : callback(true);
205
198
  });
206
199
  }
207
200
  if($watchers.has($previousValue)) {
208
- const watchValueList = $watchers.get($previousValue);
209
- watchValueList.forEach(itemValue => {
210
- if(itemValue.else.called) {
211
- return;
212
- }
213
- itemValue.else.callback();
214
- itemValue.ifTrue.called = false;
201
+ $watchers.get($previousValue).forEach(callback => {
202
+ callback.set ? callback.set(false) : callback(false);
215
203
  });
216
204
  }
217
205
  };
218
206
 
219
- ObservableItem.prototype.trigger = function(operations) {
207
+ ObservableItem.prototype.triggerAll = function(operations) {
220
208
  this.triggerListeners(operations);
221
209
  this.triggerWatchers();
222
210
  };
223
211
 
212
+ ObservableItem.prototype.assocTrigger = function() {
213
+ if(this.$watchers?.size && this.$listeners?.length) {
214
+ this.trigger = this.triggerAll;
215
+ return;
216
+ }
217
+ if(this.$listeners?.length) {
218
+ this.trigger = this.triggerListeners;
219
+ return;
220
+ }
221
+ if(this.$watchers?.size) {
222
+ this.trigger = this.triggerWatchers;
223
+ return;
224
+ }
225
+ this.trigger = noneTrigger;
226
+ };
227
+ ObservableItem.prototype.trigger = noneTrigger;
228
+
224
229
  /**
225
230
  * @param {*} data
226
231
  */
@@ -244,16 +249,13 @@ var NativeDocument = (function (exports) {
244
249
  this.$currentValue = null;
245
250
  if(this.$watchers) {
246
251
  for (const [_, watchValueList] of this.$watchers) {
247
- for (const itemValue of watchValueList) {
248
- itemValue.ifTrue.callback = null;
249
- itemValue.else.callback = null;
250
- }
251
- watchValueList.clear();
252
+ watchValueList.splice(0);
252
253
  }
253
254
  }
254
255
  this.$watchers?.clear();
255
256
  this.$listeners = null;
256
257
  this.$watchers = null;
258
+ this.trigger = noneTrigger;
257
259
  };
258
260
  ObservableItem.prototype.cleanup = function() {
259
261
  MemoryManager.unregister(this.$memoryId);
@@ -278,30 +280,32 @@ var NativeDocument = (function (exports) {
278
280
  }
279
281
 
280
282
  this.$listeners.push(callback);
281
- return () => this.unsubscribe(callback);
283
+ this.assocTrigger();
284
+ return () => {
285
+ this.unsubscribe(callback);
286
+ this.assocTrigger();
287
+ };
282
288
  };
283
289
 
284
- ObservableItem.prototype.on = function(value, callback, elseCallback) {
290
+ ObservableItem.prototype.on = function(value, callback) {
285
291
  this.$watchers = this.$watchers ?? new Map();
286
292
 
287
293
  let watchValueList = this.$watchers.get(value);
288
294
  if(!watchValueList) {
289
- watchValueList = new Set();
295
+ watchValueList = [];
290
296
  this.$watchers.set(value, watchValueList);
291
297
  }
292
298
 
293
- let itemValue = {
294
- ifTrue: { callback, called: false },
295
- else: { callback: elseCallback, called: false }
296
- };
297
- watchValueList.add(itemValue);
299
+ watchValueList.push(callback);
300
+ this.assocTrigger();
298
301
  return () => {
299
- watchValueList?.delete(itemValue);
302
+ const index = watchValueList.indexOf(callback);
303
+ watchValueList?.splice(index, 1);
300
304
  if(watchValueList.size === 0) {
301
305
  this.$watchers?.delete(value);
306
+ watchValueList = null;
302
307
  }
303
- watchValueList = null;
304
- itemValue = null;
308
+ this.assocTrigger();
305
309
  };
306
310
  };
307
311
 
@@ -314,6 +318,7 @@ var NativeDocument = (function (exports) {
314
318
  if (index > -1) {
315
319
  this.$listeners.splice(index, 1);
316
320
  }
321
+ this.assocTrigger();
317
322
  };
318
323
 
319
324
  /**
@@ -326,7 +331,10 @@ var NativeDocument = (function (exports) {
326
331
  };
327
332
  ObservableItem.prototype.get = ObservableItem.prototype.check;
328
333
 
329
- ObservableItem.prototype.toString = function() {
334
+ ObservableItem.prototype.toString = function() {
335
+ if(!this.$memoryId) {
336
+ MemoryManager.register(this);
337
+ }
330
338
  return '{{#ObItem::(' +this.$memoryId+ ')}}';
331
339
  };
332
340
 
@@ -500,6 +508,7 @@ var NativeDocument = (function (exports) {
500
508
  this.$element = element;
501
509
  this.$observer = null;
502
510
  }
511
+ NDElement.prototype.__$isNDElement = true;
503
512
 
504
513
  for(const event of EVENTS) {
505
514
  const eventName = event.toLowerCase();
@@ -532,18 +541,18 @@ var NativeDocument = (function (exports) {
532
541
  }
533
542
 
534
543
  NDElement.prototype.ref = function(target, name) {
535
- target[name] = element;
544
+ target[name] = this.$element;
536
545
  return this;
537
546
  };
538
547
 
539
548
  NDElement.prototype.unmountChildren = function() {
540
549
  let element = this.$element;
541
550
  for(let i = 0, length = element.children.length; i < length; i++) {
542
- let elementchildren = element.children[i];
543
- if(!elementchildren.$ndProx) {
544
- elementchildren.nd?.remove();
551
+ let elementChildren = element.children[i];
552
+ if(!elementChildren.$ndProx) {
553
+ elementChildren.nd?.remove();
545
554
  }
546
- elementchildren = null;
555
+ elementChildren = null;
547
556
  }
548
557
  element = null;
549
558
  return this;
@@ -571,7 +580,7 @@ var NativeDocument = (function (exports) {
571
580
  return this.lifecycle({ mounted: callback });
572
581
  };
573
582
 
574
- NDElement.prototype.mounted = function(callback) {
583
+ NDElement.prototype.unmounted = function(callback) {
575
584
  return this.lifecycle({ unmounted: callback });
576
585
  };
577
586
 
@@ -583,13 +592,13 @@ var NativeDocument = (function (exports) {
583
592
 
584
593
  const Validator = {
585
594
  isObservable(value) {
586
- return value instanceof ObservableItem || value instanceof ObservableChecker;
595
+ return value instanceof ObservableItem || value instanceof ObservableChecker || value?.__$isObservable;
587
596
  },
588
597
  isProxy(value) {
589
598
  return value?.__isProxy__
590
599
  },
591
600
  isObservableChecker(value) {
592
- return value instanceof ObservableChecker;
601
+ return value instanceof ObservableChecker || value?.__$isObservableChecker;
593
602
  },
594
603
  isArray(value) {
595
604
  return Array.isArray(value);
@@ -632,7 +641,7 @@ var NativeDocument = (function (exports) {
632
641
  ['string', 'number', 'boolean'].includes(typeof child);
633
642
  },
634
643
  isNDElement(child) {
635
- return child instanceof NDElement;
644
+ return child instanceof NDElement || child?.constructor?.__$isNDElement;
636
645
  },
637
646
  isValidChildren(children) {
638
647
  if (!Array.isArray(children)) {
@@ -1099,7 +1108,7 @@ var NativeDocument = (function (exports) {
1099
1108
  return child;
1100
1109
  }
1101
1110
  if(Validator.isNDElement(child)) {
1102
- return child.$element;
1111
+ return child.$element ?? child.$build?.() ?? null;
1103
1112
  }
1104
1113
  return ElementCreator.createStaticTextNode(null, child);
1105
1114
  },
@@ -1127,6 +1136,7 @@ var NativeDocument = (function (exports) {
1127
1136
  };
1128
1137
 
1129
1138
  Object.defineProperty(HTMLElement.prototype, 'nd', {
1139
+ configurable: true,
1130
1140
  get() {
1131
1141
  if(this.$nd) {
1132
1142
  return this.$nd;
@@ -1333,20 +1343,20 @@ var NativeDocument = (function (exports) {
1333
1343
  });
1334
1344
 
1335
1345
  observer.clear = function() {
1336
- observer.$value.length = 0;
1346
+ observer.val().length = 0;
1337
1347
  observer.trigger({ action: 'clear' });
1338
1348
  return true;
1339
1349
  };
1340
1350
 
1341
1351
  observer.merge = function(values) {
1342
- observer.$value = [...observer.$value, ...values];
1352
+ observer.set([...observer.val(), ...values]);
1343
1353
  };
1344
1354
 
1345
1355
  observer.populateAndRender = function(iteration, callback) {
1346
- observer.trigger({ action: 'populate', args: [observer.$value, iteration, callback] });
1356
+ observer.trigger({ action: 'populate', args: [observer.val(), iteration, callback] });
1347
1357
  };
1348
1358
  observer.remove = function(index) {
1349
- const deleted = observer.$value.splice(index, 1);
1359
+ const deleted = observer.val().splice(index, 1);
1350
1360
  if(deleted.length === 0) {
1351
1361
  return [];
1352
1362
  }
@@ -1355,7 +1365,7 @@ var NativeDocument = (function (exports) {
1355
1365
  };
1356
1366
 
1357
1367
  observer.swap = function(indexA, indexB) {
1358
- const value = observer.$value;
1368
+ const value = observer.val();
1359
1369
  const length = value.length;
1360
1370
  if(length < indexA || length < indexB) {
1361
1371
  return false;
@@ -1375,7 +1385,7 @@ var NativeDocument = (function (exports) {
1375
1385
  };
1376
1386
 
1377
1387
  observer.length = function() {
1378
- return observer.$value.length;
1388
+ return observer.val().length;
1379
1389
  };
1380
1390
 
1381
1391
  const overrideMethods = ['map', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat'];
@@ -1484,9 +1494,10 @@ var NativeDocument = (function (exports) {
1484
1494
  }
1485
1495
  if(Validator.isArray(data)) {
1486
1496
  const result = [];
1487
- data.forEach(item => {
1497
+ for(let i = 0, length = data.length; i < length; i++) {
1498
+ const item = data[i];
1488
1499
  result.push(Observable.value(item));
1489
- });
1500
+ }
1490
1501
  return result;
1491
1502
  }
1492
1503
  return data;
@@ -1758,7 +1769,6 @@ var NativeDocument = (function (exports) {
1758
1769
  const blockStart = element.startElement();
1759
1770
 
1760
1771
  let cache = new Map();
1761
- let nodeCacheByElement = new WeakMap();
1762
1772
  let lastNumberOfItems = 0;
1763
1773
 
1764
1774
  const keysCache = new WeakMap();
@@ -1774,6 +1784,9 @@ var NativeDocument = (function (exports) {
1774
1784
  }
1775
1785
  return getKey(item, indexKey, key);
1776
1786
  };
1787
+ const getItemChild = (item) => {
1788
+ return getChildByKey(getItemKey(item));
1789
+ };
1777
1790
 
1778
1791
  const updateIndexObservers = (items, startFrom = 0) => {
1779
1792
  if(callback.length < 2) {
@@ -1794,13 +1807,10 @@ var NativeDocument = (function (exports) {
1794
1807
  if(!cacheItem) {
1795
1808
  return;
1796
1809
  }
1797
- const child = cacheItem.child?.deref();
1810
+ const child = cacheItem.child;
1798
1811
  cacheItem.indexObserver?.deref()?.cleanup();
1799
1812
  cacheItem.child = null;
1800
1813
  cacheItem.indexObserver = null;
1801
- nodeCacheByElement.delete(cacheItem.item);
1802
- keysCache.delete(cacheItem.item);
1803
- cacheItem.item = null;
1804
1814
  if(removeChild) {
1805
1815
  child?.remove();
1806
1816
  cache.delete(cacheItem.keyId);
@@ -1824,8 +1834,7 @@ var NativeDocument = (function (exports) {
1824
1834
  if(cache.has(keyId)) {
1825
1835
  const cacheItem = cache.get(keyId);
1826
1836
  cacheItem.indexObserver?.deref()?.set(indexKey);
1827
- cacheItem.isNew = false;
1828
- const child = cacheItem.child?.deref();
1837
+ const child = cacheItem.child;
1829
1838
  if(child) {
1830
1839
  return child;
1831
1840
  }
@@ -1837,27 +1846,22 @@ var NativeDocument = (function (exports) {
1837
1846
  let child = ElementCreator.getChild(callback(item, indexObserver));
1838
1847
  cache.set(keyId, {
1839
1848
  keyId,
1840
- isNew: true,
1841
- item,
1842
- child: new WeakRef(child),
1849
+ child: child,
1843
1850
  indexObserver: (indexObserver ? new WeakRef(indexObserver) : null)
1844
1851
  });
1845
1852
  keysCache.set(item, keyId);
1846
- if(Validator.isObject(item)) {
1847
- nodeCacheByElement.set(item, child);
1848
- }
1849
1853
  return child;
1850
1854
  } catch (e) {
1851
1855
  DebugManager$1.error('ForEach', `Error creating element for key ${keyId}` , e);
1852
1856
  throw e;
1853
1857
  }
1854
1858
  };
1855
- const getChildByKey = function(keyId, fragment) {
1859
+ const getChildByKey = function(keyId) {
1856
1860
  const cacheItem = cache.get(keyId);
1857
1861
  if(!cacheItem) {
1858
1862
  return null;
1859
1863
  }
1860
- const child = cacheItem.child?.deref();
1864
+ const child = cacheItem.child;
1861
1865
  if(!child) {
1862
1866
  removeCacheItem(cacheItem, false);
1863
1867
  return null;
@@ -1870,7 +1874,7 @@ var NativeDocument = (function (exports) {
1870
1874
  if(!cacheItem) {
1871
1875
  return null;
1872
1876
  }
1873
- const child = cacheItem.child?.deref();
1877
+ const child = cacheItem.child;
1874
1878
  if(!child) {
1875
1879
  return null;
1876
1880
  }
@@ -1904,7 +1908,7 @@ var NativeDocument = (function (exports) {
1904
1908
  let child = null;
1905
1909
  const fragment = document.createDocumentFragment();
1906
1910
  for(const item of items) {
1907
- child = nodeCacheByElement.get(item);
1911
+ child = getItemChild(item);
1908
1912
  if(child) {
1909
1913
  fragment.appendChild(child);
1910
1914
  }
@@ -1913,11 +1917,9 @@ var NativeDocument = (function (exports) {
1913
1917
  element.appendElement(fragment, blockEnd);
1914
1918
  },
1915
1919
  removeOne(element, index) {
1916
- let child = nodeCacheByElement.get(element);
1920
+ let child = getItemChild(element);
1917
1921
  if(child) {
1918
- child.remove();
1919
- nodeCacheByElement.delete(element);
1920
- removeCacheItemByKey(getItemKey(element, index));
1922
+ removeCacheItemByKey(getItemKey(element, index), true);
1921
1923
  }
1922
1924
  child = null;
1923
1925
  },
@@ -1993,8 +1995,8 @@ var NativeDocument = (function (exports) {
1993
1995
  swap(args, elements) {
1994
1996
  const parent = blockEnd.parentNode;
1995
1997
 
1996
- let childA = nodeCacheByElement.get(elements[0]);
1997
- let childB = nodeCacheByElement.get(elements[1]);
1998
+ let childA = getItemChild(elements[0]);
1999
+ let childB = getItemChild(elements[1]);
1998
2000
  if(!childA || !childB) {
1999
2001
  return;
2000
2002
  }
@@ -2011,7 +2013,6 @@ var NativeDocument = (function (exports) {
2011
2013
  if(operations?.action === 'populate') {
2012
2014
  Actions.populate(operations.args, operations.result);
2013
2015
  } else {
2014
- console.log(lastNumberOfItems);
2015
2016
  if(operations.action === 'clear' || !items.length) {
2016
2017
  if(lastNumberOfItems === 0) {
2017
2018
  return;
@@ -2031,7 +2032,6 @@ var NativeDocument = (function (exports) {
2031
2032
  }
2032
2033
  }
2033
2034
 
2034
- console.log(items);
2035
2035
  updateIndexObservers(items, 0);
2036
2036
  };
2037
2037
 
@@ -3190,7 +3190,6 @@ var NativeDocument = (function (exports) {
3190
3190
  }
3191
3191
  const routerName = target.router || DEFAULT_ROUTER_NAME;
3192
3192
  const router = Router.get(routerName);
3193
- console.log(routerName);
3194
3193
  if(!router) {
3195
3194
  throw new RouterError('Router not found "'+routerName+'" for link "'+target.name+'"');
3196
3195
  }