@@ -4,19 +4,29 @@ import {ensureNonEmptyString, log, random} from "./lib/utils";
44
55export const staticStores : SharedCreated [ ] = [ ] ;
66
7+ const MANAGER_KEY = Symbol . for ( "react-shared-states.manager" ) ;
8+
9+ function getManager < T > ( instanceKey : string , defaultValue : ( ) => T = ( ) => null as T ) {
10+ const g = globalThis as any ;
11+ if ( ! g [ MANAGER_KEY ] ) {
12+ g [ MANAGER_KEY ] = { } ;
13+ }
14+ if ( ! g [ MANAGER_KEY ] [ instanceKey ] ) {
15+ g [ MANAGER_KEY ] [ instanceKey ] = new SharedValuesManager < T > ( defaultValue ) ;
16+ }
17+ return g [ MANAGER_KEY ] [ instanceKey ] as SharedValuesManager < T > ;
18+ }
19+
720export class SharedValuesManager < T > {
821 data = new Map < string , SharedValue < T > > ( ) ;
922
1023 static INSTANCES = new Map < string , SharedValuesManager < any > > ( ) ;
1124
12- private constructor ( protected defaultValue : ( ) => T = ( ) => null as T ) {
25+ constructor ( protected defaultValue : ( ) => T = ( ) => null as T ) {
1326 }
1427
15- static getInstance < T > ( instanceKey : string ) : SharedValuesManager < T > {
16- if ( ! SharedValuesManager . INSTANCES . has ( instanceKey ) ) {
17- SharedValuesManager . INSTANCES . set ( instanceKey , new SharedValuesManager ( ) ) ;
18- }
19- return SharedValuesManager . INSTANCES . get ( instanceKey ) as SharedValuesManager < T > ;
28+ static getInstance < T > ( instanceKey : string , defaultValue : ( ) => T = ( ) => null as T ) : SharedValuesManager < T > {
29+ return getManager ( instanceKey , defaultValue ) ;
2030 }
2131
2232 addListener ( key : string , prefix : Prefix , listener : AFunction ) {
0 commit comments