11import type { AFunction , DataMapValue , Prefix } from "./types" ;
22import { useEffect } from "react" ;
3- import { log } from "./lib/utils" ;
3+ import { ensureNonEmptyString , log } from "./lib/utils" ;
44
55
66type SharedDataType < T > = DataMapValue & T ;
@@ -79,7 +79,12 @@ export abstract class SharedData<T> {
7979 }
8080
8181 static prefix ( key : string , prefix : Prefix ) {
82- return `${ prefix } _${ key } ` ;
82+ if ( key . includes ( "//" ) ) throw new Error ( "key cannot contain '//'" ) ;
83+ return `${ prefix } //${ key } ` ;
84+ }
85+
86+ static extractPrefix ( mapKey : string ) {
87+ return mapKey . split ( "//" ) ;
8388 }
8489
8590 useEffect ( key : string , prefix : Prefix , unsub : ( ( ) => void ) | null = null ) {
@@ -95,11 +100,85 @@ export abstract class SharedData<T> {
95100 }
96101}
97102
98- export interface SharedApi < T > {
99- get : < S extends string = string > ( key : S , scopeName : Prefix ) => T ;
100- set : < S extends string = string > ( key : S , value : T , scopeName : Prefix ) => void ;
101- clearAll : ( ) => void ;
102- clear : ( key : string , scopeName : Prefix ) => void ;
103- has : ( key : string , scopeName : Prefix ) => boolean ;
104- getAll : ( ) => Map < string , DataMapValue > ;
103+ // noinspection JSUnusedGlobalSymbols
104+ export class SharedApi < T > {
105+ constructor ( private sharedData : SharedData < T > ) { }
106+
107+ /**
108+ * get a value from the shared data
109+ * @param key
110+ * @param scopeName
111+ */
112+ get < S extends string = string > ( key : S , scopeName : Prefix ) {
113+ key = ensureNonEmptyString ( key ) ;
114+ const prefix : Prefix = scopeName || "_global" ;
115+ return this . sharedData . get ( key , prefix ) as T ;
116+ }
117+
118+ /**
119+ * set a value in the shared data
120+ * @param key
121+ * @param value
122+ * @param scopeName
123+ */
124+ set < S extends string = string > ( key : S , value : T , scopeName : Prefix ) {
125+ key = ensureNonEmptyString ( key ) ;
126+ const prefix : Prefix = scopeName || "_global" ;
127+ this . sharedData . setValue ( key , prefix , value ) ;
128+ }
129+
130+ /**
131+ * clear all values from the shared data
132+ */
133+ clearAll ( ) {
134+ this . sharedData . clearAll ( ) ;
135+ }
136+
137+ /**
138+ * clear all values from the shared data in a scope
139+ * @param scopeName
140+ */
141+ clearScope ( scopeName ?: Prefix ) {
142+ const prefixToSearch : Prefix = scopeName || "_global" ;
143+ this . sharedData . data . forEach ( ( _ , key ) => {
144+ const [ prefix ] = SharedData . extractPrefix ( key ) ;
145+ if ( prefix === prefixToSearch ) {
146+ this . sharedData . clear ( key , prefix ) ;
147+ return ;
148+ }
149+ } ) ;
150+ }
151+
152+ /**
153+ * clear a value from the shared data
154+ * @param key
155+ * @param scopeName
156+ */
157+ clear ( key : string , scopeName : Prefix ) {
158+ const prefix : Prefix = scopeName || "_global" ;
159+ this . sharedData . clear ( key , prefix ) ;
160+ }
161+
162+ /**
163+ * check if a value exists in the shared data
164+ * @param key
165+ * @param scopeName
166+ */
167+ has ( key : string , scopeName : Prefix = "_global" ) {
168+ const prefix : Prefix = scopeName || "_global" ;
169+ return Boolean ( this . sharedData . has ( key , prefix ) ) ;
170+ }
171+
172+ /**
173+ * get all values from the shared data
174+ */
175+ getAll ( ) {
176+ const all : Record < string , Record < string , any > > = { } ;
177+ this . sharedData . data . forEach ( ( value , key ) => {
178+ const [ prefix , keyWithoutPrefix ] = SharedData . extractPrefix ( key ) ;
179+ all [ prefix ] = all [ prefix ] || { } ;
180+ all [ prefix ] [ keyWithoutPrefix ] = value ;
181+ } ) ;
182+ return all ;
183+ }
105184}
0 commit comments