native-document 1.0.20 → 1.0.21
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
|
@@ -9,13 +9,6 @@ import ObservableChecker from "./ObservableChecker";
|
|
|
9
9
|
* @class ObservableItem
|
|
10
10
|
*/
|
|
11
11
|
export default function ObservableItem(value) {
|
|
12
|
-
if (value === undefined) {
|
|
13
|
-
throw new NativeDocumentError('ObservableItem requires an initial value');
|
|
14
|
-
}
|
|
15
|
-
if(value instanceof ObservableItem) {
|
|
16
|
-
throw new NativeDocumentError('ObservableItem cannot be an Observable');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
12
|
this.$previousValue = value;
|
|
20
13
|
this.$currentValue = value;
|
|
21
14
|
this.$isCleanedUp = false;
|
|
@@ -23,7 +16,7 @@ export default function ObservableItem(value) {
|
|
|
23
16
|
this.$listeners = null;
|
|
24
17
|
this.$watchers = null;
|
|
25
18
|
|
|
26
|
-
this.$memoryId =
|
|
19
|
+
this.$memoryId = null;
|
|
27
20
|
}
|
|
28
21
|
|
|
29
22
|
Object.defineProperty(ObservableItem.prototype, '$value', {
|
|
@@ -38,6 +31,7 @@ Object.defineProperty(ObservableItem.prototype, '$value', {
|
|
|
38
31
|
|
|
39
32
|
ObservableItem.prototype.__$isObservable = true;
|
|
40
33
|
|
|
34
|
+
const noneTrigger = function() {};
|
|
41
35
|
ObservableItem.prototype.triggerListeners = function(operations) {
|
|
42
36
|
const $listeners = this.$listeners;
|
|
43
37
|
const $previousValue = this.$previousValue;
|
|
@@ -61,31 +55,38 @@ ObservableItem.prototype.triggerWatchers = function() {
|
|
|
61
55
|
const $currentValue = this.$currentValue;
|
|
62
56
|
|
|
63
57
|
if($watchers.has($currentValue)) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
itemValue.ifTrue.callback();
|
|
70
|
-
itemValue.else.called = false;
|
|
71
|
-
})
|
|
58
|
+
$watchers.get($currentValue).forEach(callback => {
|
|
59
|
+
callback(true);
|
|
60
|
+
});
|
|
72
61
|
}
|
|
73
62
|
if($watchers.has($previousValue)) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if(itemValue.else.called) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
itemValue.else.callback();
|
|
80
|
-
itemValue.ifTrue.called = false;
|
|
63
|
+
$watchers.get($previousValue).forEach(callback => {
|
|
64
|
+
callback(false);
|
|
81
65
|
});
|
|
82
66
|
}
|
|
83
67
|
};
|
|
84
68
|
|
|
85
|
-
ObservableItem.prototype.
|
|
69
|
+
ObservableItem.prototype.triggerAll = function(operations) {
|
|
86
70
|
this.triggerListeners(operations);
|
|
87
71
|
this.triggerWatchers();
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
ObservableItem.prototype.assocTrigger = function() {
|
|
75
|
+
if(this.$watchers?.size && this.$listeners?.length) {
|
|
76
|
+
this.trigger = this.triggerAll;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if(this.$listeners?.length) {
|
|
80
|
+
this.trigger = this.triggerListeners;
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if(this.$watchers?.size) {
|
|
84
|
+
this.trigger = this.triggerWatchers;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
this.trigger = noneTrigger;
|
|
88
88
|
}
|
|
89
|
+
ObservableItem.prototype.trigger = noneTrigger;
|
|
89
90
|
|
|
90
91
|
/**
|
|
91
92
|
* @param {*} data
|
|
@@ -110,16 +111,13 @@ ObservableItem.prototype.disconnectAll = function() {
|
|
|
110
111
|
this.$currentValue = null;
|
|
111
112
|
if(this.$watchers) {
|
|
112
113
|
for (const [_, watchValueList] of this.$watchers) {
|
|
113
|
-
|
|
114
|
-
itemValue.ifTrue.callback = null;
|
|
115
|
-
itemValue.else.callback = null;
|
|
116
|
-
}
|
|
117
|
-
watchValueList.clear();
|
|
114
|
+
watchValueList.splice(0);
|
|
118
115
|
}
|
|
119
116
|
}
|
|
120
117
|
this.$watchers?.clear();
|
|
121
118
|
this.$listeners = null;
|
|
122
119
|
this.$watchers = null;
|
|
120
|
+
this.trigger = noneTrigger;
|
|
123
121
|
}
|
|
124
122
|
ObservableItem.prototype.cleanup = function() {
|
|
125
123
|
MemoryManager.unregister(this.$memoryId);
|
|
@@ -144,30 +142,32 @@ ObservableItem.prototype.subscribe = function(callback) {
|
|
|
144
142
|
}
|
|
145
143
|
|
|
146
144
|
this.$listeners.push(callback);
|
|
147
|
-
|
|
145
|
+
this.assocTrigger();
|
|
146
|
+
return () => {
|
|
147
|
+
this.unsubscribe(callback);
|
|
148
|
+
this.assocTrigger();
|
|
149
|
+
};
|
|
148
150
|
};
|
|
149
151
|
|
|
150
|
-
ObservableItem.prototype.on = function(value, callback
|
|
152
|
+
ObservableItem.prototype.on = function(value, callback) {
|
|
151
153
|
this.$watchers = this.$watchers ?? new Map();
|
|
152
154
|
|
|
153
155
|
let watchValueList = this.$watchers.get(value);
|
|
154
156
|
if(!watchValueList) {
|
|
155
|
-
watchValueList =
|
|
157
|
+
watchValueList = [];
|
|
156
158
|
this.$watchers.set(value, watchValueList);
|
|
157
159
|
}
|
|
158
160
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
else: { callback: elseCallback, called: false }
|
|
162
|
-
};
|
|
163
|
-
watchValueList.add(itemValue);
|
|
161
|
+
watchValueList.push(callback);
|
|
162
|
+
this.assocTrigger();
|
|
164
163
|
return () => {
|
|
165
|
-
watchValueList
|
|
164
|
+
const index = watchValueList.indexOf(callback);
|
|
165
|
+
watchValueList?.splice(index, 1);
|
|
166
166
|
if(watchValueList.size === 0) {
|
|
167
167
|
this.$watchers?.delete(value);
|
|
168
|
+
watchValueList = null;
|
|
168
169
|
}
|
|
169
|
-
|
|
170
|
-
itemValue = null;
|
|
170
|
+
this.assocTrigger();
|
|
171
171
|
};
|
|
172
172
|
};
|
|
173
173
|
|
|
@@ -180,6 +180,7 @@ ObservableItem.prototype.unsubscribe = function(callback) {
|
|
|
180
180
|
if (index > -1) {
|
|
181
181
|
this.$listeners.splice(index, 1);
|
|
182
182
|
}
|
|
183
|
+
this.assocTrigger();
|
|
183
184
|
};
|
|
184
185
|
|
|
185
186
|
/**
|
|
@@ -192,6 +193,9 @@ ObservableItem.prototype.check = function(callback) {
|
|
|
192
193
|
};
|
|
193
194
|
ObservableItem.prototype.get = ObservableItem.prototype.check;
|
|
194
195
|
|
|
195
|
-
|
|
196
|
+
ObservableItem.prototype.toString = function() {
|
|
197
|
+
if(!this.$memoryId) {
|
|
198
|
+
MemoryManager.register(this);
|
|
199
|
+
}
|
|
196
200
|
return '{{#ObItem::(' +this.$memoryId+ ')}}';
|
|
197
201
|
}
|
|
@@ -24,20 +24,20 @@ Observable.array = function(target) {
|
|
|
24
24
|
});
|
|
25
25
|
|
|
26
26
|
observer.clear = function() {
|
|
27
|
-
observer
|
|
27
|
+
observer.val().length = 0;
|
|
28
28
|
observer.trigger({ action: 'clear' });
|
|
29
29
|
return true;
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
observer.merge = function(values) {
|
|
33
|
-
observer
|
|
33
|
+
observer.set([...observer.val(), ...values]);
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
observer.populateAndRender = function(iteration, callback) {
|
|
37
|
-
observer.trigger({ action: 'populate', args: [observer
|
|
37
|
+
observer.trigger({ action: 'populate', args: [observer.val(), iteration, callback] });
|
|
38
38
|
};
|
|
39
39
|
observer.remove = function(index) {
|
|
40
|
-
const deleted = observer
|
|
40
|
+
const deleted = observer.val().splice(index, 1);
|
|
41
41
|
if(deleted.length === 0) {
|
|
42
42
|
return [];
|
|
43
43
|
}
|
|
@@ -46,7 +46,7 @@ Observable.array = function(target) {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
observer.swap = function(indexA, indexB) {
|
|
49
|
-
const value = observer
|
|
49
|
+
const value = observer.val();
|
|
50
50
|
const length = value.length;
|
|
51
51
|
if(length < indexA || length < indexB) {
|
|
52
52
|
return false;
|
|
@@ -66,7 +66,7 @@ Observable.array = function(target) {
|
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
observer.length = function() {
|
|
69
|
-
return observer
|
|
69
|
+
return observer.val().length;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
const overrideMethods = ['map', 'filter', 'reduce', 'some', 'every', 'find', 'findIndex', 'concat'];
|
|
@@ -11,7 +11,6 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
11
11
|
const blockStart = element.startElement();
|
|
12
12
|
|
|
13
13
|
let cache = new Map();
|
|
14
|
-
let nodeCacheByElement = new WeakMap();
|
|
15
14
|
let lastNumberOfItems = 0;
|
|
16
15
|
|
|
17
16
|
const keysCache = new WeakMap();
|
|
@@ -26,7 +25,10 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
26
25
|
return keysCache.get(item);
|
|
27
26
|
}
|
|
28
27
|
return getKey(item, indexKey, key);
|
|
29
|
-
}
|
|
28
|
+
};
|
|
29
|
+
const getItemChild = (item) => {
|
|
30
|
+
return getChildByKey(getItemKey(item));
|
|
31
|
+
};
|
|
30
32
|
|
|
31
33
|
const updateIndexObservers = (items, startFrom = 0) => {
|
|
32
34
|
if(callback.length < 2) {
|
|
@@ -47,13 +49,10 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
47
49
|
if(!cacheItem) {
|
|
48
50
|
return;
|
|
49
51
|
}
|
|
50
|
-
const child = cacheItem.child
|
|
52
|
+
const child = cacheItem.child;
|
|
51
53
|
cacheItem.indexObserver?.deref()?.cleanup();
|
|
52
54
|
cacheItem.child = null;
|
|
53
55
|
cacheItem.indexObserver = null;
|
|
54
|
-
nodeCacheByElement.delete(cacheItem.item);
|
|
55
|
-
keysCache.delete(cacheItem.item);
|
|
56
|
-
cacheItem.item = null;
|
|
57
56
|
if(removeChild) {
|
|
58
57
|
child?.remove();
|
|
59
58
|
cache.delete(cacheItem.keyId);
|
|
@@ -77,8 +76,7 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
77
76
|
if(cache.has(keyId)) {
|
|
78
77
|
const cacheItem = cache.get(keyId);
|
|
79
78
|
cacheItem.indexObserver?.deref()?.set(indexKey);
|
|
80
|
-
|
|
81
|
-
const child = cacheItem.child?.deref();
|
|
79
|
+
const child = cacheItem.child;
|
|
82
80
|
if(child) {
|
|
83
81
|
return child;
|
|
84
82
|
}
|
|
@@ -90,27 +88,22 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
90
88
|
let child = ElementCreator.getChild(callback(item, indexObserver));
|
|
91
89
|
cache.set(keyId, {
|
|
92
90
|
keyId,
|
|
93
|
-
|
|
94
|
-
item,
|
|
95
|
-
child: new WeakRef(child),
|
|
91
|
+
child: child,
|
|
96
92
|
indexObserver: (indexObserver ? new WeakRef(indexObserver) : null)
|
|
97
93
|
});
|
|
98
94
|
keysCache.set(item, keyId);
|
|
99
|
-
if(Validator.isObject(item)) {
|
|
100
|
-
nodeCacheByElement.set(item, child);
|
|
101
|
-
}
|
|
102
95
|
return child;
|
|
103
96
|
} catch (e) {
|
|
104
97
|
DebugManager.error('ForEach', `Error creating element for key ${keyId}` , e);
|
|
105
98
|
throw e;
|
|
106
99
|
}
|
|
107
100
|
};
|
|
108
|
-
const getChildByKey = function(keyId
|
|
101
|
+
const getChildByKey = function(keyId) {
|
|
109
102
|
const cacheItem = cache.get(keyId);
|
|
110
103
|
if(!cacheItem) {
|
|
111
104
|
return null;
|
|
112
105
|
}
|
|
113
|
-
const child = cacheItem.child
|
|
106
|
+
const child = cacheItem.child;
|
|
114
107
|
if(!child) {
|
|
115
108
|
removeCacheItem(cacheItem, false);
|
|
116
109
|
return null;
|
|
@@ -123,7 +116,7 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
123
116
|
if(!cacheItem) {
|
|
124
117
|
return null;
|
|
125
118
|
}
|
|
126
|
-
const child = cacheItem.child
|
|
119
|
+
const child = cacheItem.child;
|
|
127
120
|
if(!child) {
|
|
128
121
|
return null;
|
|
129
122
|
}
|
|
@@ -157,7 +150,7 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
157
150
|
let child = null;
|
|
158
151
|
const fragment = document.createDocumentFragment();
|
|
159
152
|
for(const item of items) {
|
|
160
|
-
child =
|
|
153
|
+
child = getItemChild(item);
|
|
161
154
|
if(child) {
|
|
162
155
|
fragment.appendChild(child);
|
|
163
156
|
}
|
|
@@ -166,11 +159,9 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
166
159
|
element.appendElement(fragment, blockEnd);
|
|
167
160
|
},
|
|
168
161
|
removeOne(element, index) {
|
|
169
|
-
let child =
|
|
162
|
+
let child = getItemChild(element);
|
|
170
163
|
if(child) {
|
|
171
|
-
|
|
172
|
-
nodeCacheByElement.delete(element);
|
|
173
|
-
removeCacheItemByKey(getItemKey(element, index));
|
|
164
|
+
removeCacheItemByKey(getItemKey(element, index), true);
|
|
174
165
|
}
|
|
175
166
|
child = null;
|
|
176
167
|
},
|
|
@@ -246,8 +237,8 @@ export function ForEachArray(data, callback, key, configs = {}) {
|
|
|
246
237
|
swap(args, elements) {
|
|
247
238
|
const parent = blockEnd.parentNode;
|
|
248
239
|
|
|
249
|
-
let childA =
|
|
250
|
-
let childB =
|
|
240
|
+
let childA = getItemChild(elements[0]);
|
|
241
|
+
let childB = getItemChild(elements[1]);
|
|
251
242
|
if(!childA || !childB) {
|
|
252
243
|
return;
|
|
253
244
|
}
|