Skip to content

Unsubscribe not called for observables that use syncObservable #641

@msantang78

Description

@msantang78

Bug: unsubscribe Not Triggered with syncObservable

This issue is related to #621 but occurs when an observable is converted to synchronous using syncObservable.

Description

When using syncObservable, the underlying observable’s unsubscribe callback is not executed after all observers are removed. As a result, the source subscription remains active even though there are no remaining observers.

Expected Behavior

Once the last observer unsubscribes, the underlying observable should trigger its unsubscribe logic.

Actual Behavior

unsubscribe is never called after all observers are removed.

         let numSubscribes = 0;
         let numUnsubscribes = 0;

         const store$ = observable<any>({});

          syncObservable(store$, {
              subscribe: ({ update }) => {
                  console.log('subscribed');
                  numSubscribes++;
                  update({ value: { value: 1 }, mode: 'set' });
                  return () => {
                      console.log('unsubscribed');
                      numUnsubscribes++;
                  };
              },
          });

         const Test = observer(function Test() {
             
             return createElement('div', undefined, store$.value.get());
         });

         const { unmount } = render(<Test />);

         act(() => {
             unmount();
         });

         await waitFor(() => promiseTimeout(0));

         expect(numSubscribes).toBe(1);
         expect(numUnsubscribes).toBe(1);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions