set up stores and unsubscriptions in one go

pull/12277/head
Rich Harris 2 years ago
parent b93d1d74d9
commit 726ca9f0bb

@ -154,11 +154,9 @@ export function client_component(source, analysis, options) {
}
if (binding.kind === 'store_sub') {
if (store_setup.length === 0) {
store_setup.push(
b.const('$$subscriptions', b.object([])),
b.stmt(b.call('$.unsubscribe_on_destroy', b.id('$$subscriptions')))
);
store_setup.push(b.const('$$subscriptions', b.call('$.create_stores')));
}
// We're creating an arrow function that gets the store value which minifies better for two or more references
const store_reference = serialize_get_binding(b.id(name.slice(1)), instance_state);
const store_get = b.call(

@ -109,12 +109,12 @@ export {
update_prop
} from './reactivity/props.js';
export {
create_stores,
invalidate_store,
mutate_store,
store_get,
store_set,
store_unsub,
unsubscribe_on_destroy,
update_pre_store,
update_store
} from './reactivity/store.js';

@ -104,16 +104,20 @@ export function invalidate_store(stores, store_name) {
/**
* Unsubscribes from all auto-subscribed stores on destroy
* @param {StoreReferencesContainer} stores
* @returns {StoreReferencesContainer}
*/
export function unsubscribe_on_destroy(stores) {
export function create_stores() {
/** @type {StoreReferencesContainer} */
const stores = {};
teardown(() => {
let store_name;
for (store_name in stores) {
for (var store_name in stores) {
const ref = stores[store_name];
ref.unsubscribe();
}
});
return stores;
}
/**

Loading…
Cancel
Save