@zeniai/client-epic-state 5.0.33 → 5.0.34-betaRR0

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/lib/esm/init.js CHANGED
@@ -23,14 +23,19 @@ export default function initialize(restEndPoints, userId, sessionId, tenantId, i
23
23
  return store;
24
24
  }
25
25
  let isPusherInitialized = false;
26
+ // Caches an in-flight initializePusher() promise so that concurrent callers
27
+ // share a single Pusher instance instead of each constructing their own and
28
+ // leaking WebSocket connections (see Bugbot review on PR #2983). Cleared in
29
+ // the IIFE's `finally` once construction settles.
30
+ let pusherInitPromise;
26
31
  /**
27
32
  * Call initializePusher before using pusher in web-app
28
33
  */
29
- export function initializePusher(pusherClientSecret, options, headers, reInitialize = false) {
34
+ export async function initializePusher(pusherClientSecret, options, headers, reInitialize = false) {
30
35
  // Pusher subscribes/decrypts only make sense in the browser. `pusher-js/
31
36
  // with-encryption` resolves to a browser-only bundle that references
32
- // `window` at module init, so even *requiring* it on the server crashes.
33
- // Bail out early on SSR/Node so loading this module never triggers that.
37
+ // `window` at module init, so even *importing* it on the server crashes.
38
+ // Bail out early on SSR/Node so the dynamic import below never executes.
34
39
  if (typeof window === 'undefined') {
35
40
  console.warn(`initializePusher called on server-side; pusher-js/with-encryption is browser-only — skipping.`);
36
41
  return undefined;
@@ -45,27 +50,44 @@ export function initializePusher(pusherClientSecret, options, headers, reInitial
45
50
  console.warn(`Already initialized pusher`);
46
51
  return pusher;
47
52
  }
48
- // Lazy-require so the browser-only bundle is loaded only when this function
49
- // actually runs (browser path). The pusher-js UMD wrapper does
50
- // `module.exports = factory()` for CJS, so the require result is the
51
- // Pusher class directly. Some bundlers wrap CJS in `{ default: ... }` for
52
- // ESM interop fall back to the module itself when `.default` is absent.
53
- // eslint-disable-next-line @typescript-eslint/no-require-imports
54
- const pusherModule = require('pusher-js/with-encryption');
55
- const PusherConstructor = 'default' in pusherModule ? pusherModule.default : pusherModule;
56
- const endpoint = `${zeniAPI.apiEndPoints.tenantMicroServiceBaseUrl}/1.0/notifications/push_notification/auth`;
57
- pusher = new PusherConstructor(pusherClientSecret, {
58
- ...options,
59
- channelAuthorization: {
60
- endpoint,
61
- transport: 'ajax',
62
- customHandler: (params, callback) => customHandler(params, headers, callback),
63
- },
64
- }).bind('error', function (err) {
65
- console.warn('Pusher error', err);
66
- });
67
- console.info(`initializing with ${JSON.stringify(options)}`);
68
- return pusher;
53
+ // De-dupe concurrent in-flight calls so we don't construct multiple Pusher
54
+ // instances (each one a live WebSocket) and leak the older ones. We don't
55
+ // de-dupe when reInitialize=true that's the caller asking for a fresh
56
+ // instance, which is the existing contract.
57
+ if (pusherInitPromise && !reInitialize) {
58
+ return pusherInitPromise;
59
+ }
60
+ pusherInitPromise = (async () => {
61
+ try {
62
+ // Dynamic import so the browser-only bundle is loaded only when this
63
+ // function actually runs (browser path). Works in both build outputs:
64
+ // • ESM (lib/esm): preserved as native `import()` → Vite/Rollup code-split
65
+ // • CJS (lib): TypeScript lowers to `Promise.resolve().then(() => require(...))`
66
+ // The pusher-js UMD wrapper does `module.exports = factory()` for CJS,
67
+ // so the module result is the Pusher class directly. Some bundlers wrap
68
+ // CJS in `{ default: ... }` for ESM interop — fall back to the module
69
+ // itself when `.default` is absent.
70
+ const pusherModule = (await import('pusher-js/with-encryption'));
71
+ const PusherConstructor = 'default' in pusherModule ? pusherModule.default : pusherModule;
72
+ const endpoint = `${zeniAPI.apiEndPoints.tenantMicroServiceBaseUrl}/1.0/notifications/push_notification/auth`;
73
+ pusher = new PusherConstructor(pusherClientSecret, {
74
+ ...options,
75
+ channelAuthorization: {
76
+ endpoint,
77
+ transport: 'ajax',
78
+ customHandler: (params, callback) => customHandler(params, headers, callback),
79
+ },
80
+ }).bind('error', function (err) {
81
+ console.warn('Pusher error', err);
82
+ });
83
+ console.info(`initializing with ${JSON.stringify(options)}`);
84
+ return pusher;
85
+ }
86
+ finally {
87
+ pusherInitPromise = undefined;
88
+ }
89
+ })();
90
+ return pusherInitPromise;
69
91
  }
70
92
  const customHandler = async (params, headers, callback) => {
71
93
  const { socketId, channelName } = params;
package/lib/init.d.ts CHANGED
@@ -26,7 +26,7 @@ export declare function initializePusher(pusherClientSecret: ID, options: Option
26
26
  sessionId: ID;
27
27
  tenantId: ID;
28
28
  userId: ID;
29
- }, reInitialize?: boolean): Pusher | undefined;
29
+ }, reInitialize?: boolean): Promise<Pusher | undefined>;
30
30
  export declare function disconnectPusher(): void;
31
31
  export { pusher };
32
32
  export { injectFeatureModules, injectFeatureEpics } from './configureStore';
package/lib/init.js CHANGED
@@ -67,14 +67,19 @@ function initialize(restEndPoints, userId, sessionId, tenantId, isSandboxEnv, on
67
67
  return exports.store;
68
68
  }
69
69
  let isPusherInitialized = false;
70
+ // Caches an in-flight initializePusher() promise so that concurrent callers
71
+ // share a single Pusher instance instead of each constructing their own and
72
+ // leaking WebSocket connections (see Bugbot review on PR #2983). Cleared in
73
+ // the IIFE's `finally` once construction settles.
74
+ let pusherInitPromise;
70
75
  /**
71
76
  * Call initializePusher before using pusher in web-app
72
77
  */
73
- function initializePusher(pusherClientSecret, options, headers, reInitialize = false) {
78
+ async function initializePusher(pusherClientSecret, options, headers, reInitialize = false) {
74
79
  // Pusher subscribes/decrypts only make sense in the browser. `pusher-js/
75
80
  // with-encryption` resolves to a browser-only bundle that references
76
- // `window` at module init, so even *requiring* it on the server crashes.
77
- // Bail out early on SSR/Node so loading this module never triggers that.
81
+ // `window` at module init, so even *importing* it on the server crashes.
82
+ // Bail out early on SSR/Node so the dynamic import below never executes.
78
83
  if (typeof window === 'undefined') {
79
84
  console.warn(`initializePusher called on server-side; pusher-js/with-encryption is browser-only — skipping.`);
80
85
  return undefined;
@@ -89,27 +94,44 @@ function initializePusher(pusherClientSecret, options, headers, reInitialize = f
89
94
  console.warn(`Already initialized pusher`);
90
95
  return pusher;
91
96
  }
92
- // Lazy-require so the browser-only bundle is loaded only when this function
93
- // actually runs (browser path). The pusher-js UMD wrapper does
94
- // `module.exports = factory()` for CJS, so the require result is the
95
- // Pusher class directly. Some bundlers wrap CJS in `{ default: ... }` for
96
- // ESM interop fall back to the module itself when `.default` is absent.
97
- // eslint-disable-next-line @typescript-eslint/no-require-imports
98
- const pusherModule = require('pusher-js/with-encryption');
99
- const PusherConstructor = 'default' in pusherModule ? pusherModule.default : pusherModule;
100
- const endpoint = `${exports.zeniAPI.apiEndPoints.tenantMicroServiceBaseUrl}/1.0/notifications/push_notification/auth`;
101
- exports.pusher = pusher = new PusherConstructor(pusherClientSecret, {
102
- ...options,
103
- channelAuthorization: {
104
- endpoint,
105
- transport: 'ajax',
106
- customHandler: (params, callback) => customHandler(params, headers, callback),
107
- },
108
- }).bind('error', function (err) {
109
- console.warn('Pusher error', err);
110
- });
111
- console.info(`initializing with ${JSON.stringify(options)}`);
112
- return pusher;
97
+ // De-dupe concurrent in-flight calls so we don't construct multiple Pusher
98
+ // instances (each one a live WebSocket) and leak the older ones. We don't
99
+ // de-dupe when reInitialize=true that's the caller asking for a fresh
100
+ // instance, which is the existing contract.
101
+ if (pusherInitPromise && !reInitialize) {
102
+ return pusherInitPromise;
103
+ }
104
+ pusherInitPromise = (async () => {
105
+ try {
106
+ // Dynamic import so the browser-only bundle is loaded only when this
107
+ // function actually runs (browser path). Works in both build outputs:
108
+ // • ESM (lib/esm): preserved as native `import()` → Vite/Rollup code-split
109
+ // • CJS (lib): TypeScript lowers to `Promise.resolve().then(() => require(...))`
110
+ // The pusher-js UMD wrapper does `module.exports = factory()` for CJS,
111
+ // so the module result is the Pusher class directly. Some bundlers wrap
112
+ // CJS in `{ default: ... }` for ESM interop — fall back to the module
113
+ // itself when `.default` is absent.
114
+ const pusherModule = (await Promise.resolve().then(() => __importStar(require('pusher-js/with-encryption'))));
115
+ const PusherConstructor = 'default' in pusherModule ? pusherModule.default : pusherModule;
116
+ const endpoint = `${exports.zeniAPI.apiEndPoints.tenantMicroServiceBaseUrl}/1.0/notifications/push_notification/auth`;
117
+ exports.pusher = pusher = new PusherConstructor(pusherClientSecret, {
118
+ ...options,
119
+ channelAuthorization: {
120
+ endpoint,
121
+ transport: 'ajax',
122
+ customHandler: (params, callback) => customHandler(params, headers, callback),
123
+ },
124
+ }).bind('error', function (err) {
125
+ console.warn('Pusher error', err);
126
+ });
127
+ console.info(`initializing with ${JSON.stringify(options)}`);
128
+ return pusher;
129
+ }
130
+ finally {
131
+ pusherInitPromise = undefined;
132
+ }
133
+ })();
134
+ return pusherInitPromise;
113
135
  }
114
136
  const customHandler = async (params, headers, callback) => {
115
137
  const { socketId, channelName } = params;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeniai/client-epic-state",
3
- "version": "5.0.33",
3
+ "version": "5.0.34-betaRR0",
4
4
  "description": "Shared module between Web & Mobile containing required abstractions for state management, async network communication. ",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/esm/index.js",