@ -1,10 +1,10 @@
/** @import { StoreReferencesContainer , Source } from '#client' */
/** @import { StoreReferencesContainer } from '#client' */
/** @import { Store } from '#shared' */
import { subscribe _to _store } from '../../../store/utils.js' ;
import { noop } from '../../shared/utils.js' ;
import { UNINITIALIZED } from '../../../constants.js' ;
import { get , untrack } from '../runtime.js' ;
import { effect } from './effects.js' ;
import { get } from '../runtime.js' ;
import { teardown } from './effects.js' ;
import { mutable _source , set } from './sources.js' ;
/ * *
@ -18,30 +18,25 @@ import { mutable_source, set } from './sources.js';
* @ returns { V }
* /
export function store _get ( store , store _name , stores ) {
/** @type {StoreReferencesContainer[''] | undefined} */
let entry = stores [ store _name ] ;
const is _new = entry === undefined ;
if ( is _new ) {
entry = {
const entry = ( stores [ store _name ] ? ? = {
store : null ,
last _value : null ,
value : mutable _source ( UNINITIALIZED ) ,
source : mutable _source ( UNINITIALIZED ) ,
unsubscribe : noop
} ;
stores [ store _name ] = entry ;
}
} ) ;
if ( is_new || entry. store !== store ) {
if ( entry . store !== store ) {
entry . unsubscribe ( ) ;
entry . store = store ? ? null ;
entry . unsubscribe = connect _store _to _signal ( store , entry . value ) ;
if ( store == null ) {
set ( entry . source , undefined ) ;
entry . unsubscribe = noop ;
} else {
entry . unsubscribe = subscribe _to _store ( store , ( v ) => set ( entry . source , v ) ) ;
}
}
const value = get ( entry . value ) ;
// This could happen if the store was cleaned up because the component was destroyed and there's a leak on the user side.
// In that case we don't want to fail with a cryptic Symbol error, but rather return the last value we got.
return value === UNINITIALIZED ? entry . last _value : value ;
return get ( entry . source ) ;
}
/ * *
@ -65,20 +60,6 @@ export function store_unsub(store, store_name, stores) {
return store ;
}
/ * *
* @ template V
* @ param { Store < V > | null | undefined } store
* @ param { Source < V > } source
* /
function connect _store _to _signal ( store , source ) {
if ( store == null ) {
set ( source , undefined ) ;
return noop ;
}
return subscribe _to _store ( store , ( v ) => set ( source , v ) ) ;
}
/ * *
* Sets the new value of a store and returns that value .
* @ template V
@ -96,24 +77,28 @@ export function store_set(store, value) {
* @ param { string } store _name
* /
export function invalidate _store ( stores , store _name ) {
const store = stores [ store _name ] ;
if ( store. store ) {
store _set ( store. store , store . valu e. v ) ;
var entry = stores [ store _name ] ;
if ( entry. store !== null ) {
store _set ( entry. store , entry . sourc e. v ) ;
}
}
/ * *
* Unsubscribes from all auto - subscribed stores on destroy
* @ param { StoreReferencesContainer } stores
* @ returns { StoreReferencesContainer }
* /
export function unsubscribe _on _destroy ( stores ) {
on _destroy ( ( ) => {
let store _name ;
for ( store _name in stores ) {
export function setup _stores ( ) {
/** @type {StoreReferencesContainer} */
const stores = { } ;
teardown ( ( ) => {
for ( var store _name in stores ) {
const ref = stores [ store _name ] ;
ref . unsubscribe ( ) ;
}
} ) ;
return stores ;
}
/ * *
@ -150,12 +135,3 @@ export function update_pre_store(store, store_value, d = 1) {
store . set ( value ) ;
return value ;
}
/ * *
* Schedules a callback to run immediately before the component is unmounted .
* @ param { ( ) => any } fn
* @ returns { void }
* /
function on _destroy ( fn ) {
effect ( ( ) => ( ) => untrack ( fn ) ) ;
}