@ -1,10 +1,10 @@
/** @import { StoreReferencesContainer , Source } from '#client' */
/** @import { StoreReferencesContainer } from '#client' */
/** @import { Store } from '#shared' */
/** @import { Store } from '#shared' */
import { subscribe _to _store } from '../../../store/utils.js' ;
import { subscribe _to _store } from '../../../store/utils.js' ;
import { noop } from '../../shared/utils.js' ;
import { noop } from '../../shared/utils.js' ;
import { UNINITIALIZED } from '../../../constants.js' ;
import { UNINITIALIZED } from '../../../constants.js' ;
import { get , untrack } from '../runtime.js' ;
import { get } from '../runtime.js' ;
import { effect } from './effects.js' ;
import { teardown } from './effects.js' ;
import { mutable _source , set } from './sources.js' ;
import { mutable _source , set } from './sources.js' ;
/ * *
/ * *
@ -18,30 +18,25 @@ import { mutable_source, set } from './sources.js';
* @ returns { V }
* @ returns { V }
* /
* /
export function store _get ( store , store _name , stores ) {
export function store _get ( store , store _name , stores ) {
/** @type {StoreReferencesContainer[''] | undefined} */
const entry = ( stores [ store _name ] ? ? = {
let entry = stores [ store _name ] ;
const is _new = entry === undefined ;
if ( is _new ) {
entry = {
store : null ,
store : null ,
last _value : null ,
source : mutable _source ( UNINITIALIZED ) ,
value : mutable _source ( UNINITIALIZED ) ,
unsubscribe : noop
unsubscribe : noop
} ;
} ) ;
stores [ store _name ] = entry ;
}
if ( is_new || entry. store !== store ) {
if ( entry . store !== store ) {
entry . unsubscribe ( ) ;
entry . unsubscribe ( ) ;
entry . store = store ? ? null ;
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 ) ;
return get ( entry . source ) ;
// 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 ;
}
}
/ * *
/ * *
@ -65,20 +60,6 @@ export function store_unsub(store, store_name, stores) {
return store ;
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 .
* Sets the new value of a store and returns that value .
* @ template V
* @ template V
@ -96,24 +77,28 @@ export function store_set(store, value) {
* @ param { string } store _name
* @ param { string } store _name
* /
* /
export function invalidate _store ( stores , store _name ) {
export function invalidate _store ( stores , store _name ) {
const store = stores [ store _name ] ;
var entry = stores [ store _name ] ;
if ( store. store ) {
if ( entry. store !== null ) {
store _set ( store. store , store . valu e. v ) ;
store _set ( entry. store , entry . sourc e. v ) ;
}
}
}
}
/ * *
/ * *
* Unsubscribes from all auto - subscribed stores on destroy
* Unsubscribes from all auto - subscribed stores on destroy
* @ param { StoreReferencesContainer } stores
* @ returns { StoreReferencesContainer }
* /
* /
export function unsubscribe _on _destroy ( stores ) {
export function setup _stores ( ) {
on _destroy ( ( ) => {
/** @type {StoreReferencesContainer} */
let store _name ;
const stores = { } ;
for ( store _name in stores ) {
teardown ( ( ) => {
for ( var store _name in stores ) {
const ref = stores [ store _name ] ;
const ref = stores [ store _name ] ;
ref . unsubscribe ( ) ;
ref . unsubscribe ( ) ;
}
}
} ) ;
} ) ;
return stores ;
}
}
/ * *
/ * *
@ -150,12 +135,3 @@ export function update_pre_store(store, store_value, d = 1) {
store . set ( value ) ;
store . set ( value ) ;
return 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 ) ) ;
}