Skip to content

Commit

Permalink
first experimental implementation of transform function result caching
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Nov 22, 2023
1 parent 22875eb commit c857dff
Show file tree
Hide file tree
Showing 5 changed files with 703 additions and 145 deletions.
12 changes: 9 additions & 3 deletions datex_short.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ declare global {
const observe: typeof Ref.observe
const observeAndInit: typeof Ref.observeAndInit
const unobserve: typeof Ref.unobserve
/**
* Prevents any values accessed within the callback function from
* being captured by a transform function (e.g. always)
*/
const isolate: typeof Ref.disableCapturing

// conflict with UIX.template (confusing)
// const template: typeof _template;
Expand Down Expand Up @@ -561,9 +566,10 @@ Object.defineProperty(globalThis, 'equals', {value:_equals, configurable:false})
Object.defineProperty(globalThis, 'selectProperty', {value:_selectProperty, configurable:false})
Object.defineProperty(globalThis, 'not', {value:_not, configurable:false})
Object.defineProperty(globalThis, 'effect', {value:_effect, configurable:false})
Object.defineProperty(globalThis, 'observe', {value:Ref.observe, configurable:false})
Object.defineProperty(globalThis, 'observeAndInit', {value:Ref.observeAndInit, configurable:false})
Object.defineProperty(globalThis, 'unobserve', {value:Ref.unobserve, configurable:false})
Object.defineProperty(globalThis, 'observe', {value:Ref.observe.bind(Ref), configurable:false})
Object.defineProperty(globalThis, 'observeAndInit', {value:Ref.observeAndInit.bind(Ref), configurable:false})
Object.defineProperty(globalThis, 'unobserve', {value:Ref.unobserve.bind(Ref), configurable:false})
Object.defineProperty(globalThis, 'isolate', {value:Ref.disableCapturing.bind(Ref), configurable:false})

// @ts-ignore
globalThis.get = get
Expand Down
8 changes: 4 additions & 4 deletions functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
*/


import { AsyncTransformFunction, BooleanRef, CollapsedValue, CollapsedValueAdvanced, Decorators, INSERT_MARK, METADATA, MaybeObjectRef, MinimalJSRef, Pointer, Ref, RefLike, RefOrValue, Runtime, SmartTransformFunction, TransformFunction, TransformFunctionInputs, handleDecoratorArgs, primitive } from "./datex_all.ts";
import { AsyncTransformFunction, BooleanRef, CollapsedValue, CollapsedValueAdvanced, Decorators, INSERT_MARK, METADATA, MaybeObjectRef, MinimalJSRef, Pointer, Ref, RefLike, RefOrValue, Runtime, SmartTransformFunction, SmartTransformOptions, TransformFunction, TransformFunctionInputs, handleDecoratorArgs, primitive } from "./datex_all.ts";
import { Datex } from "./mod.ts";
import { IterableHandler } from "./utils/iterable-handler.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.
Expand All @@ -21,17 +22,16 @@ import { IterableHandler } from "./utils/iterable-handler.ts";
* y.val // 10
* ```
*/
export function always<T>(transform:SmartTransformFunction<T>): MinimalJSRef<T> // return signature from Value.collapseValue(Pointer.smartTransform())
export function always<T>(transform:SmartTransformFunction<T>, options?: SmartTransformOptions): MinimalJSRef<T> // return signature from Value.collapseValue(Pointer.smartTransform())
/**
* Shortcut for datex `always (...)`
* @param script
* @param vars
*/
export function always<T=unknown>(script:TemplateStringsArray, ...vars:any[]): Promise<MinimalJSRef<T>>
export function always(scriptOrJSTransform:TemplateStringsArray|SmartTransformFunction<any>, ...vars:any[]) {

// js function
if (typeof scriptOrJSTransform == "function") return Ref.collapseValue(Pointer.createSmartTransform(scriptOrJSTransform));
if (typeof scriptOrJSTransform == "function") return Ref.collapseValue(Pointer.createSmartTransform(scriptOrJSTransform, undefined, undefined, undefined, vars[0]));
// datex script
else return (async ()=>Ref.collapseValue(await datex(`always (${scriptOrJSTransform.raw.join(INSERT_MARK)})`, vars)))()
}
Expand Down
Loading

0 comments on commit c857dff

Please sign in to comment.