@datlv-trustshop/shopify-inapp-components 0.1.10 → 0.1.11

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.
@@ -19,15 +19,14 @@ export declare class GlobalDashboardManager {
19
19
  private stateListeners;
20
20
  private globalState;
21
21
  private initPromise;
22
- private autoInitTimer;
23
22
  private lastConfig;
24
- private isInitializing;
23
+ private pendingLocaleRefresh;
25
24
  private constructor();
26
25
  static getInstance(): GlobalDashboardManager;
27
26
  /**
28
27
  * Register a provider instance
29
28
  */
30
- registerProvider(id: string, config?: DashboardConfig): void;
29
+ registerProvider(id: string, config?: DashboardConfig, autoRefreshOnLocaleChange?: boolean): void;
31
30
  /**
32
31
  * Unregister a provider instance
33
32
  */
@@ -44,14 +43,6 @@ export declare class GlobalDashboardManager {
44
43
  * Check if two configs are equal
45
44
  */
46
45
  private configEquals;
47
- /**
48
- * Schedule auto-initialization
49
- */
50
- private scheduleAutoInit;
51
- /**
52
- * Auto-initialize the engine
53
- */
54
- private autoInit;
55
46
  /**
56
47
  * Initialize the shared engine
57
48
  */
@@ -23,9 +23,8 @@ export class GlobalDashboardManager {
23
23
  lastFetch: null,
24
24
  };
25
25
  this.initPromise = null;
26
- this.autoInitTimer = null;
27
26
  this.lastConfig = null;
28
- this.isInitializing = false;
27
+ this.pendingLocaleRefresh = null;
29
28
  // Private constructor for singleton
30
29
  }
31
30
  static getInstance() {
@@ -37,19 +36,22 @@ export class GlobalDashboardManager {
37
36
  /**
38
37
  * Register a provider instance
39
38
  */
40
- registerProvider(id, config) {
39
+ registerProvider(id, config, autoRefreshOnLocaleChange = false) {
41
40
  this.providers.set(id, {
42
41
  id,
43
42
  mounted: true,
44
- config
43
+ config,
44
+ autoRefreshOnLocaleChange
45
45
  });
46
46
  // Update config if provided and different
47
47
  if (config && !this.configEquals(this.lastConfig, config)) {
48
48
  this.updateGlobalConfig(config);
49
49
  }
50
- // Auto-initialize if this is the first provider
51
- if (this.providers.size === 1 && config) {
52
- this.scheduleAutoInit();
50
+ // Check if we have a pending locale refresh
51
+ if (this.pendingLocaleRefresh && this.engine?.isInitialized()) {
52
+ const locale = this.pendingLocaleRefresh;
53
+ this.pendingLocaleRefresh = null;
54
+ this.engine.setLocale(locale).catch(console.error);
53
55
  }
54
56
  }
55
57
  /**
@@ -88,21 +90,39 @@ export class GlobalDashboardManager {
88
90
  * Update the global configuration
89
91
  */
90
92
  updateGlobalConfig(config) {
93
+ const previousLocale = this.lastConfig?.locale;
91
94
  this.lastConfig = config;
92
95
  if (this.engine) {
93
96
  // Update existing engine config
94
97
  this.engine.updateConfig(config);
95
- // If locale changed and engine is initialized, refresh
96
- if (config.locale && config.locale !== this.engine.getCurrentLocale()) {
97
- const wasInitialized = this.engine.isInitialized();
98
- if (wasInitialized) {
99
- this.engine.setLocale(config.locale).catch(console.error);
98
+ // If locale changed, we need to refresh data
99
+ if (config.locale && config.locale !== previousLocale) {
100
+ // Check if any provider wants auto-refresh on locale change
101
+ const shouldRefresh = Array.from(this.providers.values()).some(p => p.autoRefreshOnLocaleChange);
102
+ if (this.engine.isInitialized()) {
103
+ // Engine is initialized, refresh immediately if any provider wants it
104
+ if (shouldRefresh) {
105
+ this.engine.setLocale(config.locale).catch(console.error);
106
+ }
107
+ else {
108
+ this.engine.updateLocale(config.locale);
109
+ }
100
110
  }
101
111
  else {
112
+ // Engine not initialized yet, update locale and mark for refresh
102
113
  this.engine.updateLocale(config.locale);
114
+ this.pendingLocaleRefresh = config.locale;
115
+ // If any provider wants refresh and we have providers, force initialization
116
+ if (shouldRefresh && this.providers.size > 0) {
117
+ this.init(config).catch(console.error);
118
+ }
103
119
  }
104
120
  }
105
121
  }
122
+ else if (config.locale && config.locale !== previousLocale) {
123
+ // No engine yet, but locale changed - mark for refresh when engine is created
124
+ this.pendingLocaleRefresh = config.locale;
125
+ }
106
126
  }
107
127
  /**
108
128
  * Check if two configs are equal
@@ -116,47 +136,31 @@ export class GlobalDashboardManager {
116
136
  a.retryAttempts === b.retryAttempts &&
117
137
  a.retryDelay === b.retryDelay);
118
138
  }
119
- /**
120
- * Schedule auto-initialization
121
- */
122
- scheduleAutoInit() {
123
- if (this.autoInitTimer) {
124
- clearTimeout(this.autoInitTimer);
125
- }
126
- // Delay slightly to allow all providers to mount
127
- this.autoInitTimer = setTimeout(() => {
128
- this.autoInit().catch(console.error);
129
- }, 10);
130
- }
131
- /**
132
- * Auto-initialize the engine
133
- */
134
- async autoInit() {
135
- if (!this.lastConfig || this.isInitializing)
136
- return;
137
- const engine = this.getOrCreateEngine();
138
- // Only init if not already initialized
139
- if (!engine.isInitialized() && !this.initPromise) {
140
- await this.init();
141
- }
142
- }
143
139
  /**
144
140
  * Initialize the shared engine
145
141
  */
146
142
  async init(config) {
143
+ // If we have a pending locale refresh and config has a different locale,
144
+ // use the pending locale instead
145
+ if (this.pendingLocaleRefresh && config) {
146
+ config = { ...config, locale: this.pendingLocaleRefresh };
147
+ }
147
148
  if (this.initPromise && !config) {
148
149
  return this.initPromise;
149
150
  }
150
151
  if (config) {
151
152
  this.updateGlobalConfig(config);
152
153
  }
153
- this.isInitializing = true;
154
154
  this.updateGlobalState({ ...this.globalState, loading: true, error: null });
155
155
  this.initPromise = (async () => {
156
156
  try {
157
157
  const engine = this.getOrCreateEngine(config);
158
158
  await engine.init();
159
159
  const data = engine.getData();
160
+ // Clear pending locale refresh since we just initialized with it
161
+ if (this.pendingLocaleRefresh) {
162
+ this.pendingLocaleRefresh = null;
163
+ }
160
164
  this.updateGlobalState({
161
165
  data,
162
166
  loading: false,
@@ -175,9 +179,6 @@ export class GlobalDashboardManager {
175
179
  });
176
180
  throw err;
177
181
  }
178
- finally {
179
- this.isInitializing = false;
180
- }
181
182
  })();
182
183
  return this.initPromise;
183
184
  }
@@ -215,14 +216,28 @@ export class GlobalDashboardManager {
215
216
  * Update locale globally
216
217
  */
217
218
  async setLocale(locale) {
219
+ // Update the last config
220
+ if (this.lastConfig) {
221
+ this.lastConfig = { ...this.lastConfig, locale };
222
+ }
218
223
  if (!this.engine) {
219
- // Update config for future initialization
220
- if (this.lastConfig) {
221
- this.lastConfig = { ...this.lastConfig, locale };
224
+ // No engine yet, mark locale for refresh when engine is created
225
+ this.pendingLocaleRefresh = locale;
226
+ // If we have providers, initialize with the new locale
227
+ if (this.providers.size > 0 && this.lastConfig) {
228
+ await this.init({ ...this.lastConfig, locale });
222
229
  }
223
230
  return;
224
231
  }
225
- await this.engine.setLocale(locale);
232
+ // Engine exists, update locale
233
+ if (this.engine.isInitialized()) {
234
+ await this.engine.setLocale(locale);
235
+ }
236
+ else {
237
+ // Engine not initialized, update locale and initialize
238
+ this.engine.updateLocale(locale);
239
+ await this.init();
240
+ }
226
241
  }
227
242
  /**
228
243
  * Get the current locale
@@ -291,10 +306,6 @@ export class GlobalDashboardManager {
291
306
  static reset() {
292
307
  if (GlobalDashboardManager.instance) {
293
308
  const manager = GlobalDashboardManager.instance;
294
- // Clear timers
295
- if (manager.autoInitTimer) {
296
- clearTimeout(manager.autoInitTimer);
297
- }
298
309
  // Clear listeners
299
310
  manager.stateListeners.clear();
300
311
  manager.providers.clear();
@@ -41,11 +41,11 @@ export const DashboardProvider = ({ children, config, locale, translations, auto
41
41
  // Register this provider
42
42
  useEffect(() => {
43
43
  const providerId = providerIdRef.current;
44
- manager.registerProvider(providerId, currentConfig);
44
+ manager.registerProvider(providerId, currentConfig, autoRefreshOnLocaleChange);
45
45
  return () => {
46
46
  manager.unregisterProvider(providerId);
47
47
  };
48
- }, [manager, currentConfig]);
48
+ }, [manager, currentConfig, autoRefreshOnLocaleChange]);
49
49
  // Subscribe to global state changes
50
50
  useEffect(() => {
51
51
  const providerId = providerIdRef.current;
@@ -77,6 +77,7 @@ export const DashboardProvider = ({ children, config, locale, translations, auto
77
77
  const oldLocale = lastLocaleRef.current;
78
78
  lastLocaleRef.current = locale;
79
79
  if (autoRefreshOnLocaleChange) {
80
+ // Always call setLocale which will handle initialization if needed
80
81
  manager.setLocale(locale).catch((error) => {
81
82
  console.error('Failed to set locale:', error);
82
83
  if (onError) {
@@ -90,15 +91,21 @@ export const DashboardProvider = ({ children, config, locale, translations, auto
90
91
  if (engine) {
91
92
  engine.updateLocale(locale);
92
93
  }
94
+ else {
95
+ // No engine yet, update config for future initialization
96
+ manager.registerProvider(providerIdRef.current, currentConfig);
97
+ }
93
98
  }
94
99
  if (onLocaleChange) {
95
100
  onLocaleChange(locale, oldLocale);
96
101
  }
97
102
  }
98
- }, [locale, manager, autoRefreshOnLocaleChange, onLocaleChange, onError]);
103
+ }, [locale, manager, autoRefreshOnLocaleChange, onLocaleChange, onError, currentConfig]);
99
104
  // Auto-init on mount if requested
100
105
  useEffect(() => {
101
- if (autoInit && !manager.isInitialized()) {
106
+ if (autoInit) {
107
+ // Always attempt to initialize when autoInit is true
108
+ // The manager will handle deduplication
102
109
  manager.init(currentConfig).catch((error) => {
103
110
  console.error("Failed to auto-initialize dashboard:", error);
104
111
  if (onError) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datlv-trustshop/shopify-inapp-components",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "private": false,
5
5
  "description": "React TypeScript components for Shopify in-app dashboard content",
6
6
  "main": "dist/index.js",