-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.js
326 lines (293 loc) · 11.3 KB
/
item.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* Class representing an item.
* @extends EventTarget
*/
export class Item extends EventTarget {
#value;
#parent;
#key;
#filled = false;
#isGetting = false;
#isSetting = false;
constructor(parent, key){
super();
this.#parent = parent;
this.#key = key;
this.addEventListener('change', () => triggerEffectsFor(this) ); // the method that triggers the change effect can be overwritten by a child class, therefore we use the event
}
get key(){ return this.#key }
get parent(){ return this.#parent }
get filled(){ return this.#filled }
set value(value){ this.set(value); }
get value(){ return this.get(); }
get(){
if (this.#isGetting) throw new Error('circular get');
this.#isGetting = true;
dispatchEvent(this, 'get', { item: this, value:this.#value });
registerCurrentEffectFor(this);
this.#isGetting = false;
return this.$get();
}
set(value){
if (this.#isSetting) throw new Error('circular set');
this.#isSetting = true;
const obj = dispatchEvent(this, 'set', { item:this, oldValue:this.#value , value });
const result = !obj.defaultPrevented ? this.$set(value) : null;
this.#isSetting = false;
return result;
}
$get(){
if (this.constructor.isPrimitive(this.#value)) {
return this.#value;
} else {
const value = this.#value ??= Object.create(null); // if undefined, create object
return Object.fromEntries(Object.entries(value).map(([key, {value}]) => [key, value]));
}
}
$set(value){
const oldValue = this.#value;
if (this.constructor.isPrimitive(value)) {
if (!this.#filled || !this.constructor.equals(oldValue, value)) {
this.#value = value; // structuralClone(value); // TODO: should we clone the value? or should we just use the reference? (if its an object)
this.#filled = true;
if (!this.#isGetting) {
dispatchEvent(this, 'change', { item: this, oldValue, value });
} else {
console.warn('just for your info: set while getting dont trigger change');
}
}
} else {
for (const key in value) this.item(key).set(value[key]);
if (this.#value && Object(this.#value)) { // remove keys that are not in value
for (const key in this.#value) if (!(key in value)) this.#value[key].remove();
}
}
}
item(key){
key = String(key);
if (this.#value == null || typeof this.#value !== 'object') { // item() forces value always to be object
this.#value = Object.create(null);
this.#filled = true;
}
if (!(key in this.#value)) {
const Klass = this.ChildClass ?? this.constructor;
const item = new Klass(this, key);
dispatchEvent(this, 'change', { item: this, add:item });
this.#value[key] = item;
}
return this.#value[key];
}
remove(){
if (!this.#parent) throw new Error('cannot remove root item');
delete this.#parent.#value[this.#key];
dispatchEvent(this.#parent, 'change', { item: this.#parent, remove: this });
}
has(key){ return key in this.#value; }
get proxy(){ return toProxy(this); }
// path
get path() {
if (this.#parent == null) return [this];
return [...this.#parent.path, this];
}
get pathKeys() {
return this.path.slice(1).map(item => item.key);
}
walkPathKeys(keys) {
if (keys.length === 0) return this;
return this.item(keys[0]).walkPathKeys(keys.slice(1));
}
toJSON() { return this.get(); }
valueOf() { return this.get(); }
toString() { return this.get()+'' } // if its an object, this.key would be better
// iterator
// iterators should probably trigger "get", but not compute the value
*[Symbol.iterator]() {
for (const key in this.get()) yield this.#value[key];
}
async *[Symbol.asyncIterator]() {
for (const item of Object.values(this.#value ?? {})) yield item;
const abortCtrl = new AbortController();
const iterator = asyncIteratorFromEventTarget(this, 'change', { signal:abortCtrl.signal });
this.loadItems().then(() => abortCtrl.abort());
for await (const {detail:{add}} of iterator) if (add) yield add;
}
loadItems(){ throw new Error('not implemented'); } // can be overwritten by child class
static isPrimitive(value){
return value !== Object(value) || 'toJSON' in value || value instanceof Promise;
}
static equals(a, b){ // comparison function between old and new value in case of primitive
if (Object.is(a, b)) return true; // // TODO: we shoul use deepEqual as "primitive" can be an object
}
static ChildClass;
ChildClass = this.constructor.ChildClass;
}
/**
* Create a new Item instance.
* @param {any} [value] - The initial value.
* @return {Item} A new Item instance.
*/
export function item(...args) {
const v = new Item();
if (args.length > 0) v.set(args[0]);
return v;
}
// signal / effect
const relatedEffects = new WeakMap();
let currentEffect = null;
/**
* Execute the provided function and re-execute it when dependencies change.
* @param {function} fn - A function that executes imeediately and collects the containing items.
* @return {function} A function to dispose the effect.
*/
export function effect(fn){ // async?
const outer = currentEffect;
if (outer) {
(outer.nested ??= new Set()).add(fn);
if (fn.parent && fn.parent !== outer) throw('effect(cb) callbacks should not be reused for other effects');
fn.parent = outer;
}
currentEffect = fn;
try { fn(); } // await, so that signals in async functions are collected?
finally { currentEffect = outer; }
return () => fn.disposed = true
}
/**
* Create a computed item based on the provided calculation function.
* @param {function} calc - The calculation function.
* @return {Item} A new computed item.
*/
export function computed(calc){
const signal = item();
effect(()=>signal.set(calc())); // todo: only primitive?
return signal;
}
let batches = null;
function batch(effect) {
if (batches) return batches.add(effect); // currently collecting
batches = new Set([effect]);
queueMicrotask(() => {
batches.forEach(fn => {
if (batches.has(fn?.parent)) return; // its parent has also to run, so it will run anyway
currentEffect = fn; // effect() called inside fn(callback) has to know his parent effect
// todo? try
fn(); // TODO? fn({rerun:fn}) to rerun effect? https://github.com/nuxodin/item.js/issues/2
});
batches = null; // restart collecting
});
}
function registerCurrentEffectFor(signal) {
if (currentEffect) {
if (!relatedEffects.has(signal)) relatedEffects.set(signal, new Set());
relatedEffects.get(signal).add(currentEffect);
}
}
function triggerEffectsFor(signal) {
const effects = relatedEffects.get(signal);
if (effects) {
effects.forEach(fn => {
fn.nested?.forEach(fn => fn.disposed = true); // dispose child-effects
if (fn.disposed) return effects.delete(fn);
batch(fn);
});
}
}
// proxy
// todo? should proxy[iterator] iterate over item
const proxyHandler = {
get: function(target, property, receiver){
if (target.asyncHandler && property === 'then') { // instanceof AsyncItem, make it a thenable, would be great if it can be handled by a proxy-trap
return (onFulfilled, onRejected) => target.get().then(onFulfilled, onRejected);
}
if (property === '$item') return target;
// if (property === Symbol.iterator) { // also loop proxys
// return function* () {
// for (const key in target.get()) {
// yield receiver[key];
// }
// }
// }
if (typeof property === 'symbol') {
if (typeof target[property] === 'function') return target[property].bind(target);
return Reflect.get(target, property, receiver);
}
const item = target.item(property);
const value = item.get(); // TODO?: Is accessing item.get() here good? it will trigger a get event (E.g. fetch data)
return item.constructor.isPrimitive(value) ? value : toProxy(item);
},
set: function(target, property, value){
target.item(property).set(value);
return true;
},
has: (target, property) => target.has(property),
ownKeys: (target)=> Reflect.ownKeys(target.get()),
getOwnPropertyDescriptor: (target, property)=>{
return { configurable: true, enumerable: true, get: ()=>target.get(property), set: (value)=>target.set(property, value) };
},
deleteProperty: function(target, property){
target.item(property).remove();
return true;
},
};
const cachedProxies = new WeakMap();
const toProxy = (item) => {
if (!cachedProxies.has(item)) cachedProxies.set(item, new Proxy(item, proxyHandler));
return cachedProxies.get(item);
}
/**
* Dispatch a custom event on the item and its ancestors.
* @param {Item} item - The item to dispatch the event on.
* @param {string} eventName - The name of the event. eventName+"In" will be dispatched on the ancestors. (bubbles)
* @param {Object} detail - The event details.
* @return {Object} An object containing a `defaultPrevented` property.
*/
export function dispatchEvent(item, eventName, detail){
const options = {detail, cancelable: true};
const event = new CustomEvent(eventName, options);
item.dispatchEvent(event);
const eventIn = new CustomEvent(eventName + 'In', options);
let current = item;
while (current) {
current.dispatchEvent(eventIn);
current = current.parent;
}
return {
defaultPrevented: eventIn.defaultPrevented || event.defaultPrevented,
}
}
/**
* Creates a async iterator from an event target.
* @param {EventTarget} eventTarget - The event target.
* @param {string} eventName - The name of the event.
* @param {Object} [options] - The event listener options.
* @return {Generator} A generator that yields events.
* @example
* const abortCtrl = new AbortController();
* for await (const event of asyncIteratorFromEventTarget(document, 'click', {signal: abortCtrl.signal})) {
* console.log(event);
* }
* setTimeout(() => abortCtrl.abort(), 1000);
*/
function* asyncIteratorFromEventTarget(eventTarget, eventName, options) {
const queue = [];
let stopAfterQueue = false;
let resolve;
const eventHandler = event => {
resolve ? resolve(event) : queue.push(event);
resolve = null;
};
eventTarget.addEventListener(eventName, eventHandler, options);
options?.signal?.addEventListener('abort', () => stopAfterQueue = true);
try {
while (true) {
if (queue.length) {
yield queue.shift();
} else if (stopAfterQueue) {
return;
} else {
yield new Promise(res => resolve = res);
}
}
} finally {
eventTarget.removeEventListener(eventName, eventHandler, options);
}
}