@olenbetong/synergi-react 4.3.0 → 4.3.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.
|
@@ -3,6 +3,8 @@ import { Box, Button, DialogActions, LinearProgress, Link, Typography } from "@m
|
|
|
3
3
|
import { ResponsiveDialog } from "@olenbetong/appframe-mui";
|
|
4
4
|
import { useEffect, useRef, useState } from "react";
|
|
5
5
|
import { getLocalizedString } from "../../i18n.js";
|
|
6
|
+
/** Waits between reconnects; together they cover a server restart of about half a minute. */
|
|
7
|
+
const RECONNECT_DELAYS_MS = [1000, 2000, 3000, 5000, 8000, 13000];
|
|
6
8
|
function formatRemaining(ms) {
|
|
7
9
|
if (ms < 60_000)
|
|
8
10
|
return `~${Math.ceil(ms / 1000)}s`;
|
|
@@ -31,6 +33,7 @@ export function MergeProgressDialog({ open, onClose, getMergerId, mergeType = "p
|
|
|
31
33
|
doneRef.current = false;
|
|
32
34
|
let cancelled = false;
|
|
33
35
|
let es = null;
|
|
36
|
+
let retryTimer;
|
|
34
37
|
async function start() {
|
|
35
38
|
try {
|
|
36
39
|
let mergerId = await getMergerId();
|
|
@@ -42,43 +45,59 @@ export function MergeProgressDialog({ open, onClose, getMergerId, mergeType = "p
|
|
|
42
45
|
let { jobId } = await response.json();
|
|
43
46
|
if (cancelled)
|
|
44
47
|
return;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
48
|
+
// The server keeps a job's state on disk and runs it again when the process running it
|
|
49
|
+
// goes away (a deploy or an app pool recycle), so a dropped connection is retried with
|
|
50
|
+
// a backoff before the merge is reported as failed. EventSource only reconnects by
|
|
51
|
+
// itself after a clean drop, not after the 502/503 a restarting server answers with.
|
|
52
|
+
let attempt = 0;
|
|
53
|
+
function connect() {
|
|
54
|
+
es = new EventSource(`/api/ob/merger/events/${jobId}`);
|
|
55
|
+
esRef.current = es;
|
|
56
|
+
es.addEventListener("progress", (e) => {
|
|
57
|
+
attempt = 0;
|
|
58
|
+
let d = JSON.parse(e.data);
|
|
59
|
+
setPhase((prev) => ({
|
|
60
|
+
kind: "processing",
|
|
61
|
+
completed: d.completed,
|
|
62
|
+
total: d.total,
|
|
63
|
+
currentFile: d.currentFile ?? "",
|
|
64
|
+
startedAt: prev.kind === "processing" ? prev.startedAt : Date.now(),
|
|
65
|
+
}));
|
|
66
|
+
setNow(Date.now());
|
|
67
|
+
});
|
|
68
|
+
es.addEventListener("done", (e) => {
|
|
69
|
+
doneRef.current = true;
|
|
70
|
+
es?.close();
|
|
71
|
+
let d = JSON.parse(e.data);
|
|
72
|
+
setPhase({ kind: "completed", jobId, fileName: d.fileName ?? "" });
|
|
73
|
+
window.open(`/api/ob/merger/download/${jobId}`);
|
|
74
|
+
});
|
|
75
|
+
// Catches both server-sent "event: error" (with data) and connection errors.
|
|
76
|
+
// After a "done" event the server closes the stream, which also fires error —
|
|
77
|
+
// doneRef guards against treating that as a failure.
|
|
78
|
+
es.addEventListener("error", (e) => {
|
|
79
|
+
if (doneRef.current)
|
|
80
|
+
return;
|
|
81
|
+
es?.close();
|
|
82
|
+
let data = e.data;
|
|
83
|
+
if (!data && attempt < RECONNECT_DELAYS_MS.length) {
|
|
84
|
+
retryTimer = setTimeout(() => {
|
|
85
|
+
if (!cancelled && !doneRef.current)
|
|
86
|
+
connect();
|
|
87
|
+
}, RECONNECT_DELAYS_MS[attempt++]);
|
|
88
|
+
return;
|
|
77
89
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
let message = getLocalizedString("Unknown error");
|
|
91
|
+
if (data) {
|
|
92
|
+
try {
|
|
93
|
+
message = JSON.parse(data).message ?? message;
|
|
94
|
+
}
|
|
95
|
+
catch { }
|
|
96
|
+
}
|
|
97
|
+
setPhase({ kind: "failed", error: message });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
connect();
|
|
82
101
|
}
|
|
83
102
|
catch (err) {
|
|
84
103
|
if (!cancelled) {
|
|
@@ -89,6 +108,7 @@ export function MergeProgressDialog({ open, onClose, getMergerId, mergeType = "p
|
|
|
89
108
|
start();
|
|
90
109
|
return () => {
|
|
91
110
|
cancelled = true;
|
|
111
|
+
clearTimeout(retryTimer);
|
|
92
112
|
es?.close();
|
|
93
113
|
esRef.current = null;
|
|
94
114
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/synergi-react",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.1",
|
|
4
4
|
"description": "Standalone React component for SynergiWeb and Partner Portal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
"@mui/icons-material": "9.4.0",
|
|
45
45
|
"@mui/material": "9.4.0",
|
|
46
46
|
"@olenbetong/appframe-core": "^2.15.2",
|
|
47
|
-
"@olenbetong/appframe-data": "^1.8.
|
|
48
|
-
"@olenbetong/appframe-ds": "^1.
|
|
49
|
-
"@olenbetong/appframe-mui": "^7.0.
|
|
47
|
+
"@olenbetong/appframe-data": "^1.8.3",
|
|
48
|
+
"@olenbetong/appframe-ds": "^1.5.0",
|
|
49
|
+
"@olenbetong/appframe-mui": "^7.0.11",
|
|
50
50
|
"@olenbetong/appframe-react": "^2.2.7",
|
|
51
51
|
"clsx": "^2.1.1",
|
|
52
52
|
"filepond": "^4.32.12",
|
|
@@ -17,6 +17,9 @@ type MergePhase =
|
|
|
17
17
|
| { kind: "completed"; jobId: string; fileName: string }
|
|
18
18
|
| { kind: "failed"; error: string };
|
|
19
19
|
|
|
20
|
+
/** Waits between reconnects; together they cover a server restart of about half a minute. */
|
|
21
|
+
const RECONNECT_DELAYS_MS = [1000, 2000, 3000, 5000, 8000, 13000];
|
|
22
|
+
|
|
20
23
|
function formatRemaining(ms: number) {
|
|
21
24
|
if (ms < 60_000) return `~${Math.ceil(ms / 1000)}s`;
|
|
22
25
|
let mins = Math.floor(ms / 60_000);
|
|
@@ -47,6 +50,7 @@ export function MergeProgressDialog({ open, onClose, getMergerId, mergeType = "p
|
|
|
47
50
|
|
|
48
51
|
let cancelled = false;
|
|
49
52
|
let es: EventSource | null = null;
|
|
53
|
+
let retryTimer: ReturnType<typeof setTimeout> | undefined;
|
|
50
54
|
|
|
51
55
|
async function start() {
|
|
52
56
|
try {
|
|
@@ -58,44 +62,61 @@ export function MergeProgressDialog({ open, onClose, getMergerId, mergeType = "p
|
|
|
58
62
|
let { jobId } = await response.json();
|
|
59
63
|
if (cancelled) return;
|
|
60
64
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
65
|
+
// The server keeps a job's state on disk and runs it again when the process running it
|
|
66
|
+
// goes away (a deploy or an app pool recycle), so a dropped connection is retried with
|
|
67
|
+
// a backoff before the merge is reported as failed. EventSource only reconnects by
|
|
68
|
+
// itself after a clean drop, not after the 502/503 a restarting server answers with.
|
|
69
|
+
let attempt = 0;
|
|
70
|
+
|
|
71
|
+
function connect() {
|
|
72
|
+
es = new EventSource(`/api/ob/merger/events/${jobId}`);
|
|
73
|
+
esRef.current = es;
|
|
74
|
+
|
|
75
|
+
es.addEventListener("progress", (e) => {
|
|
76
|
+
attempt = 0;
|
|
77
|
+
let d = JSON.parse((e as MessageEvent).data);
|
|
78
|
+
setPhase((prev) => ({
|
|
79
|
+
kind: "processing",
|
|
80
|
+
completed: d.completed,
|
|
81
|
+
total: d.total,
|
|
82
|
+
currentFile: d.currentFile ?? "",
|
|
83
|
+
startedAt: prev.kind === "processing" ? prev.startedAt : Date.now(),
|
|
84
|
+
}));
|
|
85
|
+
setNow(Date.now());
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
es.addEventListener("done", (e) => {
|
|
89
|
+
doneRef.current = true;
|
|
90
|
+
es?.close();
|
|
91
|
+
let d = JSON.parse((e as MessageEvent).data);
|
|
92
|
+
setPhase({ kind: "completed", jobId, fileName: d.fileName ?? "" });
|
|
93
|
+
window.open(`/api/ob/merger/download/${jobId}`);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Catches both server-sent "event: error" (with data) and connection errors.
|
|
97
|
+
// After a "done" event the server closes the stream, which also fires error —
|
|
98
|
+
// doneRef guards against treating that as a failure.
|
|
99
|
+
es.addEventListener("error", (e) => {
|
|
100
|
+
if (doneRef.current) return;
|
|
101
|
+
es?.close();
|
|
102
|
+
let data = (e as MessageEvent).data;
|
|
103
|
+
if (!data && attempt < RECONNECT_DELAYS_MS.length) {
|
|
104
|
+
retryTimer = setTimeout(() => {
|
|
105
|
+
if (!cancelled && !doneRef.current) connect();
|
|
106
|
+
}, RECONNECT_DELAYS_MS[attempt++]);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
let message = getLocalizedString("Unknown error");
|
|
110
|
+
if (data) {
|
|
111
|
+
try {
|
|
112
|
+
message = JSON.parse(data).message ?? message;
|
|
113
|
+
} catch {}
|
|
114
|
+
}
|
|
115
|
+
setPhase({ kind: "failed", error: message });
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
connect();
|
|
99
120
|
} catch (err: unknown) {
|
|
100
121
|
if (!cancelled) {
|
|
101
122
|
setPhase({ kind: "failed", error: (err as Error)?.message ?? String(err) });
|
|
@@ -107,6 +128,7 @@ export function MergeProgressDialog({ open, onClose, getMergerId, mergeType = "p
|
|
|
107
128
|
|
|
108
129
|
return () => {
|
|
109
130
|
cancelled = true;
|
|
131
|
+
clearTimeout(retryTimer);
|
|
110
132
|
es?.close();
|
|
111
133
|
esRef.current = null;
|
|
112
134
|
};
|