@chocbite/ts-lib-state 1.0.0
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/dist/index.d.ts +1430 -0
- package/dist/index.js +3698 -0
- package/package.json +45 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3698 @@
|
|
|
1
|
+
// src/array/sync.ts
|
|
2
|
+
import {
|
|
3
|
+
err as err2,
|
|
4
|
+
none as none2,
|
|
5
|
+
ok as ok2
|
|
6
|
+
} from "@chocbite/ts-lib-result";
|
|
7
|
+
|
|
8
|
+
// src/types.ts
|
|
9
|
+
var STATE_KEY = /* @__PURE__ */ Symbol("is_state");
|
|
10
|
+
|
|
11
|
+
// src/base.ts
|
|
12
|
+
var StateBase = class {
|
|
13
|
+
get [STATE_KEY]() {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
#subscribers = /* @__PURE__ */ new Set();
|
|
17
|
+
#read_promises;
|
|
18
|
+
sub(func, update) {
|
|
19
|
+
if (this.#subscribers.has(func)) {
|
|
20
|
+
console.error("Function already registered as subscriber", this, func);
|
|
21
|
+
return func;
|
|
22
|
+
}
|
|
23
|
+
if (this.#subscribers.size === 0) this.on_subscribe();
|
|
24
|
+
this.#subscribers.add(func);
|
|
25
|
+
if (update) this.then(func);
|
|
26
|
+
return func;
|
|
27
|
+
}
|
|
28
|
+
unsub(func) {
|
|
29
|
+
if (this.#subscribers.delete(func)) {
|
|
30
|
+
if (this.#subscribers.size == 0) this.on_unsubscribe();
|
|
31
|
+
} else console.error("Subscriber not found with state", this, func);
|
|
32
|
+
return func;
|
|
33
|
+
}
|
|
34
|
+
in_use() {
|
|
35
|
+
return this.#subscribers.size > 0 ? this : void 0;
|
|
36
|
+
}
|
|
37
|
+
has(subscriber) {
|
|
38
|
+
return this.#subscribers.has(subscriber) ? this : void 0;
|
|
39
|
+
}
|
|
40
|
+
amount() {
|
|
41
|
+
return this.#subscribers.size;
|
|
42
|
+
}
|
|
43
|
+
/**Called when subscriber is added*/
|
|
44
|
+
on_subscribe() {
|
|
45
|
+
}
|
|
46
|
+
/**Called when subscriber is removed*/
|
|
47
|
+
on_unsubscribe() {
|
|
48
|
+
}
|
|
49
|
+
/**Updates all subscribers with a value */
|
|
50
|
+
update_subs(value) {
|
|
51
|
+
for (const subscriber of this.#subscribers) {
|
|
52
|
+
try {
|
|
53
|
+
subscriber(value);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
console.error("Failed while calling subscribers ", e, this, subscriber);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//Promises
|
|
60
|
+
/**Creates a promise which can be fulfilled later with fulRProm */
|
|
61
|
+
async append_r_prom(func) {
|
|
62
|
+
return func(
|
|
63
|
+
await new Promise((a) => {
|
|
64
|
+
(this.#read_promises ??= []).push(
|
|
65
|
+
a
|
|
66
|
+
);
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
/**Fulfills all read promises with given value */
|
|
71
|
+
ful_r_prom(value) {
|
|
72
|
+
if (this.#read_promises)
|
|
73
|
+
for (let i = 0; i < this.#read_promises.length; i++)
|
|
74
|
+
this.#read_promises[i](value);
|
|
75
|
+
this.#read_promises = [];
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/sync/sync.ts
|
|
81
|
+
import {
|
|
82
|
+
err,
|
|
83
|
+
none,
|
|
84
|
+
ok
|
|
85
|
+
} from "@chocbite/ts-lib-result";
|
|
86
|
+
var RXS = class extends StateBase {
|
|
87
|
+
constructor(init, helper, setter) {
|
|
88
|
+
super();
|
|
89
|
+
if (setter === true)
|
|
90
|
+
this.#setter = (value, state2, old) => {
|
|
91
|
+
if (old && !old.err && value === old.value)
|
|
92
|
+
return ok(void 0);
|
|
93
|
+
return this.#helper?.limit ? this.#helper?.limit(value).map((e) => state2.set_ok(e)) : ok(state2.set_ok(value));
|
|
94
|
+
};
|
|
95
|
+
else this.#setter = setter;
|
|
96
|
+
if (helper) this.#helper = helper;
|
|
97
|
+
this.#value = init;
|
|
98
|
+
}
|
|
99
|
+
#value;
|
|
100
|
+
#setter;
|
|
101
|
+
#helper;
|
|
102
|
+
//#Owner Context
|
|
103
|
+
set(value) {
|
|
104
|
+
this.update_subs(this.#value = value);
|
|
105
|
+
}
|
|
106
|
+
set_ok(value) {
|
|
107
|
+
this.set(ok(value));
|
|
108
|
+
}
|
|
109
|
+
set_err(error) {
|
|
110
|
+
this.set(err(error));
|
|
111
|
+
}
|
|
112
|
+
set setter(setter) {
|
|
113
|
+
this.#setter = setter;
|
|
114
|
+
}
|
|
115
|
+
get setter() {
|
|
116
|
+
return this.#setter;
|
|
117
|
+
}
|
|
118
|
+
get state() {
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
get read_only() {
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
get read_write() {
|
|
125
|
+
return this.#setter ? this : void 0;
|
|
126
|
+
}
|
|
127
|
+
//#Reader Context
|
|
128
|
+
get rok() {
|
|
129
|
+
return this.#value.ok;
|
|
130
|
+
}
|
|
131
|
+
get rsync() {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
async then(func) {
|
|
135
|
+
return func(this.#value);
|
|
136
|
+
}
|
|
137
|
+
get() {
|
|
138
|
+
return this.#value;
|
|
139
|
+
}
|
|
140
|
+
ok() {
|
|
141
|
+
return this.#value.value;
|
|
142
|
+
}
|
|
143
|
+
related() {
|
|
144
|
+
return this.#helper?.related ? this.#helper.related() : none();
|
|
145
|
+
}
|
|
146
|
+
//#Writer Context
|
|
147
|
+
get writable() {
|
|
148
|
+
return this.#setter !== void 0;
|
|
149
|
+
}
|
|
150
|
+
get wsync() {
|
|
151
|
+
return this.writable;
|
|
152
|
+
}
|
|
153
|
+
async write(value) {
|
|
154
|
+
return this.write_sync(value);
|
|
155
|
+
}
|
|
156
|
+
write_sync(value) {
|
|
157
|
+
if (this.#setter)
|
|
158
|
+
return this.#setter(
|
|
159
|
+
value,
|
|
160
|
+
this,
|
|
161
|
+
this.#value
|
|
162
|
+
);
|
|
163
|
+
return err("State not writable");
|
|
164
|
+
}
|
|
165
|
+
limit(value) {
|
|
166
|
+
return this.#helper?.limit ? this.#helper.limit(value) : ok(value);
|
|
167
|
+
}
|
|
168
|
+
check(value) {
|
|
169
|
+
return this.#helper?.check ? this.#helper.check(value) : ok(value);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
var ros = {
|
|
173
|
+
/**Creates a sync ok state from an initial value.
|
|
174
|
+
* @param init initial value for state.
|
|
175
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
176
|
+
ok(init, helper) {
|
|
177
|
+
return new RXS(ok(init), helper);
|
|
178
|
+
},
|
|
179
|
+
/**Creates a sync ok state from an initial result.
|
|
180
|
+
* @param init initial result for state.
|
|
181
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
182
|
+
result(init, helper) {
|
|
183
|
+
return new RXS(init, helper);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
var ros_ws = {
|
|
187
|
+
/**Creates a sync ok state from an initial value.
|
|
188
|
+
* @param init initial value for state.
|
|
189
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
190
|
+
ok(init, setter = true, helper) {
|
|
191
|
+
return new RXS(
|
|
192
|
+
ok(init),
|
|
193
|
+
helper,
|
|
194
|
+
setter
|
|
195
|
+
);
|
|
196
|
+
},
|
|
197
|
+
/**Creates a sync ok state from an initial result.
|
|
198
|
+
* @param init initial result for state.
|
|
199
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
200
|
+
result(init, setter = true, helper) {
|
|
201
|
+
return new RXS(
|
|
202
|
+
init,
|
|
203
|
+
helper,
|
|
204
|
+
setter
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
var res = {
|
|
209
|
+
/**Creates a sync state from an initial value.
|
|
210
|
+
* @param init initial value for state.
|
|
211
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
212
|
+
ok(init, helper) {
|
|
213
|
+
return new RXS(
|
|
214
|
+
ok(init),
|
|
215
|
+
helper
|
|
216
|
+
);
|
|
217
|
+
},
|
|
218
|
+
/**Creates a sync state from an initial error.
|
|
219
|
+
* @param init initial error for state.
|
|
220
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
221
|
+
err(init, helper) {
|
|
222
|
+
return new RXS(
|
|
223
|
+
err(init),
|
|
224
|
+
helper
|
|
225
|
+
);
|
|
226
|
+
},
|
|
227
|
+
/**Creates a sync state from an initial result.
|
|
228
|
+
* @param init initial result for state.
|
|
229
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
230
|
+
result(init, helper) {
|
|
231
|
+
return new RXS(
|
|
232
|
+
init,
|
|
233
|
+
helper
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
var res_ws = {
|
|
238
|
+
/**Creates a writable sync state from an initial value.
|
|
239
|
+
* @param init initial value for state.
|
|
240
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
241
|
+
ok(init, setter = true, helper) {
|
|
242
|
+
return new RXS(
|
|
243
|
+
ok(init),
|
|
244
|
+
helper,
|
|
245
|
+
setter
|
|
246
|
+
);
|
|
247
|
+
},
|
|
248
|
+
/**Creates a writable sync state from an initial error.
|
|
249
|
+
* @param init initial error for state.
|
|
250
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
251
|
+
err(init, setter = true, helper) {
|
|
252
|
+
return new RXS(
|
|
253
|
+
err(init),
|
|
254
|
+
helper,
|
|
255
|
+
setter
|
|
256
|
+
);
|
|
257
|
+
},
|
|
258
|
+
/**Creates a writable sync state from an initial result.
|
|
259
|
+
* @param init initial result for state.
|
|
260
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
261
|
+
result(init, setter = true, helper) {
|
|
262
|
+
return new RXS(
|
|
263
|
+
init,
|
|
264
|
+
helper,
|
|
265
|
+
setter
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
var STATE_SYNC = {
|
|
270
|
+
/**Sync read only states with guarenteed ok*/
|
|
271
|
+
ros,
|
|
272
|
+
/**Sync read and sync write with guarenteed ok*/
|
|
273
|
+
ros_ws,
|
|
274
|
+
/**Sync read only states with error */
|
|
275
|
+
res,
|
|
276
|
+
/**Sync read and sync write with error */
|
|
277
|
+
res_ws
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// src/array/sync.ts
|
|
281
|
+
var STATE_ARRAY_KEY = /* @__PURE__ */ Symbol("is_state_array");
|
|
282
|
+
function apply_read(array, read, transform) {
|
|
283
|
+
const a = array;
|
|
284
|
+
const t = transform;
|
|
285
|
+
const { type: ty, index: ix, items: it } = read;
|
|
286
|
+
if (ty === "fresh") a.splice(ix, a.length, ...t ? it.map(t) : it);
|
|
287
|
+
else if (ty === "added") a.splice(ix, 0, ...t ? it.map(t) : it);
|
|
288
|
+
else if (ty === "removed") a.splice(ix, it.length);
|
|
289
|
+
else if (ty === "changed")
|
|
290
|
+
for (let i = 0; i < it.length; i++) a[ix + i] = t ? t(it[i], i, it) : it[i];
|
|
291
|
+
return a;
|
|
292
|
+
}
|
|
293
|
+
var RXS2 = class extends StateBase {
|
|
294
|
+
get [STATE_ARRAY_KEY]() {
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
constructor(init, helper, setter) {
|
|
298
|
+
super();
|
|
299
|
+
if (setter === true) this.#setter = (val) => ok2(this.apply_write(ok2(val)));
|
|
300
|
+
else this.#setter = setter;
|
|
301
|
+
if (helper) this.#helper = helper;
|
|
302
|
+
this.set(init);
|
|
303
|
+
}
|
|
304
|
+
#error;
|
|
305
|
+
#array = [];
|
|
306
|
+
#helper;
|
|
307
|
+
#setter;
|
|
308
|
+
#length;
|
|
309
|
+
#mr(type, index, items) {
|
|
310
|
+
return { array: this.#array, type, index, items };
|
|
311
|
+
}
|
|
312
|
+
set(value) {
|
|
313
|
+
this.#array = value.ok ? [...value.value] : [];
|
|
314
|
+
this.#error = value.ok ? void 0 : value.error;
|
|
315
|
+
this.update_subs(ok2(this.#mr("fresh", 0, this.#array)));
|
|
316
|
+
}
|
|
317
|
+
set_ok(value) {
|
|
318
|
+
this.set(ok2(value));
|
|
319
|
+
}
|
|
320
|
+
set_err(error) {
|
|
321
|
+
this.set(err2(error));
|
|
322
|
+
}
|
|
323
|
+
set setter(setter) {
|
|
324
|
+
this.#setter = setter;
|
|
325
|
+
}
|
|
326
|
+
get setter() {
|
|
327
|
+
return this.#setter;
|
|
328
|
+
}
|
|
329
|
+
get state() {
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
332
|
+
get read_only() {
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
get read_write() {
|
|
336
|
+
return this.#setter ? this : void 0;
|
|
337
|
+
}
|
|
338
|
+
//#Reader Context
|
|
339
|
+
get rok() {
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
get rsync() {
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
async then(func) {
|
|
346
|
+
return func(this.get());
|
|
347
|
+
}
|
|
348
|
+
get() {
|
|
349
|
+
if (this.#error) return err2(this.#error);
|
|
350
|
+
return ok2(this.#mr("fresh", 0, this.#array));
|
|
351
|
+
}
|
|
352
|
+
ok() {
|
|
353
|
+
return this.#mr("fresh", 0, this.#array);
|
|
354
|
+
}
|
|
355
|
+
related() {
|
|
356
|
+
return this.#helper?.related ? this.#helper.related() : none2();
|
|
357
|
+
}
|
|
358
|
+
//#Writer Context
|
|
359
|
+
get writable() {
|
|
360
|
+
return this.#setter !== void 0;
|
|
361
|
+
}
|
|
362
|
+
get wsync() {
|
|
363
|
+
return this.writable;
|
|
364
|
+
}
|
|
365
|
+
async write(value) {
|
|
366
|
+
return this.write_sync(value);
|
|
367
|
+
}
|
|
368
|
+
write_sync(value) {
|
|
369
|
+
if (this.#setter)
|
|
370
|
+
return this.#setter(value, this, this.get());
|
|
371
|
+
return err2("State not writable");
|
|
372
|
+
}
|
|
373
|
+
limit(value) {
|
|
374
|
+
return this.#helper?.limit ? this.#helper.limit(value) : ok2(value);
|
|
375
|
+
}
|
|
376
|
+
check(value) {
|
|
377
|
+
return this.#helper?.check ? this.#helper.check(value) : ok2(value);
|
|
378
|
+
}
|
|
379
|
+
//Array/Owner Context
|
|
380
|
+
get array() {
|
|
381
|
+
return this.#array;
|
|
382
|
+
}
|
|
383
|
+
get length() {
|
|
384
|
+
return this.#array.length;
|
|
385
|
+
}
|
|
386
|
+
get length_state() {
|
|
387
|
+
return this.#length ??= STATE_SYNC.ros.ok(this.length);
|
|
388
|
+
}
|
|
389
|
+
at(index) {
|
|
390
|
+
return this.#array.at(index);
|
|
391
|
+
}
|
|
392
|
+
set_index(index, value) {
|
|
393
|
+
const l = this.#array.length;
|
|
394
|
+
this.#array[index] = value;
|
|
395
|
+
this.update_subs(ok2(this.#mr("changed", index, [value])));
|
|
396
|
+
if (this.#array.length !== l && this.#length)
|
|
397
|
+
this.#length.set_ok(this.#array.length);
|
|
398
|
+
}
|
|
399
|
+
push(...items) {
|
|
400
|
+
const index = this.#array.length;
|
|
401
|
+
const new_len = this.#array.push(...items);
|
|
402
|
+
this.update_subs(ok2(this.#mr("added", index, items)));
|
|
403
|
+
if (this.#length) this.#length.set_ok(new_len);
|
|
404
|
+
return new_len;
|
|
405
|
+
}
|
|
406
|
+
pop() {
|
|
407
|
+
const l = this.#array.length;
|
|
408
|
+
const p = this.#array.pop();
|
|
409
|
+
if (this.#array.length < l) {
|
|
410
|
+
this.update_subs(
|
|
411
|
+
ok2(this.#mr("removed", this.#array.length, [p]))
|
|
412
|
+
);
|
|
413
|
+
if (this.#length) this.#length.set_ok(this.#array.length);
|
|
414
|
+
}
|
|
415
|
+
return p;
|
|
416
|
+
}
|
|
417
|
+
shift() {
|
|
418
|
+
const l = this.#array.length;
|
|
419
|
+
const s = this.#array.shift();
|
|
420
|
+
if (this.#array.length < l) {
|
|
421
|
+
this.update_subs(ok2(this.#mr("removed", 0, [s])));
|
|
422
|
+
if (this.#length) this.#length.set_ok(this.#array.length);
|
|
423
|
+
}
|
|
424
|
+
return s;
|
|
425
|
+
}
|
|
426
|
+
unshift(...items) {
|
|
427
|
+
const new_len = this.#array.unshift(...items);
|
|
428
|
+
this.update_subs(ok2(this.#mr("added", 0, items)));
|
|
429
|
+
if (this.#length) this.#length.set_ok(new_len);
|
|
430
|
+
return new_len;
|
|
431
|
+
}
|
|
432
|
+
splice(start, delete_count, ...items) {
|
|
433
|
+
const r = this.#array.splice(start, delete_count, ...items);
|
|
434
|
+
if (r.length > 0)
|
|
435
|
+
this.update_subs(ok2(this.#mr("removed", start, r)));
|
|
436
|
+
if (items.length > 0)
|
|
437
|
+
this.update_subs(ok2(this.#mr("added", start, items)));
|
|
438
|
+
if (this.#length) this.#length.set_ok(this.#array.length);
|
|
439
|
+
return r;
|
|
440
|
+
}
|
|
441
|
+
delete(val) {
|
|
442
|
+
for (let i = 0; i < this.#array.length; i++)
|
|
443
|
+
if (this.#array[i] = val) {
|
|
444
|
+
this.update_subs(ok2(this.#mr("removed", i, [val])));
|
|
445
|
+
i--;
|
|
446
|
+
}
|
|
447
|
+
if (this.#length) this.#length.set_ok(this.#array.length);
|
|
448
|
+
}
|
|
449
|
+
///Helps apply the changes from one state array to another
|
|
450
|
+
apply_read(result, transform) {
|
|
451
|
+
const { index, items: its, type } = result.value;
|
|
452
|
+
const items = transform(its, type);
|
|
453
|
+
if (type === "fresh") return this.set(ok2(items));
|
|
454
|
+
else if (type === "added") this.#array.splice(index, 0, ...items);
|
|
455
|
+
else if (type === "removed") this.#array.splice(index, items.length);
|
|
456
|
+
else if (type === "changed")
|
|
457
|
+
for (let i = 0; i < its.length; i++) this.#array[index + i] = items[i];
|
|
458
|
+
this.update_subs(ok2(this.#mr(type, index, items)));
|
|
459
|
+
}
|
|
460
|
+
apply_write(result) {
|
|
461
|
+
if (!result.ok) return;
|
|
462
|
+
const type = result.value.type;
|
|
463
|
+
if (type === "write") this.set_ok(result.value.items);
|
|
464
|
+
else if (type === "pop") this.pop();
|
|
465
|
+
else if (type === "shift") this.shift();
|
|
466
|
+
else if (type === "push") this.push(...result.value.items);
|
|
467
|
+
else if (type === "unshift") this.unshift(...result.value.items);
|
|
468
|
+
else if (type === "delete") this.delete(result.value.item);
|
|
469
|
+
else if (type === "splice")
|
|
470
|
+
this.splice(
|
|
471
|
+
result.value.index,
|
|
472
|
+
result.value.delete_count,
|
|
473
|
+
...result.value.items ? result.value.items : []
|
|
474
|
+
);
|
|
475
|
+
else if (type === "change")
|
|
476
|
+
this.set_index(result.value.index, result.value.item);
|
|
477
|
+
}
|
|
478
|
+
apply_write_transform(result, transform) {
|
|
479
|
+
if (!result.ok) return;
|
|
480
|
+
const type = result.value.type;
|
|
481
|
+
if (type === "write")
|
|
482
|
+
this.set_ok(result.value.items.map((item) => transform(item)));
|
|
483
|
+
else if (type === "pop") this.pop();
|
|
484
|
+
else if (type === "shift") this.shift();
|
|
485
|
+
else if (type === "push")
|
|
486
|
+
this.push(...result.value.items.map((item) => transform(item)));
|
|
487
|
+
else if (type === "unshift")
|
|
488
|
+
this.unshift(...result.value.items.map((item) => transform(item)));
|
|
489
|
+
else if (type === "delete") this.delete(transform(result.value.item));
|
|
490
|
+
else if (type === "splice")
|
|
491
|
+
this.splice(
|
|
492
|
+
result.value.index,
|
|
493
|
+
result.value.delete_count,
|
|
494
|
+
...result.value.items ? result.value.items.map((item) => transform(item)) : []
|
|
495
|
+
);
|
|
496
|
+
else if (type === "change")
|
|
497
|
+
this.set_index(result.value.index, transform(result.value.item));
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
var ROS = {
|
|
501
|
+
/**Creates a state representing an array
|
|
502
|
+
* @param init initial array, leave empty for empty array
|
|
503
|
+
* @param helper functions to make related*/
|
|
504
|
+
ok(init = [], helper) {
|
|
505
|
+
return new RXS2(
|
|
506
|
+
ok2(init),
|
|
507
|
+
helper
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
var ROS_WS = {
|
|
512
|
+
/**Creates a state representing an array
|
|
513
|
+
* @param init initial array, leave empty for empty array
|
|
514
|
+
* @param setter function called when state value is set via setter, set true let write set it's value
|
|
515
|
+
* @param helper functions to check and limit*/
|
|
516
|
+
ok(init = [], setter, helper) {
|
|
517
|
+
return new RXS2(
|
|
518
|
+
ok2(init),
|
|
519
|
+
helper,
|
|
520
|
+
setter
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
var RES = {
|
|
525
|
+
/**Creates a state representing an array
|
|
526
|
+
* @param init initial array, leave empty for empty array
|
|
527
|
+
* @param helper functions to make related*/
|
|
528
|
+
ok(init = [], helper) {
|
|
529
|
+
return new RXS2(
|
|
530
|
+
ok2(init),
|
|
531
|
+
helper
|
|
532
|
+
);
|
|
533
|
+
},
|
|
534
|
+
/**Creates a state representing an array
|
|
535
|
+
* @param init initial error
|
|
536
|
+
* @param helper functions to make related*/
|
|
537
|
+
err(error, helper) {
|
|
538
|
+
return new RXS2(
|
|
539
|
+
err2(error),
|
|
540
|
+
helper
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
var RES_WS = {
|
|
545
|
+
/**Creates a state representing an array
|
|
546
|
+
* @param init initial array, leave empty for empty array
|
|
547
|
+
* @param setter function called when state value is set via setter, set true let write set it's value
|
|
548
|
+
* @param helper functions to check and limit*/
|
|
549
|
+
ok(init = [], setter, helper) {
|
|
550
|
+
return new RXS2(
|
|
551
|
+
ok2(init),
|
|
552
|
+
helper,
|
|
553
|
+
setter
|
|
554
|
+
);
|
|
555
|
+
},
|
|
556
|
+
/**Creates a state representing an array
|
|
557
|
+
* @param err initial error
|
|
558
|
+
* @param setter function called when state value is set via setter, set true let write set it's value
|
|
559
|
+
* @param helper functions to check and limit*/
|
|
560
|
+
err(error, setter, helper) {
|
|
561
|
+
return new RXS2(
|
|
562
|
+
err2(error),
|
|
563
|
+
helper,
|
|
564
|
+
setter
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
var STATE_ARRAY = {
|
|
569
|
+
/**The state key is a symbol used to identify state array objects
|
|
570
|
+
* To implement a custom state, set this key to true on the object */
|
|
571
|
+
STATE_ARRAY_KEY,
|
|
572
|
+
/** Applies a read from a state array to another array
|
|
573
|
+
* @template AT - Types allowed in both arrays.
|
|
574
|
+
* @template TAT - Optional type if state array type is different from array
|
|
575
|
+
* @param array Array to modify in place
|
|
576
|
+
* @param read Read struct from state array
|
|
577
|
+
* @param transform optional tranform function for when state array is not same type of array*/
|
|
578
|
+
apply_read,
|
|
579
|
+
ros: ROS,
|
|
580
|
+
ros_ws: ROS_WS,
|
|
581
|
+
res: RES,
|
|
582
|
+
res_ws: RES_WS,
|
|
583
|
+
/**Returns true if the given object promises to be a state array */
|
|
584
|
+
is(s) {
|
|
585
|
+
return Boolean(s) && s[STATE_ARRAY_KEY] === true;
|
|
586
|
+
},
|
|
587
|
+
//# Array Write Helpers
|
|
588
|
+
write(items) {
|
|
589
|
+
return { type: "write", items };
|
|
590
|
+
},
|
|
591
|
+
index(index, value) {
|
|
592
|
+
return { type: "change", index, item: value };
|
|
593
|
+
},
|
|
594
|
+
push(...items) {
|
|
595
|
+
return { type: "push", items };
|
|
596
|
+
},
|
|
597
|
+
pop() {
|
|
598
|
+
return { type: "pop" };
|
|
599
|
+
},
|
|
600
|
+
shift() {
|
|
601
|
+
return { type: "shift" };
|
|
602
|
+
},
|
|
603
|
+
unshift(...items) {
|
|
604
|
+
return { type: "unshift", items };
|
|
605
|
+
},
|
|
606
|
+
splice(start, delete_count, ...items) {
|
|
607
|
+
return {
|
|
608
|
+
type: "splice",
|
|
609
|
+
index: start,
|
|
610
|
+
delete_count: delete_count ?? 0,
|
|
611
|
+
items
|
|
612
|
+
};
|
|
613
|
+
},
|
|
614
|
+
pluck(index) {
|
|
615
|
+
return { type: "splice", index, delete_count: 1 };
|
|
616
|
+
},
|
|
617
|
+
insert(index, ...items) {
|
|
618
|
+
return { type: "splice", index, delete_count: 0, items };
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
// src/collected/number.ts
|
|
623
|
+
import { ok as ok3 } from "@chocbite/ts-lib-result";
|
|
624
|
+
|
|
625
|
+
// src/collected/rea.ts
|
|
626
|
+
import { err as err3, none as none3 } from "@chocbite/ts-lib-result";
|
|
627
|
+
var REA = class extends StateBase {
|
|
628
|
+
constructor(transform, ...states) {
|
|
629
|
+
super();
|
|
630
|
+
if (transform) this.getter = transform;
|
|
631
|
+
this.#states = states;
|
|
632
|
+
}
|
|
633
|
+
#buffer;
|
|
634
|
+
#states;
|
|
635
|
+
#state_buffers = [];
|
|
636
|
+
#state_subscribers = [];
|
|
637
|
+
getter(values) {
|
|
638
|
+
return values[0];
|
|
639
|
+
}
|
|
640
|
+
/**Called when subscriber is added*/
|
|
641
|
+
on_subscribe() {
|
|
642
|
+
if (!this.#states.length) {
|
|
643
|
+
this.#buffer = err3("No states registered");
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
this.#state_buffers.length = this.#states.length;
|
|
647
|
+
{
|
|
648
|
+
let count = 0;
|
|
649
|
+
const amount = this.#states.length - 1;
|
|
650
|
+
Promise.all(this.#states).then((vals) => {
|
|
651
|
+
for (let i = 0; i < this.#state_buffers.length; i++)
|
|
652
|
+
this.#state_buffers[i] = this.#state_buffers[i] ?? vals[i];
|
|
653
|
+
this.#buffer = this.getter(
|
|
654
|
+
this.#state_buffers
|
|
655
|
+
);
|
|
656
|
+
this.ful_r_prom(this.#buffer);
|
|
657
|
+
count = amount;
|
|
658
|
+
});
|
|
659
|
+
let calc = false;
|
|
660
|
+
for (let i = 0; i < this.#states.length; i++) {
|
|
661
|
+
this.#state_subscribers[i] = this.#states[i].sub((value) => {
|
|
662
|
+
if (count < amount) {
|
|
663
|
+
if (!this.#state_buffers[i]) count++;
|
|
664
|
+
this.#state_buffers[i] = value;
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
this.#state_buffers[i] = value;
|
|
668
|
+
if (!calc) {
|
|
669
|
+
calc = true;
|
|
670
|
+
Promise.resolve().then(() => {
|
|
671
|
+
this.#buffer = this.getter(
|
|
672
|
+
this.#state_buffers
|
|
673
|
+
);
|
|
674
|
+
this.update_subs(this.#buffer);
|
|
675
|
+
calc = false;
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
/**Called when subscriber is removed*/
|
|
683
|
+
on_unsubscribe() {
|
|
684
|
+
for (let i = 0; i < this.#states.length; i++)
|
|
685
|
+
this.#states[i].unsub(this.#state_subscribers[i]);
|
|
686
|
+
this.#state_subscribers = [];
|
|
687
|
+
this.#state_buffers = [];
|
|
688
|
+
this.#buffer = void 0;
|
|
689
|
+
}
|
|
690
|
+
//#Owner Context
|
|
691
|
+
set_states(...states) {
|
|
692
|
+
if (this.in_use()) {
|
|
693
|
+
this.on_unsubscribe();
|
|
694
|
+
this.#states = [...states];
|
|
695
|
+
this.on_subscribe();
|
|
696
|
+
} else this.#states = [...states];
|
|
697
|
+
}
|
|
698
|
+
set_getter(getter) {
|
|
699
|
+
if (this.in_use()) {
|
|
700
|
+
this.on_unsubscribe();
|
|
701
|
+
this.getter = getter;
|
|
702
|
+
this.on_subscribe();
|
|
703
|
+
} else this.getter = getter;
|
|
704
|
+
}
|
|
705
|
+
get state() {
|
|
706
|
+
return this;
|
|
707
|
+
}
|
|
708
|
+
get read_only() {
|
|
709
|
+
return this;
|
|
710
|
+
}
|
|
711
|
+
//#Reader Context
|
|
712
|
+
get rok() {
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
get rsync() {
|
|
716
|
+
return false;
|
|
717
|
+
}
|
|
718
|
+
async then(func) {
|
|
719
|
+
if (this.#buffer) return func(this.#buffer);
|
|
720
|
+
if (!this.#state_buffers.length)
|
|
721
|
+
return func(
|
|
722
|
+
this.getter(
|
|
723
|
+
await Promise.all(this.#states)
|
|
724
|
+
)
|
|
725
|
+
);
|
|
726
|
+
return this.append_r_prom(func);
|
|
727
|
+
}
|
|
728
|
+
related() {
|
|
729
|
+
return none3();
|
|
730
|
+
}
|
|
731
|
+
//#Writer Context
|
|
732
|
+
get writable() {
|
|
733
|
+
return false;
|
|
734
|
+
}
|
|
735
|
+
get wsync() {
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
738
|
+
};
|
|
739
|
+
var STATE_COLLECTED_REA = {
|
|
740
|
+
/**Creates a state that collects multiple states values and reduces it to one.
|
|
741
|
+
* @param transform - Function to translate value of collected states, false means first states values is used.
|
|
742
|
+
* @param states - The states to collect.*/
|
|
743
|
+
from(transform, ...states) {
|
|
744
|
+
return new REA(transform, ...states);
|
|
745
|
+
},
|
|
746
|
+
class: REA
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
// src/collected/res.ts
|
|
750
|
+
import { err as err4, none as none4 } from "@chocbite/ts-lib-result";
|
|
751
|
+
var RES2 = class extends StateBase {
|
|
752
|
+
/**Creates a state which is derived from other states. The derived state will update when any of the other states update.
|
|
753
|
+
* @param transform - Function to translate value of state or states to something else, false means first states values is used.
|
|
754
|
+
* @param states - The other states to be used in the derived state.*/
|
|
755
|
+
constructor(transform, ...states) {
|
|
756
|
+
super();
|
|
757
|
+
if (transform) this.getter = transform;
|
|
758
|
+
this.#states = states;
|
|
759
|
+
}
|
|
760
|
+
#buffer;
|
|
761
|
+
#states;
|
|
762
|
+
#state_buffers = [];
|
|
763
|
+
#state_subscribers = [];
|
|
764
|
+
getter(values) {
|
|
765
|
+
return values[0];
|
|
766
|
+
}
|
|
767
|
+
/**Called when subscriber is added*/
|
|
768
|
+
on_subscribe() {
|
|
769
|
+
if (!this.#states.length) {
|
|
770
|
+
this.#buffer = err4("No states registered");
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
let calc = false;
|
|
774
|
+
for (let i = 0; i < this.#states.length; i++) {
|
|
775
|
+
this.#state_buffers[i] = this.#states[i].get();
|
|
776
|
+
this.#state_subscribers[i] = this.#states[i].sub((value) => {
|
|
777
|
+
this.#state_buffers[i] = value;
|
|
778
|
+
if (!calc) {
|
|
779
|
+
calc = true;
|
|
780
|
+
Promise.resolve().then(() => {
|
|
781
|
+
this.#buffer = this.getter(
|
|
782
|
+
this.#state_buffers
|
|
783
|
+
);
|
|
784
|
+
this.update_subs(this.#buffer);
|
|
785
|
+
calc = false;
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
this.#buffer = this.getter(
|
|
791
|
+
this.#state_buffers
|
|
792
|
+
);
|
|
793
|
+
this.update_subs(this.#buffer);
|
|
794
|
+
}
|
|
795
|
+
/**Called when subscriber is removed*/
|
|
796
|
+
on_unsubscribe() {
|
|
797
|
+
for (let i = 0; i < this.#states.length; i++)
|
|
798
|
+
this.#states[i].unsub(this.#state_subscribers[i]);
|
|
799
|
+
this.#state_subscribers = [];
|
|
800
|
+
this.#state_buffers = [];
|
|
801
|
+
this.#buffer = void 0;
|
|
802
|
+
}
|
|
803
|
+
//#Owner
|
|
804
|
+
set_states(...states) {
|
|
805
|
+
if (this.in_use()) {
|
|
806
|
+
this.on_unsubscribe();
|
|
807
|
+
this.#states = [...states];
|
|
808
|
+
this.on_subscribe();
|
|
809
|
+
} else this.#states = [...states];
|
|
810
|
+
}
|
|
811
|
+
set_getter(getter) {
|
|
812
|
+
if (this.in_use()) {
|
|
813
|
+
this.on_unsubscribe();
|
|
814
|
+
this.getter = getter;
|
|
815
|
+
this.on_subscribe();
|
|
816
|
+
} else this.getter = getter;
|
|
817
|
+
}
|
|
818
|
+
get state() {
|
|
819
|
+
return this;
|
|
820
|
+
}
|
|
821
|
+
get read_only() {
|
|
822
|
+
return this;
|
|
823
|
+
}
|
|
824
|
+
//#Reader Context
|
|
825
|
+
get rok() {
|
|
826
|
+
return false;
|
|
827
|
+
}
|
|
828
|
+
get rsync() {
|
|
829
|
+
return true;
|
|
830
|
+
}
|
|
831
|
+
async then(func) {
|
|
832
|
+
return func(this.get());
|
|
833
|
+
}
|
|
834
|
+
get() {
|
|
835
|
+
if (this.#buffer) return this.#buffer;
|
|
836
|
+
return this.#states.length ? this.getter(
|
|
837
|
+
this.#states.map((s) => s.get())
|
|
838
|
+
) : err4("No states registered");
|
|
839
|
+
}
|
|
840
|
+
related() {
|
|
841
|
+
return none4();
|
|
842
|
+
}
|
|
843
|
+
//#Writer Context
|
|
844
|
+
get writable() {
|
|
845
|
+
return false;
|
|
846
|
+
}
|
|
847
|
+
get wsync() {
|
|
848
|
+
return false;
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
var STATE_COLLECTED_RES = {
|
|
852
|
+
/**Creates a state that collects multiple states values and reduces it to one.
|
|
853
|
+
* @param transform - Function to translate value of collected states, false means first states values is used.
|
|
854
|
+
* @param states - The states to collect.*/
|
|
855
|
+
from(transform, ...states) {
|
|
856
|
+
return new RES2(transform, ...states);
|
|
857
|
+
},
|
|
858
|
+
class: RES2
|
|
859
|
+
};
|
|
860
|
+
|
|
861
|
+
// src/collected/roa.ts
|
|
862
|
+
import { none as none5 } from "@chocbite/ts-lib-result";
|
|
863
|
+
var ROA = class extends StateBase {
|
|
864
|
+
/**Creates a state which is derived from other states. The derived state will update when any of the other states update.
|
|
865
|
+
* @param transform - Function to translate value of state or states to something else, false means first states values is used.
|
|
866
|
+
* @param states - The other states to be used in the derived state.*/
|
|
867
|
+
constructor(transform, ...states) {
|
|
868
|
+
super();
|
|
869
|
+
if (transform) this.getter = transform;
|
|
870
|
+
this.#states = states;
|
|
871
|
+
}
|
|
872
|
+
#buffer;
|
|
873
|
+
#states;
|
|
874
|
+
#state_buffers = [];
|
|
875
|
+
#state_subscribers = [];
|
|
876
|
+
getter(values) {
|
|
877
|
+
return values[0];
|
|
878
|
+
}
|
|
879
|
+
/**Called when subscriber is added*/
|
|
880
|
+
on_subscribe() {
|
|
881
|
+
this.#state_buffers.length = this.#states.length;
|
|
882
|
+
{
|
|
883
|
+
let count = 0;
|
|
884
|
+
const amount = this.#states.length - 1;
|
|
885
|
+
Promise.all(this.#states).then((vals) => {
|
|
886
|
+
for (let i = 0; i < this.#state_buffers.length; i++)
|
|
887
|
+
this.#state_buffers[i] = this.#state_buffers[i] ?? vals[i];
|
|
888
|
+
this.#buffer = this.getter(
|
|
889
|
+
this.#state_buffers
|
|
890
|
+
);
|
|
891
|
+
this.ful_r_prom(this.#buffer);
|
|
892
|
+
count = amount;
|
|
893
|
+
});
|
|
894
|
+
let calc = false;
|
|
895
|
+
for (let i = 0; i < this.#states.length; i++) {
|
|
896
|
+
this.#state_subscribers[i] = this.#states[i].sub((value) => {
|
|
897
|
+
if (count < amount) {
|
|
898
|
+
if (!this.#state_buffers[i]) count++;
|
|
899
|
+
this.#state_buffers[i] = value;
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
this.#state_buffers[i] = value;
|
|
903
|
+
if (!calc) {
|
|
904
|
+
calc = true;
|
|
905
|
+
Promise.resolve().then(() => {
|
|
906
|
+
this.#buffer = this.getter(
|
|
907
|
+
this.#state_buffers
|
|
908
|
+
);
|
|
909
|
+
this.update_subs(this.#buffer);
|
|
910
|
+
calc = false;
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
/**Called when subscriber is removed*/
|
|
918
|
+
on_unsubscribe() {
|
|
919
|
+
for (let i = 0; i < this.#states.length; i++)
|
|
920
|
+
this.#states[i].unsub(this.#state_subscribers[i]);
|
|
921
|
+
this.#state_subscribers = [];
|
|
922
|
+
this.#state_buffers = [];
|
|
923
|
+
this.#buffer = void 0;
|
|
924
|
+
}
|
|
925
|
+
//#Owner
|
|
926
|
+
set_states(...states) {
|
|
927
|
+
if (this.in_use()) {
|
|
928
|
+
this.on_unsubscribe();
|
|
929
|
+
this.#states = [...states];
|
|
930
|
+
this.on_subscribe();
|
|
931
|
+
} else this.#states = [...states];
|
|
932
|
+
}
|
|
933
|
+
set_getter(getter) {
|
|
934
|
+
if (this.in_use()) {
|
|
935
|
+
this.on_unsubscribe();
|
|
936
|
+
this.getter = getter;
|
|
937
|
+
this.on_subscribe();
|
|
938
|
+
} else this.getter = getter;
|
|
939
|
+
}
|
|
940
|
+
get state() {
|
|
941
|
+
return this;
|
|
942
|
+
}
|
|
943
|
+
get read_only() {
|
|
944
|
+
return this;
|
|
945
|
+
}
|
|
946
|
+
//#Reader Context
|
|
947
|
+
get rok() {
|
|
948
|
+
return true;
|
|
949
|
+
}
|
|
950
|
+
get rsync() {
|
|
951
|
+
return false;
|
|
952
|
+
}
|
|
953
|
+
async then(func) {
|
|
954
|
+
if (this.#buffer) return func(this.#buffer);
|
|
955
|
+
if (!this.#state_buffers.length)
|
|
956
|
+
return func(
|
|
957
|
+
this.getter(
|
|
958
|
+
await Promise.all(this.#states)
|
|
959
|
+
)
|
|
960
|
+
);
|
|
961
|
+
return this.append_r_prom(func);
|
|
962
|
+
}
|
|
963
|
+
related() {
|
|
964
|
+
return none5();
|
|
965
|
+
}
|
|
966
|
+
//#Writer Context
|
|
967
|
+
get writable() {
|
|
968
|
+
return false;
|
|
969
|
+
}
|
|
970
|
+
get wsync() {
|
|
971
|
+
return false;
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
var STATE_COLLECTED_ROA = {
|
|
975
|
+
/**Creates a guarenteed ok state that collects multiple states values and reduces it to one.
|
|
976
|
+
* @param transform - Function to translate value of collected states, false means first states values is used.
|
|
977
|
+
* @param states - The states to collect.*/
|
|
978
|
+
from(transform, ...states) {
|
|
979
|
+
return new ROA(transform, ...states);
|
|
980
|
+
},
|
|
981
|
+
class: ROA
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
// src/collected/ros.ts
|
|
985
|
+
import { none as none6 } from "@chocbite/ts-lib-result";
|
|
986
|
+
var ROS2 = class extends StateBase {
|
|
987
|
+
constructor(transform, ...states) {
|
|
988
|
+
super();
|
|
989
|
+
if (transform) this.getter = transform;
|
|
990
|
+
this.#states = states;
|
|
991
|
+
}
|
|
992
|
+
#buffer;
|
|
993
|
+
#states;
|
|
994
|
+
#state_buffers = [];
|
|
995
|
+
#state_subscribers = [];
|
|
996
|
+
getter(values) {
|
|
997
|
+
return values[0];
|
|
998
|
+
}
|
|
999
|
+
/**Called when subscriber is added*/
|
|
1000
|
+
on_subscribe() {
|
|
1001
|
+
let calc = false;
|
|
1002
|
+
for (let i = 0; i < this.#states.length; i++) {
|
|
1003
|
+
this.#state_buffers[i] = this.#states[i].get();
|
|
1004
|
+
this.#state_subscribers[i] = this.#states[i].sub((value) => {
|
|
1005
|
+
this.#state_buffers[i] = value;
|
|
1006
|
+
if (!calc) {
|
|
1007
|
+
calc = true;
|
|
1008
|
+
Promise.resolve().then(() => {
|
|
1009
|
+
this.#buffer = this.getter(
|
|
1010
|
+
this.#state_buffers
|
|
1011
|
+
);
|
|
1012
|
+
this.update_subs(this.#buffer);
|
|
1013
|
+
calc = false;
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
this.#buffer = this.getter(
|
|
1019
|
+
this.#state_buffers
|
|
1020
|
+
);
|
|
1021
|
+
this.update_subs(this.#buffer);
|
|
1022
|
+
}
|
|
1023
|
+
/**Called when subscriber is removed*/
|
|
1024
|
+
on_unsubscribe() {
|
|
1025
|
+
for (let i = 0; i < this.#states.length; i++)
|
|
1026
|
+
this.#states[i].unsub(this.#state_subscribers[i]);
|
|
1027
|
+
this.#state_subscribers = [];
|
|
1028
|
+
this.#state_buffers = [];
|
|
1029
|
+
this.#buffer = void 0;
|
|
1030
|
+
}
|
|
1031
|
+
//#Owner
|
|
1032
|
+
set_states(...states) {
|
|
1033
|
+
if (this.in_use()) {
|
|
1034
|
+
this.on_unsubscribe();
|
|
1035
|
+
this.#states = [...states];
|
|
1036
|
+
this.on_subscribe();
|
|
1037
|
+
} else this.#states = [...states];
|
|
1038
|
+
}
|
|
1039
|
+
set_getter(getter) {
|
|
1040
|
+
if (this.in_use()) {
|
|
1041
|
+
this.on_unsubscribe();
|
|
1042
|
+
this.getter = getter;
|
|
1043
|
+
this.on_subscribe();
|
|
1044
|
+
} else this.getter = getter;
|
|
1045
|
+
}
|
|
1046
|
+
get state() {
|
|
1047
|
+
return this;
|
|
1048
|
+
}
|
|
1049
|
+
get read_only() {
|
|
1050
|
+
return this;
|
|
1051
|
+
}
|
|
1052
|
+
//#Reader Context
|
|
1053
|
+
get rok() {
|
|
1054
|
+
return true;
|
|
1055
|
+
}
|
|
1056
|
+
get rsync() {
|
|
1057
|
+
return true;
|
|
1058
|
+
}
|
|
1059
|
+
async then(func) {
|
|
1060
|
+
return func(this.get());
|
|
1061
|
+
}
|
|
1062
|
+
get() {
|
|
1063
|
+
if (this.#buffer) return this.#buffer;
|
|
1064
|
+
return this.getter(
|
|
1065
|
+
this.#states.map((s) => s.get())
|
|
1066
|
+
);
|
|
1067
|
+
}
|
|
1068
|
+
ok() {
|
|
1069
|
+
return this.get().value;
|
|
1070
|
+
}
|
|
1071
|
+
related() {
|
|
1072
|
+
return none6();
|
|
1073
|
+
}
|
|
1074
|
+
//#Writer Context
|
|
1075
|
+
get writable() {
|
|
1076
|
+
return false;
|
|
1077
|
+
}
|
|
1078
|
+
get wsync() {
|
|
1079
|
+
return false;
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
var STATE_COLLECTED_ROS = {
|
|
1083
|
+
/**Creates a guarenteed ok state that collects multiple states values and reduces it to one.
|
|
1084
|
+
* @param transform - Function to translate value of collected states, false means first states values is used.
|
|
1085
|
+
* @param states - The states to collect.*/
|
|
1086
|
+
from(transform, ...states) {
|
|
1087
|
+
return new ROS2(transform, ...states);
|
|
1088
|
+
},
|
|
1089
|
+
class: ROS2
|
|
1090
|
+
};
|
|
1091
|
+
|
|
1092
|
+
// src/collected/number.ts
|
|
1093
|
+
var NumberSumREA = class extends STATE_COLLECTED_REA.class {
|
|
1094
|
+
constructor(...states) {
|
|
1095
|
+
super(false, ...states);
|
|
1096
|
+
}
|
|
1097
|
+
getter(values) {
|
|
1098
|
+
let sum = 0;
|
|
1099
|
+
for (const val of values) {
|
|
1100
|
+
if (val.err) return val;
|
|
1101
|
+
sum += val.value;
|
|
1102
|
+
}
|
|
1103
|
+
return ok3(sum);
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
var NumberSumROA = class extends STATE_COLLECTED_ROA.class {
|
|
1107
|
+
constructor(...states) {
|
|
1108
|
+
super(false, ...states);
|
|
1109
|
+
}
|
|
1110
|
+
getter(values) {
|
|
1111
|
+
return ok3(values.reduce((acc, val) => acc + val.value, 0));
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
var NumberSumRES = class extends STATE_COLLECTED_RES.class {
|
|
1115
|
+
constructor(...states) {
|
|
1116
|
+
super(false, ...states);
|
|
1117
|
+
}
|
|
1118
|
+
getter(values) {
|
|
1119
|
+
let sum = 0;
|
|
1120
|
+
for (const val of values) {
|
|
1121
|
+
if (val.err) return val;
|
|
1122
|
+
sum += val.value;
|
|
1123
|
+
}
|
|
1124
|
+
return ok3(sum);
|
|
1125
|
+
}
|
|
1126
|
+
};
|
|
1127
|
+
var NumberSumROS = class extends STATE_COLLECTED_ROS.class {
|
|
1128
|
+
constructor(...states) {
|
|
1129
|
+
super(false, ...states);
|
|
1130
|
+
}
|
|
1131
|
+
getter(values) {
|
|
1132
|
+
return ok3(values.reduce((acc, val) => acc + val.value, 0));
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
var NumberPercentageREA = class extends STATE_COLLECTED_REA.class {
|
|
1136
|
+
constructor(st1, st2) {
|
|
1137
|
+
super(false, st1, st2);
|
|
1138
|
+
}
|
|
1139
|
+
getter(values) {
|
|
1140
|
+
if (values[0].err) return values[0];
|
|
1141
|
+
if (values[1].err) return values[1];
|
|
1142
|
+
return ok3(
|
|
1143
|
+
values[1].value / (values[0].value === 0 ? 1 : values[0].value) * 100
|
|
1144
|
+
);
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
1147
|
+
var NumberPercentageROA = class extends STATE_COLLECTED_ROA.class {
|
|
1148
|
+
constructor(st1, st2) {
|
|
1149
|
+
super(false, st1, st2);
|
|
1150
|
+
}
|
|
1151
|
+
getter(values) {
|
|
1152
|
+
return ok3(
|
|
1153
|
+
values[1].value / (values[0].value === 0 ? 1 : values[0].value) * 100
|
|
1154
|
+
);
|
|
1155
|
+
}
|
|
1156
|
+
};
|
|
1157
|
+
var NumberPercentageRES = class extends STATE_COLLECTED_RES.class {
|
|
1158
|
+
constructor(st1, st2) {
|
|
1159
|
+
super(false, st1, st2);
|
|
1160
|
+
}
|
|
1161
|
+
getter(values) {
|
|
1162
|
+
if (values[0].err) return values[0];
|
|
1163
|
+
if (values[1].err) return values[1];
|
|
1164
|
+
return ok3(
|
|
1165
|
+
values[1].value / (values[0].value === 0 ? 1 : values[0].value) * 100
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
};
|
|
1169
|
+
var NumberPercentageROS = class extends STATE_COLLECTED_ROS.class {
|
|
1170
|
+
constructor(st1, st2) {
|
|
1171
|
+
super(false, st1, st2);
|
|
1172
|
+
}
|
|
1173
|
+
getter(values) {
|
|
1174
|
+
return ok3(
|
|
1175
|
+
values[1].value / (values[0].value === 0 ? 1 : values[0].value) * 100
|
|
1176
|
+
);
|
|
1177
|
+
}
|
|
1178
|
+
};
|
|
1179
|
+
var STATE_COLLECTS_NUMBER = {
|
|
1180
|
+
//Calculates the sum of all the states
|
|
1181
|
+
sum: {
|
|
1182
|
+
rea(...states) {
|
|
1183
|
+
return new NumberSumREA(...states);
|
|
1184
|
+
},
|
|
1185
|
+
roa(...states) {
|
|
1186
|
+
return new NumberSumROA(...states);
|
|
1187
|
+
},
|
|
1188
|
+
res(...states) {
|
|
1189
|
+
return new NumberSumRES(...states);
|
|
1190
|
+
},
|
|
1191
|
+
ros(...states) {
|
|
1192
|
+
return new NumberSumROS(...states);
|
|
1193
|
+
}
|
|
1194
|
+
},
|
|
1195
|
+
//Calculates how many percent the second state is of the first state
|
|
1196
|
+
percentage: {
|
|
1197
|
+
rea(st1, st2) {
|
|
1198
|
+
return new NumberPercentageREA(st1, st2);
|
|
1199
|
+
},
|
|
1200
|
+
roa(st1, st2) {
|
|
1201
|
+
return new NumberPercentageROA(st1, st2);
|
|
1202
|
+
},
|
|
1203
|
+
res(st1, st2) {
|
|
1204
|
+
return new NumberPercentageRES(st1, st2);
|
|
1205
|
+
},
|
|
1206
|
+
ros(st1, st2) {
|
|
1207
|
+
return new NumberPercentageROS(st1, st2);
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
|
|
1212
|
+
// src/delayed/delayed.ts
|
|
1213
|
+
import {
|
|
1214
|
+
err as err5,
|
|
1215
|
+
none as none7,
|
|
1216
|
+
ok as ok4
|
|
1217
|
+
} from "@chocbite/ts-lib-result";
|
|
1218
|
+
var RXA = class extends StateBase {
|
|
1219
|
+
constructor(init, helper, setter_sync, setter_async) {
|
|
1220
|
+
super();
|
|
1221
|
+
if (setter_sync === true)
|
|
1222
|
+
this.#setter_sync = (value, state2, old) => {
|
|
1223
|
+
if (old && !old.err && value === old.value)
|
|
1224
|
+
return ok4(void 0);
|
|
1225
|
+
return this.#helper?.limit ? this.#helper?.limit(value).map((e) => state2.set_ok(e)) : ok4(state2.set_ok(value));
|
|
1226
|
+
};
|
|
1227
|
+
else this.#setter_sync = setter_sync;
|
|
1228
|
+
if (setter_async === true)
|
|
1229
|
+
this.#setter_async = async (value, state2, old) => {
|
|
1230
|
+
if (old && !old.err && value === old.value)
|
|
1231
|
+
return ok4(void 0);
|
|
1232
|
+
return this.#helper?.limit ? this.#helper?.limit(value).map((e) => state2.set_ok(e)) : ok4(state2.set_ok(value));
|
|
1233
|
+
};
|
|
1234
|
+
else this.#setter_async = setter_async;
|
|
1235
|
+
if (helper) this.#helper = helper;
|
|
1236
|
+
let initializing = false;
|
|
1237
|
+
this.then = async (func) => {
|
|
1238
|
+
if (init) {
|
|
1239
|
+
if (!initializing) {
|
|
1240
|
+
initializing = true;
|
|
1241
|
+
(async () => {
|
|
1242
|
+
try {
|
|
1243
|
+
this.#value = await init();
|
|
1244
|
+
this.ful_r_prom(this.#value);
|
|
1245
|
+
} catch (e) {
|
|
1246
|
+
console.error("Failed to initialize delayed RO state: ", e, this);
|
|
1247
|
+
}
|
|
1248
|
+
this.#clean();
|
|
1249
|
+
})();
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
return this.append_r_prom(func);
|
|
1253
|
+
};
|
|
1254
|
+
this.set = (value) => {
|
|
1255
|
+
this.#clean();
|
|
1256
|
+
this.set(this.ful_r_prom(value));
|
|
1257
|
+
};
|
|
1258
|
+
const write_sync = this.write_sync.bind(this);
|
|
1259
|
+
this.write_sync = (value) => write_sync(value).map((val) => this.#clean() ?? val);
|
|
1260
|
+
const write = this.write.bind(this);
|
|
1261
|
+
this.write = async (value) => (await write(value)).map((val) => this.#clean() ?? val);
|
|
1262
|
+
}
|
|
1263
|
+
#clean() {
|
|
1264
|
+
["then", "set", "write", "write_sync"].forEach(
|
|
1265
|
+
(k) => delete this[k]
|
|
1266
|
+
);
|
|
1267
|
+
}
|
|
1268
|
+
#value;
|
|
1269
|
+
#setter_sync;
|
|
1270
|
+
#setter_async;
|
|
1271
|
+
#helper;
|
|
1272
|
+
//#Owner Context
|
|
1273
|
+
set(value) {
|
|
1274
|
+
this.update_subs(this.#value = value);
|
|
1275
|
+
}
|
|
1276
|
+
set_ok(value) {
|
|
1277
|
+
this.set(ok4(value));
|
|
1278
|
+
}
|
|
1279
|
+
set_err(error) {
|
|
1280
|
+
this.set(err5(error));
|
|
1281
|
+
}
|
|
1282
|
+
set setter_sync(setter) {
|
|
1283
|
+
this.#setter_sync = setter;
|
|
1284
|
+
}
|
|
1285
|
+
get setter_sync() {
|
|
1286
|
+
return this.#setter_sync;
|
|
1287
|
+
}
|
|
1288
|
+
set setter_async(setter) {
|
|
1289
|
+
this.#setter_async = setter;
|
|
1290
|
+
}
|
|
1291
|
+
get setter_async() {
|
|
1292
|
+
return this.#setter_async;
|
|
1293
|
+
}
|
|
1294
|
+
get state() {
|
|
1295
|
+
return this;
|
|
1296
|
+
}
|
|
1297
|
+
get read_only() {
|
|
1298
|
+
return this;
|
|
1299
|
+
}
|
|
1300
|
+
get read_write_sync() {
|
|
1301
|
+
return this.#setter_sync ? this : void 0;
|
|
1302
|
+
}
|
|
1303
|
+
get read_write_async() {
|
|
1304
|
+
return this.#setter_async ? this : void 0;
|
|
1305
|
+
}
|
|
1306
|
+
//#Reader Context
|
|
1307
|
+
get rok() {
|
|
1308
|
+
return true;
|
|
1309
|
+
}
|
|
1310
|
+
//Becomes sync compatible once evaluated
|
|
1311
|
+
get rsync() {
|
|
1312
|
+
return Boolean(this.#value);
|
|
1313
|
+
}
|
|
1314
|
+
get() {
|
|
1315
|
+
return this.#value;
|
|
1316
|
+
}
|
|
1317
|
+
ok() {
|
|
1318
|
+
return this.#value.value;
|
|
1319
|
+
}
|
|
1320
|
+
async then(func) {
|
|
1321
|
+
return func(this.#value);
|
|
1322
|
+
}
|
|
1323
|
+
related() {
|
|
1324
|
+
return this.#helper?.related ? this.#helper.related() : none7();
|
|
1325
|
+
}
|
|
1326
|
+
//#Writer Context
|
|
1327
|
+
get writable() {
|
|
1328
|
+
return Boolean(this.#setter_sync || this.#setter_async);
|
|
1329
|
+
}
|
|
1330
|
+
get wsync() {
|
|
1331
|
+
return Boolean(this.#setter_sync);
|
|
1332
|
+
}
|
|
1333
|
+
async write(value) {
|
|
1334
|
+
if (this.#setter_async)
|
|
1335
|
+
return this.#setter_async(
|
|
1336
|
+
value,
|
|
1337
|
+
this,
|
|
1338
|
+
this.#value
|
|
1339
|
+
);
|
|
1340
|
+
else if (this.#setter_sync)
|
|
1341
|
+
return this.#setter_sync(
|
|
1342
|
+
value,
|
|
1343
|
+
this,
|
|
1344
|
+
this.#value
|
|
1345
|
+
);
|
|
1346
|
+
return err5("State not writable");
|
|
1347
|
+
}
|
|
1348
|
+
write_sync(value) {
|
|
1349
|
+
if (this.#setter_sync)
|
|
1350
|
+
return this.#setter_sync(
|
|
1351
|
+
value,
|
|
1352
|
+
this,
|
|
1353
|
+
this.#value
|
|
1354
|
+
);
|
|
1355
|
+
return err5("State not writable");
|
|
1356
|
+
}
|
|
1357
|
+
limit(value) {
|
|
1358
|
+
return this.#helper?.limit ? this.#helper.limit(value) : ok4(value);
|
|
1359
|
+
}
|
|
1360
|
+
check(value) {
|
|
1361
|
+
return this.#helper?.check ? this.#helper.check(value) : ok4(value);
|
|
1362
|
+
}
|
|
1363
|
+
};
|
|
1364
|
+
var roa = {
|
|
1365
|
+
/**Creates a delayed ok state from an initial value, delayed meaning the value is a promise evaluated on first access.
|
|
1366
|
+
* @param init initial value for state.
|
|
1367
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1368
|
+
ok(init, helper) {
|
|
1369
|
+
return new RXA(
|
|
1370
|
+
init ? async () => ok4(await init()) : void 0,
|
|
1371
|
+
helper
|
|
1372
|
+
);
|
|
1373
|
+
},
|
|
1374
|
+
/**Creates a delayed ok state from an initial result, delayed meaning the value is a promise evaluated on first access.
|
|
1375
|
+
* @param init initial result for state.
|
|
1376
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1377
|
+
result(init, helper) {
|
|
1378
|
+
return new RXA(init, helper);
|
|
1379
|
+
}
|
|
1380
|
+
};
|
|
1381
|
+
var roa_ws = {
|
|
1382
|
+
/**Creates a delayed ok state from an initial value, delayed meaning the value is a promise evaluated on first access.
|
|
1383
|
+
* @param init initial value for state.
|
|
1384
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1385
|
+
ok(init, setter = true, helper) {
|
|
1386
|
+
return new RXA(
|
|
1387
|
+
init ? async () => ok4(await init()) : void 0,
|
|
1388
|
+
helper,
|
|
1389
|
+
setter
|
|
1390
|
+
);
|
|
1391
|
+
},
|
|
1392
|
+
/**Creates a delayed ok state from an initial result, delayed meaning the value is a promise evaluated on first access.
|
|
1393
|
+
* @param init initial result for state.
|
|
1394
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1395
|
+
result(init, setter = true, helper) {
|
|
1396
|
+
return new RXA(
|
|
1397
|
+
init,
|
|
1398
|
+
helper,
|
|
1399
|
+
setter
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
var roa_wa = {
|
|
1404
|
+
/**Creates a delayed ok state from an initial value, delayed meaning the value is a promise evaluated on first access.
|
|
1405
|
+
* @param init initial value for state.
|
|
1406
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1407
|
+
ok(init, setter = true, helper) {
|
|
1408
|
+
return new RXA(
|
|
1409
|
+
init ? async () => ok4(await init()) : void 0,
|
|
1410
|
+
helper,
|
|
1411
|
+
void 0,
|
|
1412
|
+
setter
|
|
1413
|
+
);
|
|
1414
|
+
},
|
|
1415
|
+
/**Creates a delayed ok state from an initial result, delayed meaning the value is a promise evaluated on first access.
|
|
1416
|
+
* @param init initial result for state.
|
|
1417
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1418
|
+
result(init, setter = true, helper) {
|
|
1419
|
+
return new RXA(
|
|
1420
|
+
init,
|
|
1421
|
+
helper,
|
|
1422
|
+
void 0,
|
|
1423
|
+
setter
|
|
1424
|
+
);
|
|
1425
|
+
}
|
|
1426
|
+
};
|
|
1427
|
+
var rea = {
|
|
1428
|
+
/**Creates a delayed ok state from an initial value, delayed meaning the value is a promise evaluated on first access.
|
|
1429
|
+
* @param init initial value for state.
|
|
1430
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1431
|
+
ok(init, helper) {
|
|
1432
|
+
return new RXA(
|
|
1433
|
+
init ? async () => ok4(await init()) : void 0,
|
|
1434
|
+
helper
|
|
1435
|
+
);
|
|
1436
|
+
},
|
|
1437
|
+
/**Creates a delayed state from an initial error, delayed meaning the value is a promise evaluated on first access.
|
|
1438
|
+
* @param init initial error for state.
|
|
1439
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1440
|
+
err(init, helper) {
|
|
1441
|
+
return new RXA(
|
|
1442
|
+
init ? async () => err5(await init()) : void 0,
|
|
1443
|
+
helper
|
|
1444
|
+
);
|
|
1445
|
+
},
|
|
1446
|
+
/**Creates a delayed ok state from an initial result, delayed meaning the value is a promise evaluated on first access.
|
|
1447
|
+
* @param init initial result for state.
|
|
1448
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1449
|
+
result(init, helper) {
|
|
1450
|
+
return new RXA(
|
|
1451
|
+
init,
|
|
1452
|
+
helper
|
|
1453
|
+
);
|
|
1454
|
+
}
|
|
1455
|
+
};
|
|
1456
|
+
var rea_ws = {
|
|
1457
|
+
/**Creates a delayed ok state from an initial value, delayed meaning the value is a promise evaluated on first access.
|
|
1458
|
+
* @param init initial value for state.
|
|
1459
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1460
|
+
ok(init, setter = true, helper) {
|
|
1461
|
+
return new RXA(
|
|
1462
|
+
init ? async () => ok4(await init()) : void 0,
|
|
1463
|
+
helper,
|
|
1464
|
+
setter
|
|
1465
|
+
);
|
|
1466
|
+
},
|
|
1467
|
+
/**Creates a writable delayed state from an initial error, delayed meaning the value is a promise evaluated on first access.
|
|
1468
|
+
* @param init initial error for state.
|
|
1469
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1470
|
+
err(init, setter = true, helper) {
|
|
1471
|
+
return new RXA(
|
|
1472
|
+
init ? async () => err5(await init()) : void 0,
|
|
1473
|
+
helper,
|
|
1474
|
+
setter
|
|
1475
|
+
);
|
|
1476
|
+
},
|
|
1477
|
+
/**Creates a delayed ok state from an initial result, delayed meaning the value is a promise evaluated on first access.
|
|
1478
|
+
* @param init initial result for state.
|
|
1479
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1480
|
+
result(init, setter = true, helper) {
|
|
1481
|
+
return new RXA(
|
|
1482
|
+
init,
|
|
1483
|
+
helper,
|
|
1484
|
+
setter
|
|
1485
|
+
);
|
|
1486
|
+
}
|
|
1487
|
+
};
|
|
1488
|
+
var rea_wa = {
|
|
1489
|
+
/**Creates a delayed ok state from an initial value, delayed meaning the value is a promise evaluated on first access.
|
|
1490
|
+
* @param init initial value for state.
|
|
1491
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1492
|
+
ok(init, setter = true, helper) {
|
|
1493
|
+
return new RXA(
|
|
1494
|
+
init ? async () => ok4(await init()) : void 0,
|
|
1495
|
+
helper,
|
|
1496
|
+
void 0,
|
|
1497
|
+
setter
|
|
1498
|
+
);
|
|
1499
|
+
},
|
|
1500
|
+
/**Creates a writable delayed state from an initial error, delayed meaning the value is a promise evaluated on first access.
|
|
1501
|
+
* @param init initial error for state.
|
|
1502
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1503
|
+
err(init, setter = true, helper) {
|
|
1504
|
+
return new RXA(
|
|
1505
|
+
init ? async () => err5(await init()) : void 0,
|
|
1506
|
+
helper,
|
|
1507
|
+
void 0,
|
|
1508
|
+
setter
|
|
1509
|
+
);
|
|
1510
|
+
},
|
|
1511
|
+
/**Creates a delayed ok state from an initial result, delayed meaning the value is a promise evaluated on first access.
|
|
1512
|
+
* @param init initial result for state.
|
|
1513
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1514
|
+
result(init, setter = true, helper) {
|
|
1515
|
+
return new RXA(
|
|
1516
|
+
init,
|
|
1517
|
+
helper,
|
|
1518
|
+
void 0,
|
|
1519
|
+
setter
|
|
1520
|
+
);
|
|
1521
|
+
}
|
|
1522
|
+
};
|
|
1523
|
+
var STATE_DELAYED = {
|
|
1524
|
+
/**Read only delayed states with guarenteed ok, delayed meaning the value is a promise evaluated on first access. */
|
|
1525
|
+
roa,
|
|
1526
|
+
/**Read write delayed states with guarenteed ok and sync write, delayed meaning the value is a promise evaluated on first access. */
|
|
1527
|
+
roa_ws,
|
|
1528
|
+
/**Read write delayed states with guarenteed ok and async write, delayed meaning the value is a promise evaluated on first access. */
|
|
1529
|
+
roa_wa,
|
|
1530
|
+
/**Read only delayed states with error, delayed meaning the value is a promise evaluated on first access. */
|
|
1531
|
+
rea,
|
|
1532
|
+
/**Read write delayed states with error and sync write, delayed meaning the value is a promise evaluated on first access. */
|
|
1533
|
+
rea_ws,
|
|
1534
|
+
/**Read write delayed state with error and async write, delayed meaning the value is a promise evaluated on first access. */
|
|
1535
|
+
rea_wa
|
|
1536
|
+
};
|
|
1537
|
+
|
|
1538
|
+
// src/helpers.ts
|
|
1539
|
+
import { number_step_start_decimal } from "@chocbite/ts-lib-math";
|
|
1540
|
+
import {
|
|
1541
|
+
err as err6,
|
|
1542
|
+
ok as ok5,
|
|
1543
|
+
some
|
|
1544
|
+
} from "@chocbite/ts-lib-result";
|
|
1545
|
+
var StateNumberHelper = class {
|
|
1546
|
+
min;
|
|
1547
|
+
max;
|
|
1548
|
+
unit;
|
|
1549
|
+
decimals;
|
|
1550
|
+
step;
|
|
1551
|
+
start;
|
|
1552
|
+
constructor(min, max, unit, decimals, step, start) {
|
|
1553
|
+
if (min !== void 0) this.min = min;
|
|
1554
|
+
if (max !== void 0) this.max = max;
|
|
1555
|
+
if (unit !== void 0) this.unit = unit;
|
|
1556
|
+
if (decimals !== void 0) {
|
|
1557
|
+
this.decimals = decimals;
|
|
1558
|
+
if (step !== void 0) this.step = step;
|
|
1559
|
+
if (start !== void 0) this.start = start;
|
|
1560
|
+
} else {
|
|
1561
|
+
if (step !== void 0) {
|
|
1562
|
+
this.step = step;
|
|
1563
|
+
const match = String(step).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
|
|
1564
|
+
this.decimals = match ? Math.max(
|
|
1565
|
+
0,
|
|
1566
|
+
(match[1] ? match[1].length : 0) - (match[2] ? +match[2] : 0)
|
|
1567
|
+
) : 0;
|
|
1568
|
+
if (start !== void 0) {
|
|
1569
|
+
this.start = start;
|
|
1570
|
+
const match2 = String(start).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
|
|
1571
|
+
this.decimals = Math.max(
|
|
1572
|
+
this.decimals,
|
|
1573
|
+
match2 ? Math.max(
|
|
1574
|
+
0,
|
|
1575
|
+
(match2[1] ? match2[1].length : 0) - (match2[2] ? +match2[2] : 0)
|
|
1576
|
+
) : 0
|
|
1577
|
+
);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
limit(value) {
|
|
1583
|
+
return ok5(
|
|
1584
|
+
Math.min(
|
|
1585
|
+
this.max ?? Infinity,
|
|
1586
|
+
Math.max(
|
|
1587
|
+
this.min ?? -Infinity,
|
|
1588
|
+
number_step_start_decimal(
|
|
1589
|
+
value,
|
|
1590
|
+
this.step,
|
|
1591
|
+
this.start,
|
|
1592
|
+
this.decimals
|
|
1593
|
+
)
|
|
1594
|
+
)
|
|
1595
|
+
)
|
|
1596
|
+
);
|
|
1597
|
+
}
|
|
1598
|
+
check(value) {
|
|
1599
|
+
if (this.max !== void 0 && value > this.max)
|
|
1600
|
+
return err6(value + " is bigger than the limit of " + this.max);
|
|
1601
|
+
if (this.min !== void 0 && value < this.min)
|
|
1602
|
+
return err6(value + " is smaller than the limit of " + this.min);
|
|
1603
|
+
return ok5(value);
|
|
1604
|
+
}
|
|
1605
|
+
related() {
|
|
1606
|
+
return some(this);
|
|
1607
|
+
}
|
|
1608
|
+
};
|
|
1609
|
+
var nums = {
|
|
1610
|
+
/**Number limiter struct
|
|
1611
|
+
* @param min minimum allowed number
|
|
1612
|
+
* @param max maximum allowed number
|
|
1613
|
+
* @param unit unit for number
|
|
1614
|
+
* @param decimals number of suggested decimals to show
|
|
1615
|
+
* @param step allowed step size for number 0.1 allows 0,0.1,0.2,0.3...
|
|
1616
|
+
* @param start start offset for step, 0.5 and step 2 allows 0.5,2.5,4.5,6.5*/
|
|
1617
|
+
helper(min, max, unit, decimals, step, start) {
|
|
1618
|
+
return new StateNumberHelper(min, max, unit, decimals, step, start);
|
|
1619
|
+
}
|
|
1620
|
+
};
|
|
1621
|
+
var StateStringHelper = class {
|
|
1622
|
+
max_length;
|
|
1623
|
+
max_length_bytes;
|
|
1624
|
+
constructor(max_length, max_length_bytes) {
|
|
1625
|
+
if (max_length !== void 0) this.max_length = max_length;
|
|
1626
|
+
if (max_length_bytes !== void 0)
|
|
1627
|
+
this.max_length_bytes = max_length_bytes;
|
|
1628
|
+
}
|
|
1629
|
+
limit(value) {
|
|
1630
|
+
if (this.max_length && value.length > this.max_length)
|
|
1631
|
+
value = value.slice(0, this.max_length);
|
|
1632
|
+
if (this.max_length_bytes) {
|
|
1633
|
+
value = new TextDecoder().decode(
|
|
1634
|
+
new TextEncoder().encode(value).slice(0, this.max_length_bytes)
|
|
1635
|
+
);
|
|
1636
|
+
if (value.at(-1)?.charCodeAt(0) === 65533) value = value.slice(0, -1);
|
|
1637
|
+
}
|
|
1638
|
+
return ok5(value);
|
|
1639
|
+
}
|
|
1640
|
+
check(value) {
|
|
1641
|
+
if (this.max_length !== void 0 && value.length > this.max_length)
|
|
1642
|
+
return err6(
|
|
1643
|
+
"the text is longer than the limit of " + this.max_length + " characters"
|
|
1644
|
+
);
|
|
1645
|
+
if (this.max_length_bytes !== void 0 && new TextEncoder().encode(value).length > this.max_length_bytes)
|
|
1646
|
+
return err6(
|
|
1647
|
+
"the text is longer than the limit of " + this.max_length_bytes + " bytes"
|
|
1648
|
+
);
|
|
1649
|
+
return ok5(value);
|
|
1650
|
+
}
|
|
1651
|
+
related() {
|
|
1652
|
+
return some(this);
|
|
1653
|
+
}
|
|
1654
|
+
};
|
|
1655
|
+
var strings = {
|
|
1656
|
+
/**String limiter struct
|
|
1657
|
+
* @param max_length max length for string
|
|
1658
|
+
* @param max_length_bytes max byte length for string*/
|
|
1659
|
+
helper(max_length, max_length_bytes) {
|
|
1660
|
+
return new StateStringHelper(max_length, max_length_bytes);
|
|
1661
|
+
}
|
|
1662
|
+
};
|
|
1663
|
+
var StateEnumHelper = class {
|
|
1664
|
+
list;
|
|
1665
|
+
constructor(list) {
|
|
1666
|
+
this.list = list;
|
|
1667
|
+
}
|
|
1668
|
+
map(func) {
|
|
1669
|
+
return Object.keys(this.list).map(
|
|
1670
|
+
(key) => func(key, this.list[key])
|
|
1671
|
+
);
|
|
1672
|
+
}
|
|
1673
|
+
limit(value) {
|
|
1674
|
+
return ok5(value);
|
|
1675
|
+
}
|
|
1676
|
+
check(value) {
|
|
1677
|
+
if (value in this.list) return ok5(value);
|
|
1678
|
+
return err6(String(value) + " is not in list");
|
|
1679
|
+
}
|
|
1680
|
+
related() {
|
|
1681
|
+
return some(this);
|
|
1682
|
+
}
|
|
1683
|
+
};
|
|
1684
|
+
var enums = {
|
|
1685
|
+
/**Creates an enum helper struct, use list method to make a list with correct typing*/
|
|
1686
|
+
helper(list) {
|
|
1687
|
+
return new StateEnumHelper(list);
|
|
1688
|
+
},
|
|
1689
|
+
/**Creates an enum description list, passing the enum as a generic type to this function makes things look a bit nicer */
|
|
1690
|
+
list(list) {
|
|
1691
|
+
return list;
|
|
1692
|
+
}
|
|
1693
|
+
};
|
|
1694
|
+
async function await_value(value, state2, timeout = 500) {
|
|
1695
|
+
let func = () => {
|
|
1696
|
+
};
|
|
1697
|
+
const res3 = await Promise.race([
|
|
1698
|
+
new Promise((a) => setTimeout(a, timeout, false)),
|
|
1699
|
+
new Promise((a) => {
|
|
1700
|
+
func = state2.sub((res4) => {
|
|
1701
|
+
if (res4.ok && res4.value === value) a(true);
|
|
1702
|
+
});
|
|
1703
|
+
})
|
|
1704
|
+
]);
|
|
1705
|
+
state2.unsub(func);
|
|
1706
|
+
return res3;
|
|
1707
|
+
}
|
|
1708
|
+
async function compare(state1, state2) {
|
|
1709
|
+
const res1 = await state1;
|
|
1710
|
+
const res22 = await state2;
|
|
1711
|
+
if (res1.err || res22.err) return false;
|
|
1712
|
+
return res1.value === res22.value;
|
|
1713
|
+
}
|
|
1714
|
+
function compare_sync(state1, state2) {
|
|
1715
|
+
const res1 = state1.get();
|
|
1716
|
+
const res22 = state2.get();
|
|
1717
|
+
if (res1.err || res22.err) return true;
|
|
1718
|
+
return res1.value !== res22.value;
|
|
1719
|
+
}
|
|
1720
|
+
var is = {
|
|
1721
|
+
rea(s) {
|
|
1722
|
+
return s instanceof StateBase;
|
|
1723
|
+
},
|
|
1724
|
+
roa(s) {
|
|
1725
|
+
return s instanceof StateBase && s.rok;
|
|
1726
|
+
},
|
|
1727
|
+
res(s) {
|
|
1728
|
+
return s instanceof StateBase && s.rsync;
|
|
1729
|
+
},
|
|
1730
|
+
ros(s) {
|
|
1731
|
+
return s instanceof StateBase && s.rsync && s.rok;
|
|
1732
|
+
},
|
|
1733
|
+
rea_wa(s) {
|
|
1734
|
+
return s instanceof StateBase && s.writable;
|
|
1735
|
+
},
|
|
1736
|
+
rea_ws(s) {
|
|
1737
|
+
return s instanceof StateBase && s.writable && s.wsync;
|
|
1738
|
+
},
|
|
1739
|
+
roa_wa(s) {
|
|
1740
|
+
return s instanceof StateBase && s.writable && s.rok;
|
|
1741
|
+
},
|
|
1742
|
+
roa_ws(s) {
|
|
1743
|
+
return s instanceof StateBase && s.writable && s.wsync && s.rok;
|
|
1744
|
+
},
|
|
1745
|
+
res_wa(s) {
|
|
1746
|
+
return s instanceof StateBase && s.writable && s.rsync;
|
|
1747
|
+
},
|
|
1748
|
+
res_ws(s) {
|
|
1749
|
+
return s instanceof StateBase && s.writable && s.wsync && s.rsync;
|
|
1750
|
+
},
|
|
1751
|
+
ros_wa(s) {
|
|
1752
|
+
return s instanceof StateBase && s.writable && s.rsync && s.rok;
|
|
1753
|
+
},
|
|
1754
|
+
ros_ws(s) {
|
|
1755
|
+
return s instanceof StateBase && s.writable && s.wsync && s.rsync && s.rok;
|
|
1756
|
+
}
|
|
1757
|
+
};
|
|
1758
|
+
var STATE_HELPERS = {
|
|
1759
|
+
is,
|
|
1760
|
+
nums,
|
|
1761
|
+
strings,
|
|
1762
|
+
enums,
|
|
1763
|
+
await_value,
|
|
1764
|
+
compare,
|
|
1765
|
+
compare_sync
|
|
1766
|
+
};
|
|
1767
|
+
|
|
1768
|
+
// src/lazy/lazy.ts
|
|
1769
|
+
import {
|
|
1770
|
+
err as err7,
|
|
1771
|
+
none as none8,
|
|
1772
|
+
ok as ok6
|
|
1773
|
+
} from "@chocbite/ts-lib-result";
|
|
1774
|
+
var RXS3 = class extends StateBase {
|
|
1775
|
+
constructor(init, helper, setter) {
|
|
1776
|
+
super();
|
|
1777
|
+
if (setter === true)
|
|
1778
|
+
this.#setter = (value, state2, old) => {
|
|
1779
|
+
if (old && !old.err && value === old.value)
|
|
1780
|
+
return ok6(void 0);
|
|
1781
|
+
return this.#helper?.limit ? this.#helper?.limit(value).map((e) => state2.set_ok(e)) : ok6(state2.set_ok(value));
|
|
1782
|
+
};
|
|
1783
|
+
else this.#setter = setter;
|
|
1784
|
+
if (helper) this.#helper = helper;
|
|
1785
|
+
this.get = () => this.#clean() ?? (this.#value = init());
|
|
1786
|
+
this.set = (value) => this.set(this.#clean() ?? value);
|
|
1787
|
+
const write_sync = this.write_sync.bind(this);
|
|
1788
|
+
this.write_sync = (value) => write_sync(value).map((val) => this.#clean() ?? val);
|
|
1789
|
+
}
|
|
1790
|
+
#clean() {
|
|
1791
|
+
["get", "set", "write_sync"].forEach((k) => delete this[k]);
|
|
1792
|
+
}
|
|
1793
|
+
#value;
|
|
1794
|
+
#setter;
|
|
1795
|
+
#helper;
|
|
1796
|
+
//#Owner Context
|
|
1797
|
+
set(value) {
|
|
1798
|
+
this.update_subs(this.#value = value);
|
|
1799
|
+
}
|
|
1800
|
+
set_ok(value) {
|
|
1801
|
+
this.set(ok6(value));
|
|
1802
|
+
}
|
|
1803
|
+
set_err(error) {
|
|
1804
|
+
this.set(err7(error));
|
|
1805
|
+
}
|
|
1806
|
+
set setter(setter) {
|
|
1807
|
+
this.#setter = setter;
|
|
1808
|
+
}
|
|
1809
|
+
get setter() {
|
|
1810
|
+
return this.#setter;
|
|
1811
|
+
}
|
|
1812
|
+
get state() {
|
|
1813
|
+
return this;
|
|
1814
|
+
}
|
|
1815
|
+
get read_only() {
|
|
1816
|
+
return this;
|
|
1817
|
+
}
|
|
1818
|
+
get read_write() {
|
|
1819
|
+
return this.#setter ? this : void 0;
|
|
1820
|
+
}
|
|
1821
|
+
//#Reader Context
|
|
1822
|
+
get rok() {
|
|
1823
|
+
return true;
|
|
1824
|
+
}
|
|
1825
|
+
get rsync() {
|
|
1826
|
+
return true;
|
|
1827
|
+
}
|
|
1828
|
+
async then(func) {
|
|
1829
|
+
return func(this.get());
|
|
1830
|
+
}
|
|
1831
|
+
get() {
|
|
1832
|
+
return this.#value;
|
|
1833
|
+
}
|
|
1834
|
+
ok() {
|
|
1835
|
+
return this.get().value;
|
|
1836
|
+
}
|
|
1837
|
+
related() {
|
|
1838
|
+
return this.#helper?.related ? this.#helper.related() : none8();
|
|
1839
|
+
}
|
|
1840
|
+
//#Writer Context
|
|
1841
|
+
get writable() {
|
|
1842
|
+
return this.#setter !== void 0;
|
|
1843
|
+
}
|
|
1844
|
+
get wsync() {
|
|
1845
|
+
return this.writable;
|
|
1846
|
+
}
|
|
1847
|
+
async write(value) {
|
|
1848
|
+
return this.write_sync(value);
|
|
1849
|
+
}
|
|
1850
|
+
write_sync(value) {
|
|
1851
|
+
if (this.#setter)
|
|
1852
|
+
return this.#setter(
|
|
1853
|
+
value,
|
|
1854
|
+
this,
|
|
1855
|
+
this.#value
|
|
1856
|
+
);
|
|
1857
|
+
return err7("State not writable");
|
|
1858
|
+
}
|
|
1859
|
+
limit(value) {
|
|
1860
|
+
return this.#helper?.limit ? this.#helper.limit(value) : ok6(value);
|
|
1861
|
+
}
|
|
1862
|
+
check(value) {
|
|
1863
|
+
return this.#helper?.check ? this.#helper.check(value) : ok6(value);
|
|
1864
|
+
}
|
|
1865
|
+
};
|
|
1866
|
+
var ros2 = {
|
|
1867
|
+
/**Creates a lazy ok state from an initial value, lazy meaning the value is only evaluated on first access.
|
|
1868
|
+
* @param init initial value for state.
|
|
1869
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1870
|
+
ok(init, helper) {
|
|
1871
|
+
return new RXS3(
|
|
1872
|
+
() => ok6(init()),
|
|
1873
|
+
helper
|
|
1874
|
+
);
|
|
1875
|
+
},
|
|
1876
|
+
/**Creates a lazy ok state from an initial result, lazy meaning the value is only evaluated on first access.
|
|
1877
|
+
* @param init initial result for state.
|
|
1878
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1879
|
+
result(init, helper) {
|
|
1880
|
+
return new RXS3(init, helper);
|
|
1881
|
+
}
|
|
1882
|
+
};
|
|
1883
|
+
var ros_ws2 = {
|
|
1884
|
+
/**Creates a lazy ok state from an initial value, lazy meaning the value is only evaluated on first access.
|
|
1885
|
+
* @param init initial value for state.
|
|
1886
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1887
|
+
ok(init, setter = true, helper) {
|
|
1888
|
+
return new RXS3(
|
|
1889
|
+
() => ok6(init()),
|
|
1890
|
+
helper,
|
|
1891
|
+
setter
|
|
1892
|
+
);
|
|
1893
|
+
},
|
|
1894
|
+
/**Creates a lazy ok state from an initial result, lazy meaning the value is only evaluated on first access.
|
|
1895
|
+
* @param init initial result for state.
|
|
1896
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1897
|
+
result(init, setter = true, helper) {
|
|
1898
|
+
return new RXS3(
|
|
1899
|
+
init,
|
|
1900
|
+
helper,
|
|
1901
|
+
setter
|
|
1902
|
+
);
|
|
1903
|
+
}
|
|
1904
|
+
};
|
|
1905
|
+
var res2 = {
|
|
1906
|
+
/**Creates a lazy state from an initial value, lazy meaning the value is only evaluated on first access.
|
|
1907
|
+
* @param init initial value for state.
|
|
1908
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1909
|
+
ok(init, helper) {
|
|
1910
|
+
return new RXS3(
|
|
1911
|
+
() => ok6(init()),
|
|
1912
|
+
helper
|
|
1913
|
+
);
|
|
1914
|
+
},
|
|
1915
|
+
/**Creates a lazy state from an initial error, lazy meaning the value is only evaluated on first access.
|
|
1916
|
+
* @param init initial error for state.
|
|
1917
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1918
|
+
err(init, helper) {
|
|
1919
|
+
return new RXS3(
|
|
1920
|
+
() => err7(init()),
|
|
1921
|
+
helper
|
|
1922
|
+
);
|
|
1923
|
+
},
|
|
1924
|
+
/**Creates a lazy state from an initial result, lazy meaning the value is only evaluated on first access.
|
|
1925
|
+
* @param init initial result for state.
|
|
1926
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1927
|
+
result(init, helper) {
|
|
1928
|
+
return new RXS3(
|
|
1929
|
+
init,
|
|
1930
|
+
helper
|
|
1931
|
+
);
|
|
1932
|
+
}
|
|
1933
|
+
};
|
|
1934
|
+
var res_ws2 = {
|
|
1935
|
+
/**Creates a writable lazy state from an initial value, lazy meaning the value is only evaluated on first access.
|
|
1936
|
+
* @param init initial value for state.
|
|
1937
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1938
|
+
ok(init, setter = true, helper) {
|
|
1939
|
+
return new RXS3(
|
|
1940
|
+
() => ok6(init()),
|
|
1941
|
+
helper,
|
|
1942
|
+
setter
|
|
1943
|
+
);
|
|
1944
|
+
},
|
|
1945
|
+
/**Creates a writable lazy state from an initial error, lazy meaning the value is only evaluated on first access.
|
|
1946
|
+
* @param init initial error for state.
|
|
1947
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1948
|
+
err(init, setter = true, helper) {
|
|
1949
|
+
return new RXS3(
|
|
1950
|
+
() => err7(init()),
|
|
1951
|
+
helper,
|
|
1952
|
+
setter
|
|
1953
|
+
);
|
|
1954
|
+
},
|
|
1955
|
+
/**Creates a writable lazy state from an initial result, lazy meaning the value is only evaluated on first access.
|
|
1956
|
+
* @param init initial result for state.
|
|
1957
|
+
* @param helper functions to check and limit the value, and to return related states.*/
|
|
1958
|
+
result(init, setter = true, helper) {
|
|
1959
|
+
return new RXS3(
|
|
1960
|
+
init,
|
|
1961
|
+
helper,
|
|
1962
|
+
setter
|
|
1963
|
+
);
|
|
1964
|
+
}
|
|
1965
|
+
};
|
|
1966
|
+
var STATE_LAZY = {
|
|
1967
|
+
/**Sync Read lazy states with guarenteed ok, lazy meaning the value is only evaluated on first access. */
|
|
1968
|
+
ros: ros2,
|
|
1969
|
+
/**Sync Read And Sync Write lazy states with guarenteed ok, lazy meaning the value is only evaluated on first access. */
|
|
1970
|
+
ros_ws: ros_ws2,
|
|
1971
|
+
/**Sync Read lazy states with error, lazy meaning the value is only evaluated on first access. */
|
|
1972
|
+
res: res2,
|
|
1973
|
+
/**Sync Read And Sync Write lazy states with error, lazy meaning the value is only evaluated on first access. */
|
|
1974
|
+
res_ws: res_ws2
|
|
1975
|
+
};
|
|
1976
|
+
|
|
1977
|
+
// src/proxy/rea.ts
|
|
1978
|
+
import {
|
|
1979
|
+
err as err8,
|
|
1980
|
+
none as none9
|
|
1981
|
+
} from "@chocbite/ts-lib-result";
|
|
1982
|
+
var REA2 = class extends StateBase {
|
|
1983
|
+
constructor(state2, transform_read) {
|
|
1984
|
+
super();
|
|
1985
|
+
this.#state = state2;
|
|
1986
|
+
if (transform_read) this.transform_read = transform_read;
|
|
1987
|
+
}
|
|
1988
|
+
#state;
|
|
1989
|
+
#subscriber = (value) => {
|
|
1990
|
+
this.#buffer = this.transform_read(value);
|
|
1991
|
+
this.update_subs(this.#buffer);
|
|
1992
|
+
};
|
|
1993
|
+
#buffer;
|
|
1994
|
+
transform_read(value) {
|
|
1995
|
+
return value;
|
|
1996
|
+
}
|
|
1997
|
+
transform_write;
|
|
1998
|
+
on_subscribe(run = false) {
|
|
1999
|
+
this.#state.sub(this.#subscriber, run);
|
|
2000
|
+
}
|
|
2001
|
+
on_unsubscribe() {
|
|
2002
|
+
this.#state.unsub(this.#subscriber);
|
|
2003
|
+
this.#buffer = void 0;
|
|
2004
|
+
}
|
|
2005
|
+
//#Owner Context
|
|
2006
|
+
set_state(state2) {
|
|
2007
|
+
if (this.in_use()) {
|
|
2008
|
+
this.on_unsubscribe();
|
|
2009
|
+
this.#state = state2;
|
|
2010
|
+
this.on_subscribe(true);
|
|
2011
|
+
} else this.#state = state2;
|
|
2012
|
+
}
|
|
2013
|
+
set_transform_read(transform) {
|
|
2014
|
+
if (this.in_use()) {
|
|
2015
|
+
this.on_unsubscribe();
|
|
2016
|
+
this.transform_read = transform;
|
|
2017
|
+
this.on_subscribe(true);
|
|
2018
|
+
} else this.transform_read = transform;
|
|
2019
|
+
}
|
|
2020
|
+
set_transform_write(transform) {
|
|
2021
|
+
this.transform_write = transform;
|
|
2022
|
+
}
|
|
2023
|
+
get state() {
|
|
2024
|
+
return this;
|
|
2025
|
+
}
|
|
2026
|
+
get read_only() {
|
|
2027
|
+
return this;
|
|
2028
|
+
}
|
|
2029
|
+
//#Reader Context
|
|
2030
|
+
get rok() {
|
|
2031
|
+
return this.#state.rok;
|
|
2032
|
+
}
|
|
2033
|
+
get rsync() {
|
|
2034
|
+
return this.#state.rsync;
|
|
2035
|
+
}
|
|
2036
|
+
async then(func) {
|
|
2037
|
+
if (this.#buffer) return func(this.#buffer);
|
|
2038
|
+
return func(this.transform_read(await this.#state));
|
|
2039
|
+
}
|
|
2040
|
+
related() {
|
|
2041
|
+
return none9();
|
|
2042
|
+
}
|
|
2043
|
+
//#Writer Context
|
|
2044
|
+
get writable() {
|
|
2045
|
+
return this.#state.writable;
|
|
2046
|
+
}
|
|
2047
|
+
get wsync() {
|
|
2048
|
+
return this.#state.wsync;
|
|
2049
|
+
}
|
|
2050
|
+
async write(value) {
|
|
2051
|
+
if (!this.#state.write) return err8("State not writable");
|
|
2052
|
+
if (!this.transform_write) return err8("State not writable");
|
|
2053
|
+
return this.#state.write(this.transform_write(value));
|
|
2054
|
+
}
|
|
2055
|
+
write_sync(value) {
|
|
2056
|
+
if (!this.#state.write_sync) return err8("State not writable");
|
|
2057
|
+
if (!this.transform_write) return err8("State not writable");
|
|
2058
|
+
return this.#state.write_sync(this.transform_write(value));
|
|
2059
|
+
}
|
|
2060
|
+
limit(_value) {
|
|
2061
|
+
return err8("Limit not supported on proxy states");
|
|
2062
|
+
}
|
|
2063
|
+
check(_value) {
|
|
2064
|
+
return err8("Check not supported on proxy states");
|
|
2065
|
+
}
|
|
2066
|
+
};
|
|
2067
|
+
function rea_from(state2, transform) {
|
|
2068
|
+
return new REA2(state2, transform);
|
|
2069
|
+
}
|
|
2070
|
+
var REAWS = class extends StateBase {
|
|
2071
|
+
constructor(state2, transform_read, transform_write) {
|
|
2072
|
+
super();
|
|
2073
|
+
this.#state = state2;
|
|
2074
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2075
|
+
if (transform_write) this.transform_write = transform_write;
|
|
2076
|
+
}
|
|
2077
|
+
#state;
|
|
2078
|
+
#subscriber = (value) => {
|
|
2079
|
+
this.#buffer = this.transform_read(value);
|
|
2080
|
+
this.update_subs(this.#buffer);
|
|
2081
|
+
};
|
|
2082
|
+
#buffer;
|
|
2083
|
+
transform_read(value) {
|
|
2084
|
+
return value;
|
|
2085
|
+
}
|
|
2086
|
+
transform_write(value) {
|
|
2087
|
+
return value;
|
|
2088
|
+
}
|
|
2089
|
+
on_subscribe(run = false) {
|
|
2090
|
+
this.#state.sub(this.#subscriber, run);
|
|
2091
|
+
}
|
|
2092
|
+
on_unsubscribe() {
|
|
2093
|
+
this.#state.unsub(this.#subscriber);
|
|
2094
|
+
this.#buffer = void 0;
|
|
2095
|
+
}
|
|
2096
|
+
//#Owner Context
|
|
2097
|
+
set_state(state2) {
|
|
2098
|
+
if (this.in_use()) {
|
|
2099
|
+
this.on_unsubscribe();
|
|
2100
|
+
this.#state = state2;
|
|
2101
|
+
this.on_subscribe(true);
|
|
2102
|
+
} else this.#state = state2;
|
|
2103
|
+
}
|
|
2104
|
+
set_transform_read(transform) {
|
|
2105
|
+
if (this.in_use()) {
|
|
2106
|
+
this.on_unsubscribe();
|
|
2107
|
+
this.transform_read = transform;
|
|
2108
|
+
this.on_subscribe(true);
|
|
2109
|
+
} else this.transform_read = transform;
|
|
2110
|
+
}
|
|
2111
|
+
set_transform_write(transform) {
|
|
2112
|
+
this.transform_write = transform;
|
|
2113
|
+
}
|
|
2114
|
+
get state() {
|
|
2115
|
+
return this;
|
|
2116
|
+
}
|
|
2117
|
+
get read_only() {
|
|
2118
|
+
return this;
|
|
2119
|
+
}
|
|
2120
|
+
get read_write() {
|
|
2121
|
+
return this;
|
|
2122
|
+
}
|
|
2123
|
+
//#Reader Context
|
|
2124
|
+
get rok() {
|
|
2125
|
+
return this.#state.rok;
|
|
2126
|
+
}
|
|
2127
|
+
get rsync() {
|
|
2128
|
+
return this.#state.rsync;
|
|
2129
|
+
}
|
|
2130
|
+
async then(func) {
|
|
2131
|
+
if (this.#buffer) return func(this.#buffer);
|
|
2132
|
+
return func(this.transform_read(await this.#state));
|
|
2133
|
+
}
|
|
2134
|
+
related() {
|
|
2135
|
+
return none9();
|
|
2136
|
+
}
|
|
2137
|
+
//#Writer Context
|
|
2138
|
+
get writable() {
|
|
2139
|
+
return true;
|
|
2140
|
+
}
|
|
2141
|
+
get wsync() {
|
|
2142
|
+
return true;
|
|
2143
|
+
}
|
|
2144
|
+
write(value) {
|
|
2145
|
+
return this.#state.write(this.transform_write(value));
|
|
2146
|
+
}
|
|
2147
|
+
write_sync(value) {
|
|
2148
|
+
return this.#state.write_sync(this.transform_write(value));
|
|
2149
|
+
}
|
|
2150
|
+
limit(_value) {
|
|
2151
|
+
return err8("Limit not supported on proxy states");
|
|
2152
|
+
}
|
|
2153
|
+
check(_value) {
|
|
2154
|
+
return err8("Check not supported on proxy states");
|
|
2155
|
+
}
|
|
2156
|
+
};
|
|
2157
|
+
function rea_ws_from(state2, transform_read, transform_write) {
|
|
2158
|
+
return new REAWS(
|
|
2159
|
+
state2,
|
|
2160
|
+
transform_read,
|
|
2161
|
+
transform_write
|
|
2162
|
+
);
|
|
2163
|
+
}
|
|
2164
|
+
var REAWA = class extends StateBase {
|
|
2165
|
+
constructor(state2, transform_read, transform_write) {
|
|
2166
|
+
super();
|
|
2167
|
+
this.#state = state2;
|
|
2168
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2169
|
+
if (transform_write) this.transform_write = transform_write;
|
|
2170
|
+
}
|
|
2171
|
+
#state;
|
|
2172
|
+
#subscriber = (value) => {
|
|
2173
|
+
this.#buffer = this.transform_read(value);
|
|
2174
|
+
this.update_subs(this.#buffer);
|
|
2175
|
+
};
|
|
2176
|
+
#buffer;
|
|
2177
|
+
transform_read(value) {
|
|
2178
|
+
return value;
|
|
2179
|
+
}
|
|
2180
|
+
transform_write(value) {
|
|
2181
|
+
return value;
|
|
2182
|
+
}
|
|
2183
|
+
on_subscribe(run = false) {
|
|
2184
|
+
this.#state.sub(this.#subscriber, run);
|
|
2185
|
+
}
|
|
2186
|
+
on_unsubscribe() {
|
|
2187
|
+
this.#state.unsub(this.#subscriber);
|
|
2188
|
+
this.#buffer = void 0;
|
|
2189
|
+
}
|
|
2190
|
+
//#Owner Context
|
|
2191
|
+
set_state(state2) {
|
|
2192
|
+
if (this.in_use()) {
|
|
2193
|
+
this.on_unsubscribe();
|
|
2194
|
+
this.#state = state2;
|
|
2195
|
+
this.on_subscribe(true);
|
|
2196
|
+
} else this.#state = state2;
|
|
2197
|
+
}
|
|
2198
|
+
set_transform_read(transform) {
|
|
2199
|
+
if (this.in_use()) {
|
|
2200
|
+
this.on_unsubscribe();
|
|
2201
|
+
this.transform_read = transform;
|
|
2202
|
+
this.on_subscribe(true);
|
|
2203
|
+
} else this.transform_read = transform;
|
|
2204
|
+
}
|
|
2205
|
+
set_transform_write(transform) {
|
|
2206
|
+
this.transform_write = transform;
|
|
2207
|
+
}
|
|
2208
|
+
get state() {
|
|
2209
|
+
return this;
|
|
2210
|
+
}
|
|
2211
|
+
get read_only() {
|
|
2212
|
+
return this;
|
|
2213
|
+
}
|
|
2214
|
+
get read_write() {
|
|
2215
|
+
return this;
|
|
2216
|
+
}
|
|
2217
|
+
//#Reader Context
|
|
2218
|
+
get rok() {
|
|
2219
|
+
return this.#state.rok;
|
|
2220
|
+
}
|
|
2221
|
+
get rsync() {
|
|
2222
|
+
return this.#state.rsync;
|
|
2223
|
+
}
|
|
2224
|
+
async then(func) {
|
|
2225
|
+
if (this.#buffer) return func(this.#buffer);
|
|
2226
|
+
return func(this.transform_read(await this.#state));
|
|
2227
|
+
}
|
|
2228
|
+
related() {
|
|
2229
|
+
return none9();
|
|
2230
|
+
}
|
|
2231
|
+
//#Writer Context
|
|
2232
|
+
get writable() {
|
|
2233
|
+
return true;
|
|
2234
|
+
}
|
|
2235
|
+
get wsync() {
|
|
2236
|
+
return this.#state.wsync;
|
|
2237
|
+
}
|
|
2238
|
+
write(value) {
|
|
2239
|
+
return this.#state.write(this.transform_write(value));
|
|
2240
|
+
}
|
|
2241
|
+
limit(_value) {
|
|
2242
|
+
return err8("Limit not supported on proxy states");
|
|
2243
|
+
}
|
|
2244
|
+
check(_value) {
|
|
2245
|
+
return err8("Check not supported on proxy states");
|
|
2246
|
+
}
|
|
2247
|
+
};
|
|
2248
|
+
function rea_wa_from(state2, transform_read, transform_write) {
|
|
2249
|
+
return new REAWA(
|
|
2250
|
+
state2,
|
|
2251
|
+
transform_read,
|
|
2252
|
+
transform_write
|
|
2253
|
+
);
|
|
2254
|
+
}
|
|
2255
|
+
var STATE_PROXY_REA = {
|
|
2256
|
+
rea: rea_from,
|
|
2257
|
+
rea_ws: rea_ws_from,
|
|
2258
|
+
rea_wa: rea_wa_from
|
|
2259
|
+
};
|
|
2260
|
+
|
|
2261
|
+
// src/proxy/res.ts
|
|
2262
|
+
import {
|
|
2263
|
+
err as err9,
|
|
2264
|
+
none as none10
|
|
2265
|
+
} from "@chocbite/ts-lib-result";
|
|
2266
|
+
var RES3 = class extends StateBase {
|
|
2267
|
+
constructor(state2, transform_read) {
|
|
2268
|
+
super();
|
|
2269
|
+
this.#state = state2;
|
|
2270
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2271
|
+
}
|
|
2272
|
+
#state;
|
|
2273
|
+
#subscriber = (value) => {
|
|
2274
|
+
this.#buffer = this.transform_read(value);
|
|
2275
|
+
this.update_subs(this.#buffer);
|
|
2276
|
+
};
|
|
2277
|
+
#buffer;
|
|
2278
|
+
transform_read(value) {
|
|
2279
|
+
return value;
|
|
2280
|
+
}
|
|
2281
|
+
transform_write;
|
|
2282
|
+
on_subscribe(run = false) {
|
|
2283
|
+
this.#state.sub(this.#subscriber, run);
|
|
2284
|
+
}
|
|
2285
|
+
on_unsubscribe() {
|
|
2286
|
+
this.#state.unsub(this.#subscriber);
|
|
2287
|
+
this.#buffer = void 0;
|
|
2288
|
+
}
|
|
2289
|
+
//#Owner Context
|
|
2290
|
+
set_state(state2) {
|
|
2291
|
+
if (this.in_use()) {
|
|
2292
|
+
this.on_unsubscribe();
|
|
2293
|
+
this.#state = state2;
|
|
2294
|
+
this.on_subscribe(true);
|
|
2295
|
+
} else this.#state = state2;
|
|
2296
|
+
}
|
|
2297
|
+
set_transform_read(transform) {
|
|
2298
|
+
if (this.in_use()) {
|
|
2299
|
+
this.on_unsubscribe();
|
|
2300
|
+
this.transform_read = transform;
|
|
2301
|
+
this.on_subscribe(true);
|
|
2302
|
+
} else this.transform_read = transform;
|
|
2303
|
+
}
|
|
2304
|
+
set_transform_write(transform) {
|
|
2305
|
+
this.transform_write = transform;
|
|
2306
|
+
}
|
|
2307
|
+
get state() {
|
|
2308
|
+
return this;
|
|
2309
|
+
}
|
|
2310
|
+
get read_only() {
|
|
2311
|
+
return this;
|
|
2312
|
+
}
|
|
2313
|
+
//#Reader Context
|
|
2314
|
+
get rok() {
|
|
2315
|
+
return this.#state.rok;
|
|
2316
|
+
}
|
|
2317
|
+
get rsync() {
|
|
2318
|
+
return true;
|
|
2319
|
+
}
|
|
2320
|
+
async then(func) {
|
|
2321
|
+
return func(this.get());
|
|
2322
|
+
}
|
|
2323
|
+
get() {
|
|
2324
|
+
if (this.#buffer) return this.#buffer;
|
|
2325
|
+
return this.transform_read(this.#state.get());
|
|
2326
|
+
}
|
|
2327
|
+
related() {
|
|
2328
|
+
return none10();
|
|
2329
|
+
}
|
|
2330
|
+
//#Writer Context
|
|
2331
|
+
get writable() {
|
|
2332
|
+
return this.#state.writable;
|
|
2333
|
+
}
|
|
2334
|
+
get wsync() {
|
|
2335
|
+
return this.#state.wsync;
|
|
2336
|
+
}
|
|
2337
|
+
async write(value) {
|
|
2338
|
+
if (!this.#state.write) return err9("State not writable");
|
|
2339
|
+
if (!this.transform_write) return err9("State not writable");
|
|
2340
|
+
return this.#state.write(this.transform_write(value));
|
|
2341
|
+
}
|
|
2342
|
+
write_sync(value) {
|
|
2343
|
+
if (!this.#state.write_sync) return err9("State not writable");
|
|
2344
|
+
if (!this.transform_write) return err9("State not writable");
|
|
2345
|
+
return this.#state.write_sync(this.transform_write(value));
|
|
2346
|
+
}
|
|
2347
|
+
limit() {
|
|
2348
|
+
return err9("Limit not supported on proxy states");
|
|
2349
|
+
}
|
|
2350
|
+
check() {
|
|
2351
|
+
return err9("Check not supported on proxy states");
|
|
2352
|
+
}
|
|
2353
|
+
};
|
|
2354
|
+
function res_from(state2, transform) {
|
|
2355
|
+
return new RES3(state2, transform);
|
|
2356
|
+
}
|
|
2357
|
+
var RESWS = class extends StateBase {
|
|
2358
|
+
constructor(state2, transform_read, transform_write) {
|
|
2359
|
+
super();
|
|
2360
|
+
this.#state = state2;
|
|
2361
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2362
|
+
if (transform_write) this.transform_write = transform_write;
|
|
2363
|
+
}
|
|
2364
|
+
#state;
|
|
2365
|
+
#subscriber = (value) => {
|
|
2366
|
+
this.#buffer = this.transform_read(value);
|
|
2367
|
+
this.update_subs(this.#buffer);
|
|
2368
|
+
};
|
|
2369
|
+
#buffer;
|
|
2370
|
+
transform_read(value) {
|
|
2371
|
+
return value;
|
|
2372
|
+
}
|
|
2373
|
+
transform_write(value) {
|
|
2374
|
+
return value;
|
|
2375
|
+
}
|
|
2376
|
+
on_subscribe(run = false) {
|
|
2377
|
+
this.#state.sub(this.#subscriber, run);
|
|
2378
|
+
}
|
|
2379
|
+
on_unsubscribe() {
|
|
2380
|
+
this.#state.unsub(this.#subscriber);
|
|
2381
|
+
this.#buffer = void 0;
|
|
2382
|
+
}
|
|
2383
|
+
//#Owner Context
|
|
2384
|
+
/**Sets the state that is being proxied, and updates subscribers with new value*/
|
|
2385
|
+
set_state(state2) {
|
|
2386
|
+
if (this.in_use()) {
|
|
2387
|
+
this.on_unsubscribe();
|
|
2388
|
+
this.#state = state2;
|
|
2389
|
+
this.on_subscribe(true);
|
|
2390
|
+
} else this.#state = state2;
|
|
2391
|
+
}
|
|
2392
|
+
/**Changes the transform function of the proxy, and updates subscribers with new value*/
|
|
2393
|
+
set_transform_read(transform) {
|
|
2394
|
+
if (this.in_use()) {
|
|
2395
|
+
this.on_unsubscribe();
|
|
2396
|
+
this.transform_read = transform;
|
|
2397
|
+
this.on_subscribe(true);
|
|
2398
|
+
} else this.transform_read = transform;
|
|
2399
|
+
}
|
|
2400
|
+
/**Changes the transform function of the proxy, and updates subscribers with new value*/
|
|
2401
|
+
set_transform_write(transform) {
|
|
2402
|
+
this.transform_write = transform;
|
|
2403
|
+
}
|
|
2404
|
+
get state() {
|
|
2405
|
+
return this;
|
|
2406
|
+
}
|
|
2407
|
+
get read_only() {
|
|
2408
|
+
return this;
|
|
2409
|
+
}
|
|
2410
|
+
get read_write() {
|
|
2411
|
+
return this;
|
|
2412
|
+
}
|
|
2413
|
+
//#Reader Context
|
|
2414
|
+
get rok() {
|
|
2415
|
+
return this.#state.rok;
|
|
2416
|
+
}
|
|
2417
|
+
get rsync() {
|
|
2418
|
+
return true;
|
|
2419
|
+
}
|
|
2420
|
+
async then(func) {
|
|
2421
|
+
if (this.#buffer) return func(this.#buffer);
|
|
2422
|
+
return func(this.transform_read(await this.#state));
|
|
2423
|
+
}
|
|
2424
|
+
get() {
|
|
2425
|
+
if (this.#buffer) return this.#buffer;
|
|
2426
|
+
return this.transform_read(this.#state.get());
|
|
2427
|
+
}
|
|
2428
|
+
related() {
|
|
2429
|
+
return none10();
|
|
2430
|
+
}
|
|
2431
|
+
//#Writer Context
|
|
2432
|
+
get writable() {
|
|
2433
|
+
return true;
|
|
2434
|
+
}
|
|
2435
|
+
get wsync() {
|
|
2436
|
+
return true;
|
|
2437
|
+
}
|
|
2438
|
+
write(value) {
|
|
2439
|
+
return this.#state.write(this.transform_write(value));
|
|
2440
|
+
}
|
|
2441
|
+
write_sync(value) {
|
|
2442
|
+
return this.#state.write_sync(this.transform_write(value));
|
|
2443
|
+
}
|
|
2444
|
+
limit() {
|
|
2445
|
+
return err9("Limit not supported on proxy states");
|
|
2446
|
+
}
|
|
2447
|
+
check() {
|
|
2448
|
+
return err9("Check not supported on proxy states");
|
|
2449
|
+
}
|
|
2450
|
+
};
|
|
2451
|
+
function res_ws_from(state2, transform_read, transform_write) {
|
|
2452
|
+
return new RESWS(
|
|
2453
|
+
state2,
|
|
2454
|
+
transform_read,
|
|
2455
|
+
transform_write
|
|
2456
|
+
);
|
|
2457
|
+
}
|
|
2458
|
+
var RESWA = class extends StateBase {
|
|
2459
|
+
constructor(state2, transform_read, transform_write) {
|
|
2460
|
+
super();
|
|
2461
|
+
this.#state = state2;
|
|
2462
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2463
|
+
if (transform_write) this.transform_write = transform_write;
|
|
2464
|
+
}
|
|
2465
|
+
#state;
|
|
2466
|
+
#subscriber = (value) => {
|
|
2467
|
+
this.#buffer = this.transform_read(value);
|
|
2468
|
+
this.update_subs(this.#buffer);
|
|
2469
|
+
};
|
|
2470
|
+
#buffer;
|
|
2471
|
+
transform_read(value) {
|
|
2472
|
+
return value;
|
|
2473
|
+
}
|
|
2474
|
+
transform_write(value) {
|
|
2475
|
+
return value;
|
|
2476
|
+
}
|
|
2477
|
+
on_subscribe(run = false) {
|
|
2478
|
+
this.#state.sub(this.#subscriber, run);
|
|
2479
|
+
}
|
|
2480
|
+
on_unsubscribe() {
|
|
2481
|
+
this.#state.unsub(this.#subscriber);
|
|
2482
|
+
this.#buffer = void 0;
|
|
2483
|
+
}
|
|
2484
|
+
//#Owner Context
|
|
2485
|
+
/**Sets the state that is being proxied, and updates subscribers with new value*/
|
|
2486
|
+
set_state(state2) {
|
|
2487
|
+
if (this.in_use()) {
|
|
2488
|
+
this.on_unsubscribe();
|
|
2489
|
+
this.#state = state2;
|
|
2490
|
+
this.on_subscribe(true);
|
|
2491
|
+
} else this.#state = state2;
|
|
2492
|
+
}
|
|
2493
|
+
/**Changes the transform function of the proxy, and updates subscribers with new value*/
|
|
2494
|
+
set_transform_read(transform) {
|
|
2495
|
+
if (this.in_use()) {
|
|
2496
|
+
this.on_unsubscribe();
|
|
2497
|
+
this.transform_read = transform;
|
|
2498
|
+
this.on_subscribe(true);
|
|
2499
|
+
} else this.transform_read = transform;
|
|
2500
|
+
}
|
|
2501
|
+
/**Changes the transform function of the proxy, and updates subscribers with new value*/
|
|
2502
|
+
set_transform_write(transform) {
|
|
2503
|
+
this.transform_write = transform;
|
|
2504
|
+
}
|
|
2505
|
+
get state() {
|
|
2506
|
+
return this;
|
|
2507
|
+
}
|
|
2508
|
+
get read_only() {
|
|
2509
|
+
return this;
|
|
2510
|
+
}
|
|
2511
|
+
get read_write() {
|
|
2512
|
+
return this;
|
|
2513
|
+
}
|
|
2514
|
+
//#Reader Context
|
|
2515
|
+
get rok() {
|
|
2516
|
+
return this.#state.rok;
|
|
2517
|
+
}
|
|
2518
|
+
get rsync() {
|
|
2519
|
+
return true;
|
|
2520
|
+
}
|
|
2521
|
+
async then(func) {
|
|
2522
|
+
if (this.#buffer) return func(this.#buffer);
|
|
2523
|
+
return func(this.transform_read(await this.#state));
|
|
2524
|
+
}
|
|
2525
|
+
get() {
|
|
2526
|
+
if (this.#buffer) return this.#buffer;
|
|
2527
|
+
return this.transform_read(this.#state.get());
|
|
2528
|
+
}
|
|
2529
|
+
related() {
|
|
2530
|
+
return none10();
|
|
2531
|
+
}
|
|
2532
|
+
//#Writer Context
|
|
2533
|
+
get writable() {
|
|
2534
|
+
return true;
|
|
2535
|
+
}
|
|
2536
|
+
get wsync() {
|
|
2537
|
+
return this.#state.wsync;
|
|
2538
|
+
}
|
|
2539
|
+
write(value) {
|
|
2540
|
+
return this.#state.write(this.transform_write(value));
|
|
2541
|
+
}
|
|
2542
|
+
limit() {
|
|
2543
|
+
return err9("Limit not supported on proxy states");
|
|
2544
|
+
}
|
|
2545
|
+
check() {
|
|
2546
|
+
return err9("Check not supported on proxy states");
|
|
2547
|
+
}
|
|
2548
|
+
};
|
|
2549
|
+
function res_wa_from(state2, transform_read, transform_write) {
|
|
2550
|
+
return new RESWA(
|
|
2551
|
+
state2,
|
|
2552
|
+
transform_read,
|
|
2553
|
+
transform_write
|
|
2554
|
+
);
|
|
2555
|
+
}
|
|
2556
|
+
var STATE_PROXY_RES = {
|
|
2557
|
+
res: res_from,
|
|
2558
|
+
res_ws: res_ws_from,
|
|
2559
|
+
res_wa: res_wa_from
|
|
2560
|
+
};
|
|
2561
|
+
|
|
2562
|
+
// src/proxy/roa.ts
|
|
2563
|
+
import {
|
|
2564
|
+
err as err10,
|
|
2565
|
+
none as none11
|
|
2566
|
+
} from "@chocbite/ts-lib-result";
|
|
2567
|
+
var ROA2 = class extends StateBase {
|
|
2568
|
+
constructor(state2, transform_read) {
|
|
2569
|
+
super();
|
|
2570
|
+
this.#state = state2;
|
|
2571
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2572
|
+
}
|
|
2573
|
+
#state;
|
|
2574
|
+
#subscriber = (value) => {
|
|
2575
|
+
this.#buffer = this.transform_read(value);
|
|
2576
|
+
this.update_subs(this.#buffer);
|
|
2577
|
+
};
|
|
2578
|
+
#buffer;
|
|
2579
|
+
transform_read(value) {
|
|
2580
|
+
return value;
|
|
2581
|
+
}
|
|
2582
|
+
transform_write;
|
|
2583
|
+
on_subscribe(run = false) {
|
|
2584
|
+
this.#state.sub(this.#subscriber, run);
|
|
2585
|
+
}
|
|
2586
|
+
on_unsubscribe() {
|
|
2587
|
+
this.#state.unsub(this.#subscriber);
|
|
2588
|
+
this.#buffer = void 0;
|
|
2589
|
+
}
|
|
2590
|
+
//#Owner Context
|
|
2591
|
+
set_state(state2) {
|
|
2592
|
+
if (this.in_use()) {
|
|
2593
|
+
this.on_unsubscribe();
|
|
2594
|
+
this.#state = state2;
|
|
2595
|
+
this.on_subscribe(true);
|
|
2596
|
+
} else this.#state = state2;
|
|
2597
|
+
}
|
|
2598
|
+
set_transform_read(transform) {
|
|
2599
|
+
if (this.in_use()) {
|
|
2600
|
+
this.on_unsubscribe();
|
|
2601
|
+
this.transform_read = transform;
|
|
2602
|
+
this.on_subscribe(true);
|
|
2603
|
+
} else this.transform_read = transform;
|
|
2604
|
+
}
|
|
2605
|
+
set_transform_write(transform) {
|
|
2606
|
+
this.transform_write = transform;
|
|
2607
|
+
}
|
|
2608
|
+
get state() {
|
|
2609
|
+
return this;
|
|
2610
|
+
}
|
|
2611
|
+
get read_only() {
|
|
2612
|
+
return this;
|
|
2613
|
+
}
|
|
2614
|
+
//#Reader Context
|
|
2615
|
+
get rok() {
|
|
2616
|
+
return true;
|
|
2617
|
+
}
|
|
2618
|
+
get rsync() {
|
|
2619
|
+
return this.#state.rsync;
|
|
2620
|
+
}
|
|
2621
|
+
async then(func) {
|
|
2622
|
+
if (this.#buffer) return func(this.#buffer);
|
|
2623
|
+
return func(this.transform_read(await this.#state));
|
|
2624
|
+
}
|
|
2625
|
+
related() {
|
|
2626
|
+
return none11();
|
|
2627
|
+
}
|
|
2628
|
+
//#Writer Context
|
|
2629
|
+
get writable() {
|
|
2630
|
+
return this.#state.writable;
|
|
2631
|
+
}
|
|
2632
|
+
get wsync() {
|
|
2633
|
+
return this.#state.wsync;
|
|
2634
|
+
}
|
|
2635
|
+
async write(value) {
|
|
2636
|
+
if (!this.#state.write) return err10("State not writable");
|
|
2637
|
+
if (!this.transform_write) return err10("State not writable");
|
|
2638
|
+
return this.#state.write(this.transform_write(value));
|
|
2639
|
+
}
|
|
2640
|
+
write_sync(value) {
|
|
2641
|
+
if (!this.#state.write_sync) return err10("State not writable");
|
|
2642
|
+
if (!this.transform_write) return err10("State not writable");
|
|
2643
|
+
return this.#state.write_sync(this.transform_write(value));
|
|
2644
|
+
}
|
|
2645
|
+
limit(_value) {
|
|
2646
|
+
return err10("Limit not supported on proxy states");
|
|
2647
|
+
}
|
|
2648
|
+
check(_value) {
|
|
2649
|
+
return err10("Check not supported on proxy states");
|
|
2650
|
+
}
|
|
2651
|
+
};
|
|
2652
|
+
function roa_from(state2, transform) {
|
|
2653
|
+
return new ROA2(state2, transform);
|
|
2654
|
+
}
|
|
2655
|
+
var ROAWS = class extends StateBase {
|
|
2656
|
+
constructor(state2, transform_read, transform_write) {
|
|
2657
|
+
super();
|
|
2658
|
+
this.#state = state2;
|
|
2659
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2660
|
+
if (transform_write) this.transform_write = transform_write;
|
|
2661
|
+
}
|
|
2662
|
+
#state;
|
|
2663
|
+
#subscriber = (value) => {
|
|
2664
|
+
this.#buffer = this.transform_read(value);
|
|
2665
|
+
this.update_subs(this.#buffer);
|
|
2666
|
+
};
|
|
2667
|
+
#buffer;
|
|
2668
|
+
transform_read(value) {
|
|
2669
|
+
return value;
|
|
2670
|
+
}
|
|
2671
|
+
transform_write(value) {
|
|
2672
|
+
return value;
|
|
2673
|
+
}
|
|
2674
|
+
on_subscribe(run = false) {
|
|
2675
|
+
this.#state.sub(this.#subscriber, run);
|
|
2676
|
+
}
|
|
2677
|
+
on_unsubscribe() {
|
|
2678
|
+
this.#state.unsub(this.#subscriber);
|
|
2679
|
+
this.#buffer = void 0;
|
|
2680
|
+
}
|
|
2681
|
+
//#Owner Context
|
|
2682
|
+
set_state(state2) {
|
|
2683
|
+
if (this.in_use()) {
|
|
2684
|
+
this.on_unsubscribe();
|
|
2685
|
+
this.#state = state2;
|
|
2686
|
+
this.on_subscribe(true);
|
|
2687
|
+
} else this.#state = state2;
|
|
2688
|
+
}
|
|
2689
|
+
set_transform_read(transform) {
|
|
2690
|
+
if (this.in_use()) {
|
|
2691
|
+
this.on_unsubscribe();
|
|
2692
|
+
this.transform_read = transform;
|
|
2693
|
+
this.on_subscribe(true);
|
|
2694
|
+
} else this.transform_read = transform;
|
|
2695
|
+
}
|
|
2696
|
+
set_transform_write(transform) {
|
|
2697
|
+
this.transform_write = transform;
|
|
2698
|
+
}
|
|
2699
|
+
get state() {
|
|
2700
|
+
return this;
|
|
2701
|
+
}
|
|
2702
|
+
get read_only() {
|
|
2703
|
+
return this;
|
|
2704
|
+
}
|
|
2705
|
+
get read_write() {
|
|
2706
|
+
return this;
|
|
2707
|
+
}
|
|
2708
|
+
//#Reader Context
|
|
2709
|
+
get rok() {
|
|
2710
|
+
return true;
|
|
2711
|
+
}
|
|
2712
|
+
get rsync() {
|
|
2713
|
+
return this.#state.rsync;
|
|
2714
|
+
}
|
|
2715
|
+
async then(func) {
|
|
2716
|
+
if (this.#buffer) return func(this.#buffer);
|
|
2717
|
+
return func(this.transform_read(await this.#state));
|
|
2718
|
+
}
|
|
2719
|
+
related() {
|
|
2720
|
+
return none11();
|
|
2721
|
+
}
|
|
2722
|
+
//#Writer Context
|
|
2723
|
+
get writable() {
|
|
2724
|
+
return true;
|
|
2725
|
+
}
|
|
2726
|
+
get wsync() {
|
|
2727
|
+
return true;
|
|
2728
|
+
}
|
|
2729
|
+
write(value) {
|
|
2730
|
+
return this.#state.write(this.transform_write(value));
|
|
2731
|
+
}
|
|
2732
|
+
write_sync(value) {
|
|
2733
|
+
return this.#state.write_sync(this.transform_write(value));
|
|
2734
|
+
}
|
|
2735
|
+
limit(_value) {
|
|
2736
|
+
return err10("Limit not supported on proxy states");
|
|
2737
|
+
}
|
|
2738
|
+
check(_value) {
|
|
2739
|
+
return err10("Check not supported on proxy states");
|
|
2740
|
+
}
|
|
2741
|
+
};
|
|
2742
|
+
function roa_ws_from(state2, transform_read, transform_write) {
|
|
2743
|
+
return new ROAWS(
|
|
2744
|
+
state2,
|
|
2745
|
+
transform_read,
|
|
2746
|
+
transform_write
|
|
2747
|
+
);
|
|
2748
|
+
}
|
|
2749
|
+
var ROAWA = class extends StateBase {
|
|
2750
|
+
constructor(state2, transform_read, transform_write) {
|
|
2751
|
+
super();
|
|
2752
|
+
this.#state = state2;
|
|
2753
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2754
|
+
if (transform_write) this.transform_write = transform_write;
|
|
2755
|
+
}
|
|
2756
|
+
#state;
|
|
2757
|
+
#subscriber = (value) => {
|
|
2758
|
+
this.#buffer = this.transform_read(value);
|
|
2759
|
+
this.update_subs(this.#buffer);
|
|
2760
|
+
};
|
|
2761
|
+
#buffer;
|
|
2762
|
+
transform_read(value) {
|
|
2763
|
+
return value;
|
|
2764
|
+
}
|
|
2765
|
+
transform_write(value) {
|
|
2766
|
+
return value;
|
|
2767
|
+
}
|
|
2768
|
+
on_subscribe(run = false) {
|
|
2769
|
+
this.#state.sub(this.#subscriber, run);
|
|
2770
|
+
}
|
|
2771
|
+
on_unsubscribe() {
|
|
2772
|
+
this.#state.unsub(this.#subscriber);
|
|
2773
|
+
this.#buffer = void 0;
|
|
2774
|
+
}
|
|
2775
|
+
//#Owner Context
|
|
2776
|
+
set_state(state2) {
|
|
2777
|
+
if (this.in_use()) {
|
|
2778
|
+
this.on_unsubscribe();
|
|
2779
|
+
this.#state = state2;
|
|
2780
|
+
this.on_subscribe(true);
|
|
2781
|
+
} else this.#state = state2;
|
|
2782
|
+
}
|
|
2783
|
+
set_transform_read(transform) {
|
|
2784
|
+
if (this.in_use()) {
|
|
2785
|
+
this.on_unsubscribe();
|
|
2786
|
+
this.transform_read = transform;
|
|
2787
|
+
this.on_subscribe(true);
|
|
2788
|
+
} else this.transform_read = transform;
|
|
2789
|
+
}
|
|
2790
|
+
set_transform_write(transform) {
|
|
2791
|
+
this.transform_write = transform;
|
|
2792
|
+
}
|
|
2793
|
+
get state() {
|
|
2794
|
+
return this;
|
|
2795
|
+
}
|
|
2796
|
+
get read_only() {
|
|
2797
|
+
return this;
|
|
2798
|
+
}
|
|
2799
|
+
get read_write() {
|
|
2800
|
+
return this;
|
|
2801
|
+
}
|
|
2802
|
+
//#Reader Context
|
|
2803
|
+
get rok() {
|
|
2804
|
+
return true;
|
|
2805
|
+
}
|
|
2806
|
+
get rsync() {
|
|
2807
|
+
return this.#state.rsync;
|
|
2808
|
+
}
|
|
2809
|
+
async then(func) {
|
|
2810
|
+
if (this.#buffer) return func(this.#buffer);
|
|
2811
|
+
return func(this.transform_read(await this.#state));
|
|
2812
|
+
}
|
|
2813
|
+
related() {
|
|
2814
|
+
return none11();
|
|
2815
|
+
}
|
|
2816
|
+
//#Writer Context
|
|
2817
|
+
get writable() {
|
|
2818
|
+
return true;
|
|
2819
|
+
}
|
|
2820
|
+
get wsync() {
|
|
2821
|
+
return this.#state.wsync;
|
|
2822
|
+
}
|
|
2823
|
+
write(value) {
|
|
2824
|
+
return this.#state.write(this.transform_write(value));
|
|
2825
|
+
}
|
|
2826
|
+
limit(_value) {
|
|
2827
|
+
return err10("Limit not supported on proxy states");
|
|
2828
|
+
}
|
|
2829
|
+
check(_value) {
|
|
2830
|
+
return err10("Check not supported on proxy states");
|
|
2831
|
+
}
|
|
2832
|
+
};
|
|
2833
|
+
function roa_wa_from(state2, transform_read, transform_write) {
|
|
2834
|
+
return new ROAWA(
|
|
2835
|
+
state2,
|
|
2836
|
+
transform_read,
|
|
2837
|
+
transform_write
|
|
2838
|
+
);
|
|
2839
|
+
}
|
|
2840
|
+
var STATE_PROXY_ROA = {
|
|
2841
|
+
roa: roa_from,
|
|
2842
|
+
roa_ws: roa_ws_from,
|
|
2843
|
+
roa_wa: roa_wa_from
|
|
2844
|
+
};
|
|
2845
|
+
|
|
2846
|
+
// src/proxy/ros.ts
|
|
2847
|
+
import {
|
|
2848
|
+
err as err11,
|
|
2849
|
+
none as none12
|
|
2850
|
+
} from "@chocbite/ts-lib-result";
|
|
2851
|
+
var ROS3 = class extends StateBase {
|
|
2852
|
+
constructor(state2, transform_read) {
|
|
2853
|
+
super();
|
|
2854
|
+
this.#state = state2;
|
|
2855
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2856
|
+
}
|
|
2857
|
+
#state;
|
|
2858
|
+
#subscriber = (value) => {
|
|
2859
|
+
this.#buffer = this.transform_read(value);
|
|
2860
|
+
this.update_subs(this.#buffer);
|
|
2861
|
+
};
|
|
2862
|
+
#buffer;
|
|
2863
|
+
transform_read(value) {
|
|
2864
|
+
return value;
|
|
2865
|
+
}
|
|
2866
|
+
transform_write;
|
|
2867
|
+
on_subscribe(run = false) {
|
|
2868
|
+
this.#state.sub(this.#subscriber, run);
|
|
2869
|
+
}
|
|
2870
|
+
on_unsubscribe() {
|
|
2871
|
+
this.#state.unsub(this.#subscriber);
|
|
2872
|
+
this.#buffer = void 0;
|
|
2873
|
+
}
|
|
2874
|
+
//#Owner Context
|
|
2875
|
+
set_state(state2) {
|
|
2876
|
+
if (this.in_use()) {
|
|
2877
|
+
this.on_unsubscribe();
|
|
2878
|
+
this.#state = state2;
|
|
2879
|
+
this.on_subscribe(true);
|
|
2880
|
+
} else this.#state = state2;
|
|
2881
|
+
}
|
|
2882
|
+
set_transform_read(transform) {
|
|
2883
|
+
if (this.in_use()) {
|
|
2884
|
+
this.on_unsubscribe();
|
|
2885
|
+
this.transform_read = transform;
|
|
2886
|
+
this.on_subscribe(true);
|
|
2887
|
+
} else this.transform_read = transform;
|
|
2888
|
+
}
|
|
2889
|
+
set_transform_write(transform) {
|
|
2890
|
+
this.transform_write = transform;
|
|
2891
|
+
}
|
|
2892
|
+
get state() {
|
|
2893
|
+
return this;
|
|
2894
|
+
}
|
|
2895
|
+
get read_only() {
|
|
2896
|
+
return this;
|
|
2897
|
+
}
|
|
2898
|
+
//#Reader Context
|
|
2899
|
+
get rok() {
|
|
2900
|
+
return true;
|
|
2901
|
+
}
|
|
2902
|
+
get rsync() {
|
|
2903
|
+
return true;
|
|
2904
|
+
}
|
|
2905
|
+
async then(func) {
|
|
2906
|
+
return func(this.get());
|
|
2907
|
+
}
|
|
2908
|
+
get() {
|
|
2909
|
+
if (this.#buffer) return this.#buffer;
|
|
2910
|
+
return this.transform_read(this.#state.get());
|
|
2911
|
+
}
|
|
2912
|
+
ok() {
|
|
2913
|
+
return this.get().value;
|
|
2914
|
+
}
|
|
2915
|
+
related() {
|
|
2916
|
+
return none12();
|
|
2917
|
+
}
|
|
2918
|
+
//#Writer Context
|
|
2919
|
+
get writable() {
|
|
2920
|
+
return this.#state.writable;
|
|
2921
|
+
}
|
|
2922
|
+
get wsync() {
|
|
2923
|
+
return this.#state.wsync;
|
|
2924
|
+
}
|
|
2925
|
+
async write(value) {
|
|
2926
|
+
if (!this.#state.write) return err11("State not writable");
|
|
2927
|
+
if (!this.transform_write) return err11("State not writable");
|
|
2928
|
+
return this.#state.write(this.transform_write(value));
|
|
2929
|
+
}
|
|
2930
|
+
write_sync(value) {
|
|
2931
|
+
if (!this.#state.write_sync) return err11("State not writable");
|
|
2932
|
+
if (!this.transform_write) return err11("State not writable");
|
|
2933
|
+
return this.#state.write_sync(this.transform_write(value));
|
|
2934
|
+
}
|
|
2935
|
+
limit(_value) {
|
|
2936
|
+
return err11("Limit not supported on proxy states");
|
|
2937
|
+
}
|
|
2938
|
+
check(_value) {
|
|
2939
|
+
return err11("Check not supported on proxy states");
|
|
2940
|
+
}
|
|
2941
|
+
};
|
|
2942
|
+
function ros_from(state2, transform) {
|
|
2943
|
+
return new ROS3(state2, transform);
|
|
2944
|
+
}
|
|
2945
|
+
var ROSWS = class extends StateBase {
|
|
2946
|
+
constructor(state2, transform_read, transform_write) {
|
|
2947
|
+
super();
|
|
2948
|
+
this.#state = state2;
|
|
2949
|
+
if (transform_read) this.transform_read = transform_read;
|
|
2950
|
+
if (transform_write) this.transform_write = transform_write;
|
|
2951
|
+
}
|
|
2952
|
+
#state;
|
|
2953
|
+
#subscriber = (value) => {
|
|
2954
|
+
this.#buffer = this.transform_read(value);
|
|
2955
|
+
this.update_subs(this.#buffer);
|
|
2956
|
+
};
|
|
2957
|
+
#buffer;
|
|
2958
|
+
transform_read(value) {
|
|
2959
|
+
return value;
|
|
2960
|
+
}
|
|
2961
|
+
transform_write(value) {
|
|
2962
|
+
return value;
|
|
2963
|
+
}
|
|
2964
|
+
on_subscribe(run = false) {
|
|
2965
|
+
this.#state.sub(this.#subscriber, run);
|
|
2966
|
+
}
|
|
2967
|
+
on_unsubscribe() {
|
|
2968
|
+
this.#state.unsub(this.#subscriber);
|
|
2969
|
+
this.#buffer = void 0;
|
|
2970
|
+
}
|
|
2971
|
+
//#Owner Context
|
|
2972
|
+
set_state(state2) {
|
|
2973
|
+
if (this.in_use()) {
|
|
2974
|
+
this.on_unsubscribe();
|
|
2975
|
+
this.#state = state2;
|
|
2976
|
+
this.on_subscribe(true);
|
|
2977
|
+
} else this.#state = state2;
|
|
2978
|
+
}
|
|
2979
|
+
set_transform_read(transform) {
|
|
2980
|
+
if (this.in_use()) {
|
|
2981
|
+
this.on_unsubscribe();
|
|
2982
|
+
this.transform_read = transform;
|
|
2983
|
+
this.on_subscribe(true);
|
|
2984
|
+
} else this.transform_read = transform;
|
|
2985
|
+
}
|
|
2986
|
+
set_transform_write(transform) {
|
|
2987
|
+
this.transform_write = transform;
|
|
2988
|
+
}
|
|
2989
|
+
get state() {
|
|
2990
|
+
return this;
|
|
2991
|
+
}
|
|
2992
|
+
get read_only() {
|
|
2993
|
+
return this;
|
|
2994
|
+
}
|
|
2995
|
+
get read_write() {
|
|
2996
|
+
return this;
|
|
2997
|
+
}
|
|
2998
|
+
//#Reader Context
|
|
2999
|
+
get rok() {
|
|
3000
|
+
return true;
|
|
3001
|
+
}
|
|
3002
|
+
get rsync() {
|
|
3003
|
+
return true;
|
|
3004
|
+
}
|
|
3005
|
+
async then(func) {
|
|
3006
|
+
if (this.#buffer) return func(this.#buffer);
|
|
3007
|
+
return func(this.transform_read(await this.#state));
|
|
3008
|
+
}
|
|
3009
|
+
get() {
|
|
3010
|
+
if (this.#buffer) return this.#buffer;
|
|
3011
|
+
return this.transform_read(this.#state.get());
|
|
3012
|
+
}
|
|
3013
|
+
ok() {
|
|
3014
|
+
return this.get().value;
|
|
3015
|
+
}
|
|
3016
|
+
related() {
|
|
3017
|
+
return none12();
|
|
3018
|
+
}
|
|
3019
|
+
//#Writer Context
|
|
3020
|
+
get writable() {
|
|
3021
|
+
return true;
|
|
3022
|
+
}
|
|
3023
|
+
get wsync() {
|
|
3024
|
+
return true;
|
|
3025
|
+
}
|
|
3026
|
+
write(value) {
|
|
3027
|
+
return this.#state.write(this.transform_write(value));
|
|
3028
|
+
}
|
|
3029
|
+
write_sync(value) {
|
|
3030
|
+
return this.#state.write_sync(this.transform_write(value));
|
|
3031
|
+
}
|
|
3032
|
+
limit(_value) {
|
|
3033
|
+
return err11("Limit not supported on proxy states");
|
|
3034
|
+
}
|
|
3035
|
+
check(_value) {
|
|
3036
|
+
return err11("Check not supported on proxy states");
|
|
3037
|
+
}
|
|
3038
|
+
};
|
|
3039
|
+
function ros_ws_from(state2, transform_read, transform_write) {
|
|
3040
|
+
return new ROSWS(
|
|
3041
|
+
state2,
|
|
3042
|
+
transform_read,
|
|
3043
|
+
transform_write
|
|
3044
|
+
);
|
|
3045
|
+
}
|
|
3046
|
+
var ROSWA = class extends StateBase {
|
|
3047
|
+
constructor(state2, transform_read, transform_write) {
|
|
3048
|
+
super();
|
|
3049
|
+
this.#state = state2;
|
|
3050
|
+
if (transform_read) this.transform_read = transform_read;
|
|
3051
|
+
if (transform_write) this.transform_write = transform_write;
|
|
3052
|
+
}
|
|
3053
|
+
#state;
|
|
3054
|
+
#subscriber = (value) => {
|
|
3055
|
+
this.#buffer = this.transform_read(value);
|
|
3056
|
+
this.update_subs(this.#buffer);
|
|
3057
|
+
};
|
|
3058
|
+
#buffer;
|
|
3059
|
+
transform_read(value) {
|
|
3060
|
+
return value;
|
|
3061
|
+
}
|
|
3062
|
+
transform_write(value) {
|
|
3063
|
+
return value;
|
|
3064
|
+
}
|
|
3065
|
+
on_subscribe(run = false) {
|
|
3066
|
+
this.#state.sub(this.#subscriber, run);
|
|
3067
|
+
}
|
|
3068
|
+
on_unsubscribe() {
|
|
3069
|
+
this.#state.unsub(this.#subscriber);
|
|
3070
|
+
this.#buffer = void 0;
|
|
3071
|
+
}
|
|
3072
|
+
//#Owner Context
|
|
3073
|
+
set_state(state2) {
|
|
3074
|
+
if (this.in_use()) {
|
|
3075
|
+
this.on_unsubscribe();
|
|
3076
|
+
this.#state = state2;
|
|
3077
|
+
this.on_subscribe(true);
|
|
3078
|
+
} else this.#state = state2;
|
|
3079
|
+
}
|
|
3080
|
+
set_transform_read(transform) {
|
|
3081
|
+
if (this.in_use()) {
|
|
3082
|
+
this.on_unsubscribe();
|
|
3083
|
+
this.transform_read = transform;
|
|
3084
|
+
this.on_subscribe(true);
|
|
3085
|
+
} else this.transform_read = transform;
|
|
3086
|
+
}
|
|
3087
|
+
set_transform_write(transform) {
|
|
3088
|
+
this.transform_write = transform;
|
|
3089
|
+
}
|
|
3090
|
+
get state() {
|
|
3091
|
+
return this;
|
|
3092
|
+
}
|
|
3093
|
+
get read_only() {
|
|
3094
|
+
return this;
|
|
3095
|
+
}
|
|
3096
|
+
get read_write() {
|
|
3097
|
+
return this;
|
|
3098
|
+
}
|
|
3099
|
+
//#Reader Context
|
|
3100
|
+
get rok() {
|
|
3101
|
+
return true;
|
|
3102
|
+
}
|
|
3103
|
+
get rsync() {
|
|
3104
|
+
return true;
|
|
3105
|
+
}
|
|
3106
|
+
async then(func) {
|
|
3107
|
+
if (this.#buffer) return func(this.#buffer);
|
|
3108
|
+
return func(this.transform_read(await this.#state));
|
|
3109
|
+
}
|
|
3110
|
+
get() {
|
|
3111
|
+
if (this.#buffer) return this.#buffer;
|
|
3112
|
+
return this.transform_read(this.#state.get());
|
|
3113
|
+
}
|
|
3114
|
+
ok() {
|
|
3115
|
+
return this.get().value;
|
|
3116
|
+
}
|
|
3117
|
+
related() {
|
|
3118
|
+
return none12();
|
|
3119
|
+
}
|
|
3120
|
+
//#Writer Context
|
|
3121
|
+
get writable() {
|
|
3122
|
+
return true;
|
|
3123
|
+
}
|
|
3124
|
+
get wsync() {
|
|
3125
|
+
return this.#state.wsync;
|
|
3126
|
+
}
|
|
3127
|
+
write(value) {
|
|
3128
|
+
return this.#state.write(this.transform_write(value));
|
|
3129
|
+
}
|
|
3130
|
+
limit(_value) {
|
|
3131
|
+
return err11("Limit not supported on proxy states");
|
|
3132
|
+
}
|
|
3133
|
+
check(_value) {
|
|
3134
|
+
return err11("Check not supported on proxy states");
|
|
3135
|
+
}
|
|
3136
|
+
};
|
|
3137
|
+
function ros_wa_from(state2, transform_read, transform_write) {
|
|
3138
|
+
return new ROSWA(
|
|
3139
|
+
state2,
|
|
3140
|
+
transform_read,
|
|
3141
|
+
transform_write
|
|
3142
|
+
);
|
|
3143
|
+
}
|
|
3144
|
+
var STATE_PROXY_ROS = {
|
|
3145
|
+
ros: ros_from,
|
|
3146
|
+
ros_ws: ros_ws_from,
|
|
3147
|
+
ros_wa: ros_wa_from
|
|
3148
|
+
};
|
|
3149
|
+
|
|
3150
|
+
// src/resource/rea.ts
|
|
3151
|
+
import {
|
|
3152
|
+
err as err12,
|
|
3153
|
+
none as none13,
|
|
3154
|
+
ok as ok7
|
|
3155
|
+
} from "@chocbite/ts-lib-result";
|
|
3156
|
+
var StateResourceREA = class extends StateBase {
|
|
3157
|
+
#valid = 0;
|
|
3158
|
+
#fetching = false;
|
|
3159
|
+
#buffer;
|
|
3160
|
+
#retention_timout = 0;
|
|
3161
|
+
#debounce_timout = 0;
|
|
3162
|
+
#timeout_timout = 0;
|
|
3163
|
+
on_subscribe() {
|
|
3164
|
+
if (this.#retention_timout) {
|
|
3165
|
+
clearTimeout(this.#retention_timout);
|
|
3166
|
+
this.#retention_timout = 0;
|
|
3167
|
+
} else {
|
|
3168
|
+
if (this.debounce > 0)
|
|
3169
|
+
this.#debounce_timout = setTimeout(() => {
|
|
3170
|
+
this.setup_connection(this);
|
|
3171
|
+
this.#debounce_timout = 0;
|
|
3172
|
+
}, this.debounce);
|
|
3173
|
+
else this.setup_connection(this);
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3176
|
+
on_unsubscribe() {
|
|
3177
|
+
if (this.#debounce_timout) {
|
|
3178
|
+
clearTimeout(this.#debounce_timout);
|
|
3179
|
+
this.#debounce_timout = 0;
|
|
3180
|
+
} else {
|
|
3181
|
+
if (this.retention > 0) {
|
|
3182
|
+
this.#retention_timout = setTimeout(() => {
|
|
3183
|
+
this.teardown_connection(this);
|
|
3184
|
+
this.#retention_timout = 0;
|
|
3185
|
+
}, this.retention);
|
|
3186
|
+
} else {
|
|
3187
|
+
this.teardown_connection(this);
|
|
3188
|
+
}
|
|
3189
|
+
}
|
|
3190
|
+
if (this.validity === true) this.#valid = 0;
|
|
3191
|
+
}
|
|
3192
|
+
update_single(value, update = false) {
|
|
3193
|
+
this.#fetching = false;
|
|
3194
|
+
clearTimeout(this.#timeout_timout);
|
|
3195
|
+
this.ful_r_prom(value);
|
|
3196
|
+
if (update) {
|
|
3197
|
+
if (!this.#buffer?.compare(value)) this.update_subs(value);
|
|
3198
|
+
this.#buffer = value;
|
|
3199
|
+
this.#valid = this.validity === true ? true : performance.now() + this.validity;
|
|
3200
|
+
}
|
|
3201
|
+
}
|
|
3202
|
+
update_resource(value) {
|
|
3203
|
+
if (!this.#buffer?.compare(value)) this.update_subs(value);
|
|
3204
|
+
this.#buffer = value;
|
|
3205
|
+
this.#valid = this.validity === true ? true : performance.now() + this.validity;
|
|
3206
|
+
}
|
|
3207
|
+
get buffer() {
|
|
3208
|
+
return this.#buffer;
|
|
3209
|
+
}
|
|
3210
|
+
get state() {
|
|
3211
|
+
return this;
|
|
3212
|
+
}
|
|
3213
|
+
get read_only() {
|
|
3214
|
+
return this;
|
|
3215
|
+
}
|
|
3216
|
+
//#Reader Context
|
|
3217
|
+
get rok() {
|
|
3218
|
+
return false;
|
|
3219
|
+
}
|
|
3220
|
+
get rsync() {
|
|
3221
|
+
return false;
|
|
3222
|
+
}
|
|
3223
|
+
async then(func) {
|
|
3224
|
+
if (this.#valid === true || this.#valid >= performance.now())
|
|
3225
|
+
return func(this.#buffer);
|
|
3226
|
+
else {
|
|
3227
|
+
const prom = this.append_r_prom(func);
|
|
3228
|
+
if (!this.#fetching) {
|
|
3229
|
+
this.#fetching = true;
|
|
3230
|
+
this.#timeout_timout = setTimeout(
|
|
3231
|
+
() => this.#fetching = false,
|
|
3232
|
+
this.timeout
|
|
3233
|
+
);
|
|
3234
|
+
if (this.debounce > 0)
|
|
3235
|
+
setTimeout(() => this.single_get(this), this.debounce);
|
|
3236
|
+
else this.single_get(this);
|
|
3237
|
+
}
|
|
3238
|
+
return prom;
|
|
3239
|
+
}
|
|
3240
|
+
}
|
|
3241
|
+
//#Writer Context
|
|
3242
|
+
get writable() {
|
|
3243
|
+
return false;
|
|
3244
|
+
}
|
|
3245
|
+
get wsync() {
|
|
3246
|
+
return false;
|
|
3247
|
+
}
|
|
3248
|
+
};
|
|
3249
|
+
var FuncREA = class extends StateResourceREA {
|
|
3250
|
+
constructor(once, setup, teardown, timeout, debounce, validity, retention, helper) {
|
|
3251
|
+
super();
|
|
3252
|
+
this.single_get = once;
|
|
3253
|
+
this.setup_connection = setup;
|
|
3254
|
+
this.teardown_connection = teardown;
|
|
3255
|
+
this.timeout = timeout;
|
|
3256
|
+
this.debounce = debounce;
|
|
3257
|
+
this.validity = validity;
|
|
3258
|
+
this.retention = retention;
|
|
3259
|
+
if (helper) this.#helper = helper;
|
|
3260
|
+
}
|
|
3261
|
+
timeout;
|
|
3262
|
+
debounce;
|
|
3263
|
+
validity;
|
|
3264
|
+
retention;
|
|
3265
|
+
#helper;
|
|
3266
|
+
/**Called if the state is awaited, returns the value once*/
|
|
3267
|
+
single_get(_state) {
|
|
3268
|
+
}
|
|
3269
|
+
/**Called when state is subscribed to to setup connection to remote resource*/
|
|
3270
|
+
setup_connection(_state) {
|
|
3271
|
+
}
|
|
3272
|
+
/**Called when state is no longer subscribed to to cleanup connection to remote resource*/
|
|
3273
|
+
teardown_connection(_state) {
|
|
3274
|
+
}
|
|
3275
|
+
related() {
|
|
3276
|
+
return this.#helper?.related ? this.#helper.related() : none13();
|
|
3277
|
+
}
|
|
3278
|
+
};
|
|
3279
|
+
var rea2 = {
|
|
3280
|
+
/**Alternative state resource which can be initialized with functions
|
|
3281
|
+
* @template READ - The type of the state’s value when read.
|
|
3282
|
+
* @template REL - The type of related states, defaults to an empty object.
|
|
3283
|
+
* @param once function called when state value is requested once
|
|
3284
|
+
* @param setup function called when state has been subscribed to
|
|
3285
|
+
* @param teardown function called when state has been unsubscribed from completely
|
|
3286
|
+
* @param debounce delay added to once value retrival, which will collect multiple once requests into a single one
|
|
3287
|
+
* @param validity how long the last retrived value is considered valid
|
|
3288
|
+
* @param retention delay after last subscriber unsubscribes before teardown is called, to allow quick resubscribe without teardown
|
|
3289
|
+
* */
|
|
3290
|
+
from(once, setup, teardown, times, helper) {
|
|
3291
|
+
return new FuncREA(
|
|
3292
|
+
once,
|
|
3293
|
+
setup,
|
|
3294
|
+
teardown,
|
|
3295
|
+
times?.timeout ?? 1e3,
|
|
3296
|
+
times?.debounce ?? 0,
|
|
3297
|
+
times?.validity ?? 0,
|
|
3298
|
+
times?.retention ?? 0,
|
|
3299
|
+
helper
|
|
3300
|
+
);
|
|
3301
|
+
},
|
|
3302
|
+
class: StateResourceREA
|
|
3303
|
+
};
|
|
3304
|
+
var StateResourceREAWA = class extends StateBase {
|
|
3305
|
+
#valid = 0;
|
|
3306
|
+
#fetching = false;
|
|
3307
|
+
#buffer;
|
|
3308
|
+
#retention_timout = 0;
|
|
3309
|
+
#debounce_timout = 0;
|
|
3310
|
+
#timeout_timout = 0;
|
|
3311
|
+
#write_buffer;
|
|
3312
|
+
#write_debounce_timout = 0;
|
|
3313
|
+
#write_promises = [];
|
|
3314
|
+
on_subscribe() {
|
|
3315
|
+
if (this.#retention_timout) {
|
|
3316
|
+
clearTimeout(this.#retention_timout);
|
|
3317
|
+
this.#retention_timout = 0;
|
|
3318
|
+
} else {
|
|
3319
|
+
if (this.debounce > 0)
|
|
3320
|
+
this.#debounce_timout = setTimeout(() => {
|
|
3321
|
+
this.setup_connection(this);
|
|
3322
|
+
this.#debounce_timout = 0;
|
|
3323
|
+
}, this.debounce);
|
|
3324
|
+
else this.setup_connection(this);
|
|
3325
|
+
}
|
|
3326
|
+
}
|
|
3327
|
+
on_unsubscribe() {
|
|
3328
|
+
if (this.#debounce_timout) {
|
|
3329
|
+
clearTimeout(this.#debounce_timout);
|
|
3330
|
+
this.#debounce_timout = 0;
|
|
3331
|
+
} else {
|
|
3332
|
+
if (this.retention > 0) {
|
|
3333
|
+
this.#retention_timout = setTimeout(() => {
|
|
3334
|
+
this.teardown_connection(this);
|
|
3335
|
+
this.#retention_timout = 0;
|
|
3336
|
+
}, this.retention);
|
|
3337
|
+
} else {
|
|
3338
|
+
this.teardown_connection(this);
|
|
3339
|
+
}
|
|
3340
|
+
}
|
|
3341
|
+
if (this.validity === true) this.#valid = 0;
|
|
3342
|
+
}
|
|
3343
|
+
update_single(value, update = false) {
|
|
3344
|
+
this.#fetching = false;
|
|
3345
|
+
clearTimeout(this.#timeout_timout);
|
|
3346
|
+
this.ful_r_prom(value);
|
|
3347
|
+
if (update) {
|
|
3348
|
+
if (!this.#buffer?.compare(value)) this.update_subs(value);
|
|
3349
|
+
this.#buffer = value;
|
|
3350
|
+
this.#valid = this.validity === true ? true : performance.now() + this.validity;
|
|
3351
|
+
}
|
|
3352
|
+
}
|
|
3353
|
+
update_resource(value) {
|
|
3354
|
+
if (!this.#buffer?.compare(value)) this.update_subs(value);
|
|
3355
|
+
this.#buffer = value;
|
|
3356
|
+
this.#valid = this.validity === true ? true : performance.now() + this.validity;
|
|
3357
|
+
}
|
|
3358
|
+
get buffer() {
|
|
3359
|
+
return this.#buffer;
|
|
3360
|
+
}
|
|
3361
|
+
get state() {
|
|
3362
|
+
return this;
|
|
3363
|
+
}
|
|
3364
|
+
get read_only() {
|
|
3365
|
+
return this;
|
|
3366
|
+
}
|
|
3367
|
+
get read_write() {
|
|
3368
|
+
return this;
|
|
3369
|
+
}
|
|
3370
|
+
//Reader Context
|
|
3371
|
+
get rok() {
|
|
3372
|
+
return false;
|
|
3373
|
+
}
|
|
3374
|
+
get rsync() {
|
|
3375
|
+
return false;
|
|
3376
|
+
}
|
|
3377
|
+
async then(func) {
|
|
3378
|
+
if (this.#valid === true || this.#valid >= performance.now())
|
|
3379
|
+
return func(this.#buffer);
|
|
3380
|
+
else {
|
|
3381
|
+
const prom = this.append_r_prom(func);
|
|
3382
|
+
if (!this.#fetching) {
|
|
3383
|
+
this.#fetching = true;
|
|
3384
|
+
this.#timeout_timout = setTimeout(
|
|
3385
|
+
() => this.#fetching = false,
|
|
3386
|
+
this.timeout
|
|
3387
|
+
);
|
|
3388
|
+
if (this.debounce > 0)
|
|
3389
|
+
setTimeout(() => this.single_get(this), this.debounce);
|
|
3390
|
+
else this.single_get(this);
|
|
3391
|
+
}
|
|
3392
|
+
return prom;
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
//Writer Context
|
|
3396
|
+
get writable() {
|
|
3397
|
+
return true;
|
|
3398
|
+
}
|
|
3399
|
+
get wsync() {
|
|
3400
|
+
return false;
|
|
3401
|
+
}
|
|
3402
|
+
async write(value) {
|
|
3403
|
+
this.#write_buffer = value;
|
|
3404
|
+
if (this.write_debounce === 0) return this.write_action(value, this);
|
|
3405
|
+
else if (this.#write_debounce_timout === 0)
|
|
3406
|
+
this.#write_debounce_timout = window.setTimeout(async () => {
|
|
3407
|
+
this.#write_debounce_timout = 0;
|
|
3408
|
+
const write_buffer = this.#write_buffer;
|
|
3409
|
+
this.#write_buffer = void 0;
|
|
3410
|
+
const promises = this.#write_promises;
|
|
3411
|
+
this.#write_promises = [];
|
|
3412
|
+
const res3 = await this.write_action(write_buffer, this);
|
|
3413
|
+
for (let i = 0; i < promises.length; i++) promises[i](res3);
|
|
3414
|
+
}, this.write_debounce);
|
|
3415
|
+
return new Promise((a) => {
|
|
3416
|
+
this.#write_promises.push(a);
|
|
3417
|
+
});
|
|
3418
|
+
}
|
|
3419
|
+
};
|
|
3420
|
+
var FuncREAWA = class extends StateResourceREAWA {
|
|
3421
|
+
constructor(once, setup, teardown, timeout, debounce, validity, retention, write_debounce, write_action, helper) {
|
|
3422
|
+
super();
|
|
3423
|
+
this.single_get = once;
|
|
3424
|
+
this.setup_connection = setup;
|
|
3425
|
+
this.teardown_connection = teardown;
|
|
3426
|
+
if (write_action) this.write_action = write_action;
|
|
3427
|
+
this.timeout = timeout;
|
|
3428
|
+
this.debounce = debounce;
|
|
3429
|
+
this.validity = validity;
|
|
3430
|
+
this.retention = retention;
|
|
3431
|
+
this.write_debounce = write_debounce || 0;
|
|
3432
|
+
if (helper) this.#helper = helper;
|
|
3433
|
+
}
|
|
3434
|
+
timeout;
|
|
3435
|
+
debounce;
|
|
3436
|
+
validity;
|
|
3437
|
+
retention;
|
|
3438
|
+
write_debounce;
|
|
3439
|
+
#helper;
|
|
3440
|
+
/**Called if the state is awaited, returns the value once*/
|
|
3441
|
+
single_get(_state) {
|
|
3442
|
+
}
|
|
3443
|
+
/**Called when state is subscribed to to setup connection to remote resource*/
|
|
3444
|
+
setup_connection(_state) {
|
|
3445
|
+
}
|
|
3446
|
+
/**Called when state is no longer subscribed to to cleanup connection to remote resource*/
|
|
3447
|
+
teardown_connection(_state) {
|
|
3448
|
+
}
|
|
3449
|
+
/**Called after write debounce finished with the last written value*/
|
|
3450
|
+
async write_action(_value, _state) {
|
|
3451
|
+
return err12("State not writable");
|
|
3452
|
+
}
|
|
3453
|
+
limit(value) {
|
|
3454
|
+
return this.#helper?.limit ? this.#helper.limit(value) : ok7(value);
|
|
3455
|
+
}
|
|
3456
|
+
check(value) {
|
|
3457
|
+
return this.#helper?.check ? this.#helper.check(value) : ok7(value);
|
|
3458
|
+
}
|
|
3459
|
+
related() {
|
|
3460
|
+
return this.#helper?.related ? this.#helper.related() : none13();
|
|
3461
|
+
}
|
|
3462
|
+
};
|
|
3463
|
+
var rea_wa2 = {
|
|
3464
|
+
/**Alternative state resource which can be initialized with functions
|
|
3465
|
+
* @template READ - The type of the state’s value when read.
|
|
3466
|
+
* @template WT - The type which can be written to the state.
|
|
3467
|
+
* @template REL - The type of related states, defaults to an empty object.
|
|
3468
|
+
* @param once function called when state value is requested once, returns a Err(string) on failure
|
|
3469
|
+
* @param setup function called when state has been subscribed to
|
|
3470
|
+
* @param teardown function called when state has been unsubscribed from completely
|
|
3471
|
+
* @param write_action function called after write debounce finished with the last written value
|
|
3472
|
+
* @param debounce delay added to once value retrival, which will collect multiple once requests into a single one
|
|
3473
|
+
* @param validity how long the last retrived value is considered valid
|
|
3474
|
+
* @param retention delay after last subscriber unsubscribes before teardown is called, to allow quick resubscribe without teardown
|
|
3475
|
+
* @param write_debounce debounce delay for write calls, only the last write within the delay is used
|
|
3476
|
+
* */
|
|
3477
|
+
from(once, setup, teardown, write_action, times, helper) {
|
|
3478
|
+
return new FuncREAWA(
|
|
3479
|
+
once,
|
|
3480
|
+
setup,
|
|
3481
|
+
teardown,
|
|
3482
|
+
times?.timeout ?? 1e3,
|
|
3483
|
+
times?.debounce ?? 0,
|
|
3484
|
+
times?.validity ?? 0,
|
|
3485
|
+
times?.retention ?? 0,
|
|
3486
|
+
times?.write_debounce ?? 0,
|
|
3487
|
+
write_action,
|
|
3488
|
+
helper
|
|
3489
|
+
);
|
|
3490
|
+
},
|
|
3491
|
+
class: StateResourceREAWA
|
|
3492
|
+
};
|
|
3493
|
+
var STATE_RESOURCE_REA = {
|
|
3494
|
+
/**Remote resource */
|
|
3495
|
+
rea: rea2,
|
|
3496
|
+
/**Remote resource with write cabability */
|
|
3497
|
+
rea_wa: rea_wa2
|
|
3498
|
+
};
|
|
3499
|
+
|
|
3500
|
+
// src/resource/roa.ts
|
|
3501
|
+
import { none as none14 } from "@chocbite/ts-lib-result";
|
|
3502
|
+
var StateResourceROA = class extends StateBase {
|
|
3503
|
+
#valid = 0;
|
|
3504
|
+
#fetching = false;
|
|
3505
|
+
#buffer;
|
|
3506
|
+
#retention_timout = 0;
|
|
3507
|
+
#debounce_timout = 0;
|
|
3508
|
+
#timeout_timout = 0;
|
|
3509
|
+
on_subscribe() {
|
|
3510
|
+
if (this.#retention_timout) {
|
|
3511
|
+
clearTimeout(this.#retention_timout);
|
|
3512
|
+
this.#retention_timout = 0;
|
|
3513
|
+
} else {
|
|
3514
|
+
if (this.debounce > 0)
|
|
3515
|
+
this.#debounce_timout = setTimeout(() => {
|
|
3516
|
+
this.setup_connection(this);
|
|
3517
|
+
this.#debounce_timout = 0;
|
|
3518
|
+
}, this.debounce);
|
|
3519
|
+
else this.setup_connection(this);
|
|
3520
|
+
}
|
|
3521
|
+
}
|
|
3522
|
+
on_unsubscribe() {
|
|
3523
|
+
if (this.#debounce_timout) {
|
|
3524
|
+
clearTimeout(this.#debounce_timout);
|
|
3525
|
+
this.#debounce_timout = 0;
|
|
3526
|
+
} else {
|
|
3527
|
+
if (this.retention > 0) {
|
|
3528
|
+
this.#retention_timout = setTimeout(() => {
|
|
3529
|
+
this.teardown_connection(this);
|
|
3530
|
+
this.#retention_timout = 0;
|
|
3531
|
+
}, this.retention);
|
|
3532
|
+
} else {
|
|
3533
|
+
this.teardown_connection(this);
|
|
3534
|
+
}
|
|
3535
|
+
}
|
|
3536
|
+
if (this.validity === true) this.#valid = 0;
|
|
3537
|
+
}
|
|
3538
|
+
update_single(value, update = false) {
|
|
3539
|
+
this.#fetching = false;
|
|
3540
|
+
clearTimeout(this.#timeout_timout);
|
|
3541
|
+
this.ful_r_prom(value);
|
|
3542
|
+
if (update) {
|
|
3543
|
+
if (!this.#buffer?.compare(value)) this.update_subs(value);
|
|
3544
|
+
this.#buffer = value;
|
|
3545
|
+
this.#valid = this.validity === true ? true : performance.now() + this.validity;
|
|
3546
|
+
}
|
|
3547
|
+
}
|
|
3548
|
+
update_resource(value) {
|
|
3549
|
+
if (!this.#buffer?.compare(value)) this.update_subs(value);
|
|
3550
|
+
this.#buffer = value;
|
|
3551
|
+
this.#valid = this.validity === true ? true : performance.now() + this.validity;
|
|
3552
|
+
}
|
|
3553
|
+
get buffer() {
|
|
3554
|
+
return this.#buffer;
|
|
3555
|
+
}
|
|
3556
|
+
get state() {
|
|
3557
|
+
return this;
|
|
3558
|
+
}
|
|
3559
|
+
get read_only() {
|
|
3560
|
+
return this;
|
|
3561
|
+
}
|
|
3562
|
+
//#Reader Context
|
|
3563
|
+
get rok() {
|
|
3564
|
+
return true;
|
|
3565
|
+
}
|
|
3566
|
+
get rsync() {
|
|
3567
|
+
return false;
|
|
3568
|
+
}
|
|
3569
|
+
async then(func) {
|
|
3570
|
+
if (this.#valid === true || this.#valid >= performance.now())
|
|
3571
|
+
return func(this.#buffer);
|
|
3572
|
+
else {
|
|
3573
|
+
const prom = this.append_r_prom(func);
|
|
3574
|
+
if (!this.#fetching) {
|
|
3575
|
+
this.#fetching = true;
|
|
3576
|
+
this.#timeout_timout = setTimeout(
|
|
3577
|
+
() => this.#fetching = false,
|
|
3578
|
+
this.timeout
|
|
3579
|
+
);
|
|
3580
|
+
if (this.debounce > 0)
|
|
3581
|
+
setTimeout(() => this.single_get(this), this.debounce);
|
|
3582
|
+
else this.single_get(this);
|
|
3583
|
+
}
|
|
3584
|
+
return prom;
|
|
3585
|
+
}
|
|
3586
|
+
}
|
|
3587
|
+
//#Writer Context
|
|
3588
|
+
get writable() {
|
|
3589
|
+
return false;
|
|
3590
|
+
}
|
|
3591
|
+
get wsync() {
|
|
3592
|
+
return false;
|
|
3593
|
+
}
|
|
3594
|
+
};
|
|
3595
|
+
var FuncROA = class extends StateResourceROA {
|
|
3596
|
+
constructor(once, setup, teardown, timeout, debounce, validity, retention, helper) {
|
|
3597
|
+
super();
|
|
3598
|
+
this.single_get = once;
|
|
3599
|
+
this.setup_connection = setup;
|
|
3600
|
+
this.teardown_connection = teardown;
|
|
3601
|
+
this.timeout = timeout;
|
|
3602
|
+
this.debounce = debounce;
|
|
3603
|
+
this.validity = validity;
|
|
3604
|
+
this.retention = retention;
|
|
3605
|
+
if (helper) this.#helper = helper;
|
|
3606
|
+
}
|
|
3607
|
+
timeout;
|
|
3608
|
+
debounce;
|
|
3609
|
+
validity;
|
|
3610
|
+
retention;
|
|
3611
|
+
#helper;
|
|
3612
|
+
/**Called if the state is awaited, returns the value once*/
|
|
3613
|
+
single_get(_state) {
|
|
3614
|
+
}
|
|
3615
|
+
/**Called when state is subscribed to to setup connection to remote resource*/
|
|
3616
|
+
setup_connection(_state) {
|
|
3617
|
+
}
|
|
3618
|
+
/**Called when state is no longer subscribed to to cleanup connection to remote resource*/
|
|
3619
|
+
teardown_connection(_state) {
|
|
3620
|
+
}
|
|
3621
|
+
related() {
|
|
3622
|
+
return this.#helper?.related ? this.#helper.related() : none14();
|
|
3623
|
+
}
|
|
3624
|
+
};
|
|
3625
|
+
var roa2 = {
|
|
3626
|
+
/**Alternative state resource which can be initialized with functions
|
|
3627
|
+
* @template READ - The type of the state’s value when read.
|
|
3628
|
+
* @template REL - The type of related states, defaults to an empty object.
|
|
3629
|
+
* @param once function called when state value is requested once
|
|
3630
|
+
* @param setup function called when state has been subscribed to
|
|
3631
|
+
* @param teardown function called when state has been unsubscribed from completely
|
|
3632
|
+
* @param debounce delay added to once value retrival, which will collect multiple once requests into a single one
|
|
3633
|
+
* @param validity how long the last retrived value is considered valid, if true, value is valid until all unsubscribes
|
|
3634
|
+
* @param retention delay after last subscriber unsubscribes before teardown is called, to allow quick resubscribe without teardown
|
|
3635
|
+
* */
|
|
3636
|
+
from(once, setup, teardown, times, helper) {
|
|
3637
|
+
return new FuncROA(
|
|
3638
|
+
once,
|
|
3639
|
+
setup,
|
|
3640
|
+
teardown,
|
|
3641
|
+
times?.timeout ?? 1e3,
|
|
3642
|
+
times?.debounce ?? 0,
|
|
3643
|
+
times?.validity ?? 0,
|
|
3644
|
+
times?.retention ?? 0,
|
|
3645
|
+
helper
|
|
3646
|
+
);
|
|
3647
|
+
},
|
|
3648
|
+
class: StateResourceROA
|
|
3649
|
+
};
|
|
3650
|
+
var STATE_RESOURCE_ROA = {
|
|
3651
|
+
/**Remote resource with guaranteed ok value */
|
|
3652
|
+
roa: roa2
|
|
3653
|
+
};
|
|
3654
|
+
|
|
3655
|
+
// src/index.ts
|
|
3656
|
+
var state = {
|
|
3657
|
+
/**The state key is a symbol used to identify state objects
|
|
3658
|
+
* To implement a custom state, set this key to true on the object */
|
|
3659
|
+
STATE_KEY,
|
|
3660
|
+
a: STATE_ARRAY,
|
|
3661
|
+
/**Collected states, collects values from multiple states and reduces it to one */
|
|
3662
|
+
c: {
|
|
3663
|
+
rea: STATE_COLLECTED_REA,
|
|
3664
|
+
res: STATE_COLLECTED_RES,
|
|
3665
|
+
roa: STATE_COLLECTED_ROA,
|
|
3666
|
+
ros: STATE_COLLECTED_ROS,
|
|
3667
|
+
num: STATE_COLLECTS_NUMBER
|
|
3668
|
+
},
|
|
3669
|
+
d: STATE_DELAYED,
|
|
3670
|
+
h: STATE_HELPERS,
|
|
3671
|
+
l: STATE_LAZY,
|
|
3672
|
+
p: {
|
|
3673
|
+
...STATE_PROXY_REA,
|
|
3674
|
+
...STATE_PROXY_RES,
|
|
3675
|
+
...STATE_PROXY_ROA,
|
|
3676
|
+
...STATE_PROXY_ROS
|
|
3677
|
+
},
|
|
3678
|
+
r: { ...STATE_RESOURCE_REA, ...STATE_RESOURCE_ROA },
|
|
3679
|
+
s: STATE_SYNC,
|
|
3680
|
+
/**Returns true if the given object promises to be a state */
|
|
3681
|
+
is(s) {
|
|
3682
|
+
return Boolean(s) && s[STATE_KEY] === true;
|
|
3683
|
+
},
|
|
3684
|
+
/**Utility base class for state, with basic state functionality */
|
|
3685
|
+
class: StateBase,
|
|
3686
|
+
ok: STATE_SYNC.ros.ok,
|
|
3687
|
+
err: STATE_SYNC.res.err,
|
|
3688
|
+
from: STATE_SYNC.res.ok,
|
|
3689
|
+
ok_ws: STATE_SYNC.ros_ws.ok,
|
|
3690
|
+
err_ws: STATE_SYNC.res_ws.err,
|
|
3691
|
+
from_ws: STATE_SYNC.res_ws.ok
|
|
3692
|
+
};
|
|
3693
|
+
export {
|
|
3694
|
+
StateEnumHelper,
|
|
3695
|
+
StateNumberHelper,
|
|
3696
|
+
StateStringHelper,
|
|
3697
|
+
state
|
|
3698
|
+
};
|