diff --git a/packages/react-sdk/src/useObservable.ts b/packages/react-sdk/src/useObservable.ts index e2eb53b5b..0f189664d 100644 --- a/packages/react-sdk/src/useObservable.ts +++ b/packages/react-sdk/src/useObservable.ts @@ -81,10 +81,10 @@ export function useObservable( if (!observable) { return } - const subscription = observable.subscribe(onSubscribe, { + const unsub = observable.subscribe(onSubscribe, { fireImediately: opts?.fireImmediately, }) - return () => subscription.unsubscribe(onSubscribe) + return unsub }, [opts, observable, onSubscribe]) const data = useMemo(() => { diff --git a/packages/sdk/src/observable/observable.ts b/packages/sdk/src/observable/observable.ts index b951dc212..97a57cb6a 100644 --- a/packages/sdk/src/observable/observable.ts +++ b/packages/sdk/src/observable/observable.ts @@ -26,7 +26,7 @@ export class Observable { subscribe( subscriber: (newValue: T) => void, opts: { fireImediately?: boolean; once?: boolean; condition?: (value: T) => boolean } = {}, - ): this { + ): () => void { const sub = { id: this._nextId++, fn: subscriber, @@ -37,7 +37,7 @@ export class Observable { if (opts.fireImediately) { this._notify(sub, this.value) } - return this + return () => this.unsubscribe(subscriber) } when(