native-document 1.0.253 → 1.0.255

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "native-document",
3
- "version": "1.0.253",
3
+ "version": "1.0.255",
4
4
  "description": "A reactive JavaScript framework that preserves native DOM simplicity without sacrificing modern features",
5
5
  "author": "AfroCodeur <https://github.com/afrocodeur>",
6
6
  "license": "MIT",
@@ -49,9 +49,6 @@ export default function FieldCollection(name, props = {}) {
49
49
  props,
50
50
  };
51
51
 
52
- this.$description.value.interceptMutations((items) =>
53
- items.map(item => Validator.isObservable(item) ? item : $.object(item)),
54
- );
55
52
  }
56
53
 
57
54
  BaseComponent.extends(FieldCollection);
@@ -149,9 +146,6 @@ FieldCollection.prototype.transition = function(transitionName) {
149
146
  FieldCollection.prototype.model = function(observable) {
150
147
  if(Validator.isObservable(observable)) {
151
148
  this.$description.value = observable;
152
- this.$description.value.interceptMutations((items) => {
153
- return items.map(item => Validator.isObservable(item) ? item : $.object(item));
154
- });
155
149
  return this;
156
150
  }
157
151
  this.$description.value.set(observable);
@@ -196,6 +196,7 @@ Observable.auto = (value, options = null) => {
196
196
  return new ObservableItem(value, options);
197
197
  };
198
198
  ObservableItem.auto = Observable.auto;
199
+ ObservableArray.auto = Observable.auto;
199
200
 
200
201
 
201
202
  Observable.init = function(initialValue, configs = null) {
@@ -8,6 +8,8 @@ import {nextTick} from '../utils/helpers';
8
8
  export const mutationMethods = ['push', 'pop', 'shift', 'unshift', 'reverse', 'sort', 'splice'];
9
9
  export const noMutationMethods = ['map', 'forEach', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat', 'includes', 'indexOf'];
10
10
 
11
+ const toObservable = (item) => item?.__$Observable ? item : ObservableArray.auto(item);
12
+
11
13
  /**
12
14
  * Reactive array container extending ObservableItem.
13
15
  * Wraps a native array and triggers reactivity on mutations (push, pop, splice, etc.).
@@ -22,11 +24,31 @@ export const noMutationMethods = ['map', 'forEach', 'filter', 'reduce', 'some',
22
24
  * const items = Observable.array([1, 2, 3]);
23
25
  * items.push(4); // triggers reactivity
24
26
  */
25
- const ObservableArray = function (target, configs = null) {
27
+ const ObservableArray = function (target, configs = { propagation: false, deep: true, reset: false }) {
26
28
  if(!Array.isArray(target)) {
27
29
  throw new NativeDocumentError('Observable.array : target must be an array');
28
30
  }
29
31
 
32
+
33
+ if(configs?.deep) {
34
+ target = target.map(toObservable);
35
+ this.interceptMutations((data, operations) => {
36
+ if(!operations.action || ['push', 'unshift', 'set', 'splice'].includes(operations.action)) {
37
+ if(operations.action === 'splice') {
38
+ for(let i = 2, length = data.length; i < length; ++i) {
39
+ data[i] = toObservable(data[i]);
40
+ }
41
+ return data;
42
+ }
43
+ if(operations.action === 'set') {
44
+ data[0] = data[0].map(toObservable);
45
+ return data;
46
+ }
47
+ return data.map(toObservable);
48
+ }
49
+ });
50
+ }
51
+
30
52
  ObservableItem.call(this, target, configs);
31
53
  if(process.env.NODE_ENV === 'development') {
32
54
  PluginsManager.emit('CreateObservableArray', this);
@@ -36,7 +58,7 @@ const ObservableArray = function (target, configs = null) {
36
58
  ObservableArray.prototype = Object.create(ObservableItem.prototype);
37
59
  ObservableArray.prototype.constructor = ObservableArray;
38
60
  ObservableArray.prototype.__$isObservableArray = true;
39
-
61
+ ObservableArray.auto = () => {};
40
62
 
41
63
  Object.defineProperty(ObservableArray.prototype, 'length', {
42
64
  get() {
@@ -48,13 +70,21 @@ Object.defineProperty(ObservableArray.prototype, 'length', {
48
70
  ObservableArray.prototype.$mutate = function(action, args, mutateFn) {
49
71
  if(this.$mutationInterceptor) {
50
72
  const value = this.$mutationInterceptor(args, { action });
51
- if(args !== undefined) {
73
+ if(value != null) {
52
74
  args = value;
53
75
  }
54
76
  }
55
77
  mutateFn(args);
56
78
  };
57
79
 
80
+ const $originalSet = ObservableArray.prototype.set;
81
+ ObservableArray.prototype.set = function(...values) {
82
+ return this.$mutate('set', values, (argsToUse) => {
83
+ $originalSet.call(this, ...argsToUse);
84
+ return argsToUse[0];
85
+ });
86
+ };
87
+
58
88
  mutationMethods.forEach((method) => {
59
89
  ObservableArray.prototype[method] = function(...values) {
60
90
  return this.$mutate(method, values, (argsToUse) => {
@@ -525,31 +555,27 @@ ObservableArray.prototype.isNotEmpty = function () {
525
555
  };
526
556
 
527
557
  ObservableArray.prototype.fnPush = function (item) {
528
- const self = this;
529
- return () => self.push(item);
558
+ return () => this.push(item);
530
559
  };
531
560
 
532
561
  ObservableArray.prototype.fnRemove = function (item) {
533
- const self = this;
534
- return () => self.removeItem(item);
562
+ return () => this.removeItem(item);
535
563
  };
536
564
 
537
565
  ObservableArray.prototype.fnRemoveAt = function (index) {
538
- const self = this;
539
- return () => self.remove(index);
566
+ return () => this.remove(index);
540
567
  };
541
568
 
542
569
  ObservableArray.prototype.fnClear = function () {
543
- const self = this;
544
- return () => self.clear();
570
+ return () => this.clear();
545
571
  };
546
572
 
547
- ObservableArray.prototype.rawValue = function () {
548
- const result = [];
549
- for(const element of this.$currentValue) {
550
- result.push(element?.__$Observable ? element.val() : element);
551
- }
552
- return result;
553
- };
573
+ // ObservableArray.prototype.resolve = function () {
574
+ // const result = [];
575
+ // for(const element of this.$currentValue) {
576
+ // result.push(element?.__$Observable ? element.val() : element);
577
+ // }
578
+ // return result;
579
+ // };
554
580
 
555
581
  export default ObservableArray;
@@ -734,26 +734,21 @@ ObservableItem.prototype.watch = function(fn, autoCall = true) {
734
734
 
735
735
 
736
736
  ObservableItem.prototype.fnSet = function(value) {
737
- const self = this;
738
- return () => self.set(value);
737
+ return () => this.set(value);
739
738
  };
740
739
 
741
740
  ObservableItem.prototype.fnToggle = function() {
742
- const self = this;
743
- return () => self.toggle();
741
+ return () => this.toggle();
744
742
  };
745
743
 
746
744
  ObservableItem.prototype.fnSetTrue = function() {
747
- const self = this;
748
- return () => self.set(true);
745
+ return () => this.set(true);
749
746
  };
750
747
 
751
748
  ObservableItem.prototype.fnSetFalse = function() {
752
- const self = this;
753
- return () => self.set(false);
749
+ return () => this.set(false);
754
750
  };
755
751
 
756
752
  ObservableItem.prototype.fnReset = function() {
757
- const self = this;
758
- return () => self.reset();
753
+ return () => this.reset();
759
754
  };