@datlv-trustshop/shopify-inapp-components 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,347 +0,0 @@
1
- import { DashboardEngine } from "./engine";
2
- /**
3
- * GlobalDashboardManager - Singleton manager for shared SDK state
4
- *
5
- * This manager ensures all DashboardProvider instances share:
6
- * - The same engine instance
7
- * - The same data/cache
8
- * - The same locale state
9
- * - Deduplicated API calls
10
- *
11
- * Multiple providers can mount/unmount independently while maintaining
12
- * a stable, shared runtime.
13
- */
14
- export class GlobalDashboardManager {
15
- constructor() {
16
- this.engine = null;
17
- this.providers = new Map();
18
- this.stateListeners = new Set();
19
- this.globalState = {
20
- data: null,
21
- loading: false,
22
- error: null,
23
- lastFetch: null,
24
- };
25
- this.initPromise = null;
26
- this.lastConfig = null;
27
- this.pendingLocaleRefresh = null;
28
- // Private constructor for singleton
29
- }
30
- static getInstance() {
31
- if (!GlobalDashboardManager.instance) {
32
- GlobalDashboardManager.instance = new GlobalDashboardManager();
33
- }
34
- return GlobalDashboardManager.instance;
35
- }
36
- /**
37
- * Register a provider instance
38
- */
39
- registerProvider(id, config, autoRefreshOnLocaleChange = false) {
40
- this.providers.set(id, {
41
- id,
42
- mounted: true,
43
- config,
44
- autoRefreshOnLocaleChange
45
- });
46
- // Update config if provided and different
47
- if (config && !this.configEquals(this.lastConfig, config)) {
48
- this.updateGlobalConfig(config);
49
- }
50
- // Check if we need to initialize due to pending locale refresh
51
- // This handles the case where all providers have autoInit=false but want locale refresh
52
- if (this.pendingLocaleRefresh && autoRefreshOnLocaleChange) {
53
- const locale = this.pendingLocaleRefresh;
54
- this.pendingLocaleRefresh = null;
55
- // Force initialization with the new locale if not initialized
56
- if (!this.engine || !this.engine.isInitialized()) {
57
- if (this.lastConfig) {
58
- this.init({ ...this.lastConfig, locale }).catch(console.error);
59
- }
60
- }
61
- else {
62
- this.engine.setLocale(locale).catch(console.error);
63
- }
64
- }
65
- }
66
- /**
67
- * Unregister a provider instance
68
- */
69
- unregisterProvider(id) {
70
- this.providers.delete(id);
71
- // Remove associated listeners
72
- this.stateListeners = new Set(Array.from(this.stateListeners).filter(l => !l.id.startsWith(id)));
73
- // Don't destroy engine even if no providers - maintain stable runtime
74
- }
75
- /**
76
- * Get or create the shared engine instance
77
- */
78
- getOrCreateEngine(config) {
79
- if (!this.engine) {
80
- if (!config && !this.lastConfig) {
81
- throw new Error("GlobalDashboardManager requires config on first use");
82
- }
83
- const finalConfig = config || this.lastConfig;
84
- this.engine = DashboardEngine.getInstance(finalConfig);
85
- this.lastConfig = finalConfig;
86
- // Subscribe to engine changes
87
- this.engine.subscribe((data) => {
88
- this.updateGlobalState({
89
- data,
90
- loading: false,
91
- error: null,
92
- lastFetch: data ? new Date() : this.globalState.lastFetch,
93
- });
94
- });
95
- }
96
- return this.engine;
97
- }
98
- /**
99
- * Update the global configuration
100
- */
101
- updateGlobalConfig(config) {
102
- const previousLocale = this.lastConfig?.locale;
103
- this.lastConfig = config;
104
- if (this.engine) {
105
- // Update existing engine config
106
- this.engine.updateConfig(config);
107
- // If locale changed, we need to refresh data
108
- if (config.locale && config.locale !== previousLocale) {
109
- // Check if any provider wants auto-refresh on locale change
110
- const shouldRefresh = Array.from(this.providers.values()).some(p => p.autoRefreshOnLocaleChange);
111
- if (this.engine.isInitialized()) {
112
- // Engine is initialized, refresh immediately if any provider wants it
113
- if (shouldRefresh) {
114
- this.engine.setLocale(config.locale).catch(console.error);
115
- }
116
- else {
117
- this.engine.updateLocale(config.locale);
118
- }
119
- }
120
- else {
121
- // Engine not initialized yet
122
- this.engine.updateLocale(config.locale);
123
- // If any provider wants refresh, force initialization NOW
124
- // This is critical for autoInit=false providers
125
- if (shouldRefresh) {
126
- this.init(config).catch(console.error);
127
- }
128
- else {
129
- // Mark for later refresh when a provider initializes
130
- this.pendingLocaleRefresh = config.locale;
131
- }
132
- }
133
- }
134
- }
135
- else if (config.locale && config.locale !== previousLocale) {
136
- // No engine yet, but locale changed - mark for refresh when engine is created
137
- this.pendingLocaleRefresh = config.locale;
138
- }
139
- }
140
- /**
141
- * Check if two configs are equal
142
- */
143
- configEquals(a, b) {
144
- if (!a || !b)
145
- return a === b;
146
- return (a.apiUrl === b.apiUrl &&
147
- a.locale === b.locale &&
148
- a.cacheTime === b.cacheTime &&
149
- a.retryAttempts === b.retryAttempts &&
150
- a.retryDelay === b.retryDelay);
151
- }
152
- /**
153
- * Initialize the shared engine
154
- */
155
- async init(config) {
156
- // If we have a pending locale refresh and config has a different locale,
157
- // use the pending locale instead
158
- if (this.pendingLocaleRefresh && config) {
159
- config = { ...config, locale: this.pendingLocaleRefresh };
160
- }
161
- if (this.initPromise && !config) {
162
- return this.initPromise;
163
- }
164
- if (config) {
165
- this.updateGlobalConfig(config);
166
- }
167
- this.updateGlobalState({ ...this.globalState, loading: true, error: null });
168
- this.initPromise = (async () => {
169
- try {
170
- const engine = this.getOrCreateEngine(config);
171
- await engine.init();
172
- const data = engine.getData();
173
- // Clear pending locale refresh since we just initialized with it
174
- if (this.pendingLocaleRefresh) {
175
- this.pendingLocaleRefresh = null;
176
- }
177
- this.updateGlobalState({
178
- data,
179
- loading: false,
180
- error: null,
181
- lastFetch: new Date(),
182
- });
183
- }
184
- catch (error) {
185
- const err = error instanceof Error
186
- ? error
187
- : new Error("Failed to initialize dashboard");
188
- this.updateGlobalState({
189
- ...this.globalState,
190
- loading: false,
191
- error: err,
192
- });
193
- throw err;
194
- }
195
- })();
196
- return this.initPromise;
197
- }
198
- /**
199
- * Refresh the dashboard data
200
- */
201
- async refresh() {
202
- if (!this.engine) {
203
- return this.init();
204
- }
205
- this.updateGlobalState({ ...this.globalState, loading: true, error: null });
206
- try {
207
- await this.engine.refresh();
208
- const data = this.engine.getData();
209
- this.updateGlobalState({
210
- data,
211
- loading: false,
212
- error: null,
213
- lastFetch: new Date(),
214
- });
215
- }
216
- catch (error) {
217
- const err = error instanceof Error
218
- ? error
219
- : new Error("Failed to refresh dashboard");
220
- this.updateGlobalState({
221
- ...this.globalState,
222
- loading: false,
223
- error: err,
224
- });
225
- throw err;
226
- }
227
- }
228
- /**
229
- * Update locale globally
230
- */
231
- async setLocale(locale) {
232
- // Update the last config
233
- if (this.lastConfig) {
234
- this.lastConfig = { ...this.lastConfig, locale };
235
- }
236
- // Check if any provider wants auto-refresh
237
- const shouldRefresh = Array.from(this.providers.values()).some(p => p.autoRefreshOnLocaleChange);
238
- if (!this.engine) {
239
- // No engine yet, but if any provider wants refresh, initialize NOW
240
- if (shouldRefresh && this.lastConfig) {
241
- await this.init({ ...this.lastConfig, locale });
242
- }
243
- else {
244
- // Mark locale for refresh when engine is created
245
- this.pendingLocaleRefresh = locale;
246
- }
247
- return;
248
- }
249
- // Engine exists, update locale
250
- if (this.engine.isInitialized()) {
251
- await this.engine.setLocale(locale);
252
- }
253
- else {
254
- // Engine exists but not initialized
255
- this.engine.updateLocale(locale);
256
- // If any provider wants refresh, initialize now
257
- if (shouldRefresh) {
258
- await this.init();
259
- }
260
- }
261
- }
262
- /**
263
- * Get the current locale
264
- */
265
- getCurrentLocale() {
266
- if (this.engine) {
267
- return this.engine.getCurrentLocale();
268
- }
269
- return this.lastConfig?.locale || "en";
270
- }
271
- /**
272
- * Subscribe to global state changes
273
- */
274
- subscribe(id, callback) {
275
- const listener = { id, callback };
276
- this.stateListeners.add(listener);
277
- // Immediately call with current state
278
- callback(this.globalState);
279
- // Return unsubscribe function
280
- return () => {
281
- this.stateListeners.delete(listener);
282
- };
283
- }
284
- /**
285
- * Update and broadcast global state
286
- */
287
- updateGlobalState(state) {
288
- this.globalState = state;
289
- // Notify all listeners
290
- this.stateListeners.forEach(listener => {
291
- try {
292
- listener.callback(state);
293
- }
294
- catch (error) {
295
- console.error(`Error in state listener ${listener.id}:`, error);
296
- }
297
- });
298
- }
299
- /**
300
- * Get the current global state
301
- */
302
- getState() {
303
- return this.globalState;
304
- }
305
- /**
306
- * Get the shared engine instance (if created)
307
- */
308
- getEngine() {
309
- return this.engine;
310
- }
311
- /**
312
- * Check if manager is initialized
313
- */
314
- isInitialized() {
315
- return this.engine?.isInitialized() || false;
316
- }
317
- /**
318
- * Get provider count
319
- */
320
- getProviderCount() {
321
- return this.providers.size;
322
- }
323
- /**
324
- * Reset the global manager (for testing)
325
- */
326
- static reset() {
327
- if (GlobalDashboardManager.instance) {
328
- const manager = GlobalDashboardManager.instance;
329
- // Clear listeners
330
- manager.stateListeners.clear();
331
- manager.providers.clear();
332
- // Reset state
333
- manager.globalState = {
334
- data: null,
335
- loading: false,
336
- error: null,
337
- lastFetch: null,
338
- };
339
- manager.initPromise = null;
340
- manager.lastConfig = null;
341
- manager.engine = null;
342
- }
343
- GlobalDashboardManager.instance = null;
344
- }
345
- }
346
- GlobalDashboardManager.instance = null;
347
- //# sourceMappingURL=global-manager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"global-manager.js","sourceRoot":"","sources":["../../src/core/global-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAe3C;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,sBAAsB;IAejC;QAbQ,WAAM,GAA2B,IAAI,CAAC;QACtC,cAAS,GAA8B,IAAI,GAAG,EAAE,CAAC;QACjD,mBAAc,GAA6B,IAAI,GAAG,EAAE,CAAC;QACrD,gBAAW,GAAmB;YACpC,IAAI,EAAE,IAAI;YACV,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,IAAI;YACX,SAAS,EAAE,IAAI;SAChB,CAAC;QACM,gBAAW,GAAyB,IAAI,CAAC;QACzC,eAAU,GAA2B,IAAI,CAAC;QAC1C,yBAAoB,GAAkB,IAAI,CAAC;QAGjD,oCAAoC;IACtC,CAAC;IAEM,MAAM,CAAC,WAAW;QACvB,IAAI,CAAC,sBAAsB,CAAC,QAAQ,EAAE,CAAC;YACrC,sBAAsB,CAAC,QAAQ,GAAG,IAAI,sBAAsB,EAAE,CAAC;QACjE,CAA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AS;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;OAEG;IACI,aAAa;QAClB,OAAO,IAAI,CAAC,MAAM,EAAE,aAAa,EAAE,IAAI,KAAK,CAAC;IAC/C,CAAC;IAED;;OAEG;IACI,gBAAgB;QACrB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;IAC7B,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,KAAK;QACjB,IAAI,sBAAsB,CAAC,QAAQ,EAAE,CAAC;YACpC,MAAM,OAAO,GAAG,sBAAsB,CAAC,QAAQ,CAAC;YAEhD,kBAAkB;YAClB,OAAO,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC;YAC/B,OAAO,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;YAE1B,cAAc;YACd,OAAO,CAAC,WAAW,GAAG;gBACpB,IAAI,EAAE,IAAI;gBACV,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,IAAI;gBACX,SAAS,EAAE,IAAI;aAChB,CAAC;YAEF,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;YAC3B,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;YAC1B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;QACxB,CAAC;QAED,sBAAsB,CAAC,QAAQ,GAAG,IAAI,CAAC;IACzC,CAAC;;AAxXc,+BAAQ,GAAkC,IAAI,AAAtC,CAAuC","sourcesContent":["import { DashboardEngine } from \"./engine\";\nimport { DashboardConfig, DashboardState } from \"../types\";\n\ninterface ProviderInfo {\n id: string;\n mounted: boolean;\n config?: DashboardConfig;\n autoRefreshOnLocaleChange?: boolean;\n}\n\ninterface GlobalStateListener {\n id: string;\n callback: (state: DashboardState) => void;\n}\n\n/**\n * GlobalDashboardManager - Singleton manager for shared SDK state\n * \n * This manager ensures all DashboardProvider instances share:\n * - The same engine instance\n * - The same data/cache\n * - The same locale state\n * - Deduplicated API calls\n * \n * Multiple providers can mount/unmount independently while maintaining\n * a stable, shared runtime.\n */\nexport class GlobalDashboardManager {\n private static instance: GlobalDashboardManager | null = null;\n private engine: DashboardEngine | null = null;\n private providers: Map<string, ProviderInfo> = new Map();\n private stateListeners: Set<GlobalStateListener> = new Set();\n private globalState: DashboardState = {\n data: null,\n loading: false,\n error: null,\n lastFetch: null,\n };\n private initPromise: Promise<void> | null = null;\n private lastConfig: DashboardConfig | null = null;\n private pendingLocaleRefresh: string | null = null;\n\n private constructor() {\n // Private constructor for singleton\n }\n\n public static getInstance(): GlobalDashboardManager {\n if (!GlobalDashboardManager.instance) {\n GlobalDashboardManager.instance = new GlobalDashboardManager();\n }\n return GlobalDashboardManager.instance;\n }\n\n /**\n * Register a provider instance\n */\n public registerProvider(id: string, config?: DashboardConfig, autoRefreshOnLocaleChange: boolean = false): void {\n this.providers.set(id, { \n id, \n mounted: true, \n config,\n autoRefreshOnLocaleChange\n });\n\n // Update config if provided and different\n if (config && !this.configEquals(this.lastConfig, config)) {\n this.updateGlobalConfig(config);\n }\n\n // Check if we need to initialize due to pending locale refresh\n // This handles the case where all providers have autoInit=false but want locale refresh\n if (this.pendingLocaleRefresh && autoRefreshOnLocaleChange) {\n const locale = this.pendingLocaleRefresh;\n this.pendingLocaleRefresh = null;\n \n // Force initialization with the new locale if not initialized\n if (!this.engine || !this.engine.isInitialized()) {\n if (this.lastConfig) {\n this.init({ ...this.lastConfig, locale }).catch(console.error);\n }\n } else {\n this.engine.setLocale(locale).catch(console.error);\n }\n }\n }\n\n /**\n * Unregister a provider instance\n */\n public unregisterProvider(id: string): void {\n this.providers.delete(id);\n \n // Remove associated listeners\n this.stateListeners = new Set(\n Array.from(this.stateListeners).filter(l => !l.id.startsWith(id))\n );\n\n // Don't destroy engine even if no providers - maintain stable runtime\n }\n\n /**\n * Get or create the shared engine instance\n */\n private getOrCreateEngine(config?: DashboardConfig): DashboardEngine {\n if (!this.engine) {\n if (!config && !this.lastConfig) {\n throw new Error(\"GlobalDashboardManager requires config on first use\");\n }\n const finalConfig = config || this.lastConfig!;\n this.engine = DashboardEngine.getInstance(finalConfig);\n this.lastConfig = finalConfig;\n \n // Subscribe to engine changes\n this.engine.subscribe((data) => {\n this.updateGlobalState({\n data,\n loading: false,\n error: null,\n lastFetch: data ? new Date() : this.globalState.lastFetch,\n });\n });\n }\n return this.engine;\n }\n\n /**\n * Update the global configuration\n */\n private updateGlobalConfig(config: DashboardConfig): void {\n const previousLocale = this.lastConfig?.locale;\n this.lastConfig = config;\n \n if (this.engine) {\n // Update existing engine config\n this.engine.updateConfig(config);\n \n // If locale changed, we need to refresh data\n if (config.locale && config.locale !== previousLocale) {\n // Check if any provider wants auto-refresh on locale change\n const shouldRefresh = Array.from(this.providers.values()).some(p => p.autoRefreshOnLocaleChange);\n \n if (this.engine.isInitialized()) {\n // Engine is initialized, refresh immediately if any provider wants it\n if (shouldRefresh) {\n this.engine.setLocale(config.locale).catch(console.error);\n } else {\n this.engine.updateLocale(config.locale);\n }\n } else {\n // Engine not initialized yet\n this.engine.updateLocale(config.locale);\n \n // If any provider wants refresh, force initialization NOW\n // This is critical for autoInit=false providers\n if (shouldRefresh) {\n this.init(config).catch(console.error);\n } else {\n // Mark for later refresh when a provider initializes\n this.pendingLocaleRefresh = config.locale;\n }\n }\n }\n } else if (config.locale && config.locale !== previousLocale) {\n // No engine yet, but locale changed - mark for refresh when engine is created\n this.pendingLocaleRefresh = config.locale;\n }\n }\n\n /**\n * Check if two configs are equal\n */\n private configEquals(a: DashboardConfig | null, b: DashboardConfig | null): boolean {\n if (!a || !b) return a === b;\n return (\n a.apiUrl === b.apiUrl &&\n a.locale === b.locale &&\n a.cacheTime === b.cacheTime &&\n a.retryAttempts === b.retryAttempts &&\n a.retryDelay === b.retryDelay\n );\n }\n\n /**\n * Initialize the shared engine\n */\n public async init(config?: DashboardConfig): Promise<void> {\n // If we have a pending locale refresh and config has a different locale,\n // use the pending locale instead\n if (this.pendingLocaleRefresh && config) {\n config = { ...config, locale: this.pendingLocaleRefresh };\n }\n \n if (this.initPromise && !config) {\n return this.initPromise;\n }\n\n if (config) {\n this.updateGlobalConfig(config);\n }\n\n this.updateGlobalState({ ...this.globalState, loading: true, error: null });\n\n this.initPromise = (async () => {\n try {\n const engine = this.getOrCreateEngine(config);\n await engine.init();\n const data = engine.getData();\n \n // Clear pending locale refresh since we just initialized with it\n if (this.pendingLocaleRefresh) {\n this.pendingLocaleRefresh = null;\n }\n \n this.updateGlobalState({\n data,\n loading: false,\n error: null,\n lastFetch: new Date(),\n });\n } catch (error) {\n const err = error instanceof Error \n ? error \n : new Error(\"Failed to initialize dashboard\");\n \n this.updateGlobalState({\n ...this.globalState,\n loading: false,\n error: err,\n });\n \n throw err;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Refresh the dashboard data\n */\n public async refresh(): Promise<void> {\n if (!this.engine) {\n return this.init();\n }\n\n this.updateGlobalState({ ...this.globalState, loading: true, error: null });\n\n try {\n await this.engine.refresh();\n const data = this.engine.getData();\n \n this.updateGlobalState({\n data,\n loading: false,\n error: null,\n lastFetch: new Date(),\n });\n } catch (error) {\n const err = error instanceof Error \n ? error \n : new Error(\"Failed to refresh dashboard\");\n \n this.updateGlobalState({\n ...this.globalState,\n loading: false,\n error: err,\n });\n \n throw err;\n }\n }\n\n /**\n * Update locale globally\n */\n public async setLocale(locale: string): Promise<void> {\n // Update the last config\n if (this.lastConfig) {\n this.lastConfig = { ...this.lastConfig, locale };\n }\n \n // Check if any provider wants auto-refresh\n const shouldRefresh = Array.from(this.providers.values()).some(p => p.autoRefreshOnLocaleChange);\n \n if (!this.engine) {\n // No engine yet, but if any provider wants refresh, initialize NOW\n if (shouldRefresh && this.lastConfig) {\n await this.init({ ...this.lastConfig, locale });\n } else {\n // Mark locale for refresh when engine is created\n this.pendingLocaleRefresh = locale;\n }\n return;\n }\n\n // Engine exists, update locale\n if (this.engine.isInitialized()) {\n await this.engine.setLocale(locale);\n } else {\n // Engine exists but not initialized\n this.engine.updateLocale(locale);\n \n // If any provider wants refresh, initialize now\n if (shouldRefresh) {\n await this.init();\n }\n }\n }\n\n /**\n * Get the current locale\n */\n public getCurrentLocale(): string {\n if (this.engine) {\n return this.engine.getCurrentLocale();\n }\n return this.lastConfig?.locale || \"en\";\n }\n\n /**\n * Subscribe to global state changes\n */\n public subscribe(id: string, callback: (state: DashboardState) => void): () => void {\n const listener = { id, callback };\n this.stateListeners.add(listener);\n \n // Immediately call with current state\n callback(this.globalState);\n \n // Return unsubscribe function\n return () => {\n this.stateListeners.delete(listener);\n };\n }\n\n /**\n * Update and broadcast global state\n */\n private updateGlobalState(state: DashboardState): void {\n this.globalState = state;\n \n // Notify all listeners\n this.stateListeners.forEach(listener => {\n try {\n listener.callback(state);\n } catch (error) {\n console.error(`Error in state listener ${listener.id}:`, error);\n }\n });\n }\n\n /**\n * Get the current global state\n */\n public getState(): DashboardState {\n return this.globalState;\n }\n\n /**\n * Get the shared engine instance (if created)\n */\n public getEngine(): DashboardEngine | null {\n return this.engine;\n }\n\n /**\n * Check if manager is initialized\n */\n public isInitialized(): boolean {\n return this.engine?.isInitialized() || false;\n }\n\n /**\n * Get provider count\n */\n public getProviderCount(): number {\n return this.providers.size;\n }\n\n /**\n * Reset the global manager (for testing)\n */\n public static reset(): void {\n if (GlobalDashboardManager.instance) {\n const manager = GlobalDashboardManager.instance;\n \n // Clear listeners\n manager.stateListeners.clear();\n manager.providers.clear();\n \n // Reset state\n manager.globalState = {\n data: null,\n loading: false,\n error: null,\n lastFetch: null,\n };\n \n manager.initPromise = null;\n manager.lastConfig = null;\n manager.engine = null;\n }\n \n GlobalDashboardManager.instance = null;\n }\n}"]}