native-document 1.0.20 → 1.0.22
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
|
@@ -2,6 +2,7 @@ import DebugManager from "../utils/debug-manager";
|
|
|
2
2
|
import MemoryManager from "./MemoryManager";
|
|
3
3
|
import NativeDocumentError from "../errors/NativeDocumentError";
|
|
4
4
|
import ObservableChecker from "./ObservableChecker";
|
|
5
|
+
import Validator from "../utils/validator";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
*
|
|
@@ -9,13 +10,6 @@ import ObservableChecker from "./ObservableChecker";
|
|
|
9
10
|
* @class ObservableItem
|
|
10
11
|
*/
|
|
11
12
|
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
13
|
this.$previousValue = value;
|
|
20
14
|
this.$currentValue = value;
|
|
21
15
|
this.$isCleanedUp = false;
|
|
@@ -23,7 +17,7 @@ export default function ObservableItem(value) {
|
|
|
23
17
|
this.$listeners = null;
|
|
24
18
|
this.$watchers = null;
|
|
25
19
|
|
|
26
|
-
this.$memoryId =
|
|
20
|
+
this.$memoryId = null;
|
|
27
21
|
}
|
|
28
22
|
|
|
29
23
|
Object.defineProperty(ObservableItem.prototype, '$value', {
|
|
@@ -38,6 +32,7 @@ Object.defineProperty(ObservableItem.prototype, '$value', {
|
|
|
38
32
|
|
|
39
33
|
ObservableItem.prototype.__$isObservable = true;
|
|
40
34
|
|
|
35
|
+
const noneTrigger = function() {};
|
|
41
36
|
ObservableItem.prototype.triggerListeners = function(operations) {
|
|
42
37
|
const $listeners = this.$listeners;
|
|
43
38
|
const $previousValue = this.$previousValue;
|
|
@@ -61,31 +56,38 @@ ObservableItem.prototype.triggerWatchers = function() {
|
|
|
61
56
|
const $currentValue = this.$currentValue;
|
|
62
57
|
|
|
63
58
|
if($watchers.has($currentValue)) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
itemValue.ifTrue.callback();
|
|
70
|
-
itemValue.else.called = false;
|
|
71
|
-
})
|
|
59
|
+
$watchers.get($currentValue).forEach(callback => {
|
|
60
|
+
callback.set ? callback.set(true) : callback(true);
|
|
61
|
+
});
|
|
72
62
|
}
|
|
73
63
|
if($watchers.has($previousValue)) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if(itemValue.else.called) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
itemValue.else.callback();
|
|
80
|
-
itemValue.ifTrue.called = false;
|
|
64
|
+
$watchers.get($previousValue).forEach(callback => {
|
|
65
|
+
callback.set ? callback.set(false) : callback(false);
|
|
81
66
|
});
|
|
82
67
|
}
|
|
83
68
|
};
|
|
84
69
|
|
|
85
|
-
ObservableItem.prototype.
|
|
70
|
+
ObservableItem.prototype.triggerAll = function(operations) {
|
|
86
71
|
this.triggerListeners(operations);
|
|
87
72
|
this.triggerWatchers();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
ObservableItem.prototype.assocTrigger = function() {
|
|
76
|
+
if(this.$watchers?.size && this.$listeners?.length) {
|
|
77
|
+
this.trigger = this.triggerAll;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if(this.$listeners?.length) {
|
|
81
|
+
this.trigger = this.triggerListeners;
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if(this.$watchers?.size) {
|
|
85
|
+
this.trigger = this.triggerWatchers;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
this.trigger = noneTrigger;
|
|
88
89
|
}
|
|
90
|
+
ObservableItem.prototype.trigger = noneTrigger;
|
|
89
91
|
|
|
90
92
|
/**
|
|
91
93
|
* @param {*} data
|
|
@@ -110,16 +112,13 @@ ObservableItem.prototype.disconnectAll = function() {
|
|
|
110
112
|
this.$currentValue = null;
|
|
111
113
|
if(this.$watchers) {
|
|
112
114
|
for (const [_, watchValueList] of this.$watchers) {
|
|
113
|
-
|
|
114
|
-
itemValue.ifTrue.callback = null;
|
|
115
|
-
itemValue.else.callback = null;
|
|
116
|
-
}
|
|
117
|
-
watchValueList.clear();
|
|
115
|
+
watchValueList.splice(0);
|
|
118
116
|
}
|
|
119
117
|
}
|
|
120
118
|
this.$watchers?.clear();
|
|
121
119
|
this.$listeners = null;
|
|
122
120
|
this.$watchers = null;
|
|
121
|
+
this.trigger = noneTrigger;
|
|
123
122
|
}
|
|
124
123
|
ObservableItem.prototype.cleanup = function() {
|
|
125
124
|
MemoryManager.unregister(this.$memoryId);
|
|
@@ -144,30 +143,32 @@ ObservableItem.prototype.subscribe = function(callback) {
|
|
|
144
143
|
}
|
|
145
144
|
|
|
146
145
|
this.$listeners.push(callback);
|
|
147
|
-
|
|
146
|
+
this.assocTrigger();
|
|
147
|
+
return () => {
|
|
148
|
+
this.unsubscribe(callback);
|
|
149
|
+
this.assocTrigger();
|
|
150
|
+
};
|
|
148
151
|
};
|
|
149
152
|
|
|
150
|
-
ObservableItem.prototype.on = function(value, callback
|
|
153
|
+
ObservableItem.prototype.on = function(value, callback) {
|
|
151
154
|
this.$watchers = this.$watchers ?? new Map();
|
|
152
155
|
|
|
153
156
|
let watchValueList = this.$watchers.get(value);
|
|
154
157
|
if(!watchValueList) {
|
|
155
|
-
watchValueList =
|
|
158
|
+
watchValueList = [];
|
|
156
159
|
this.$watchers.set(value, watchValueList);
|
|
157
160
|
}
|
|
158
161
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
else: { callback: elseCallback, called: false }
|
|
162
|
-
};
|
|
163
|
-
watchValueList.add(itemValue);
|
|
162
|
+
watchValueList.push(callback);
|
|
163
|
+
this.assocTrigger();
|
|
164
164
|
return () => {
|
|
165
|
-
watchValueList
|
|
165
|
+
const index = watchValueList.indexOf(callback);
|
|
166
|
+
watchValueList?.splice(index, 1);
|
|
166
167
|
if(watchValueList.size === 0) {
|
|
167
168
|
this.$watchers?.delete(value);
|
|
169
|
+
watchValueList = null;
|
|
168
170
|
}
|
|
169
|
-
|
|
170
|
-
itemValue = null;
|
|
171
|
+
this.assocTrigger();
|
|
171
172
|
};
|
|
172
173
|
};
|
|
173
174
|
|
|
@@ -180,6 +181,7 @@ ObservableItem.prototype.unsubscribe = function(callback) {
|
|
|
180
181
|
if (index > -1) {
|
|
181
182
|
this.$listeners.splice(index, 1);
|
|
182
183
|
}
|
|
184
|
+
this.assocTrigger();
|
|
183
185
|
};
|
|
184
186
|
|
|
185
187
|
/**
|
|
@@ -192,6 +194,9 @@ ObservableItem.prototype.check = function(callback) {
|
|
|
192
194
|
};
|
|
193
195
|
ObservableItem.prototype.get = ObservableItem.prototype.check;
|
|
194
196
|
|
|
195
|
-
|
|
197
|
+
ObservableItem.prototype.toString = function() {
|
|
198
|
+
if(!this.$memoryId) {
|
|
199
|
+
MemoryManager.register(this);
|
|
200
|
+
}
|
|
196
201
|
return '{{#ObItem::(' +this.$memoryId+ ')}}';
|
|
197
202
|
}
|
|
@@ -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
|
}
|