Skip to content

Commit

Permalink
add MinimalJSRef distinction
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Feb 13, 2024
1 parent b8dd84c commit d989e05
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 4 additions & 4 deletions functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AsyncTransformFunction, CollapsedValue, CollapsedValueAdvanced, Decorat
import { Datex } from "./mod.ts";
import { PointerError } from "./types/errors.ts";
import { IterableHandler } from "./utils/iterable-handler.ts";
import { RestrictSameType } from "./runtime/pointers.ts";
import { MinimalJSRefWithIndirectRef, RestrictSameType } from "./runtime/pointers.ts";


/**
Expand All @@ -23,13 +23,13 @@ import { RestrictSameType } from "./runtime/pointers.ts";
* y.val // 10
* ```
*/
export function always<T>(transform:SmartTransformFunction<T>, options?: SmartTransformOptions): MinimalJSRef<T> // return signature from Value.collapseValue(Pointer.smartTransform())
export function always<T>(transform:SmartTransformFunction<T>, options?: SmartTransformOptions): MinimalJSRefWithIndirectRef<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<T=unknown>(script:TemplateStringsArray, ...vars:any[]): Promise<MinimalJSRefWithIndirectRef<T>>
export function always(scriptOrJSTransform:TemplateStringsArray|SmartTransformFunction<any>, ...vars:any[]) {
// js function
if (typeof scriptOrJSTransform == "function") {
Expand Down Expand Up @@ -64,7 +64,7 @@ export function always(scriptOrJSTransform:TemplateStringsArray|SmartTransformFu
* }
* ```
*/
export async function asyncAlways<T>(transform:SmartTransformFunction<T>, options?: SmartTransformOptions): Promise<MinimalJSRef<T>> { // return signature from Value.collapseValue(Pointer.smartTransform())
export async function asyncAlways<T>(transform:SmartTransformFunction<T>, options?: SmartTransformOptions): Promise<MinimalJSRefWithIndirectRef<T>> { // return signature from Value.collapseValue(Pointer.smartTransform())
// make sure handler is not an async function
if (transform.constructor.name == "AsyncFunction") {
throw new Error("asyncAlways cannot be used with async functions, but with functions returning a Promise")
Expand Down
10 changes: 9 additions & 1 deletion runtime/pointers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,8 @@ export type AnyObjectRef = {$: Record<string,unknown>, $$: Record<string,unknown
export type WrappedPointerValue = number|string|boolean|bigint|URL|Endpoint

// convert from any JS/DATEX value to minimal representation with reference
export type MinimalJSRef<T, _C = CollapsedValue<T>> =
// if a value is a object ref, it is converted to a Pointer<T>
export type MinimalJSRefWithIndirectRef<T, _C = CollapsedValue<T>> =
_C extends symbol ? symbol : (
_C extends WrappedPointerValue ?
PointerWithPrimitive<_C>: // keep pointer reference
Expand All @@ -849,6 +850,13 @@ export type MinimalJSRef<T, _C = CollapsedValue<T>> =
ObjectRef<_C> // collapsed object
)

export type MinimalJSRef<T, _C = CollapsedValue<T>> =
_C extends symbol ? symbol : (
_C extends WrappedPointerValue ?
PointerWithPrimitive<_C>: // keep pointer reference
ObjectRef<_C> // collapsed object
)

// return Pointer<T>&T for primitives (excluding boolean) and Pointer<T> otherwise
export type PointerWithPrimitive<T> = T extends WrappedPointerValue ?
T extends primitive ?
Expand Down

0 comments on commit d989e05

Please sign in to comment.