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

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,6 +23,11 @@ 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
  */
@@ -45,29 +50,59 @@ export async function initializePusher(pusherClientSecret, options, headers, reI
45
50
  console.warn(`Already initialized pusher`);
46
51
  return pusher;
47
52
  }
48
- // Dynamic import so the browser-only bundle is loaded only when this
49
- // function actually runs (browser path). Works in both build outputs:
50
- // ESM (lib/esm): preserved as native `import()` Vite/Rollup code-split
51
- // CJS (lib): TypeScript lowers to `Promise.resolve().then(() => require(...))`
52
- // The pusher-js UMD wrapper does `module.exports = factory()` for CJS, so
53
- // the module result is the Pusher class directly. Some bundlers wrap CJS in
54
- // `{ default: ... }` for ESM interop — fall back to the module itself when
55
- // `.default` is absent.
56
- const pusherModule = (await import('pusher-js/with-encryption'));
57
- const PusherConstructor = 'default' in pusherModule ? pusherModule.default : pusherModule;
58
- const endpoint = `${zeniAPI.apiEndPoints.tenantMicroServiceBaseUrl}/1.0/notifications/push_notification/auth`;
59
- pusher = new PusherConstructor(pusherClientSecret, {
60
- ...options,
61
- channelAuthorization: {
62
- endpoint,
63
- transport: 'ajax',
64
- customHandler: (params, callback) => customHandler(params, headers, callback),
65
- },
66
- }).bind('error', function (err) {
67
- console.warn('Pusher error', err);
68
- });
69
- console.info(`initializing with ${JSON.stringify(options)}`);
70
- 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
+ // The IIFE captures `currentInit` so that, when its `finally` runs, it can
61
+ // identity-check before clearing the module-level `pusherInitPromise`. This
62
+ // matters when a reInitialize=true call overwrites `pusherInitPromise`
63
+ // while we're still in flight: without the identity check, our finally
64
+ // would clobber the newer call's slot and the next concurrent caller
65
+ // would bypass the dedup guard. Capture is safe because the first action
66
+ // inside the try is `await import(...)`, which always yields control —
67
+ // so the finally runs in a future microtask, after `currentInit` has
68
+ // already been assigned below. We declare it as `let … | undefined` so
69
+ // TS's definite-assignment analysis (which doesn't model the await) accepts
70
+ // the closure read; at runtime it's always the IIFE promise by then.
71
+ let currentInit = undefined;
72
+ currentInit = (async () => {
73
+ try {
74
+ // Dynamic import so the browser-only bundle is loaded only when this
75
+ // function actually runs (browser path). Works in both build outputs:
76
+ // • ESM (lib/esm): preserved as native `import()` → Vite/Rollup code-split
77
+ // • CJS (lib): TypeScript lowers to `Promise.resolve().then(() => require(...))`
78
+ // The pusher-js UMD wrapper does `module.exports = factory()` for CJS,
79
+ // so the module result is the Pusher class directly. Some bundlers wrap
80
+ // CJS in `{ default: ... }` for ESM interop — fall back to the module
81
+ // itself when `.default` is absent.
82
+ const pusherModule = (await import('pusher-js/with-encryption'));
83
+ const PusherConstructor = 'default' in pusherModule ? pusherModule.default : pusherModule;
84
+ const endpoint = `${zeniAPI.apiEndPoints.tenantMicroServiceBaseUrl}/1.0/notifications/push_notification/auth`;
85
+ pusher = new PusherConstructor(pusherClientSecret, {
86
+ ...options,
87
+ channelAuthorization: {
88
+ endpoint,
89
+ transport: 'ajax',
90
+ customHandler: (params, callback) => customHandler(params, headers, callback),
91
+ },
92
+ }).bind('error', function (err) {
93
+ console.warn('Pusher error', err);
94
+ });
95
+ console.info(`initializing with ${JSON.stringify(options)}`);
96
+ return pusher;
97
+ }
98
+ finally {
99
+ if (pusherInitPromise === currentInit) {
100
+ pusherInitPromise = undefined;
101
+ }
102
+ }
103
+ })();
104
+ pusherInitPromise = currentInit;
105
+ return currentInit;
71
106
  }
72
107
  const customHandler = async (params, headers, callback) => {
73
108
  const { socketId, channelName } = params;
@@ -1,5 +1,5 @@
1
1
  import { from, of } from 'rxjs';
2
- import { catchError, concatMap, filter, mergeMap, switchMap, } from 'rxjs/operators';
2
+ import { catchError, concatMap, exhaustMap, filter, mergeMap, } from 'rxjs/operators';
3
3
  import { openSnackbar } from '../../../entity/snackbar/snackbarReducer';
4
4
  import { createZeniAPIStatus } from '../../../responsePayload';
5
5
  import { approveOAuthConsent, approveOAuthConsentFailure, approveOAuthConsentSuccess, } from '../zeniOAuthReducer';
@@ -9,10 +9,11 @@ const failureSnackbar = (reason) => openSnackbar({
9
9
  type: 'error',
10
10
  variables: [{ variableName: '__reason__', variableValue: reason }],
11
11
  });
12
- export const approveOAuthConsentEpic = (actions$, state$, zeniAPI) => actions$.pipe(filter(approveOAuthConsent.match), switchMap((action) => {
13
- if (state$.value.zeniOAuthViewState.fetchState === 'In-Progress') {
14
- return from([]);
15
- }
12
+ export const approveOAuthConsentEpic = (actions$, _state$, zeniAPI) => actions$.pipe(filter(approveOAuthConsent.match),
13
+ // `approveOAuthConsent` reducer sets fetchState to In-Progress before epics
14
+ // run, so a guard on In-Progress would block every legitimate request.
15
+ // exhaustMap drops duplicate approve clicks while the HTTP call is in flight.
16
+ exhaustMap((action) => {
16
17
  const { userId, zeniSessionId, tenantId, clientId, redirectUri, codeChallenge, codeChallengeMethod, state, responseType, } = action.payload;
17
18
  const authHeaders = {
18
19
  'zeni-user-id': userId,
package/lib/init.js CHANGED
@@ -67,6 +67,11 @@ 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
  */
@@ -89,29 +94,59 @@ async function initializePusher(pusherClientSecret, options, headers, reInitiali
89
94
  console.warn(`Already initialized pusher`);
90
95
  return pusher;
91
96
  }
92
- // Dynamic import so the browser-only bundle is loaded only when this
93
- // function actually runs (browser path). Works in both build outputs:
94
- // ESM (lib/esm): preserved as native `import()` Vite/Rollup code-split
95
- // CJS (lib): TypeScript lowers to `Promise.resolve().then(() => require(...))`
96
- // The pusher-js UMD wrapper does `module.exports = factory()` for CJS, so
97
- // the module result is the Pusher class directly. Some bundlers wrap CJS in
98
- // `{ default: ... }` for ESM interop — fall back to the module itself when
99
- // `.default` is absent.
100
- const pusherModule = (await Promise.resolve().then(() => __importStar(require('pusher-js/with-encryption'))));
101
- const PusherConstructor = 'default' in pusherModule ? pusherModule.default : pusherModule;
102
- const endpoint = `${exports.zeniAPI.apiEndPoints.tenantMicroServiceBaseUrl}/1.0/notifications/push_notification/auth`;
103
- exports.pusher = pusher = new PusherConstructor(pusherClientSecret, {
104
- ...options,
105
- channelAuthorization: {
106
- endpoint,
107
- transport: 'ajax',
108
- customHandler: (params, callback) => customHandler(params, headers, callback),
109
- },
110
- }).bind('error', function (err) {
111
- console.warn('Pusher error', err);
112
- });
113
- console.info(`initializing with ${JSON.stringify(options)}`);
114
- 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
+ // The IIFE captures `currentInit` so that, when its `finally` runs, it can
105
+ // identity-check before clearing the module-level `pusherInitPromise`. This
106
+ // matters when a reInitialize=true call overwrites `pusherInitPromise`
107
+ // while we're still in flight: without the identity check, our finally
108
+ // would clobber the newer call's slot and the next concurrent caller
109
+ // would bypass the dedup guard. Capture is safe because the first action
110
+ // inside the try is `await import(...)`, which always yields control —
111
+ // so the finally runs in a future microtask, after `currentInit` has
112
+ // already been assigned below. We declare it as `let … | undefined` so
113
+ // TS's definite-assignment analysis (which doesn't model the await) accepts
114
+ // the closure read; at runtime it's always the IIFE promise by then.
115
+ let currentInit = undefined;
116
+ currentInit = (async () => {
117
+ try {
118
+ // Dynamic import so the browser-only bundle is loaded only when this
119
+ // function actually runs (browser path). Works in both build outputs:
120
+ // • ESM (lib/esm): preserved as native `import()` → Vite/Rollup code-split
121
+ // • CJS (lib): TypeScript lowers to `Promise.resolve().then(() => require(...))`
122
+ // The pusher-js UMD wrapper does `module.exports = factory()` for CJS,
123
+ // so the module result is the Pusher class directly. Some bundlers wrap
124
+ // CJS in `{ default: ... }` for ESM interop — fall back to the module
125
+ // itself when `.default` is absent.
126
+ const pusherModule = (await Promise.resolve().then(() => __importStar(require('pusher-js/with-encryption'))));
127
+ const PusherConstructor = 'default' in pusherModule ? pusherModule.default : pusherModule;
128
+ const endpoint = `${exports.zeniAPI.apiEndPoints.tenantMicroServiceBaseUrl}/1.0/notifications/push_notification/auth`;
129
+ exports.pusher = pusher = new PusherConstructor(pusherClientSecret, {
130
+ ...options,
131
+ channelAuthorization: {
132
+ endpoint,
133
+ transport: 'ajax',
134
+ customHandler: (params, callback) => customHandler(params, headers, callback),
135
+ },
136
+ }).bind('error', function (err) {
137
+ console.warn('Pusher error', err);
138
+ });
139
+ console.info(`initializing with ${JSON.stringify(options)}`);
140
+ return pusher;
141
+ }
142
+ finally {
143
+ if (pusherInitPromise === currentInit) {
144
+ pusherInitPromise = undefined;
145
+ }
146
+ }
147
+ })();
148
+ pusherInitPromise = currentInit;
149
+ return currentInit;
115
150
  }
116
151
  const customHandler = async (params, headers, callback) => {
117
152
  const { socketId, channelName } = params;