-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.ts
501 lines (435 loc) · 17.2 KB
/
functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
* This file contains utility funtions to transform pointer to other pointers.
* The resulting pointers are automatically updated when the input pointers are updated
*/
import { AsyncTransformFunction, INSERT_MARK, MaybeObjectRef, MinimalJSRef, Pointer, ReactiveValue, RefLike, RefOrValue, SmartTransformFunction, SmartTransformOptions, TransformFunction, TransformFunctionInputs, logger } from "./datex_all.ts";
import { Datex } from "./mod.ts";
import { PointerError } from "./types/errors.ts";
import { IterableHandler } from "./utils/iterable-handler.ts";
import { AsyncSmartTransformFunction, MinimalJSRefWithIndirectRef, RestrictSameType } from "./runtime/pointers.ts";
import { handleError } from "./utils/error-handling.ts";
import { KnownError } from "./utils/error-handling.ts";
/**
* A generic transform function, creates a new pointer containing the result of the callback function.
* At any point in time, the pointer is the result of the callback function.
* In contrast to the `transform` function, dependency references are automatically detected, they don't need to be explicitly specified.
* ```ts
* const x = $$(10);
* const y = always (() => x * 2);
* y.val // 20
* x.val = 5;
* y.val // 10
* ```
*/
export function always<T>(transform:SmartTransformFunction<T>, options?: SmartTransformOptions): MinimalJSRefWithIndirectRef<T> // return signature from Value.collapseValue(Pointer.smartTransform())
// only works with JUSIX compilation
export function always<const T>(value:T, options?: SmartTransformOptions): MinimalJSRef<T>
/**
* Shortcut for datex `always (...)`
* @param script
* @param vars
*/
export function always<T=unknown>(script:TemplateStringsArray, ...vars:any[]): Promise<MinimalJSRefWithIndirectRef<T>>
export function always(scriptOrJSTransform:TemplateStringsArray|SmartTransformFunction<any>, ...vars:any[]) {
// js function
if (typeof scriptOrJSTransform == "function") {
const options: SmartTransformOptions|undefined = typeof vars[0] == "object" ? vars[0] : undefined;
// make sure handler is not an async function
if (scriptOrJSTransform.constructor.name == "AsyncFunction" && !options?._allowAsync) {
throw new Error("Async functions are not allowed as always transforms")
}
const ptr = Pointer.createSmartTransform(scriptOrJSTransform, undefined, undefined, undefined, options);
if (options?._allowAsync && !ptr.value_initialized && ptr.waiting_for_always_promise) {
return ptr.waiting_for_always_promise.then(()=>collapseTransformPointer(ptr, options?._collapseStatic, options?._returnWrapper, options?._allowAnyType));
}
if (!ptr.value_initialized && ptr.waiting_for_always_promise) {
throw new PointerError(`Promises cannot be returned from always transforms - use 'asyncAlways' instead`);
}
else {
return collapseTransformPointer(ptr, options?._collapseStatic, options?._returnWrapper, options?._allowAnyType);
}
}
// datex script
else if (scriptOrJSTransform.raw instanceof Array) {
return (async ()=>collapseTransformPointer(await datex(`always (${scriptOrJSTransform.raw.join(INSERT_MARK)})`, vars)))()
}
else {
handleError(new KnownError("You called 'always' with invalid arguments. It seems like you are not using Deno for UIX.", [
"Install Deno for UIX, see https://docs.unyt.org/manual/uix/getting-started#install-deno",
"Call 'always' with a function: always(() => ...)"
]))
}
}
function collapseTransformPointer(ptr: Pointer, collapseStatic = false, alwaysReturnWrapper = false, _allowAnyType = false) {
// collapse if transform function is static
const collapse = collapseStatic && ptr.isStaticTransform;
if (_allowAnyType) {
ptr.allowAnyType(true);
}
if (alwaysReturnWrapper && !collapse) {
return ptr;
}
const val = ReactiveValue.collapseValue(ptr, false, collapse);
if (collapse) ptr.delete();
// TODO: deproxify static non-primitive objects to garbage-collect pointer and associated data
return val;
}
/**
* A generic transform function, creates a new pointer containing the result of the callback function.
* At any point in time, the pointer is the result of the callback function.
* In contrast to the always function, this function can return a Promise
* ```ts
* const x = $$(42);
* const y = await asyncAlways (() => complexCalculation(x.val * 10));
*
* async function complexCalculation(input: number) {
* const res = await ...// some async operation
* return res
* }
* ```
*/
export async function asyncAlways<T>(transform:AsyncSmartTransformFunction<T>, options?: SmartTransformOptions): Promise<MinimalJSRefWithIndirectRef<T>> { // return signature from Value.collapseValue(Pointer.smartTransform())
const ptr = Pointer.createSmartTransform(transform, undefined, undefined, undefined, options);
if (!ptr.value_initialized && ptr.waiting_for_always_promise) {
await ptr.waiting_for_always_promise;
}
else {
logger.warn("asyncAlways: transform function did not return a Promise, you should use 'always' instead")
}
return ReactiveValue.collapseValue(ptr) as MinimalJSRef<T>
}
/**
* Decorator for creating a reactive function.
* Functions decorated with `reactiveFn` always return a pointer that is automatically updated when input references are updated.
* This has the same effect as wrapping the function body with `always`.
* A reactive functions accepts references or values as arguments, but is always called with collapsed values.
* This means that you don't have to specifiy `Ref` values as arguments, but can use regular types.
*
* Example:
* ```ts
* // create reactive function 'getSquared'
* const getSquared = reactiveFn((x: number) => x * x);
*
* const x = $$(2);
* const y = getSquared(x); // Ref<4>
* x.val = 3;
* y // Ref<9>
* ```
*/
export function reactiveFn<ReturnType, Args extends unknown[]>(fn: (...args: Args) => Awaited<RestrictSameType<RefOrValue<ReturnType>>>) {
return (...args: MapToRefOrVal<Args>) => always(() => {
const collapsedArgs = args.map(arg => ReactiveValue.collapseValue(arg, true, true)) as Args;
return fn(...collapsedArgs)
});
}
type MapToRefOrVal<T extends unknown[]> = {[K in keyof T]: T[K] extends ReactiveValue ? T[K] : RefOrValue<T[K]>}
const getGreetingMessage = (country: RefOrValue<string>) => {
return always(() => {
switch (country) {
case "de": return "Hallo";
case "fr": return "Bonjour";
case "es": return "Hola";
default: return "Hello";
}
})
}
/**
* Runs each time a dependency reference value changes.
* Dependency references are automatically detected.
* ```ts
* const x = $$(10);
* effect (() => console.log("x is " + x));
* x.val = 5; // logs "x is 5"
* ```
*
* Disposing effects:
* ```ts
* const x = $$(10);
* const {dispose} = effect (() => console.log("x is " + x));
* x.val = 5; // logs "x is 5"
* dispose();
* x.val = 6; // no log
* ```
*/
export function effect<W extends Record<string, WeakKey>|undefined>(handler:W extends undefined ? () => void|Promise<void> :(weakVariables: W) => void|Promise<void>, weakVariables?: W): {dispose: () => void, [Symbol.dispose]: () => void} {
let ptr: Pointer;
// weak variable binding
if (weakVariables) {
const weakVariablesProxy = {};
for (const [k, v] of Object.entries(weakVariables)) {
const weakRef = new WeakRef(v);
Object.defineProperty(weakVariablesProxy, k, {get() {
const val = weakRef.deref()
if (!val) {
// dispose effect
ptr.is_persistent = false;
ptr.delete()
throw Pointer.WEAK_EFFECT_DISPOSED;
}
else return val;
}})
}
const originalHandler = handler;
handler = (() => originalHandler(weakVariablesProxy)) as any;
}
ptr = Pointer.createSmartTransform(handler as any, undefined, true, true);
ptr.is_persistent = true;
return {
[Symbol.dispose||Symbol.for("Symbol.dispose")]() {
ptr.is_persistent = false;
ptr.delete()
},
dispose() {
ptr.is_persistent = false;
ptr.delete()
}
}
}
/**
* A generic transform function, creates a new pointer containing the result of the callback function.
* At any point in time, the pointer is the result of the callback function.
* Dependency references have to be specified. If one of the dependencies changes, the resulting pointe
* value is recalculated.
* @param dependencies dependency references
* @param transform transform function
* @param persistent_datex_transform optional equivalent datex script describing the transform
* @returns
*/
export function transform<T,V extends TransformFunctionInputs>(dependencies:V, transform:TransformFunction<V,T>, persistent_datex_transform?:string) {
return ReactiveValue.collapseValue(Pointer.createTransform(dependencies, transform, persistent_datex_transform));
}
/**
* A generic transform function, creates a new pointer containing the result of the callback function.
* At any point in time, the pointer is the result of the callback function.
* Dependency references have to be specified. If one of the dependencies changes, the resulting pointe
* value is recalculated.
* @param dependencies dependency references
* @param transform an async transform function (returning a Promise)
* @param persistent_datex_transform optional equivalent datex script describing the transform
* @returns
*/
export async function transformAsync<T,V extends TransformFunctionInputs>(dependencies:V, transform:AsyncTransformFunction<V,T>, persistent_datex_transform?:string) {
return ReactiveValue.collapseValue(await Pointer.createTransformAsync(dependencies, transform, persistent_datex_transform));
}
export function map<T, U, O extends 'array'|'map' = 'array'>(iterable: Iterable<T>, mapFn: (value: MaybeObjectRef<T>, index: number, array: Iterable<T>) => U, options?: {outType: O}): O extends "array" ? U[] : Map<number, U> {
let mapped:U[]|Map<number, U>
// live map
if (Datex.ReactiveValue.isRef(iterable)) {
// return map
if (options?.outType == "map") {
mapped = $$(new Map())
const iterableHandler = new IterableHandler(iterable, {
map: (v,k)=>{
return mapFn(v,k,iterable)
},
onEntryRemoved: (v,k) => {
(mapped as Map<number,U>).delete(k)
},
onNewEntry: (v,k) => (mapped as Map<number,U>).set(k,v),
onEmpty: () => (mapped as Map<number,U>).clear()
})
// reverse transform binding
Datex.Pointer.bindDisposable(mapped, iterableHandler)
}
// return array
else {
mapped = $$([])
// no gaps in a set -> array splice required
const spliceArray = iterable instanceof Set;
const iterableHandler = new IterableHandler(iterable, {
map: (v,k)=>{
return mapFn(v,k,iterable)
},
onEntryRemoved: (v,k) => {
if (spliceArray) (mapped as U[]).splice(k, 1);
else delete (mapped as U[])[k];
},
onNewEntry: (v,k) => {
(mapped as U[])[k] = v
},
onEmpty: () => {
(mapped as U[]).length = 0
}
})
// reverse transform binding
Datex.Pointer.bindDisposable(mapped, iterableHandler)
}
}
// static map
else {
if (options?.outType == "map") {
mapped = new Map()
let i = 0;
for (const val of iterable) {
mapped.set(i, mapFn(val, i++, iterable))
}
}
else {
mapped = []
let i = 0;
for (const val of iterable) {
mapped.push(mapFn(val, i++, iterable))
}
}
}
const ptr = Pointer.getByValue(mapped);
if (ptr) ptr.isTransform = true;
return mapped as any;
}
// TODO: (remove empty entries inbetween)
// export function filter<T, U>(array: Array<T>, predicate: (value: T, index: number, array: T[]) => value is T&U) {
// // live map
// if (Datex.Ref.isRef(array)) {
// const filtered:U[] = $$([])
// const spliceArray = true;
// new IterableHandler<T,U>(array, {
// filter: (v,k):v is T&U => {
// return predicate(v,k,array)
// },
// onEntryRemoved: (v,k) => {
// if (spliceArray) filtered.splice(k, 1);
// else delete filtered[k];
// },
// onNewEntry: (v,k) => {
// filtered[k] = v
// },
// onEmpty: () => {
// filtered.length = 0
// }
// })
// return filtered;
// }
// // static map
// else return array.filter(predicate)
// }
/**
* Switches between two values depending if a value is true or false
* @param value input value
* @param if_true value selected if true
* @param if_false value selected if false
*/
export function toggle<T>(value:RefLike<boolean>, if_true:T, if_false:T = null as T): MinimalJSRef<T> {
return transform([value], v=>v?<any>if_true:<any>if_false,
// dx transforms not working correctly (with uix)
/*`
always (
if (${Runtime.valueToDatexString(value)}) (${Runtime.valueToDatexString(if_true)})
else (${Runtime.valueToDatexString(if_false)})
)`*/);
}
/**
* @deprecated, use toggle()
*/
export const select = toggle;
/**
* Returns a pointer representing a === b
* @param a input value
* @param b input value
*/
export function equals<T,V>(a:RefLike<T>|T, b: RefLike<V>|V): Datex.Pointer<boolean> {
return transform([a, b], (a,b) => Datex.ReactiveValue.collapseValue(a, true, true) === Datex.ReactiveValue.collapseValue(b, true, true),
// dx transforms not working correctly (with uix)
/*`always (${Runtime.valueToDatexString(a)} === ${Runtime.valueToDatexString(b)})`*/) as any;
}
/**
* Selects a property from an object
* @param property property name
* @param object the reference object
* @returns
*/
export function selectProperty<K extends string|number, V>(property:RefLike<K>, object:Readonly<Record<K, V>>):MinimalJSRef<V> {
return <MinimalJSRef<V>> transform([property], (v)=><any>object[<K>v]);
}
/**
* Inverts a boolean value/reference
* @param value
* @returns
*/
export function not(value:RefOrValue<boolean>): Pointer<boolean> {
return transform([value], v=>!v);
}
/**
* Performs a boolean 'and' operation on one or more boolean values/references
* @param values
* @returns
*/
export function and(...values:RefOrValue<boolean>[]): Pointer<boolean> {
return transform(values, (...values)=>{
for (const v of values) {
if (!v) return false;
}
return true;
});
}
/**
* Performs a boolean 'or' operation on one or more boolean values/references
* @param values
* @returns
*/
export function or(...values:RefOrValue<boolean>[]): Pointer<boolean> {
return transform(values, (...values)=>{
for (const v of values) {
if (v) return true;
}
return false;
});
}
/**
* Performs an 'add' operation on one or more values (strings, numbers)
* @param numbers
* @returns
*/
export function add<T>(...numbers:RefOrValue<T>[]): MinimalJSRef<T>
export function add(...args:any[]) {
return transform([...args], (...args) => args.reduce((a, b) => a + b, 0));
}
/**
* Performs a 'subtract' operation on one or more numbers
* @param numbers
* @returns
*/
export function sub(...numbers:RefOrValue<bigint>[]): MinimalJSRef<bigint>
export function sub(...numbers:RefOrValue<number>[]): MinimalJSRef<number>
export function sub(...args:any[]) {
return transform([...args], (...args) => args.slice(1).reduce((a, b) => a - b, args[0]));
}
/**
* Performs a 'multiply' operation on one or more numbers
* @param numbers
* @returns
*/
export function mul(...numbers:RefOrValue<bigint>[]): MinimalJSRef<bigint>
export function mul(...numbers:RefOrValue<number>[]): MinimalJSRef<number>
export function mul(...args:any[]) {
return transform([...args], (...args) => args.reduce((a, b) => a * b, 1));
}
/**
* Performs a 'division' operation on one or more numbers
* @param numbers
* @returns
*/
export function div(...numbers:RefOrValue<bigint>[]): MinimalJSRef<bigint>
export function div(...numbers:RefOrValue<number>[]): MinimalJSRef<number>
export function div(...args:any[]) {
return transform([...args], (...args) => args.slice(1).reduce((a, b) => a / b, args[0]));
}
/**
* Performs a 'power' operation on one or more numbers
* @param numbers
* @returns
*/
export function pow(...numbers:RefOrValue<bigint>[]): MinimalJSRef<bigint>
export function pow(...numbers:RefOrValue<number>[]): MinimalJSRef<number>
export function pow(...args:any[]) {
return transform([...args], (...args) => args.slice(1).reduce((a, b) => a ** b, args[0]));
}
// @ts-ignore
globalThis.transform = transform;
// @ts-ignore
globalThis.transformAsync = transformAsync;
// @ts-ignore
globalThis.and = and;
// @ts-ignore
globalThis.or = or;
// @ts-ignore
globalThis.not = not;