native-document 1.0.22 → 1.0.24

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)) {
@@ -976,20 +985,14 @@ var NativeDocument = (function (exports) {
976
985
  let value = attributes[attributeName];
977
986
  if(Validator.isString(value) && Validator.isFunction(value.resolveObservableTemplate)) {
978
987
  value = value.resolveObservableTemplate();
979
- if(Validator.isArray(value)) {
980
- const observables = value.filter(item => Validator.isObservable(item));
981
- value = Observable.computed(() => {
982
- return value.map(item => Validator.isObservable(item) ? item.val() : item).join(' ') || ' ';
983
- }, observables);
988
+ if(Validator.isString(value)) {
989
+ element.setAttribute(attributeName, value);
990
+ continue;
984
991
  }
985
- }
986
- if(BOOLEAN_ATTRIBUTES.includes(attributeName)) {
987
- bindBooleanAttribute(element, attributeName, value);
988
- continue;
989
- }
990
- if(Validator.isObservable(value)) {
991
- bindAttributeWithObservable(element, attributeName, value);
992
- continue;
992
+ const observables = value.filter(item => Validator.isObservable(item));
993
+ value = Observable.computed(() => {
994
+ return value.map(item => Validator.isObservable(item) ? item.val() : item).join(' ') || ' ';
995
+ }, observables);
993
996
  }
994
997
  if(attributeName === 'class' && Validator.isJson(value)) {
995
998
  bindClassAttribute(element, value);
@@ -999,6 +1002,14 @@ var NativeDocument = (function (exports) {
999
1002
  bindStyleAttribute(element, value);
1000
1003
  continue;
1001
1004
  }
1005
+ if(BOOLEAN_ATTRIBUTES.includes(attributeName)) {
1006
+ bindBooleanAttribute(element, attributeName, value);
1007
+ continue;
1008
+ }
1009
+ if(Validator.isObservable(value)) {
1010
+ bindAttributeWithObservable(element, attributeName, value);
1011
+ continue;
1012
+ }
1002
1013
  element.setAttribute(attributeName, value);
1003
1014
 
1004
1015
  }
@@ -1099,7 +1110,7 @@ var NativeDocument = (function (exports) {
1099
1110
  return child;
1100
1111
  }
1101
1112
  if(Validator.isNDElement(child)) {
1102
- return child.$element;
1113
+ return child.$element ?? child.$build?.() ?? null;
1103
1114
  }
1104
1115
  return ElementCreator.createStaticTextNode(null, child);
1105
1116
  },
@@ -1127,6 +1138,7 @@ var NativeDocument = (function (exports) {
1127
1138
  };
1128
1139
 
1129
1140
  Object.defineProperty(HTMLElement.prototype, 'nd', {
1141
+ configurable: true,
1130
1142
  get() {
1131
1143
  if(this.$nd) {
1132
1144
  return this.$nd;
@@ -1333,20 +1345,20 @@ var NativeDocument = (function (exports) {
1333
1345
  });
1334
1346
 
1335
1347
  observer.clear = function() {
1336
- observer.$value.length = 0;
1348
+ observer.val().length = 0;
1337
1349
  observer.trigger({ action: 'clear' });
1338
1350
  return true;
1339
1351
  };
1340
1352
 
1341
1353
  observer.merge = function(values) {
1342
- observer.$value = [...observer.$value, ...values];
1354
+ observer.set([...observer.val(), ...values]);
1343
1355
  };
1344
1356
 
1345
1357
  observer.populateAndRender = function(iteration, callback) {
1346
- observer.trigger({ action: 'populate', args: [observer.$value, iteration, callback] });
1358
+ observer.trigger({ action: 'populate', args: [observer.val(), iteration, callback] });
1347
1359
  };
1348
1360
  observer.remove = function(index) {
1349
- const deleted = observer.$value.splice(index, 1);
1361
+ const deleted = observer.val().splice(index, 1);
1350
1362
  if(deleted.length === 0) {
1351
1363
  return [];
1352
1364
  }
@@ -1355,7 +1367,7 @@ var NativeDocument = (function (exports) {
1355
1367
  };
1356
1368
 
1357
1369
  observer.swap = function(indexA, indexB) {
1358
- const value = observer.$value;
1370
+ const value = observer.val();
1359
1371
  const length = value.length;
1360
1372
  if(length < indexA || length < indexB) {
1361
1373
  return false;
@@ -1375,7 +1387,7 @@ var NativeDocument = (function (exports) {
1375
1387
  };
1376
1388
 
1377
1389
  observer.length = function() {
1378
- return observer.$value.length;
1390
+ return observer.val().length;
1379
1391
  };
1380
1392
 
1381
1393
  const overrideMethods = ['map', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat'];
@@ -1484,9 +1496,10 @@ var NativeDocument = (function (exports) {
1484
1496
  }
1485
1497
  if(Validator.isArray(data)) {
1486
1498
  const result = [];
1487
- data.forEach(item => {
1499
+ for(let i = 0, length = data.length; i < length; i++) {
1500
+ const item = data[i];
1488
1501
  result.push(Observable.value(item));
1489
- });
1502
+ }
1490
1503
  return result;
1491
1504
  }
1492
1505
  return data;
@@ -1758,8 +1771,8 @@ var NativeDocument = (function (exports) {
1758
1771
  const blockStart = element.startElement();
1759
1772
 
1760
1773
  let cache = new Map();
1761
- let nodeCacheByElement = new WeakMap();
1762
1774
  let lastNumberOfItems = 0;
1775
+ const isIndexRequired = callback.length >= 2;
1763
1776
 
1764
1777
  const keysCache = new WeakMap();
1765
1778
 
@@ -1774,9 +1787,12 @@ var NativeDocument = (function (exports) {
1774
1787
  }
1775
1788
  return getKey(item, indexKey, key);
1776
1789
  };
1790
+ const getItemChild = (item) => {
1791
+ return getChildByKey(getItemKey(item));
1792
+ };
1777
1793
 
1778
1794
  const updateIndexObservers = (items, startFrom = 0) => {
1779
- if(callback.length < 2) {
1795
+ if(!isIndexRequired) {
1780
1796
  return;
1781
1797
  }
1782
1798
  let index = startFrom;
@@ -1794,13 +1810,10 @@ var NativeDocument = (function (exports) {
1794
1810
  if(!cacheItem) {
1795
1811
  return;
1796
1812
  }
1797
- const child = cacheItem.child?.deref();
1813
+ const child = cacheItem.child;
1798
1814
  cacheItem.indexObserver?.deref()?.cleanup();
1799
1815
  cacheItem.child = null;
1800
1816
  cacheItem.indexObserver = null;
1801
- nodeCacheByElement.delete(cacheItem.item);
1802
- keysCache.delete(cacheItem.item);
1803
- cacheItem.item = null;
1804
1817
  if(removeChild) {
1805
1818
  child?.remove();
1806
1819
  cache.delete(cacheItem.keyId);
@@ -1824,8 +1837,7 @@ var NativeDocument = (function (exports) {
1824
1837
  if(cache.has(keyId)) {
1825
1838
  const cacheItem = cache.get(keyId);
1826
1839
  cacheItem.indexObserver?.deref()?.set(indexKey);
1827
- cacheItem.isNew = false;
1828
- const child = cacheItem.child?.deref();
1840
+ const child = cacheItem.child;
1829
1841
  if(child) {
1830
1842
  return child;
1831
1843
  }
@@ -1833,31 +1845,26 @@ var NativeDocument = (function (exports) {
1833
1845
  }
1834
1846
 
1835
1847
  try {
1836
- const indexObserver = callback.length >= 2 ? Observable(indexKey) : null;
1848
+ const indexObserver = isIndexRequired ? Observable(indexKey) : null;
1837
1849
  let child = ElementCreator.getChild(callback(item, indexObserver));
1838
1850
  cache.set(keyId, {
1839
1851
  keyId,
1840
- isNew: true,
1841
- item,
1842
- child: new WeakRef(child),
1852
+ child: child,
1843
1853
  indexObserver: (indexObserver ? new WeakRef(indexObserver) : null)
1844
1854
  });
1845
1855
  keysCache.set(item, keyId);
1846
- if(Validator.isObject(item)) {
1847
- nodeCacheByElement.set(item, child);
1848
- }
1849
1856
  return child;
1850
1857
  } catch (e) {
1851
1858
  DebugManager$1.error('ForEach', `Error creating element for key ${keyId}` , e);
1852
1859
  throw e;
1853
1860
  }
1854
1861
  };
1855
- const getChildByKey = function(keyId, fragment) {
1862
+ const getChildByKey = function(keyId) {
1856
1863
  const cacheItem = cache.get(keyId);
1857
1864
  if(!cacheItem) {
1858
1865
  return null;
1859
1866
  }
1860
- const child = cacheItem.child?.deref();
1867
+ const child = cacheItem.child;
1861
1868
  if(!child) {
1862
1869
  removeCacheItem(cacheItem, false);
1863
1870
  return null;
@@ -1870,7 +1877,7 @@ var NativeDocument = (function (exports) {
1870
1877
  if(!cacheItem) {
1871
1878
  return null;
1872
1879
  }
1873
- const child = cacheItem.child?.deref();
1880
+ const child = cacheItem.child;
1874
1881
  if(!child) {
1875
1882
  return null;
1876
1883
  }
@@ -1904,7 +1911,7 @@ var NativeDocument = (function (exports) {
1904
1911
  let child = null;
1905
1912
  const fragment = document.createDocumentFragment();
1906
1913
  for(const item of items) {
1907
- child = nodeCacheByElement.get(item);
1914
+ child = getItemChild(item);
1908
1915
  if(child) {
1909
1916
  fragment.appendChild(child);
1910
1917
  }
@@ -1913,11 +1920,9 @@ var NativeDocument = (function (exports) {
1913
1920
  element.appendElement(fragment, blockEnd);
1914
1921
  },
1915
1922
  removeOne(element, index) {
1916
- let child = nodeCacheByElement.get(element);
1923
+ let child = getItemChild(element);
1917
1924
  if(child) {
1918
- child.remove();
1919
- nodeCacheByElement.delete(element);
1920
- removeCacheItemByKey(getItemKey(element, index));
1925
+ removeCacheItemByKey(getItemKey(element, index), true);
1921
1926
  }
1922
1927
  child = null;
1923
1928
  },
@@ -1993,8 +1998,8 @@ var NativeDocument = (function (exports) {
1993
1998
  swap(args, elements) {
1994
1999
  const parent = blockEnd.parentNode;
1995
2000
 
1996
- let childA = nodeCacheByElement.get(elements[0]);
1997
- let childB = nodeCacheByElement.get(elements[1]);
2001
+ let childA = getItemChild(elements[0]);
2002
+ let childB = getItemChild(elements[1]);
1998
2003
  if(!childA || !childB) {
1999
2004
  return;
2000
2005
  }
@@ -2011,7 +2016,6 @@ var NativeDocument = (function (exports) {
2011
2016
  if(operations?.action === 'populate') {
2012
2017
  Actions.populate(operations.args, operations.result);
2013
2018
  } else {
2014
- console.log(lastNumberOfItems);
2015
2019
  if(operations.action === 'clear' || !items.length) {
2016
2020
  if(lastNumberOfItems === 0) {
2017
2021
  return;
@@ -2031,7 +2035,6 @@ var NativeDocument = (function (exports) {
2031
2035
  }
2032
2036
  }
2033
2037
 
2034
- console.log(items);
2035
2038
  updateIndexObservers(items, 0);
2036
2039
  };
2037
2040
 
@@ -3190,7 +3193,6 @@ var NativeDocument = (function (exports) {
3190
3193
  }
3191
3194
  const routerName = target.router || DEFAULT_ROUTER_NAME;
3192
3195
  const router = Router.get(routerName);
3193
- console.log(routerName);
3194
3196
  if(!router) {
3195
3197
  throw new RouterError('Router not found "'+routerName+'" for link "'+target.name+'"');
3196
3198
  }