(store, store_name, stores)
| 34 | * @returns {V} |
| 35 | */ |
| 36 | export function store_get(store, store_name, stores) { |
| 37 | const entry = (stores[store_name] ??= { |
| 38 | store: null, |
| 39 | source: mutable_source(undefined), |
| 40 | unsubscribe: noop |
| 41 | }); |
| 42 | |
| 43 | if (DEV) { |
| 44 | entry.source.label = store_name; |
| 45 | } |
| 46 | |
| 47 | // if the component that setup this is already unmounted we don't want to register a subscription |
| 48 | if (entry.store !== store && !(IS_UNMOUNTED in stores)) { |
| 49 | entry.unsubscribe(); |
| 50 | entry.store = store ?? null; |
| 51 | |
| 52 | if (store == null) { |
| 53 | entry.source.v = undefined; // see synchronous callback comment below |
| 54 | entry.unsubscribe = noop; |
| 55 | } else { |
| 56 | var is_synchronous_callback = true; |
| 57 | |
| 58 | entry.unsubscribe = subscribe_to_store(store, (v) => { |
| 59 | if (is_synchronous_callback) { |
| 60 | // If the first updates to the store value (possibly multiple of them) are synchronously |
| 61 | // inside a derived, we will hit the `state_unsafe_mutation` error if we `set` the value |
| 62 | entry.source.v = v; |
| 63 | } else { |
| 64 | set(entry.source, v); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | is_synchronous_callback = false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // if the component that setup this stores is already unmounted the source will be out of sync |
| 73 | // so we just use the `get` for the stores, less performant but it avoids to create a memory leak |
| 74 | // and it will keep the value consistent |
| 75 | if (store && IS_UNMOUNTED in stores) { |
| 76 | return get_store(store); |
| 77 | } |
| 78 | |
| 79 | return get(entry.source); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Unsubscribe from a store if it's not the same as the one in the store references container. |
nothing calls this directly
no test coverage detected