Skip to content

Commit

Permalink
fix iterable-handler issues
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Nov 18, 2023
1 parent 20d5ffb commit 84eb643
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
6 changes: 4 additions & 2 deletions functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ export function map<T, U, O extends 'array'|'map' = 'array'>(iterable: Iterable<
onNewEntry: (v,k) => {
(mapped as U[])[k] = v
},
onEmpty: () => (mapped as U[]).length = 0
onEmpty: () => {
(mapped as U[]).length = 0
}
})
}

Expand Down Expand Up @@ -197,7 +199,7 @@ export const select = toggle;
* @param a input value
* @param b input value
*/
export function equals<T,V>(a:RefLike<T>|T, b: RefLike<V>|V): T extends V ? (V extends T ? Datex.Pointer<boolean> : Datex.Pointer<false>) : Datex.Pointer<false> {
export function equals<T,V>(a:RefLike<T>|T, b: RefLike<V>|V): Datex.Pointer<boolean> {
return transform([a, b], (a,b) => Datex.Ref.collapseValue(a, true, true) === Datex.Ref.collapseValue(b, true, true),
// dx transforms not working correctly (with uix)
/*`always (${Runtime.valueToDatexString(a)} === ${Runtime.valueToDatexString(b)})`*/) as any;
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5165,7 +5165,7 @@ export class Runtime {
}).filter(o => o && !(o.equals(Runtime.endpoint) || o.equals(SCOPE.sender))))
];
if (origins.length > 1) {
logger.debug("pre-fetching online state for "+ origins.length + " endpoints", content)
logger.debug("pre-fetching online state for "+ origins.length + " endpoints")
await Promise.race([
Promise.all(origins.map(origin => origin.isOnline())),
new Promise(resolve => setTimeout(resolve, 10_000))
Expand Down
2 changes: 1 addition & 1 deletion runtime/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ export namespace Storage {

// @ts-ignore NO_INIT
if (!globalThis.NO_INIT) {
addEventListener("unload", ()=>Storage.handleExit(), {capture: true});
if (client_type == "deno") addEventListener("unload", ()=>Storage.handleExit(), {capture: true});
addEventListener("beforeunload", ()=>Storage.handleExit(), {capture: true});
// @ts-ignore document
if (globalThis.document) addEventListener("visibilitychange", ()=>{
Expand Down
11 changes: 9 additions & 2 deletions types/function-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,24 @@ export async function getDeclaredExternalVariablesAsync(fn: (...args:unknown[])=
export function getSourceWithoutUsingDeclaration(fn: (...args:unknown[])=>unknown) {
let fnSource = fn.toString();
// object methods check if 'this' context is component context;
if (isObjectMethod(fnSource)) {
if (!isNormalFunction(fnSource) && !isArrowFunction(fnSource) && isObjectMethod(fnSource)) {
if (fnSource.startsWith("async")) fnSource = fnSource.replace("async", "async function")
else fnSource = "function " + fnSource
}
return fnSource
.replace(/(?<=(?:(?:[\w\s*])+\(.*\)\s*{|\(.*\)\s*=>\s*{?|.*\s*=>\s*{?)\s*)(use\s*\((?:[\s\S]*?)\))/, 'true /*$1*/')
}

function isObjectMethod(fnSrc:string) {
const isObjectMethod = (fnSrc:string) => {
return !!fnSrc.match(/^(async\s+)?[^\s(]+ *(\(|\*)/)
}
const isNormalFunction = (fnSrc:string) => {
return !!fnSrc.match(/^(async\s+)?function(\(| |\*)/)
}
const isArrowFunction = (fnSrc:string) => {
return !!fnSrc.match(/^(async\s+)?\([^)]*\)\s*=>/)
}


/**
* Create a new function from JS source code with injected dependency variables
Expand Down
23 changes: 16 additions & 7 deletions utils/iterable-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export class IterableHandler<T, U = T> {
private map: ((value: T, index: number, array: Iterable<T>) => U) | undefined
private onNewEntry: (entry:U, key:number,) => void
private onEntryRemoved: (entry:U, key:number,) => void
private onEmpty: () => void
private onEmpty?: () => void


constructor(private iterable: Datex.RefOrValue<Iterable<T>>, callbacks: {
map?: (value: T, index: number, array: Iterable<T>) => U,
onNewEntry: (this: IterableHandler<T, U>, entry:U, key:number) => void
onEntryRemoved: (entry: U, key:number) => void,
onEmpty: () => void
onEmpty?: () => void
}) {
this.map = callbacks.map;
this.onNewEntry = callbacks.onNewEntry;
Expand Down Expand Up @@ -102,10 +102,17 @@ export class IterableHandler<T, U = T> {
// single property update
if (type == Datex.Ref.UPDATE_TYPE.SET) this.handleNewEntry(<T>value, key)
else if (type == Datex.Ref.UPDATE_TYPE.ADD) this.handleNewEntry(<T>value, this.getPseudoIndex(key, <T>value));
// property removed
// clear all
else if (type == Datex.Ref.UPDATE_TYPE.CLEAR) {
for (const [key,] of this.#entries??[]) {
this.handleRemoveEntry(key);
// handle onEmpty
if (this.onEmpty) {
this.onEmpty.call ? this.onEmpty.call(this) : this.onEmpty();
}
// alternative: delete all entries individually
else {
for (const [key,] of [...this.#entries??[]].toReversed()) {
this.handleRemoveEntry(key);
}
}
}
else if (type == Datex.Ref.UPDATE_TYPE.BEFORE_DELETE) this.handleRemoveEntry(key);
Expand All @@ -130,7 +137,8 @@ export class IterableHandler<T, U = T> {
const entry = this.valueToEntry(value, key)

if (key != undefined) {
if (this.entries.has(key)) this.handleRemoveEntry(key) // entry is overridden
// TODO: is this correct
if (!this.isPseudoIndex() && this.entries.has(key)) this.handleRemoveEntry(key) // entry is overridden
this.entries.set(key, entry);
}
this.onNewEntry.call ? this.onNewEntry.call(this, entry, Number(key)) : this.onNewEntry(entry, Number(key)); // new entry handler
Expand All @@ -142,11 +150,12 @@ export class IterableHandler<T, U = T> {
const entry = this.entries.get(key)!;
this.deleteEntry(key);
this.onEntryRemoved.call ? this.onEntryRemoved.call(this, entry, key) : this.onEntryRemoved(entry, key);

this.checkEmpty();
}

private checkEmpty() {
if (this.#entries?.size == 0) this.onEmpty.call ? this.onEmpty.call(this) : this.onEmpty();
if (this.onEmpty && this.#entries?.size == 0) this.onEmpty.call ? this.onEmpty.call(this) : this.onEmpty();
}

}

0 comments on commit 84eb643

Please sign in to comment.