From dfcb6eb28467e890664b8f6dc09754a811031ad2 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Thu, 13 Jul 2023 14:48:48 +0200 Subject: [PATCH] fix: fix initial reactivity double trigger --- .../apps/components/helpers/InputValue.svelte | 2 +- frontend/src/lib/components/apps/rx.ts | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/frontend/src/lib/components/apps/components/helpers/InputValue.svelte b/frontend/src/lib/components/apps/components/helpers/InputValue.svelte index 765184d59a..f7f17f2f70 100644 --- a/frontend/src/lib/components/apps/components/helpers/InputValue.svelte +++ b/frontend/src/lib/components/apps/components/helpers/InputValue.svelte @@ -119,7 +119,7 @@ async function handleConnection() { if (lastInput?.type === 'connected') { - $worldStore?.connect(lastInput, onValueChange, `${id}-${key}`) + $worldStore?.connect(lastInput, onValueChange, `${id}-${key}`, value) } else if (lastInput?.type === 'static' || lastInput?.type == 'template') { value = await getValue(lastInput) } else if (lastInput?.type == 'eval') { diff --git a/frontend/src/lib/components/apps/rx.ts b/frontend/src/lib/components/apps/rx.ts index d8751723ea..8f71ccd7c4 100644 --- a/frontend/src/lib/components/apps/rx.ts +++ b/frontend/src/lib/components/apps/rx.ts @@ -8,7 +8,7 @@ export interface Subscriber { } export interface Observable { - subscribe(x: Subscriber): void + subscribe(x: Subscriber, previousValue: T): void } export interface Output extends Observable { set(x: T, force?: boolean): void @@ -21,7 +21,12 @@ export interface Input extends Subscriber { export type World = { outputsById: Record>> - connect: (inputSpec: AppInput, next: (x: T) => void, id?: string) => Input + connect: ( + inputSpec: AppInput, + next: (x: T) => void, + id: string, + previousValue: any + ) => Input stateId: Writable newOutput: (id: string, name: string, previousValue: T) => Output } @@ -64,7 +69,12 @@ export function buildWorld(context: Record): Writable { export function buildObservableWorld() { const observables: Record> = {} - function connect(inputSpec: AppInput, next: (x: T) => void, id?: string): Input { + function connect( + inputSpec: AppInput, + next: (x: T) => void, + id: string, + previousValue: T + ): Input { if (inputSpec.type === 'static') { return { id, @@ -97,8 +107,7 @@ export function buildObservableWorld() { } } - obs.subscribe(input) - input.next(obs.peak()) + obs.subscribe(input, previousValue) return input } else if (inputSpec.type === 'user') { return { @@ -150,7 +159,7 @@ export function settableOutput(state: Writable, previousValue: T): Ou let value: T | undefined = previousValue const subscribers: Subscriber[] = [] - function subscribe(x: Subscriber) { + function subscribe(x: Subscriber, npreviousValue: any) { let currentSubscriber = subscribers.findIndex((y) => y === x || (y.id && y.id === x.id)) if (currentSubscriber == -1) { subscribers.push(x) @@ -159,7 +168,7 @@ export function settableOutput(state: Writable, previousValue: T): Ou } // Send the current value to the new subscriber if it already exists - if (value !== undefined) { + if (value !== undefined && !deepEqual(value, npreviousValue)) { x.next(value) } }