diff --git a/src/internal/utils.js b/src/internal/utils.js index 5e2f8b1165..ea3f2ddde5 100644 --- a/src/internal/utils.js +++ b/src/internal/utils.js @@ -48,7 +48,12 @@ export function validate_store(store, name) { } export function subscribe(component, store, callback) { - component.$$.on_destroy.push(store.subscribe(callback)); + let unsub = store.subscribe(callback); + // Prevent memory leaks for RxJS users. + if (typeof unsub === 'object' && typeof unsub.unsubscribe === 'function') { + unsub = () => unsub.unsubscribe(); + } + component.$$.on_destroy.push(unsub); } export function create_slot(definition, ctx, fn) { @@ -74,4 +79,4 @@ export function exclude_internal_props(props) { const result = {}; for (const k in props) if (k[0] !== '$') result[k] = props[k]; return result; -} \ No newline at end of file +}