Skip to content

Commit c0c69a5

Browse files
JeanMechethePunderWoman
authored andcommitted
fix(core): unregister onDestroy in toSignal. (#61514)
We cleanup if the observable completes before the onDestroy fires. fixes #61511 PR Close #61514
1 parent af19705 commit c0c69a5

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

packages/core/rxjs-interop/src/to_signal.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ export function toSignal<T, U = undefined>(
164164
);
165165
}
166166

167+
let destroyUnregisterFn: (() => void) | undefined;
168+
167169
// Note: This code cannot run inside a reactive context (see assertion above). If we'd support
168170
// this, we would subscribe to the observable outside of the current reactive context, avoiding
169171
// that side-effect signal reads/writes are attribute to the current consumer. The current
@@ -180,6 +182,9 @@ export function toSignal<T, U = undefined>(
180182
}
181183
state.set({kind: StateKind.Error, error});
182184
},
185+
complete: () => {
186+
destroyUnregisterFn?.();
187+
},
183188
// Completion of the Observable is meaningless to the signal. Signals don't have a concept of
184189
// "complete".
185190
});
@@ -193,7 +198,7 @@ export function toSignal<T, U = undefined>(
193198
}
194199

195200
// Unsubscribe when the current context is destroyed, if requested.
196-
cleanupRef?.onDestroy(sub.unsubscribe.bind(sub));
201+
destroyUnregisterFn = cleanupRef?.onDestroy(sub.unsubscribe.bind(sub));
197202

198203
// The actual returned signal is a `computed` of the `State` signal, which maps the various states
199204
// to either values or errors.

0 commit comments

Comments
 (0)