@controleonline/ui-common 1.2.70
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/.github/agents/developer.agent.md +30 -0
- package/.github/agents/devops.agent.md +30 -0
- package/.github/agents/qa.agent.md +30 -0
- package/.github/agents/security.agent.md +30 -0
- package/.github/workflows/npmjs.yml +35 -0
- package/.github/workflows/pull-request-checks.yml +19 -0
- package/.scrutinizer.yml +24 -0
- package/FUNDING.yml +1 -0
- package/package.json +28 -0
- package/src/api/fetch.js +74 -0
- package/src/api/index.js +155 -0
- package/src/api/localDB.js +131 -0
- package/src/api/queue.js +66 -0
- package/src/react/assets/tap.mp3 +0 -0
- package/src/react/components/AddImportModal.js +157 -0
- package/src/react/components/AddImportModal.styles.js +81 -0
- package/src/react/components/BackgroundRuntimeBridge.js +219 -0
- package/src/react/components/BottomNavigationBar.config.js +238 -0
- package/src/react/components/BottomNavigationBar.js +120 -0
- package/src/react/components/BottomNavigationBar.styles.js +110 -0
- package/src/react/components/CategoryForm.js +572 -0
- package/src/react/components/CategoryForm.styles.js +334 -0
- package/src/react/components/ConfirmModal.js +24 -0
- package/src/react/components/ConfirmModal.styles.js +43 -0
- package/src/react/components/ContextHelpButton.js +69 -0
- package/src/react/components/ContextHelpButton.styles.js +62 -0
- package/src/react/components/DefaultProvider.native.js +848 -0
- package/src/react/components/DefaultProvider.styles.js +21 -0
- package/src/react/components/DefaultProvider.web.js +909 -0
- package/src/react/components/DeliveryPushBridge.native.js +314 -0
- package/src/react/components/DeliveryPushBridge.web.js +5 -0
- package/src/react/components/DeviceAlertSoundService.js +499 -0
- package/src/react/components/EntityLogContent.js +909 -0
- package/src/react/components/EntityLogContent.styles.js +400 -0
- package/src/react/components/EventBus.js +12 -0
- package/src/react/components/KioskModeBridge.js +65 -0
- package/src/react/components/LauncherModeBridge.js +47 -0
- package/src/react/components/LinkedOrderProductsTab.js +388 -0
- package/src/react/components/LinkedOrderProductsTab.styles.js +180 -0
- package/src/react/components/ManagerPushBridge.native.js +345 -0
- package/src/react/components/MessageService.js +314 -0
- package/src/react/components/Paywall/Google.js +89 -0
- package/src/react/components/PrintService.js +778 -0
- package/src/react/components/ProductCatalogCacheService.js +45 -0
- package/src/react/components/RemoteCheckoutService.js +292 -0
- package/src/react/components/RuntimeBottomNavigationBar.js +151 -0
- package/src/react/components/RuntimeInfoFooter.js +214 -0
- package/src/react/components/RuntimeInfoFooter.styles.js +56 -0
- package/src/react/components/SystemErrorToast.js +29 -0
- package/src/react/components/SystemErrorToast.styles.js +38 -0
- package/src/react/components/TouchFeedbackProvider.js +65 -0
- package/src/react/components/UnifiedPaymentBar.js +155 -0
- package/src/react/components/UnifiedPaymentBar.styles.js +174 -0
- package/src/react/components/WebsocketListener.native.js +487 -0
- package/src/react/components/WebsocketListener.web.js +102 -0
- package/src/react/components/inputs/IdInput.js +49 -0
- package/src/react/components/inputs/IdInput.styles.js +26 -0
- package/src/react/components/inputs/InvoiceDueDateFilterInput.js +74 -0
- package/src/react/components/inputs/InvoiceDueDateFilterInput.styles.js +34 -0
- package/src/react/components/inputs/InvoiceDueDateRangeFilterInput.js +115 -0
- package/src/react/components/inputs/InvoiceDueDateRangeFilterInput.styles.js +39 -0
- package/src/react/components/lists/CategoriesList.js +56 -0
- package/src/react/components/lists/PaymentTypeList.js +55 -0
- package/src/react/components/lists/ReceiverList.js +56 -0
- package/src/react/components/lists/SelectList.styles.js +18 -0
- package/src/react/components/lists/StatusList.js +55 -0
- package/src/react/components/lists/WalletList.js +54 -0
- package/src/react/components/radio/Spotify.js +144 -0
- package/src/react/components/radio/Spotify.styles.js +32 -0
- package/src/react/components/radio/YouTube.js +82 -0
- package/src/react/components/radio/YouTube.styles.js +16 -0
- package/src/react/components/toastConfig.js +16 -0
- package/src/react/config/deviceConfigBootstrap.js +571 -0
- package/src/react/index.js +4 -0
- package/src/react/localStorage.js +30 -0
- package/src/react/pages/EntityLogPage.js +124 -0
- package/src/react/pages/EntityLogPage.styles.js +17 -0
- package/src/react/pages/GenericLogPage.js +760 -0
- package/src/react/pages/GenericLogPage.styles.js +401 -0
- package/src/react/pages/Imports.js +361 -0
- package/src/react/pages/Imports.styles.js +223 -0
- package/src/react/pages/SettingsPage/PaymentTypesByWalletTab.js +462 -0
- package/src/react/pages/SettingsPage/index.js +1171 -0
- package/src/react/pages/SettingsPage/index.styles.js +122 -0
- package/src/react/print/jobs.js +147 -0
- package/src/react/print/providers/local.js +53 -0
- package/src/react/print/providers/remote.js +81 -0
- package/src/react/print/selection.js +296 -0
- package/src/react/print/usePrintButtonController.js +474 -0
- package/src/react/router/routes.js +42 -0
- package/src/react/services/NetworkPrinterService.native.js +391 -0
- package/src/react/services/NetworkPrinterService.web.js +15 -0
- package/src/react/stores/index.js +128 -0
- package/src/react/stores/storeErrorBridge.js +36 -0
- package/src/react/utils/backgroundRuntimeRegistration.js +69 -0
- package/src/react/utils/cashPayment.js +87 -0
- package/src/react/utils/commercialDocumentOrders.js +269 -0
- package/src/react/utils/dateRangeFilter.js +264 -0
- package/src/react/utils/deviceRuntime.js +193 -0
- package/src/react/utils/entityDisplay.js +231 -0
- package/src/react/utils/entityLog.js +649 -0
- package/src/react/utils/fileUrl.js +102 -0
- package/src/react/utils/frontendDebugLog.js +45 -0
- package/src/react/utils/googleMapsConfig.js +49 -0
- package/src/react/utils/importStatus.js +100 -0
- package/src/react/utils/invoiceDueDateMessages.js +14 -0
- package/src/react/utils/invoicePresentation.js +46 -0
- package/src/react/utils/logSettings.js +143 -0
- package/src/react/utils/maintenanceSettings.js +53 -0
- package/src/react/utils/managerOrderNotifications.js +523 -0
- package/src/react/utils/mapNavigation.js +76 -0
- package/src/react/utils/menuCatalogConfig.js +8 -0
- package/src/react/utils/menuCatalogDownload.js +157 -0
- package/src/react/utils/networkCameraDevices.js +175 -0
- package/src/react/utils/networkDeviceProfiles.js +135 -0
- package/src/react/utils/normalizedCatalogDownload.js +157 -0
- package/src/react/utils/notificationNavigation.js +94 -0
- package/src/react/utils/notificationSound.js +42 -0
- package/src/react/utils/paymentDevices.js +397 -0
- package/src/react/utils/paymentGatewayExecution.js +91 -0
- package/src/react/utils/paymentOptions.js +88 -0
- package/src/react/utils/peopleDisplay.js +39 -0
- package/src/react/utils/printerDevices.js +465 -0
- package/src/react/utils/remotePayment.js +61 -0
- package/src/react/utils/runtimeFooter.js +475 -0
- package/src/react/utils/runtimeLanguage.js +50 -0
- package/src/react/utils/runtimeMenu.js +224 -0
- package/src/react/utils/screenMetrics.js +30 -0
- package/src/react/utils/shopConfig.js +294 -0
- package/src/react/utils/shopFranchises.js +114 -0
- package/src/react/utils/socketRuntimePipeline.js +454 -0
- package/src/react/utils/spoolAckTracker.js +77 -0
- package/src/react/utils/storeColumns.js +427 -0
- package/src/react/utils/systemErrorChannel.js +109 -0
- package/src/react/utils/systemErrorMessage.js +67 -0
- package/src/react/utils/toastPresentation.js +53 -0
- package/src/react/utils/websocketSharedRuntime.js +826 -0
- package/src/store/acl/getters.js +4 -0
- package/src/store/acl/index.js +19 -0
- package/src/store/acl/mutation_types.js +4 -0
- package/src/store/acl/mutations.js +17 -0
- package/src/store/address/city.js +22 -0
- package/src/store/categories/category_file/index.js +25 -0
- package/src/store/categories/index.js +93 -0
- package/src/store/configs/customActions.js +131 -0
- package/src/store/configs/index.js +25 -0
- package/src/store/connections/customActions.js +26 -0
- package/src/store/connections/index.js +124 -0
- package/src/store/device/index.js +22 -0
- package/src/store/device_config/customActions.js +190 -0
- package/src/store/device_config/index.js +28 -0
- package/src/store/entity_log/customActions.js +63 -0
- package/src/store/entity_log/index.js +25 -0
- package/src/store/extra/data.js +85 -0
- package/src/store/extra/fields.js +88 -0
- package/src/store/file/actions.js +27 -0
- package/src/store/file/index.js +24 -0
- package/src/store/imports/index.js +88 -0
- package/src/store/print/customActions.js +251 -0
- package/src/store/print/index.js +28 -0
- package/src/store/printer/customActions.js +54 -0
- package/src/store/printer/index.js +26 -0
- package/src/store/runtime_debug/customActions.js +71 -0
- package/src/store/runtime_debug/index.js +30 -0
- package/src/store/status/index.js +65 -0
- package/src/store/timezones/index.js +43 -0
- package/src/store/websocket/customActions.js +24 -0
- package/src/store/websocket/index.js +28 -0
- package/src/tests/react/components/LauncherModeBridge.test.js +93 -0
- package/src/tests/react/config/deviceConfigBootstrap.test.js +337 -0
- package/src/tests/react/print/localPrintProvider.test.js +45 -0
- package/src/tests/react/utils/backgroundRuntimeRegistration.test.js +123 -0
- package/src/tests/react/utils/cashPayment.test.js +41 -0
- package/src/tests/react/utils/dateRangeFilter.test.mjs +29 -0
- package/src/tests/react/utils/entityDisplay.test.js +18 -0
- package/src/tests/react/utils/googleMapsConfig.test.js +32 -0
- package/src/tests/react/utils/importStatus.test.js +85 -0
- package/src/tests/react/utils/invoicePresentation.test.mjs +40 -0
- package/src/tests/react/utils/managerOrderNotifications.test.js +127 -0
- package/src/tests/react/utils/mapNavigation.test.js +58 -0
- package/src/tests/react/utils/notificationNavigation.test.js +39 -0
- package/src/tests/react/utils/notificationSound.test.js +27 -0
- package/src/tests/react/utils/paymentDevices.test.js +88 -0
- package/src/tests/react/utils/runtimeFooter.test.js +429 -0
- package/src/tests/react/utils/runtimeLanguage.test.js +80 -0
- package/src/tests/react/utils/runtimeMenu.test.js +174 -0
- package/src/tests/react/utils/shopFranchises.test.js +82 -0
- package/src/tests/react/utils/socketRuntimePipeline.test.js +124 -0
- package/src/tests/react/utils/spoolAckTracker.test.js +40 -0
- package/src/tests/react/utils/storeErrorBridge.test.js +66 -0
- package/src/tests/react/utils/systemErrorChannel.test.js +58 -0
- package/src/tests/react/utils/systemErrorMessage.test.js +59 -0
- package/src/tests/react/utils/toastPresentation.test.js +75 -0
- package/src/tests/react/utils/translate.test.mjs +396 -0
- package/src/tests/react/utils/websocketSharedRuntime.test.js +245 -0
- package/src/utils/apiEntryPoint.js +31 -0
- package/src/utils/appDomain.js +50 -0
- package/src/utils/config.js +14 -0
- package/src/utils/formatter.js +419 -0
- package/src/utils/integrationConfigs.js +79 -0
- package/src/utils/methods.js +23 -0
- package/src/utils/oauth.js +42 -0
- package/src/utils/translate.js +620 -0
|
@@ -0,0 +1,826 @@
|
|
|
1
|
+
import {normalizeText} from '@controleonline/ui-common/src/react/utils/socketRuntimePipeline';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_CHANNEL_NAME = 'controleonline:websocket-runtime';
|
|
4
|
+
|
|
5
|
+
export const compareRuntimePriority = (left, right) => {
|
|
6
|
+
const leftStarted = Number(left?.startedAt || 0);
|
|
7
|
+
const rightStarted = Number(right?.startedAt || 0);
|
|
8
|
+
|
|
9
|
+
if (leftStarted !== rightStarted) {
|
|
10
|
+
return leftStarted - rightStarted;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return String(left?.instanceId || '').localeCompare(
|
|
14
|
+
String(right?.instanceId || ''),
|
|
15
|
+
);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const isHigherPriorityRuntime = (candidate, current) =>
|
|
19
|
+
compareRuntimePriority(candidate, current) < 0;
|
|
20
|
+
|
|
21
|
+
export const createWebsocketSharedRuntime = ({
|
|
22
|
+
socketUrl = '',
|
|
23
|
+
getDeviceId = () => '',
|
|
24
|
+
pipeline,
|
|
25
|
+
BroadcastChannelImpl = typeof BroadcastChannel === 'function'
|
|
26
|
+
? BroadcastChannel
|
|
27
|
+
: null,
|
|
28
|
+
WebSocketImpl = typeof WebSocket !== 'undefined' ? WebSocket : null,
|
|
29
|
+
now = () => Date.now(),
|
|
30
|
+
setTimeoutImpl = setTimeout,
|
|
31
|
+
clearTimeoutImpl = clearTimeout,
|
|
32
|
+
claimDelayMs = 180,
|
|
33
|
+
heartbeatMs = 2000,
|
|
34
|
+
leaderStaleMs = 6000,
|
|
35
|
+
reconnectBaseMs = 1500,
|
|
36
|
+
reconnectMaxMs = 12000,
|
|
37
|
+
channelName = DEFAULT_CHANNEL_NAME,
|
|
38
|
+
} = {}) => {
|
|
39
|
+
const startedAt = now();
|
|
40
|
+
const instanceId = `${startedAt}-${Math.random().toString(36).slice(2, 10)}`;
|
|
41
|
+
|
|
42
|
+
let channel = null;
|
|
43
|
+
let socket = null;
|
|
44
|
+
let claimTimer = null;
|
|
45
|
+
let heartbeatTimer = null;
|
|
46
|
+
let monitorTimer = null;
|
|
47
|
+
let reconnectTimer = null;
|
|
48
|
+
let reconnectAttempt = 0;
|
|
49
|
+
let manualClose = false;
|
|
50
|
+
let destroyed = false;
|
|
51
|
+
let leaderDescriptor = null;
|
|
52
|
+
let socketGeneration = 0;
|
|
53
|
+
let identifyTimer = null;
|
|
54
|
+
let socketStateSnapshot = pipeline?.getConnectionState?.() || null;
|
|
55
|
+
const leaderStorageKey = `${channelName}:leader`;
|
|
56
|
+
const storage =
|
|
57
|
+
typeof localStorage !== 'undefined' ? localStorage : null;
|
|
58
|
+
|
|
59
|
+
const readLeaderRecord = () => {
|
|
60
|
+
if (!storage) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const raw = storage.getItem(leaderStorageKey);
|
|
66
|
+
if (!raw) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const parsed = JSON.parse(raw);
|
|
71
|
+
if (!parsed?.instanceId) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return parsed;
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const writeLeaderRecord = record => {
|
|
82
|
+
if (!storage || !record?.instanceId) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
storage.setItem(leaderStorageKey, JSON.stringify(record));
|
|
88
|
+
} catch {
|
|
89
|
+
// localStorage may be unavailable in privacy modes.
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const clearLeaderRecord = instanceIdToClear => {
|
|
94
|
+
if (!storage) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const currentRecord = readLeaderRecord();
|
|
100
|
+
if (!currentRecord || currentRecord.instanceId === instanceIdToClear) {
|
|
101
|
+
storage.removeItem(leaderStorageKey);
|
|
102
|
+
}
|
|
103
|
+
} catch {
|
|
104
|
+
// noop
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const buildLeaderRecord = (state = 'claiming', reason = '') => ({
|
|
109
|
+
instanceId,
|
|
110
|
+
startedAt,
|
|
111
|
+
state,
|
|
112
|
+
reason,
|
|
113
|
+
updatedAt: now(),
|
|
114
|
+
leaseMs: leaderStaleMs + heartbeatMs,
|
|
115
|
+
deviceId: normalizeText(getDeviceId()),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const isActiveLeaderRecord = record => {
|
|
119
|
+
if (!record?.instanceId) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const leaseMs = Number(record.leaseMs || 0) || leaderStaleMs + heartbeatMs;
|
|
124
|
+
const updatedAt = Number(record.updatedAt || 0);
|
|
125
|
+
return updatedAt > 0 && now() - updatedAt <= leaseMs;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const clearTimer = timerId => {
|
|
129
|
+
if (timerId) {
|
|
130
|
+
clearTimeoutImpl(timerId);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return null;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const clearIntervalTimer = timerId => {
|
|
137
|
+
if (timerId) {
|
|
138
|
+
clearInterval(timerId);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return null;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const isCurrentSocket = (candidateSocket, generation) =>
|
|
145
|
+
socket === candidateSocket && socketGeneration === generation;
|
|
146
|
+
|
|
147
|
+
const broadcast = message => {
|
|
148
|
+
if (channel) {
|
|
149
|
+
channel.postMessage(message);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const broadcastState = () => {
|
|
154
|
+
if (!channel || !socketStateSnapshot) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
broadcast({
|
|
159
|
+
type: 'socket-state',
|
|
160
|
+
state: socketStateSnapshot,
|
|
161
|
+
instanceId,
|
|
162
|
+
startedAt,
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const sendLeaderHeartbeat = () => {
|
|
167
|
+
if (!channel) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
writeLeaderRecord(buildLeaderRecord('leader', 'heartbeat'));
|
|
172
|
+
broadcast({
|
|
173
|
+
type: 'leader-heartbeat',
|
|
174
|
+
instanceId,
|
|
175
|
+
startedAt,
|
|
176
|
+
at: now(),
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const rememberLeader = candidate => {
|
|
181
|
+
if (!candidate?.instanceId) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const nextDescriptor = {
|
|
186
|
+
instanceId: candidate.instanceId,
|
|
187
|
+
startedAt: Number(candidate.startedAt || 0),
|
|
188
|
+
lastSeenAt: Number(candidate.lastSeenAt || now()),
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
if (
|
|
192
|
+
!leaderDescriptor ||
|
|
193
|
+
nextDescriptor.instanceId === leaderDescriptor.instanceId ||
|
|
194
|
+
isHigherPriorityRuntime(nextDescriptor, leaderDescriptor)
|
|
195
|
+
) {
|
|
196
|
+
leaderDescriptor = nextDescriptor;
|
|
197
|
+
} else {
|
|
198
|
+
leaderDescriptor = {
|
|
199
|
+
...leaderDescriptor,
|
|
200
|
+
lastSeenAt: Math.max(leaderDescriptor.lastSeenAt || 0, now()),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const stopSocket = () => {
|
|
206
|
+
const activeSocket = socket;
|
|
207
|
+
socket = null;
|
|
208
|
+
socketGeneration += 1;
|
|
209
|
+
|
|
210
|
+
if (!activeSocket) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
activeSocket.onopen = null;
|
|
215
|
+
activeSocket.onmessage = null;
|
|
216
|
+
activeSocket.onerror = null;
|
|
217
|
+
activeSocket.onclose = null;
|
|
218
|
+
activeSocket.close?.();
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const updateSocketState = state => {
|
|
222
|
+
socketStateSnapshot = pipeline?.updateConnectionState?.(state) || state;
|
|
223
|
+
return socketStateSnapshot;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const clearReconnectTimer = () => {
|
|
227
|
+
reconnectTimer = clearTimer(reconnectTimer);
|
|
228
|
+
reconnectAttempt = 0;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const scheduleReconnect = () => {
|
|
232
|
+
if (manualClose || destroyed || !isLeader() || reconnectTimer) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
reconnectAttempt = Math.min(reconnectAttempt + 1, 8);
|
|
237
|
+
updateSocketState({
|
|
238
|
+
connected: false,
|
|
239
|
+
identified: false,
|
|
240
|
+
status: 'reconnecting',
|
|
241
|
+
attempts: reconnectAttempt,
|
|
242
|
+
updatedAt: new Date().toISOString(),
|
|
243
|
+
});
|
|
244
|
+
broadcastState();
|
|
245
|
+
|
|
246
|
+
const delayMs = Math.min(
|
|
247
|
+
reconnectMaxMs,
|
|
248
|
+
reconnectBaseMs * (2 ** Math.min(reconnectAttempt, 4)),
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
reconnectTimer = setTimeoutImpl(() => {
|
|
252
|
+
reconnectTimer = null;
|
|
253
|
+
if (!manualClose && !destroyed && isLeader()) {
|
|
254
|
+
openSocket();
|
|
255
|
+
}
|
|
256
|
+
}, delayMs);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const relinquishLeadership = ({reason = 'step-down', broadcastRelease = true} = {}) => {
|
|
260
|
+
if (!isLeader()) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
clearTimer(heartbeatTimer);
|
|
265
|
+
heartbeatTimer = null;
|
|
266
|
+
clearTimer(claimTimer);
|
|
267
|
+
claimTimer = null;
|
|
268
|
+
clearTimer(identifyTimer);
|
|
269
|
+
identifyTimer = null;
|
|
270
|
+
clearIntervalTimer(monitorTimer);
|
|
271
|
+
monitorTimer = null;
|
|
272
|
+
clearReconnectTimer();
|
|
273
|
+
stopSocket();
|
|
274
|
+
|
|
275
|
+
updateSocketState({
|
|
276
|
+
connected: false,
|
|
277
|
+
identified: false,
|
|
278
|
+
status: 'idle',
|
|
279
|
+
attempts: 0,
|
|
280
|
+
updatedAt: new Date().toISOString(),
|
|
281
|
+
device: normalizeText(getDeviceId()) || null,
|
|
282
|
+
error: null,
|
|
283
|
+
code: null,
|
|
284
|
+
reason,
|
|
285
|
+
lastEventAt: null,
|
|
286
|
+
lastEventCount: 0,
|
|
287
|
+
lastStores: [],
|
|
288
|
+
lastCompanies: [],
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
if (broadcastRelease) {
|
|
292
|
+
broadcast({
|
|
293
|
+
type: 'leader-release',
|
|
294
|
+
instanceId,
|
|
295
|
+
startedAt,
|
|
296
|
+
reason,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
clearLeaderRecord(instanceId);
|
|
301
|
+
leaderDescriptor = null;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const becomeLeader = ({reason = 'claim'} = {}) => {
|
|
305
|
+
if (destroyed || manualClose || isLeader()) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
clearTimer(claimTimer);
|
|
310
|
+
claimTimer = null;
|
|
311
|
+
clearTimer(reconnectTimer);
|
|
312
|
+
reconnectTimer = null;
|
|
313
|
+
|
|
314
|
+
rememberLeader({
|
|
315
|
+
instanceId,
|
|
316
|
+
startedAt,
|
|
317
|
+
lastSeenAt: now(),
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
updateSocketState({
|
|
321
|
+
connected: false,
|
|
322
|
+
identified: false,
|
|
323
|
+
status: 'connecting',
|
|
324
|
+
attempts: reconnectAttempt,
|
|
325
|
+
updatedAt: new Date().toISOString(),
|
|
326
|
+
device: normalizeText(getDeviceId()) || null,
|
|
327
|
+
error: null,
|
|
328
|
+
code: null,
|
|
329
|
+
reason,
|
|
330
|
+
lastEventAt: socketStateSnapshot?.lastEventAt || null,
|
|
331
|
+
lastEventCount: socketStateSnapshot?.lastEventCount || 0,
|
|
332
|
+
lastStores: socketStateSnapshot?.lastStores || [],
|
|
333
|
+
lastCompanies: socketStateSnapshot?.lastCompanies || [],
|
|
334
|
+
});
|
|
335
|
+
broadcastState();
|
|
336
|
+
writeLeaderRecord(buildLeaderRecord('leader', reason));
|
|
337
|
+
|
|
338
|
+
sendLeaderHeartbeat();
|
|
339
|
+
|
|
340
|
+
heartbeatTimer = clearTimer(heartbeatTimer);
|
|
341
|
+
heartbeatTimer = setInterval(() => {
|
|
342
|
+
if (destroyed || manualClose || !isLeader()) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
sendLeaderHeartbeat();
|
|
347
|
+
}, heartbeatMs);
|
|
348
|
+
|
|
349
|
+
openSocket();
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const shouldLeadNow = () => {
|
|
353
|
+
if (!channel) {
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (!leaderDescriptor) {
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (leaderDescriptor.instanceId === instanceId) {
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const elapsed = now() - Number(leaderDescriptor.lastSeenAt || 0);
|
|
366
|
+
return elapsed > leaderStaleMs;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const requestLeadership = reason => {
|
|
370
|
+
if (destroyed || manualClose) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const deviceId = normalizeText(getDeviceId());
|
|
375
|
+
if (!deviceId || !socketUrl) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const activeRecord = readLeaderRecord();
|
|
380
|
+
if (
|
|
381
|
+
activeRecord &&
|
|
382
|
+
isActiveLeaderRecord(activeRecord) &&
|
|
383
|
+
activeRecord.instanceId !== instanceId
|
|
384
|
+
) {
|
|
385
|
+
const activeCandidate = {
|
|
386
|
+
instanceId: activeRecord.instanceId,
|
|
387
|
+
startedAt: Number(activeRecord.startedAt || 0),
|
|
388
|
+
};
|
|
389
|
+
const selfCandidate = {
|
|
390
|
+
instanceId,
|
|
391
|
+
startedAt,
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
if (!isHigherPriorityRuntime(selfCandidate, activeCandidate)) {
|
|
395
|
+
rememberLeader({
|
|
396
|
+
...activeCandidate,
|
|
397
|
+
lastSeenAt: Number(activeRecord.updatedAt || now()),
|
|
398
|
+
});
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (!channel) {
|
|
404
|
+
becomeLeader({reason});
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const claimRecord = buildLeaderRecord('claiming', reason);
|
|
409
|
+
|
|
410
|
+
writeLeaderRecord(claimRecord);
|
|
411
|
+
broadcast({
|
|
412
|
+
type: 'leader-claim',
|
|
413
|
+
...claimRecord,
|
|
414
|
+
deviceId,
|
|
415
|
+
reason,
|
|
416
|
+
at: claimRecord.updatedAt,
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
clearTimer(claimTimer);
|
|
420
|
+
claimTimer = setTimeoutImpl(() => {
|
|
421
|
+
claimTimer = null;
|
|
422
|
+
|
|
423
|
+
if (destroyed || manualClose || isLeader()) {
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (!shouldLeadNow()) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
becomeLeader({reason: 'claim-window'});
|
|
432
|
+
}, claimDelayMs);
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
const handleSocketDisconnect = ({
|
|
436
|
+
code = null,
|
|
437
|
+
reason = '',
|
|
438
|
+
errorMessage = '',
|
|
439
|
+
} = {}) => {
|
|
440
|
+
if (destroyed || manualClose || !isLeader()) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
stopSocket();
|
|
445
|
+
|
|
446
|
+
updateSocketState({
|
|
447
|
+
connected: false,
|
|
448
|
+
identified: false,
|
|
449
|
+
status: errorMessage ? 'error' : 'disconnected',
|
|
450
|
+
attempts: reconnectAttempt,
|
|
451
|
+
updatedAt: new Date().toISOString(),
|
|
452
|
+
device: normalizeText(getDeviceId()) || null,
|
|
453
|
+
error: errorMessage || reason || null,
|
|
454
|
+
code,
|
|
455
|
+
reason,
|
|
456
|
+
lastEventAt: socketStateSnapshot?.lastEventAt || null,
|
|
457
|
+
lastEventCount: socketStateSnapshot?.lastEventCount || 0,
|
|
458
|
+
lastStores: socketStateSnapshot?.lastStores || [],
|
|
459
|
+
lastCompanies: socketStateSnapshot?.lastCompanies || [],
|
|
460
|
+
});
|
|
461
|
+
broadcastState();
|
|
462
|
+
|
|
463
|
+
if (errorMessage) {
|
|
464
|
+
scheduleReconnect();
|
|
465
|
+
} else {
|
|
466
|
+
reconnectAttempt = 0;
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
const openSocket = () => {
|
|
471
|
+
if (
|
|
472
|
+
destroyed ||
|
|
473
|
+
manualClose ||
|
|
474
|
+
!isLeader() ||
|
|
475
|
+
!socketUrl ||
|
|
476
|
+
!normalizeText(getDeviceId()) ||
|
|
477
|
+
!WebSocketImpl
|
|
478
|
+
) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
if (
|
|
483
|
+
socket?.readyState === WebSocketImpl.OPEN ||
|
|
484
|
+
socket?.readyState === WebSocketImpl.CONNECTING
|
|
485
|
+
) {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const generation = socketGeneration + 1;
|
|
490
|
+
socketGeneration = generation;
|
|
491
|
+
const nextSocket = new WebSocketImpl(socketUrl);
|
|
492
|
+
socket = nextSocket;
|
|
493
|
+
|
|
494
|
+
nextSocket.onopen = () => {
|
|
495
|
+
if (!isCurrentSocket(nextSocket, generation)) {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
reconnectAttempt = 0;
|
|
500
|
+
updateSocketState({
|
|
501
|
+
connected: false,
|
|
502
|
+
identified: false,
|
|
503
|
+
status: 'open',
|
|
504
|
+
attempts: 0,
|
|
505
|
+
updatedAt: new Date().toISOString(),
|
|
506
|
+
device: normalizeText(getDeviceId()) || null,
|
|
507
|
+
error: null,
|
|
508
|
+
code: null,
|
|
509
|
+
reason: '',
|
|
510
|
+
lastEventAt: socketStateSnapshot?.lastEventAt || null,
|
|
511
|
+
lastEventCount: socketStateSnapshot?.lastEventCount || 0,
|
|
512
|
+
lastStores: socketStateSnapshot?.lastStores || [],
|
|
513
|
+
lastCompanies: socketStateSnapshot?.lastCompanies || [],
|
|
514
|
+
});
|
|
515
|
+
broadcastState();
|
|
516
|
+
|
|
517
|
+
clearTimer(identifyTimer);
|
|
518
|
+
identifyTimer = setTimeoutImpl(() => {
|
|
519
|
+
identifyTimer = null;
|
|
520
|
+
|
|
521
|
+
if (
|
|
522
|
+
destroyed ||
|
|
523
|
+
manualClose ||
|
|
524
|
+
!isCurrentSocket(nextSocket, generation) ||
|
|
525
|
+
nextSocket.readyState !== WebSocketImpl.OPEN
|
|
526
|
+
) {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
nextSocket.send(
|
|
531
|
+
JSON.stringify({
|
|
532
|
+
command: 'identify',
|
|
533
|
+
device: normalizeText(getDeviceId()),
|
|
534
|
+
}),
|
|
535
|
+
);
|
|
536
|
+
}, 150);
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
nextSocket.onmessage = event => {
|
|
540
|
+
if (
|
|
541
|
+
destroyed ||
|
|
542
|
+
manualClose ||
|
|
543
|
+
!isCurrentSocket(nextSocket, generation)
|
|
544
|
+
) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
const rawPayload = event?.data;
|
|
549
|
+
if (rawPayload == null || rawPayload === '') {
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const nextState = pipeline?.processPayload?.(rawPayload);
|
|
554
|
+
|
|
555
|
+
if (channel) {
|
|
556
|
+
broadcast({
|
|
557
|
+
type: 'socket-payload',
|
|
558
|
+
payload: rawPayload,
|
|
559
|
+
instanceId,
|
|
560
|
+
startedAt,
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (nextState?.status === 'error') {
|
|
565
|
+
handleSocketDisconnect({
|
|
566
|
+
errorMessage: nextState?.error || nextState?.reason || 'WebSocket error',
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
nextSocket.onerror = () => {
|
|
572
|
+
if (
|
|
573
|
+
destroyed ||
|
|
574
|
+
manualClose ||
|
|
575
|
+
!isCurrentSocket(nextSocket, generation)
|
|
576
|
+
) {
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
handleSocketDisconnect({
|
|
581
|
+
errorMessage: 'WebSocket error',
|
|
582
|
+
});
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
nextSocket.onclose = event => {
|
|
586
|
+
if (
|
|
587
|
+
destroyed ||
|
|
588
|
+
!isCurrentSocket(nextSocket, generation)
|
|
589
|
+
) {
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const wasManualClose = manualClose;
|
|
594
|
+
stopSocket();
|
|
595
|
+
|
|
596
|
+
if (wasManualClose) {
|
|
597
|
+
updateSocketState({
|
|
598
|
+
connected: false,
|
|
599
|
+
identified: false,
|
|
600
|
+
status: 'closed',
|
|
601
|
+
attempts: reconnectAttempt,
|
|
602
|
+
updatedAt: new Date().toISOString(),
|
|
603
|
+
device: normalizeText(getDeviceId()) || null,
|
|
604
|
+
error: null,
|
|
605
|
+
code: event?.code ?? null,
|
|
606
|
+
reason: event?.reason || '',
|
|
607
|
+
lastEventAt: socketStateSnapshot?.lastEventAt || null,
|
|
608
|
+
lastEventCount: socketStateSnapshot?.lastEventCount || 0,
|
|
609
|
+
lastStores: socketStateSnapshot?.lastStores || [],
|
|
610
|
+
lastCompanies: socketStateSnapshot?.lastCompanies || [],
|
|
611
|
+
});
|
|
612
|
+
broadcastState();
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
handleSocketDisconnect({
|
|
617
|
+
code: event?.code ?? null,
|
|
618
|
+
reason: event?.reason || '',
|
|
619
|
+
});
|
|
620
|
+
scheduleReconnect();
|
|
621
|
+
};
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
const handleChannelMessage = event => {
|
|
625
|
+
const message = event?.data;
|
|
626
|
+
if (!message || typeof message !== 'object') {
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
if (message.instanceId === instanceId) {
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (
|
|
635
|
+
message.type === 'leader-claim' ||
|
|
636
|
+
message.type === 'leader-heartbeat'
|
|
637
|
+
) {
|
|
638
|
+
const candidate = {
|
|
639
|
+
instanceId: message.instanceId,
|
|
640
|
+
startedAt: Number(message.startedAt || 0),
|
|
641
|
+
};
|
|
642
|
+
const selfCandidate = {
|
|
643
|
+
instanceId,
|
|
644
|
+
startedAt,
|
|
645
|
+
};
|
|
646
|
+
const bestKnownCandidate = leaderDescriptor || selfCandidate;
|
|
647
|
+
const shouldRememberLeader =
|
|
648
|
+
message.instanceId === bestKnownCandidate.instanceId ||
|
|
649
|
+
isHigherPriorityRuntime(candidate, bestKnownCandidate);
|
|
650
|
+
|
|
651
|
+
if (shouldRememberLeader) {
|
|
652
|
+
rememberLeader(message);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if (
|
|
656
|
+
isLeader() &&
|
|
657
|
+
isHigherPriorityRuntime(candidate, selfCandidate)
|
|
658
|
+
) {
|
|
659
|
+
relinquishLeadership({reason: 'better leader detected'});
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
if (claimTimer && isHigherPriorityRuntime(candidate, selfCandidate)) {
|
|
664
|
+
clearTimer(claimTimer);
|
|
665
|
+
claimTimer = null;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
if (message.type === 'leader-release') {
|
|
672
|
+
if (
|
|
673
|
+
leaderDescriptor?.instanceId &&
|
|
674
|
+
leaderDescriptor.instanceId === message.instanceId
|
|
675
|
+
) {
|
|
676
|
+
leaderDescriptor = null;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
if (!isLeader()) {
|
|
680
|
+
requestLeadership('leader-released');
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
if (message.type === 'socket-state') {
|
|
687
|
+
socketStateSnapshot = pipeline?.processPayload?.(message.state || message);
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (message.type === 'socket-payload') {
|
|
692
|
+
socketStateSnapshot = pipeline?.processPayload?.(message.payload);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
const startMonitor = () => {
|
|
697
|
+
clearIntervalTimer(monitorTimer);
|
|
698
|
+
monitorTimer = setInterval(() => {
|
|
699
|
+
if (destroyed || manualClose) {
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
if (isLeader()) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (!leaderDescriptor) {
|
|
708
|
+
const activeRecord = readLeaderRecord();
|
|
709
|
+
if (
|
|
710
|
+
activeRecord &&
|
|
711
|
+
isActiveLeaderRecord(activeRecord) &&
|
|
712
|
+
activeRecord.instanceId !== instanceId
|
|
713
|
+
) {
|
|
714
|
+
rememberLeader({
|
|
715
|
+
instanceId: activeRecord.instanceId,
|
|
716
|
+
startedAt: Number(activeRecord.startedAt || 0),
|
|
717
|
+
lastSeenAt: Number(activeRecord.updatedAt || now()),
|
|
718
|
+
});
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
requestLeadership('no-leader');
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const activeRecord = readLeaderRecord();
|
|
727
|
+
if (activeRecord && activeRecord.instanceId !== instanceId) {
|
|
728
|
+
if (isActiveLeaderRecord(activeRecord)) {
|
|
729
|
+
rememberLeader({
|
|
730
|
+
instanceId: activeRecord.instanceId,
|
|
731
|
+
startedAt: Number(activeRecord.startedAt || 0),
|
|
732
|
+
lastSeenAt: Number(activeRecord.updatedAt || now()),
|
|
733
|
+
});
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
if (now() - Number(leaderDescriptor.lastSeenAt || 0) > leaderStaleMs) {
|
|
739
|
+
requestLeadership('leader-stale');
|
|
740
|
+
}
|
|
741
|
+
}, 1000);
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
const start = () => {
|
|
745
|
+
destroyed = false;
|
|
746
|
+
manualClose = false;
|
|
747
|
+
|
|
748
|
+
if (BroadcastChannelImpl) {
|
|
749
|
+
channel = new BroadcastChannelImpl(channelName);
|
|
750
|
+
channel.onmessage = handleChannelMessage;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
startMonitor();
|
|
754
|
+
|
|
755
|
+
if (!channel) {
|
|
756
|
+
becomeLeader({reason: 'single-tab'});
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
requestLeadership('start');
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
const stop = () => {
|
|
764
|
+
manualClose = true;
|
|
765
|
+
destroyed = true;
|
|
766
|
+
|
|
767
|
+
clearTimer(claimTimer);
|
|
768
|
+
claimTimer = null;
|
|
769
|
+
clearTimer(heartbeatTimer);
|
|
770
|
+
heartbeatTimer = null;
|
|
771
|
+
clearTimer(identifyTimer);
|
|
772
|
+
identifyTimer = null;
|
|
773
|
+
clearTimer(reconnectTimer);
|
|
774
|
+
reconnectTimer = null;
|
|
775
|
+
clearIntervalTimer(monitorTimer);
|
|
776
|
+
monitorTimer = null;
|
|
777
|
+
|
|
778
|
+
if (channel) {
|
|
779
|
+
broadcast({
|
|
780
|
+
type: 'leader-release',
|
|
781
|
+
instanceId,
|
|
782
|
+
startedAt,
|
|
783
|
+
reason: 'stop',
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
channel.onmessage = null;
|
|
787
|
+
channel.close?.();
|
|
788
|
+
channel = null;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
stopSocket();
|
|
792
|
+
clearLeaderRecord(instanceId);
|
|
793
|
+
pipeline?.reset?.();
|
|
794
|
+
leaderDescriptor = null;
|
|
795
|
+
};
|
|
796
|
+
|
|
797
|
+
const isLeader = () => {
|
|
798
|
+
if (manualClose || destroyed) {
|
|
799
|
+
return false;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
if (!channel) {
|
|
803
|
+
return true;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
return leaderDescriptor?.instanceId === instanceId;
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
const refreshFooter = () => {
|
|
810
|
+
pipeline?.refreshFooter?.();
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
return {
|
|
814
|
+
becomeLeader,
|
|
815
|
+
broadcastState,
|
|
816
|
+
handleChannelMessage,
|
|
817
|
+
handleSocketDisconnect,
|
|
818
|
+
isLeader,
|
|
819
|
+
openSocket,
|
|
820
|
+
refreshFooter,
|
|
821
|
+
relinquishLeadership,
|
|
822
|
+
requestLeadership,
|
|
823
|
+
start,
|
|
824
|
+
stop,
|
|
825
|
+
};
|
|
826
|
+
};
|