@michengai/dsh-archive-manager 0.1.38 → 0.1.40

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.
@@ -0,0 +1,68 @@
1
+ class ArchiveNavigationError extends Error {
2
+ constructor(code) {
3
+ super(code);
4
+ this.name = "ArchiveNavigationError";
5
+ this.code = code;
6
+ }
7
+ }
8
+ function formatArchiveNavigationError(error, t) {
9
+ return error instanceof ArchiveNavigationError ? t(`archives.${error.code}`) : String(error?.message ?? error);
10
+ }
11
+ async function openArchivedConversation(actions, sessionId, restore, isActive = () => true) {
12
+ if (restore) await actions.restore(sessionId);
13
+ if (isActive()) await actions.open(sessionId);
14
+ }
15
+ function allowArchivedNavigation(navigation, sessions, workspaces, { onOpened = () => {
16
+ }, warn = (...args) => console.warn(...args) } = {}) {
17
+ let allowed;
18
+ const original = navigation?.clearArchivedCurrent;
19
+ const descriptor = navigation && Object.getOwnPropertyDescriptor(navigation, "clearArchivedCurrent");
20
+ const wrapped = function(...args) {
21
+ const current = sessions.list.getSnapshot().current;
22
+ if (current === allowed && current !== void 0 && workspaces.list.getSnapshot().archivedSessionIds.includes(current)) return false;
23
+ allowed = void 0;
24
+ return original.apply(this, args);
25
+ };
26
+ const ownsWrapper = () => navigation && Object.getOwnPropertyDescriptor(navigation, "clearArchivedCurrent")?.value === wrapped;
27
+ if (typeof original === "function") {
28
+ try {
29
+ navigation.clearArchivedCurrent = wrapped;
30
+ if (!ownsWrapper()) throw new Error("\u5BFC\u822A\u65B9\u6CD5\u672A\u63A5\u7EB3\u5F52\u6863\u9002\u914D");
31
+ } catch (error) {
32
+ warn("archive-manager: \u65E0\u6CD5\u9002\u914D\u5F52\u6863\u5BFC\u822A\uFF0C\u8BF7\u6062\u590D\u4F1A\u8BDD\u540E\u6253\u5F00\u3002", error);
33
+ }
34
+ }
35
+ return {
36
+ open(id) {
37
+ allowed = id;
38
+ try {
39
+ if (typeof original === "function" && !ownsWrapper() && workspaces.list.getSnapshot().archivedSessionIds.includes(id)) {
40
+ warn("archive-manager: \u5F52\u6863\u5BFC\u822A\u9002\u914D\u4E0D\u53EF\u7528\u6216\u5DF2\u88AB\u66FF\u6362\u3002");
41
+ throw new ArchiveNavigationError("navigationUnavailable");
42
+ }
43
+ sessions.open(id);
44
+ if (sessions.list.getSnapshot().current !== id) {
45
+ warn("archive-manager: \u5BBF\u4E3B\u672A\u4FDD\u7559\u76EE\u6807\u4F1A\u8BDD\u3002", { cleanupAvailable: typeof original === "function" });
46
+ throw new ArchiveNavigationError("sessionNotRetained");
47
+ }
48
+ onOpened();
49
+ } catch (error) {
50
+ allowed = void 0;
51
+ throw error;
52
+ }
53
+ },
54
+ dispose() {
55
+ allowed = void 0;
56
+ if (!navigation || Object.getOwnPropertyDescriptor(navigation, "clearArchivedCurrent")?.value !== wrapped) return;
57
+ if (descriptor) Object.defineProperty(navigation, "clearArchivedCurrent", descriptor);
58
+ else delete navigation.clearArchivedCurrent;
59
+ original.call(navigation);
60
+ }
61
+ };
62
+ }
63
+ export {
64
+ ArchiveNavigationError,
65
+ allowArchivedNavigation,
66
+ formatArchiveNavigationError,
67
+ openArchivedConversation
68
+ };