@pubflow/react 0.2.0 → 0.2.1

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.
package/dist/index.esm.js CHANGED
@@ -237,7 +237,7 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
237
237
  };
238
238
  }
239
239
  }
240
- }, [enableDebugTools, contextValue, loginRedirectPath, publicPaths, theme]);
240
+ }, [enableDebugTools]); // OPTIMIZED: Only enableDebugTools as dependency
241
241
  // Handle session expiration
242
242
  const handleSessionExpired = useCallback(() => {
243
243
  if (showSessionAlerts) {
@@ -257,6 +257,7 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
257
257
  }
258
258
  }, [onSessionRefreshed, showSessionAlerts]);
259
259
  useEffect(() => {
260
+ let isMounted = true;
260
261
  const initialize = async () => {
261
262
  var _a, _b;
262
263
  try {
@@ -265,6 +266,8 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
265
266
  if (instances && instances.length > 0) {
266
267
  // Initialize multiple instances
267
268
  for (const instanceConfig of instances) {
269
+ if (!isMounted)
270
+ return; // Prevent state updates if unmounted
268
271
  // Initialize configuration
269
272
  const fullConfig = initConfig({
270
273
  ...instanceConfig,
@@ -282,34 +285,39 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
282
285
  const apiClient = new ApiClient(fullConfig, storage);
283
286
  // Create auth service
284
287
  const authService = new AuthService(apiClient, storage, fullConfig);
285
- // Get current user
288
+ // Get current user (optimized - no await in initialization)
286
289
  let user = null;
287
290
  let isAuthenticated = false;
288
- try {
289
- user = await authService.getCurrentUser();
290
- isAuthenticated = !!user;
291
- }
292
- catch (error) {
291
+ // Don't block initialization with API calls
292
+ authService.getCurrentUser().then(currentUser => {
293
+ if (isMounted && currentUser) {
294
+ setContextValue(prev => ({
295
+ ...prev,
296
+ instances: {
297
+ ...prev.instances,
298
+ [instanceConfig.id]: {
299
+ ...prev.instances[instanceConfig.id],
300
+ user: currentUser,
301
+ isAuthenticated: true,
302
+ isLoading: false
303
+ }
304
+ }
305
+ }));
306
+ }
307
+ }).catch(error => {
293
308
  console.error(`Error getting current user for instance ${instanceConfig.id}:`, error);
294
- }
295
- // Create instance
296
- instancesMap[instanceConfig.id] = {
297
- config: fullConfig,
298
- apiClient,
299
- authService,
300
- user,
301
- isAuthenticated,
302
- isLoading: false,
309
+ });
310
+ // Create instance with optimized functions
311
+ const createInstanceFunctions = (id) => ({
303
312
  login: async (credentials) => {
304
313
  const result = await authService.login(credentials);
305
- if (result.success && result.user) {
306
- // Update instance
314
+ if (result.success && result.user && isMounted) {
307
315
  setContextValue(prev => ({
308
316
  ...prev,
309
317
  instances: {
310
318
  ...prev.instances,
311
- [instanceConfig.id]: {
312
- ...prev.instances[instanceConfig.id],
319
+ [id]: {
320
+ ...prev.instances[id],
313
321
  user: result.user,
314
322
  isAuthenticated: true
315
323
  }
@@ -320,35 +328,46 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
320
328
  },
321
329
  logout: async () => {
322
330
  await authService.logout();
323
- // Update instance
324
- setContextValue(prev => ({
325
- ...prev,
326
- instances: {
327
- ...prev.instances,
328
- [instanceConfig.id]: {
329
- ...prev.instances[instanceConfig.id],
330
- user: null,
331
- isAuthenticated: false
331
+ if (isMounted) {
332
+ setContextValue(prev => ({
333
+ ...prev,
334
+ instances: {
335
+ ...prev.instances,
336
+ [id]: {
337
+ ...prev.instances[id],
338
+ user: null,
339
+ isAuthenticated: false
340
+ }
332
341
  }
333
- }
334
- }));
342
+ }));
343
+ }
335
344
  },
336
345
  validateSession: async () => {
337
346
  const result = await authService.validateSession();
338
- // Update instance
339
- setContextValue(prev => ({
340
- ...prev,
341
- instances: {
342
- ...prev.instances,
343
- [instanceConfig.id]: {
344
- ...prev.instances[instanceConfig.id],
345
- user: result.user || null,
346
- isAuthenticated: result.isValid
347
+ if (isMounted) {
348
+ setContextValue(prev => ({
349
+ ...prev,
350
+ instances: {
351
+ ...prev.instances,
352
+ [id]: {
353
+ ...prev.instances[id],
354
+ user: result.user || null,
355
+ isAuthenticated: result.isValid
356
+ }
347
357
  }
348
- }
349
- }));
358
+ }));
359
+ }
350
360
  return result;
351
361
  }
362
+ });
363
+ instancesMap[instanceConfig.id] = {
364
+ config: fullConfig,
365
+ apiClient,
366
+ authService,
367
+ user,
368
+ isAuthenticated,
369
+ isLoading: true, // Start as loading
370
+ ...createInstanceFunctions(instanceConfig.id)
352
371
  };
353
372
  }
354
373
  }
@@ -453,6 +472,10 @@ function PubflowProvider({ children, config, instances, defaultInstance = 'defau
453
472
  }
454
473
  };
455
474
  initialize();
475
+ // Cleanup function
476
+ return () => {
477
+ isMounted = false;
478
+ };
456
479
  }, [config, instances, defaultInstance, handleSessionExpired, handleSessionRefreshed]);
457
480
  // Show loading state
458
481
  if (!isInitialized) {