native-document 1.0.91 → 1.0.92

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.
@@ -472,7 +472,6 @@ var NativeComponents = (function (exports) {
472
472
 
473
473
  this.$previousValue = null;
474
474
  this.$currentValue = value;
475
- this.$isCleanedUp = false;
476
475
 
477
476
  this.$firstListener = null;
478
477
  this.$listeners = null;
@@ -499,16 +498,16 @@ var NativeComponents = (function (exports) {
499
498
  });
500
499
 
501
500
  ObservableItem.prototype.__$isObservable = true;
502
- const DEFAULT_OPERATIONS = {};
503
501
  const noneTrigger = function() {};
504
502
 
505
503
  ObservableItem.prototype.intercept = function(callback) {
506
504
  this.$interceptor = callback;
505
+ this.set = this.$setWithInterceptor;
507
506
  return this;
508
507
  };
509
508
 
510
509
  ObservableItem.prototype.triggerFirstListener = function(operations) {
511
- this.$firstListener(this.$currentValue, this.$previousValue, operations || {});
510
+ this.$firstListener(this.$currentValue, this.$previousValue, operations);
512
511
  };
513
512
 
514
513
  ObservableItem.prototype.triggerListeners = function(operations) {
@@ -516,33 +515,12 @@ var NativeComponents = (function (exports) {
516
515
  const $previousValue = this.$previousValue;
517
516
  const $currentValue = this.$currentValue;
518
517
 
519
- operations = operations || DEFAULT_OPERATIONS;
520
518
  for(let i = 0, length = $listeners.length; i < length; i++) {
521
519
  $listeners[i]($currentValue, $previousValue, operations);
522
520
  }
523
521
  };
524
522
 
525
- const handleWatcherCallback = function(callbacks, value) {
526
- if(typeof callbacks === "function") {
527
- callbacks(value);
528
- return;
529
- }
530
- if (callbacks.set) {
531
- callbacks.set(value);
532
- return;
533
- }
534
- for(let i = 0, length = callbacks.length; i < length; i++) {
535
- const callback = callbacks[i];
536
- callback.set ? callback.set(value) : callback(value);
537
-
538
- }
539
- };
540
-
541
- ObservableItem.prototype.triggerWatchers = function() {
542
- if(!this.$watchers) {
543
- return;
544
- }
545
-
523
+ ObservableItem.prototype.triggerWatchers = function(operations) {
546
524
  const $watchers = this.$watchers;
547
525
  const $previousValue = this.$previousValue;
548
526
  const $currentValue = this.$currentValue;
@@ -550,20 +528,20 @@ var NativeComponents = (function (exports) {
550
528
  const $currentValueCallbacks = $watchers.get($currentValue);
551
529
  const $previousValueCallbacks = $watchers.get($previousValue);
552
530
  if($currentValueCallbacks) {
553
- handleWatcherCallback($currentValueCallbacks, true);
531
+ $currentValueCallbacks(true, $previousValue, operations);
554
532
  }
555
533
  if($previousValueCallbacks) {
556
- handleWatcherCallback($previousValueCallbacks, false);
534
+ $previousValueCallbacks(false, $currentValue, operations);
557
535
  }
558
536
  };
559
537
 
560
538
  ObservableItem.prototype.triggerAll = function(operations) {
561
- this.triggerWatchers();
539
+ this.triggerWatchers(operations);
562
540
  this.triggerListeners(operations);
563
541
  };
564
542
 
565
543
  ObservableItem.prototype.triggerWatchersAndFirstListener = function(operations) {
566
- this.triggerWatchers();
544
+ this.triggerWatchers(operations);
567
545
  this.triggerFirstListener(operations);
568
546
  };
569
547
 
@@ -591,21 +569,8 @@ var NativeComponents = (function (exports) {
591
569
  };
592
570
  ObservableItem.prototype.trigger = noneTrigger;
593
571
 
594
- /**
595
- * @param {*} data
596
- */
597
- ObservableItem.prototype.set = function(data) {
598
- let newValue = (typeof data === 'function') ? data(this.$currentValue) : data;
599
- newValue = Validator.isObservable(newValue) ? newValue.val() : newValue;
600
-
601
- if (this.$interceptor) {
602
- const result = this.$interceptor(newValue, this.$currentValue);
603
-
604
- if (result !== undefined) {
605
- newValue = result;
606
- }
607
- }
608
-
572
+ ObservableItem.prototype.$updateWithNewValue = function(newValue) {
573
+ newValue = newValue?.__$isObservable ? newValue.val() : newValue;
609
574
  if(this.$currentValue === newValue) {
610
575
  return;
611
576
  }
@@ -615,6 +580,30 @@ var NativeComponents = (function (exports) {
615
580
  this.$previousValue = null;
616
581
  };
617
582
 
583
+ /**
584
+ * @param {*} data
585
+ */
586
+ ObservableItem.prototype.$setWithInterceptor = function(data) {
587
+ let newValue = (typeof data === 'function') ? data(this.$currentValue) : data;
588
+ const result = this.$interceptor(newValue, this.$currentValue);
589
+
590
+ if (result !== undefined) {
591
+ newValue = result;
592
+ }
593
+
594
+ this.$updateWithNewValue(newValue);
595
+ };
596
+
597
+ /**
598
+ * @param {*} data
599
+ */
600
+ ObservableItem.prototype.$basicSet = function(data) {
601
+ let newValue = (typeof data === 'function') ? data(this.$currentValue) : data;
602
+ this.$updateWithNewValue(newValue);
603
+ };
604
+
605
+ ObservableItem.prototype.set = ObservableItem.prototype.$basicSet;
606
+
618
607
  ObservableItem.prototype.val = function() {
619
608
  return this.$currentValue;
620
609
  };
@@ -650,32 +639,19 @@ var NativeComponents = (function (exports) {
650
639
  }
651
640
  MemoryManager.unregister(this.$memoryId);
652
641
  this.disconnectAll();
653
- this.$isCleanedUp = true;
654
642
  delete this.$value;
655
643
  };
656
644
 
657
645
  /**
658
646
  *
659
647
  * @param {Function} callback
660
- * @param {any} target
661
648
  * @returns {(function(): void)}
662
649
  */
663
- ObservableItem.prototype.subscribe = function(callback, target = null) {
650
+ ObservableItem.prototype.subscribe = function(callback) {
664
651
  this.$listeners = this.$listeners ?? [];
665
- if (this.$isCleanedUp) {
666
- DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
667
- return () => {};
668
- }
669
- if (typeof callback !== 'function') {
670
- throw new NativeDocumentError('Callback must be a function');
671
- }
672
652
 
673
653
  this.$listeners.push(callback);
674
654
  this.assocTrigger();
675
- return () => {
676
- this.unsubscribe(callback);
677
- this.assocTrigger();
678
- };
679
655
  };
680
656
 
681
657
  ObservableItem.prototype.on = function(value, callback) {
@@ -683,40 +659,66 @@ var NativeComponents = (function (exports) {
683
659
 
684
660
  let watchValueList = this.$watchers.get(value);
685
661
 
662
+ if(callback.__$isObservable) {
663
+ callback = callback.set.bind(callback);
664
+ }
665
+
686
666
  if(!watchValueList) {
667
+ watchValueList = callback;
687
668
  this.$watchers.set(value, callback);
688
- } else if(!Validator.isArray(watchValueList)) {
669
+ } else if(!Validator.isArray(watchValueList.list)) {
689
670
  watchValueList = [watchValueList, callback];
690
- this.$watchers.set(value, watchValueList);
671
+ callback = (value) => {
672
+ for(let i = 0, length = watchValueList.length; i < length; i++) {
673
+ watchValueList[i](value);
674
+ }
675
+ };
676
+ callback.list = watchValueList;
677
+ this.$watchers.set(value, callback);
691
678
  } else {
692
- watchValueList.push(callback);
679
+ watchValueList.list.push(callback);
693
680
  }
694
681
 
695
682
  this.assocTrigger();
696
- return () => {
697
- const index = watchValueList.indexOf(callback);
698
- watchValueList?.splice(index, 1);
699
- if(watchValueList.size === 1) {
700
- this.$watchers.set(value, watchValueList[0]);
701
- }
702
- else if(watchValueList.size === 0) {
703
- this.$watchers?.delete(value);
704
- watchValueList = null;
705
- }
683
+ };
684
+
685
+ /**
686
+ * @param {*} value
687
+ * @param {Function} callback - if omitted, removes all watchers for this value
688
+ */
689
+ ObservableItem.prototype.off = function(value, callback) {
690
+ if(!this.$watchers) return;
691
+
692
+ const watchValueList = this.$watchers.get(value);
693
+ if(!watchValueList) return;
694
+
695
+ if(!callback || !Array.isArray(watchValueList.list)) {
696
+ this.$watchers?.delete(value);
706
697
  this.assocTrigger();
707
- };
698
+ return;
699
+ }
700
+ const index = watchValueList.indexOf(callback);
701
+ watchValueList?.splice(index, 1);
702
+ if(watchValueList.length === 1) {
703
+ this.$watchers.set(value, watchValueList[0]);
704
+ }
705
+ else if(watchValueList.length === 0) {
706
+ this.$watchers?.delete(value);
707
+ watchValueList = null;
708
+ }
709
+ this.assocTrigger();
708
710
  };
709
711
 
710
712
  ObservableItem.prototype.once = function(predicate, callback) {
711
713
  const fn = typeof predicate === 'function' ? predicate : (v) => v === predicate;
712
714
 
713
- const unsub = this.subscribe((val) => {
715
+ const handler = (val) => {
714
716
  if (fn(val)) {
715
- unsub();
717
+ this.unsubscribe(handler);
716
718
  callback(val);
717
719
  }
718
- });
719
- return unsub;
720
+ };
721
+ this.subscribe(handler);
720
722
  };
721
723
 
722
724
  /**
@@ -724,6 +726,7 @@ var NativeComponents = (function (exports) {
724
726
  * @param {Function} callback
725
727
  */
726
728
  ObservableItem.prototype.unsubscribe = function(callback) {
729
+ if(!this.$listeners) return;
727
730
  const index = this.$listeners.indexOf(callback);
728
731
  if (index > -1) {
729
732
  this.$listeners.splice(index, 1);
@@ -781,6 +784,10 @@ var NativeComponents = (function (exports) {
781
784
  return String(this.$currentValue);
782
785
  };
783
786
 
787
+ ObservableItem.prototype.valueOf = function() {
788
+ return this.$currentValue;
789
+ };
790
+
784
791
  const DocumentObserver = {
785
792
  mounted: new WeakMap(),
786
793
  mountedSupposedSize: 0,
@@ -326,7 +326,9 @@ var NativeDocument = (function (exports) {
326
326
 
327
327
  this.$previousValue = null;
328
328
  this.$currentValue = value;
329
- this.$isCleanedUp = false;
329
+ {
330
+ this.$isCleanedUp = false;
331
+ }
330
332
 
331
333
  this.$firstListener = null;
332
334
  this.$listeners = null;
@@ -356,16 +358,16 @@ var NativeDocument = (function (exports) {
356
358
  });
357
359
 
358
360
  ObservableItem.prototype.__$isObservable = true;
359
- const DEFAULT_OPERATIONS = {};
360
361
  const noneTrigger = function() {};
361
362
 
362
363
  ObservableItem.prototype.intercept = function(callback) {
363
364
  this.$interceptor = callback;
365
+ this.set = this.$setWithInterceptor;
364
366
  return this;
365
367
  };
366
368
 
367
369
  ObservableItem.prototype.triggerFirstListener = function(operations) {
368
- this.$firstListener(this.$currentValue, this.$previousValue, operations || {});
370
+ this.$firstListener(this.$currentValue, this.$previousValue, operations);
369
371
  };
370
372
 
371
373
  ObservableItem.prototype.triggerListeners = function(operations) {
@@ -373,33 +375,12 @@ var NativeDocument = (function (exports) {
373
375
  const $previousValue = this.$previousValue;
374
376
  const $currentValue = this.$currentValue;
375
377
 
376
- operations = operations || DEFAULT_OPERATIONS;
377
378
  for(let i = 0, length = $listeners.length; i < length; i++) {
378
379
  $listeners[i]($currentValue, $previousValue, operations);
379
380
  }
380
381
  };
381
382
 
382
- const handleWatcherCallback = function(callbacks, value) {
383
- if(typeof callbacks === "function") {
384
- callbacks(value);
385
- return;
386
- }
387
- if (callbacks.set) {
388
- callbacks.set(value);
389
- return;
390
- }
391
- for(let i = 0, length = callbacks.length; i < length; i++) {
392
- const callback = callbacks[i];
393
- callback.set ? callback.set(value) : callback(value);
394
-
395
- }
396
- };
397
-
398
- ObservableItem.prototype.triggerWatchers = function() {
399
- if(!this.$watchers) {
400
- return;
401
- }
402
-
383
+ ObservableItem.prototype.triggerWatchers = function(operations) {
403
384
  const $watchers = this.$watchers;
404
385
  const $previousValue = this.$previousValue;
405
386
  const $currentValue = this.$currentValue;
@@ -407,20 +388,20 @@ var NativeDocument = (function (exports) {
407
388
  const $currentValueCallbacks = $watchers.get($currentValue);
408
389
  const $previousValueCallbacks = $watchers.get($previousValue);
409
390
  if($currentValueCallbacks) {
410
- handleWatcherCallback($currentValueCallbacks, true);
391
+ $currentValueCallbacks(true, $previousValue, operations);
411
392
  }
412
393
  if($previousValueCallbacks) {
413
- handleWatcherCallback($previousValueCallbacks, false);
394
+ $previousValueCallbacks(false, $currentValue, operations);
414
395
  }
415
396
  };
416
397
 
417
398
  ObservableItem.prototype.triggerAll = function(operations) {
418
- this.triggerWatchers();
399
+ this.triggerWatchers(operations);
419
400
  this.triggerListeners(operations);
420
401
  };
421
402
 
422
403
  ObservableItem.prototype.triggerWatchersAndFirstListener = function(operations) {
423
- this.triggerWatchers();
404
+ this.triggerWatchers(operations);
424
405
  this.triggerFirstListener(operations);
425
406
  };
426
407
 
@@ -448,21 +429,8 @@ var NativeDocument = (function (exports) {
448
429
  };
449
430
  ObservableItem.prototype.trigger = noneTrigger;
450
431
 
451
- /**
452
- * @param {*} data
453
- */
454
- ObservableItem.prototype.set = function(data) {
455
- let newValue = (typeof data === 'function') ? data(this.$currentValue) : data;
456
- newValue = Validator.isObservable(newValue) ? newValue.val() : newValue;
457
-
458
- if (this.$interceptor) {
459
- const result = this.$interceptor(newValue, this.$currentValue);
460
-
461
- if (result !== undefined) {
462
- newValue = result;
463
- }
464
- }
465
-
432
+ ObservableItem.prototype.$updateWithNewValue = function(newValue) {
433
+ newValue = newValue?.__$isObservable ? newValue.val() : newValue;
466
434
  if(this.$currentValue === newValue) {
467
435
  return;
468
436
  }
@@ -478,6 +446,30 @@ var NativeDocument = (function (exports) {
478
446
  }
479
447
  };
480
448
 
449
+ /**
450
+ * @param {*} data
451
+ */
452
+ ObservableItem.prototype.$setWithInterceptor = function(data) {
453
+ let newValue = (typeof data === 'function') ? data(this.$currentValue) : data;
454
+ const result = this.$interceptor(newValue, this.$currentValue);
455
+
456
+ if (result !== undefined) {
457
+ newValue = result;
458
+ }
459
+
460
+ this.$updateWithNewValue(newValue);
461
+ };
462
+
463
+ /**
464
+ * @param {*} data
465
+ */
466
+ ObservableItem.prototype.$basicSet = function(data) {
467
+ let newValue = (typeof data === 'function') ? data(this.$currentValue) : data;
468
+ this.$updateWithNewValue(newValue);
469
+ };
470
+
471
+ ObservableItem.prototype.set = ObservableItem.prototype.$basicSet;
472
+
481
473
  ObservableItem.prototype.val = function() {
482
474
  return this.$currentValue;
483
475
  };
@@ -513,38 +505,34 @@ var NativeDocument = (function (exports) {
513
505
  }
514
506
  MemoryManager.unregister(this.$memoryId);
515
507
  this.disconnectAll();
516
- this.$isCleanedUp = true;
508
+ {
509
+ this.$isCleanedUp = true;
510
+ }
517
511
  delete this.$value;
518
512
  };
519
513
 
520
514
  /**
521
515
  *
522
516
  * @param {Function} callback
523
- * @param {any} target
524
517
  * @returns {(function(): void)}
525
518
  */
526
- ObservableItem.prototype.subscribe = function(callback, target = null) {
527
- this.$listeners = this.$listeners ?? [];
528
- if (this.$isCleanedUp) {
529
- DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
530
- return () => {};
531
- }
532
- if (typeof callback !== 'function') {
533
- throw new NativeDocumentError('Callback must be a function');
519
+ ObservableItem.prototype.subscribe = function(callback) {
520
+ {
521
+ if (this.$isCleanedUp) {
522
+ DebugManager.warn('Observable subscription', '⚠️ Attempted to subscribe to a cleaned up observable.');
523
+ return;
524
+ }
525
+ if (typeof callback !== 'function') {
526
+ throw new NativeDocumentError('Callback must be a function');
527
+ }
534
528
  }
529
+ this.$listeners = this.$listeners ?? [];
535
530
 
536
531
  this.$listeners.push(callback);
537
532
  this.assocTrigger();
538
533
  {
539
- PluginsManager.emit('ObservableSubscribe', this, target);
534
+ PluginsManager.emit('ObservableSubscribe', this);
540
535
  }
541
- return () => {
542
- this.unsubscribe(callback);
543
- this.assocTrigger();
544
- {
545
- PluginsManager.emit('ObservableUnsubscribe', this);
546
- }
547
- };
548
536
  };
549
537
 
550
538
  ObservableItem.prototype.on = function(value, callback) {
@@ -552,40 +540,66 @@ var NativeDocument = (function (exports) {
552
540
 
553
541
  let watchValueList = this.$watchers.get(value);
554
542
 
543
+ if(callback.__$isObservable) {
544
+ callback = callback.set.bind(callback);
545
+ }
546
+
555
547
  if(!watchValueList) {
548
+ watchValueList = callback;
556
549
  this.$watchers.set(value, callback);
557
- } else if(!Validator.isArray(watchValueList)) {
550
+ } else if(!Validator.isArray(watchValueList.list)) {
558
551
  watchValueList = [watchValueList, callback];
559
- this.$watchers.set(value, watchValueList);
552
+ callback = (value) => {
553
+ for(let i = 0, length = watchValueList.length; i < length; i++) {
554
+ watchValueList[i](value);
555
+ }
556
+ };
557
+ callback.list = watchValueList;
558
+ this.$watchers.set(value, callback);
560
559
  } else {
561
- watchValueList.push(callback);
560
+ watchValueList.list.push(callback);
562
561
  }
563
562
 
564
563
  this.assocTrigger();
565
- return () => {
566
- const index = watchValueList.indexOf(callback);
567
- watchValueList?.splice(index, 1);
568
- if(watchValueList.size === 1) {
569
- this.$watchers.set(value, watchValueList[0]);
570
- }
571
- else if(watchValueList.size === 0) {
572
- this.$watchers?.delete(value);
573
- watchValueList = null;
574
- }
564
+ };
565
+
566
+ /**
567
+ * @param {*} value
568
+ * @param {Function} callback - if omitted, removes all watchers for this value
569
+ */
570
+ ObservableItem.prototype.off = function(value, callback) {
571
+ if(!this.$watchers) return;
572
+
573
+ const watchValueList = this.$watchers.get(value);
574
+ if(!watchValueList) return;
575
+
576
+ if(!callback || !Array.isArray(watchValueList.list)) {
577
+ this.$watchers?.delete(value);
575
578
  this.assocTrigger();
576
- };
579
+ return;
580
+ }
581
+ const index = watchValueList.indexOf(callback);
582
+ watchValueList?.splice(index, 1);
583
+ if(watchValueList.length === 1) {
584
+ this.$watchers.set(value, watchValueList[0]);
585
+ }
586
+ else if(watchValueList.length === 0) {
587
+ this.$watchers?.delete(value);
588
+ watchValueList = null;
589
+ }
590
+ this.assocTrigger();
577
591
  };
578
592
 
579
593
  ObservableItem.prototype.once = function(predicate, callback) {
580
594
  const fn = typeof predicate === 'function' ? predicate : (v) => v === predicate;
581
595
 
582
- const unsub = this.subscribe((val) => {
596
+ const handler = (val) => {
583
597
  if (fn(val)) {
584
- unsub();
598
+ this.unsubscribe(handler);
585
599
  callback(val);
586
600
  }
587
- });
588
- return unsub;
601
+ };
602
+ this.subscribe(handler);
589
603
  };
590
604
 
591
605
  /**
@@ -593,11 +607,15 @@ var NativeDocument = (function (exports) {
593
607
  * @param {Function} callback
594
608
  */
595
609
  ObservableItem.prototype.unsubscribe = function(callback) {
610
+ if(!this.$listeners) return;
596
611
  const index = this.$listeners.indexOf(callback);
597
612
  if (index > -1) {
598
613
  this.$listeners.splice(index, 1);
599
614
  }
600
615
  this.assocTrigger();
616
+ {
617
+ PluginsManager.emit('ObservableUnsubscribe', this);
618
+ }
601
619
  };
602
620
 
603
621
  /**
@@ -650,6 +668,10 @@ var NativeDocument = (function (exports) {
650
668
  return String(this.$currentValue);
651
669
  };
652
670
 
671
+ ObservableItem.prototype.valueOf = function() {
672
+ return this.$currentValue;
673
+ };
674
+
653
675
  const DocumentObserver = {
654
676
  mounted: new WeakMap(),
655
677
  mountedSupposedSize: 0,
@@ -3723,7 +3745,7 @@ var NativeDocument = (function (exports) {
3723
3745
  };
3724
3746
 
3725
3747
  const buildContent = (items, _, operations) => {
3726
- if(operations.action === 'clear' || !items.length) {
3748
+ if(operations?.action === 'clear' || !items.length) {
3727
3749
  if(lastNumberOfItems === 0) {
3728
3750
  return;
3729
3751
  }