@datlv-trustshop/shopify-inapp-components 0.1.10 → 0.1.12
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
|
|
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.
|
|
27
|
+
this.pendingLocaleRefresh = null;
|
|
29
28
|
// Private constructor for singleton
|
|
30
29
|
}
|
|
31
30
|
static getInstance() {
|
|
@@ -37,19 +36,31 @@ 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
|
-
//
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
}
|
|
53
64
|
}
|
|
54
65
|
}
|
|
55
66
|
/**
|
|
@@ -88,21 +99,43 @@ export class GlobalDashboardManager {
|
|
|
88
99
|
* Update the global configuration
|
|
89
100
|
*/
|
|
90
101
|
updateGlobalConfig(config) {
|
|
102
|
+
const previousLocale = this.lastConfig?.locale;
|
|
91
103
|
this.lastConfig = config;
|
|
92
104
|
if (this.engine) {
|
|
93
105
|
// Update existing engine config
|
|
94
106
|
this.engine.updateConfig(config);
|
|
95
|
-
// If locale changed
|
|
96
|
-
if (config.locale && config.locale !==
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
+
}
|
|
100
119
|
}
|
|
101
120
|
else {
|
|
121
|
+
// Engine not initialized yet
|
|
102
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
|
+
}
|
|
103
132
|
}
|
|
104
133
|
}
|
|
105
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
|
+
}
|
|
106
139
|
}
|
|
107
140
|
/**
|
|
108
141
|
* Check if two configs are equal
|
|
@@ -116,47 +149,31 @@ export class GlobalDashboardManager {
|
|
|
116
149
|
a.retryAttempts === b.retryAttempts &&
|
|
117
150
|
a.retryDelay === b.retryDelay);
|
|
118
151
|
}
|
|
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
152
|
/**
|
|
144
153
|
* Initialize the shared engine
|
|
145
154
|
*/
|
|
146
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
|
+
}
|
|
147
161
|
if (this.initPromise && !config) {
|
|
148
162
|
return this.initPromise;
|
|
149
163
|
}
|
|
150
164
|
if (config) {
|
|
151
165
|
this.updateGlobalConfig(config);
|
|
152
166
|
}
|
|
153
|
-
this.isInitializing = true;
|
|
154
167
|
this.updateGlobalState({ ...this.globalState, loading: true, error: null });
|
|
155
168
|
this.initPromise = (async () => {
|
|
156
169
|
try {
|
|
157
170
|
const engine = this.getOrCreateEngine(config);
|
|
158
171
|
await engine.init();
|
|
159
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
|
+
}
|
|
160
177
|
this.updateGlobalState({
|
|
161
178
|
data,
|
|
162
179
|
loading: false,
|
|
@@ -175,9 +192,6 @@ export class GlobalDashboardManager {
|
|
|
175
192
|
});
|
|
176
193
|
throw err;
|
|
177
194
|
}
|
|
178
|
-
finally {
|
|
179
|
-
this.isInitializing = false;
|
|
180
|
-
}
|
|
181
195
|
})();
|
|
182
196
|
return this.initPromise;
|
|
183
197
|
}
|
|
@@ -215,14 +229,35 @@ export class GlobalDashboardManager {
|
|
|
215
229
|
* Update locale globally
|
|
216
230
|
*/
|
|
217
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);
|
|
218
238
|
if (!this.engine) {
|
|
219
|
-
//
|
|
220
|
-
if (this.lastConfig) {
|
|
221
|
-
this.
|
|
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;
|
|
222
246
|
}
|
|
223
247
|
return;
|
|
224
248
|
}
|
|
225
|
-
|
|
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
|
+
}
|
|
226
261
|
}
|
|
227
262
|
/**
|
|
228
263
|
* Get the current locale
|
|
@@ -291,10 +326,6 @@ export class GlobalDashboardManager {
|
|
|
291
326
|
static reset() {
|
|
292
327
|
if (GlobalDashboardManager.instance) {
|
|
293
328
|
const manager = GlobalDashboardManager.instance;
|
|
294
|
-
// Clear timers
|
|
295
|
-
if (manager.autoInitTimer) {
|
|
296
|
-
clearTimeout(manager.autoInitTimer);
|
|
297
|
-
}
|
|
298
329
|
// Clear listeners
|
|
299
330
|
manager.stateListeners.clear();
|
|
300
331
|
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
|
|
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) {
|