agl 1.0.17 → 21.0.2
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/.smartconfig.json +55 -0
- package/changelog.md +1026 -0
- package/cli.js +14 -0
- package/dist_serve/bundle.js +44778 -0
- package/dist_serve/index.html +13 -0
- package/dist_ts/00_commitinfo_data.d.ts +8 -0
- package/dist_ts/00_commitinfo_data.js +9 -0
- package/dist_ts/classes.authmodels.d.ts +126 -0
- package/dist_ts/classes.authmodels.js +1422 -0
- package/dist_ts/classes.authstore.d.ts +160 -0
- package/dist_ts/classes.authstore.js +2483 -0
- package/dist_ts/classes.browserresourcehost.d.ts +85 -0
- package/dist_ts/classes.browserresourcehost.js +912 -0
- package/dist_ts/classes.cli.d.ts +21 -0
- package/dist_ts/classes.cli.js +1082 -0
- package/dist_ts/classes.config.d.ts +23 -0
- package/dist_ts/classes.config.js +267 -0
- package/dist_ts/classes.controller.d.ts +523 -0
- package/dist_ts/classes.controller.js +10611 -0
- package/dist_ts/classes.controllermanagement.d.ts +32 -0
- package/dist_ts/classes.controllermanagement.js +222 -0
- package/dist_ts/classes.draftmanager.d.ts +30 -0
- package/dist_ts/classes.draftmanager.js +179 -0
- package/dist_ts/classes.embeddeddb.d.ts +30 -0
- package/dist_ts/classes.embeddeddb.js +159 -0
- package/dist_ts/classes.flexframedtransport.d.ts +56 -0
- package/dist_ts/classes.flexframedtransport.js +275 -0
- package/dist_ts/classes.flexmodels.private.d.ts +231 -0
- package/dist_ts/classes.flexmodels.private.js +985 -0
- package/dist_ts/classes.flexmodels.public.d.ts +207 -0
- package/dist_ts/classes.flexmodels.public.js +885 -0
- package/dist_ts/classes.flexprojectionreader.d.ts +89 -0
- package/dist_ts/classes.flexprojectionreader.js +580 -0
- package/dist_ts/classes.flexservice.d.ts +196 -0
- package/dist_ts/classes.flexservice.js +3704 -0
- package/dist_ts/classes.flexstore.d.ts +174 -0
- package/dist_ts/classes.flexstore.js +3400 -0
- package/dist_ts/classes.flexsupervisor.d.ts +105 -0
- package/dist_ts/classes.flexsupervisor.js +1191 -0
- package/dist_ts/classes.gitreversion.d.ts +431 -0
- package/dist_ts/classes.gitreversion.js +10592 -0
- package/dist_ts/classes.harnessoperationmanager.d.ts +113 -0
- package/dist_ts/classes.harnessoperationmanager.js +602 -0
- package/dist_ts/classes.opencodeclient.d.ts +146 -0
- package/dist_ts/classes.opencodeclient.js +3003 -0
- package/dist_ts/classes.opencodesupervisor.d.ts +60 -0
- package/dist_ts/classes.opencodesupervisor.js +1003 -0
- package/dist_ts/classes.passkeymanager.d.ts +34 -0
- package/dist_ts/classes.passkeymanager.js +456 -0
- package/dist_ts/classes.processinspection.d.ts +50 -0
- package/dist_ts/classes.processinspection.js +484 -0
- package/dist_ts/classes.resourcecoordinator.d.ts +64 -0
- package/dist_ts/classes.resourcecoordinator.js +294 -0
- package/dist_ts/classes.sessionintelligencereader.d.ts +13 -0
- package/dist_ts/classes.sessionintelligencereader.js +40 -0
- package/dist_ts/classes.systemmetricssampler.d.ts +53 -0
- package/dist_ts/classes.systemmetricssampler.js +168 -0
- package/dist_ts/classes.terminalmanager.d.ts +95 -0
- package/dist_ts/classes.terminalmanager.js +981 -0
- package/dist_ts/classes.terminalresourcehost.d.ts +20 -0
- package/dist_ts/classes.terminalresourcehost.js +84 -0
- package/dist_ts/classes.upgradecoordinator.d.ts +244 -0
- package/dist_ts/classes.upgradecoordinator.js +2155 -0
- package/dist_ts/classes.upgradetransaction.d.ts +92 -0
- package/dist_ts/classes.upgradetransaction.js +2125 -0
- package/dist_ts/classes.uploadmanager.d.ts +24 -0
- package/dist_ts/classes.uploadmanager.js +210 -0
- package/dist_ts/classes.validation.d.ts +91 -0
- package/dist_ts/classes.validation.js +1087 -0
- package/dist_ts/flexharness.child.d.ts +1 -0
- package/dist_ts/flexharness.child.js +28 -0
- package/dist_ts/functions.canonicaldirectory.d.ts +21 -0
- package/dist_ts/functions.canonicaldirectory.js +144 -0
- package/dist_ts/functions.controllerdataroot.d.ts +15 -0
- package/dist_ts/functions.controllerdataroot.js +76 -0
- package/dist_ts/functions.embeddeddb.d.ts +2 -0
- package/dist_ts/functions.embeddeddb.js +21 -0
- package/dist_ts/functions.flexcredentialstore.d.ts +16 -0
- package/dist_ts/functions.flexcredentialstore.js +93 -0
- package/dist_ts/functions.opencodeauth.d.ts +3 -0
- package/dist_ts/functions.opencodeauth.js +31 -0
- package/dist_ts/functions.runtimeenvironment.d.ts +2 -0
- package/dist_ts/functions.runtimeenvironment.js +79 -0
- package/dist_ts/functions.upgradefollower.d.ts +3 -0
- package/dist_ts/functions.upgradefollower.js +95 -0
- package/dist_ts/index.d.ts +24 -0
- package/dist_ts/index.js +25 -0
- package/dist_ts/interfaces.auth.d.ts +181 -0
- package/dist_ts/interfaces.auth.js +82 -0
- package/dist_ts/interfaces.config.d.ts +34 -0
- package/dist_ts/interfaces.config.js +2 -0
- package/dist_ts/interfaces.flexipc.d.ts +939 -0
- package/dist_ts/interfaces.flexipc.js +1492 -0
- package/dist_ts/interfaces.gitreversion.d.ts +246 -0
- package/dist_ts/interfaces.gitreversion.js +2 -0
- package/dist_ts/interfaces.lifecycle.d.ts +37 -0
- package/dist_ts/interfaces.lifecycle.js +2 -0
- package/dist_ts/interfaces.opencode.d.ts +101 -0
- package/dist_ts/interfaces.opencode.js +3 -0
- package/dist_ts/interfaces.projects.d.ts +156 -0
- package/dist_ts/interfaces.projects.js +5 -0
- package/dist_ts/plugins.d.ts +36 -0
- package/dist_ts/plugins.js +40 -0
- package/dist_ts_interfaces/index.d.ts +2 -0
- package/dist_ts_interfaces/index.js +3 -0
- package/dist_ts_interfaces/interfaces.d.ts +747 -0
- package/dist_ts_interfaces/interfaces.js +47 -0
- package/dist_ts_interfaces/plugins.d.ts +4 -0
- package/dist_ts_interfaces/plugins.js +3 -0
- package/dist_ts_interfaces/requests.d.ts +806 -0
- package/dist_ts_interfaces/requests.js +2 -0
- package/dist_ts_migration/classes.documentmigrationrunner.d.ts +8 -0
- package/dist_ts_migration/classes.documentmigrationrunner.js +24 -0
- package/dist_ts_migration/classes.flexmigrationrunner.d.ts +11 -0
- package/dist_ts_migration/classes.flexmigrationrunner.js +25 -0
- package/dist_ts_migration/classes.flexpostharnessmigrationrunner.d.ts +10 -0
- package/dist_ts_migration/classes.flexpostharnessmigrationrunner.js +15 -0
- package/dist_ts_migration/classes.migrationrunner.d.ts +8 -0
- package/dist_ts_migration/classes.migrationrunner.js +34 -0
- package/dist_ts_migration/index.d.ts +12 -0
- package/dist_ts_migration/index.js +13 -0
- package/dist_ts_migration/v10_flexharnessv3migration.d.ts +11 -0
- package/dist_ts_migration/v10_flexharnessv3migration.js +23 -0
- package/dist_ts_migration/v13_flexprojectionorder.d.ts +11 -0
- package/dist_ts_migration/v13_flexprojectionorder.js +23 -0
- package/dist_ts_migration/v16_flexprovidercredentials.d.ts +22 -0
- package/dist_ts_migration/v16_flexprovidercredentials.js +149 -0
- package/dist_ts_migration/v18_resourcelayout.d.ts +22 -0
- package/dist_ts_migration/v18_resourcelayout.js +23 -0
- package/dist_ts_migration/v18_resourcelayoutmigration.d.ts +6 -0
- package/dist_ts_migration/v18_resourcelayoutmigration.js +61 -0
- package/dist_ts_migration/v19_flexprojectmanagement.d.ts +11 -0
- package/dist_ts_migration/v19_flexprojectmanagement.js +21 -0
- package/dist_ts_migration/v1_embeddeddatabaselocation.d.ts +28 -0
- package/dist_ts_migration/v1_embeddeddatabaselocation.js +415 -0
- package/dist_ts_migration/v22_projectfilesystemidentitymigration.d.ts +7 -0
- package/dist_ts_migration/v22_projectfilesystemidentitymigration.js +105 -0
- package/dist_ts_migration/v2_controllerdataroot.d.ts +130 -0
- package/dist_ts_migration/v2_controllerdataroot.js +1887 -0
- package/dist_ts_migration/v6_protocoldocumentmigration.d.ts +7 -0
- package/dist_ts_migration/v6_protocoldocumentmigration.js +100 -0
- package/dist_ts_migration/v6_protocoldocuments.d.ts +7 -0
- package/dist_ts_migration/v6_protocoldocuments.js +138 -0
- package/html/index.html +13 -0
- package/{LICENSE → license.md} +2 -2
- package/package.json +86 -48
- package/readme.md +480 -0
- package/readme.plan.md +188 -0
- package/ts/00_commitinfo_data.ts +8 -0
- package/ts/classes.authmodels.ts +1245 -0
- package/ts/classes.authstore.ts +3274 -0
- package/ts/classes.browserresourcehost.ts +1205 -0
- package/ts/classes.cli.ts +1254 -0
- package/ts/classes.config.ts +351 -0
- package/ts/classes.controller.ts +14426 -0
- package/ts/classes.controllermanagement.ts +330 -0
- package/ts/classes.draftmanager.ts +232 -0
- package/ts/classes.embeddeddb.ts +166 -0
- package/ts/classes.flexframedtransport.ts +283 -0
- package/ts/classes.flexmodels.private.ts +823 -0
- package/ts/classes.flexmodels.public.ts +732 -0
- package/ts/classes.flexprojectionreader.ts +816 -0
- package/ts/classes.flexservice.ts +4302 -0
- package/ts/classes.flexstore.ts +4565 -0
- package/ts/classes.flexsupervisor.ts +1453 -0
- package/ts/classes.gitreversion.ts +13353 -0
- package/ts/classes.harnessoperationmanager.ts +870 -0
- package/ts/classes.opencodeclient.ts +4110 -0
- package/ts/classes.opencodesupervisor.ts +1280 -0
- package/ts/classes.passkeymanager.ts +570 -0
- package/ts/classes.processinspection.ts +623 -0
- package/ts/classes.resourcecoordinator.ts +436 -0
- package/ts/classes.sessionintelligencereader.ts +52 -0
- package/ts/classes.systemmetricssampler.ts +215 -0
- package/ts/classes.terminalmanager.ts +1207 -0
- package/ts/classes.terminalresourcehost.ts +122 -0
- package/ts/classes.upgradecoordinator.ts +2675 -0
- package/ts/classes.upgradetransaction.ts +2900 -0
- package/ts/classes.uploadmanager.ts +240 -0
- package/ts/classes.validation.ts +1409 -0
- package/ts/flexharness.child.ts +35 -0
- package/ts/functions.canonicaldirectory.ts +213 -0
- package/ts/functions.controllerdataroot.ts +107 -0
- package/ts/functions.embeddeddb.ts +22 -0
- package/ts/functions.flexcredentialstore.ts +128 -0
- package/ts/functions.opencodeauth.ts +34 -0
- package/ts/functions.runtimeenvironment.ts +90 -0
- package/ts/functions.upgradefollower.ts +115 -0
- package/ts/index.ts +36 -6
- package/ts/interfaces.auth.ts +316 -0
- package/ts/interfaces.config.ts +37 -0
- package/ts/interfaces.flexipc.ts +2586 -0
- package/ts/interfaces.gitreversion.ts +336 -0
- package/ts/interfaces.lifecycle.ts +46 -0
- package/ts/interfaces.opencode.ts +153 -0
- package/ts/interfaces.projects.ts +182 -0
- package/ts/plugins.ts +64 -0
- package/ts_interfaces/index.ts +2 -0
- package/ts_interfaces/interfaces.ts +827 -0
- package/ts_interfaces/plugins.ts +15 -0
- package/ts_interfaces/requests.ts +1186 -0
- package/ts_migration/classes.documentmigrationrunner.ts +32 -0
- package/ts_migration/classes.flexmigrationrunner.ts +33 -0
- package/ts_migration/classes.flexpostharnessmigrationrunner.ts +22 -0
- package/ts_migration/classes.migrationrunner.ts +40 -0
- package/ts_migration/index.ts +12 -0
- package/ts_migration/v10_flexharnessv3migration.ts +35 -0
- package/ts_migration/v13_flexprojectionorder.ts +33 -0
- package/ts_migration/v16_flexprovidercredentials.ts +196 -0
- package/ts_migration/v18_resourcelayout.ts +55 -0
- package/ts_migration/v18_resourcelayoutmigration.ts +68 -0
- package/ts_migration/v19_flexprojectmanagement.ts +28 -0
- package/ts_migration/v1_embeddeddatabaselocation.ts +560 -0
- package/ts_migration/v22_projectfilesystemidentitymigration.ts +121 -0
- package/ts_migration/v2_controllerdataroot.ts +2349 -0
- package/ts_migration/v6_protocoldocumentmigration.ts +144 -0
- package/ts_migration/v6_protocoldocuments.ts +198 -0
- package/ts_web/00_commitinfo_data.ts +8 -0
- package/ts_web/classes.controllersocketclient.ts +517 -0
- package/ts_web/classes.sessiondraftsync.ts +390 -0
- package/ts_web/elements.harnesscontrollerapp.ts +11494 -0
- package/ts_web/index.ts +2 -0
- package/ts_web/plugins.ts +31 -0
- package/.gitlab-ci.yml +0 -71
- package/.npmignore +0 -5
- package/README.md +0 -40
- package/dist/cli.js +0 -2
- package/dist/index.d.ts +0 -0
- package/dist/index.js +0 -9
- package/dist/mod_bundle/index.d.ts +0 -1
- package/dist/mod_bundle/index.js +0 -18
- package/dist/mod_bundle/mod.plugins.d.ts +0 -4
- package/dist/mod_bundle/mod.plugins.js +0 -11
- package/dist/mod_install/index.d.ts +0 -4
- package/dist/mod_install/index.js +0 -21
- package/dist/mod_install/mod.plugins.d.ts +0 -1
- package/dist/mod_install/mod.plugins.js +0 -7
- package/dist/mod_standard/index.d.ts +0 -4
- package/dist/mod_standard/index.js +0 -21
- package/dist/mod_standard/mod.browser.d.ts +0 -6
- package/dist/mod_standard/mod.browser.js +0 -14
- package/dist/mod_standard/mod.clean.d.ts +0 -4
- package/dist/mod_standard/mod.clean.js +0 -12
- package/dist/mod_standard/mod.compile.d.ts +0 -4
- package/dist/mod_standard/mod.compile.js +0 -21
- package/dist/mod_standard/mod.plugins.d.ts +0 -8
- package/dist/mod_standard/mod.plugins.js +0 -19
- package/dist/mod_standard/mod.test.d.ts +0 -4
- package/dist/mod_standard/mod.test.js +0 -12
- package/dist/plr.cli.d.ts +0 -3
- package/dist/plr.cli.js +0 -57
- package/dist/plr.config.d.ts +0 -11
- package/dist/plr.config.js +0 -26
- package/dist/plr.mods.d.ts +0 -17
- package/dist/plr.mods.js +0 -17
- package/dist/plr.paths.d.ts +0 -19
- package/dist/plr.paths.js +0 -26
- package/dist/plr.plugins.d.ts +0 -8
- package/dist/plr.plugins.js +0 -16
- package/npmextra.json +0 -10
- package/ts/mod_bundle/index.ts +0 -20
- package/ts/mod_bundle/mod.plugins.ts +0 -9
- package/ts/mod_install/index.ts +0 -20
- package/ts/mod_install/mod.plugins.ts +0 -1
- package/ts/mod_standard/index.ts +0 -23
- package/ts/mod_standard/mod.browser.ts +0 -17
- package/ts/mod_standard/mod.clean.ts +0 -13
- package/ts/mod_standard/mod.compile.ts +0 -23
- package/ts/mod_standard/mod.plugins.ts +0 -17
- package/ts/mod_standard/mod.test.ts +0 -13
- package/ts/plr.cli.ts +0 -60
- package/ts/plr.config.ts +0 -36
- package/ts/plr.mods.ts +0 -25
- package/ts/plr.paths.ts +0 -29
- package/ts/plr.plugins.ts +0 -17
- package/tslint.json +0 -3
- package/yarn.lock +0 -3464
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IControllerDraftAttachment,
|
|
3
|
+
IControllerRuntimeId,
|
|
4
|
+
IControllerSessionDraft,
|
|
5
|
+
IControllerSessionDraftUpdate,
|
|
6
|
+
} from '../ts_interfaces/interfaces.js';
|
|
7
|
+
|
|
8
|
+
const defaultDebounceMs = 150;
|
|
9
|
+
const maximumConflictRetries = 4;
|
|
10
|
+
|
|
11
|
+
export interface ISessionDraftSyncTransport {
|
|
12
|
+
getDraft(
|
|
13
|
+
projectIdArg: string,
|
|
14
|
+
sessionIdArg: IControllerRuntimeId,
|
|
15
|
+
): Promise<IControllerSessionDraft>;
|
|
16
|
+
updateDraft(
|
|
17
|
+
projectIdArg: string,
|
|
18
|
+
sessionIdArg: IControllerRuntimeId,
|
|
19
|
+
expectedRevisionArg: number,
|
|
20
|
+
patchArg: { text?: string; attachments?: IControllerDraftAttachment[] },
|
|
21
|
+
): Promise<IControllerSessionDraft>;
|
|
22
|
+
isConflict(errorArg: unknown): boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ISessionDraftSyncState extends IControllerSessionDraft {
|
|
26
|
+
projectId: string;
|
|
27
|
+
sessionId: IControllerRuntimeId;
|
|
28
|
+
loading: boolean;
|
|
29
|
+
saving: boolean;
|
|
30
|
+
error: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ISessionDraftSyncOptions {
|
|
34
|
+
transport: ISessionDraftSyncTransport;
|
|
35
|
+
onState: (stateArg: ISessionDraftSyncState | undefined) => void;
|
|
36
|
+
onError?: (errorArg: unknown) => void;
|
|
37
|
+
debounceMs?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface IActiveDraft {
|
|
41
|
+
projectId: string;
|
|
42
|
+
sessionId: IControllerRuntimeId;
|
|
43
|
+
generation: number;
|
|
44
|
+
authoritative: IControllerSessionDraft;
|
|
45
|
+
desiredText: string;
|
|
46
|
+
desiredAttachments: IControllerDraftAttachment[];
|
|
47
|
+
dirtyText: boolean;
|
|
48
|
+
dirtyAttachments: boolean;
|
|
49
|
+
drainAfterDeactivate: boolean;
|
|
50
|
+
loading: boolean;
|
|
51
|
+
saving: boolean;
|
|
52
|
+
error: string;
|
|
53
|
+
debounceTimer?: ReturnType<typeof setTimeout>;
|
|
54
|
+
hydrateTask?: Promise<void>;
|
|
55
|
+
writeTask?: Promise<IControllerSessionDraft>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const cloneAttachments = (
|
|
59
|
+
attachmentsArg: readonly IControllerDraftAttachment[],
|
|
60
|
+
): IControllerDraftAttachment[] => attachmentsArg.map((attachment) => ({ ...attachment }));
|
|
61
|
+
|
|
62
|
+
const cloneDraft = (draftArg: IControllerSessionDraft): IControllerSessionDraft => ({
|
|
63
|
+
text: draftArg.text,
|
|
64
|
+
attachments: cloneAttachments(draftArg.attachments),
|
|
65
|
+
revision: draftArg.revision,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const attachmentsEqual = (
|
|
69
|
+
leftArg: readonly IControllerDraftAttachment[],
|
|
70
|
+
rightArg: readonly IControllerDraftAttachment[],
|
|
71
|
+
): boolean => leftArg.length === rightArg.length && leftArg.every((left, index) => {
|
|
72
|
+
const right = rightArg[index];
|
|
73
|
+
return right !== undefined
|
|
74
|
+
&& left.id === right.id
|
|
75
|
+
&& left.name === right.name
|
|
76
|
+
&& left.mediaType === right.mediaType
|
|
77
|
+
&& left.size === right.size
|
|
78
|
+
&& left.kind === right.kind
|
|
79
|
+
&& left.dataBase64 === right.dataBase64;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const runtimeIdsEqual = (leftArg: IControllerRuntimeId, rightArg: IControllerRuntimeId): boolean => (
|
|
83
|
+
leftArg.harnessId === rightArg.harnessId && leftArg.nativeId === rightArg.nativeId
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
export class SessionDraftSync {
|
|
87
|
+
private readonly debounceMs: number;
|
|
88
|
+
private generation = 0;
|
|
89
|
+
private active?: IActiveDraft;
|
|
90
|
+
private disposed = false;
|
|
91
|
+
|
|
92
|
+
constructor(private readonly options: ISessionDraftSyncOptions) {
|
|
93
|
+
this.debounceMs = options.debounceMs ?? defaultDebounceMs;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public get state(): ISessionDraftSyncState | undefined {
|
|
97
|
+
return this.active ? this.publicState(this.active) : undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public async activate(
|
|
101
|
+
projectIdArg: string,
|
|
102
|
+
sessionIdArg: IControllerRuntimeId,
|
|
103
|
+
): Promise<ISessionDraftSyncState | undefined> {
|
|
104
|
+
if (this.disposed) return undefined;
|
|
105
|
+
this.deactivate();
|
|
106
|
+
const generation = ++this.generation;
|
|
107
|
+
const active: IActiveDraft = {
|
|
108
|
+
projectId: projectIdArg,
|
|
109
|
+
sessionId: { ...sessionIdArg },
|
|
110
|
+
generation,
|
|
111
|
+
authoritative: { text: '', attachments: [], revision: 0 },
|
|
112
|
+
desiredText: '',
|
|
113
|
+
desiredAttachments: [],
|
|
114
|
+
dirtyText: false,
|
|
115
|
+
dirtyAttachments: false,
|
|
116
|
+
drainAfterDeactivate: false,
|
|
117
|
+
loading: true,
|
|
118
|
+
saving: false,
|
|
119
|
+
error: '',
|
|
120
|
+
};
|
|
121
|
+
this.active = active;
|
|
122
|
+
this.emit(active);
|
|
123
|
+
await this.hydrate(active);
|
|
124
|
+
return this.isCurrent(active) ? this.publicState(active) : undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public deactivate(): void {
|
|
128
|
+
const active = this.active;
|
|
129
|
+
if (active?.debounceTimer) clearTimeout(active.debounceTimer);
|
|
130
|
+
if (
|
|
131
|
+
active
|
|
132
|
+
&& !active.loading
|
|
133
|
+
&& (active.dirtyText || active.dirtyAttachments)
|
|
134
|
+
) {
|
|
135
|
+
active.drainAfterDeactivate = true;
|
|
136
|
+
if (!active.writeTask) {
|
|
137
|
+
const writeTask = this.flushActive(active);
|
|
138
|
+
active.writeTask = writeTask;
|
|
139
|
+
void writeTask
|
|
140
|
+
.catch((errorArg) => this.options.onError?.(errorArg))
|
|
141
|
+
.finally(() => {
|
|
142
|
+
if (active.writeTask === writeTask) active.writeTask = undefined;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
this.generation += 1;
|
|
147
|
+
this.active = undefined;
|
|
148
|
+
this.options.onState(undefined);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public setText(textArg: string): void {
|
|
152
|
+
const active = this.active;
|
|
153
|
+
if (!active || active.loading) return;
|
|
154
|
+
active.desiredText = textArg;
|
|
155
|
+
active.dirtyText = active.desiredText !== active.authoritative.text;
|
|
156
|
+
active.error = '';
|
|
157
|
+
this.emit(active);
|
|
158
|
+
this.schedule(active);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public setAttachments(attachmentsArg: readonly IControllerDraftAttachment[]): void {
|
|
162
|
+
const active = this.active;
|
|
163
|
+
if (!active || active.loading) return;
|
|
164
|
+
active.desiredAttachments = cloneAttachments(attachmentsArg);
|
|
165
|
+
active.dirtyAttachments = !attachmentsEqual(
|
|
166
|
+
active.desiredAttachments,
|
|
167
|
+
active.authoritative.attachments,
|
|
168
|
+
);
|
|
169
|
+
active.error = '';
|
|
170
|
+
this.emit(active);
|
|
171
|
+
this.schedule(active);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
public async flush(): Promise<IControllerSessionDraft> {
|
|
175
|
+
const active = this.active;
|
|
176
|
+
if (!active) throw new Error('No session draft is active.');
|
|
177
|
+
if (active.debounceTimer) {
|
|
178
|
+
clearTimeout(active.debounceTimer);
|
|
179
|
+
active.debounceTimer = undefined;
|
|
180
|
+
}
|
|
181
|
+
if (active.loading) await this.hydrate(active);
|
|
182
|
+
if (!this.isCurrent(active)) throw new Error('The session draft changed during synchronization.');
|
|
183
|
+
if (active.writeTask) return active.writeTask;
|
|
184
|
+
const writeTask = this.flushActive(active);
|
|
185
|
+
active.writeTask = writeTask;
|
|
186
|
+
try {
|
|
187
|
+
return await writeTask;
|
|
188
|
+
} finally {
|
|
189
|
+
if (active.writeTask === writeTask) active.writeTask = undefined;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public async refresh(): Promise<ISessionDraftSyncState | undefined> {
|
|
194
|
+
const active = this.active;
|
|
195
|
+
if (!active) return undefined;
|
|
196
|
+
await this.hydrate(active);
|
|
197
|
+
return this.isCurrent(active) ? this.publicState(active) : undefined;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
public applyRemoteUpdate(
|
|
201
|
+
projectIdArg: string,
|
|
202
|
+
sessionIdArg: IControllerRuntimeId,
|
|
203
|
+
updateArg: IControllerSessionDraftUpdate,
|
|
204
|
+
): boolean {
|
|
205
|
+
const active = this.active;
|
|
206
|
+
if (
|
|
207
|
+
!active
|
|
208
|
+
|| active.projectId !== projectIdArg
|
|
209
|
+
|| !runtimeIdsEqual(active.sessionId, sessionIdArg)
|
|
210
|
+
|| updateArg.revision <= active.authoritative.revision
|
|
211
|
+
) return false;
|
|
212
|
+
if (updateArg.revision !== active.authoritative.revision + 1) {
|
|
213
|
+
void this.hydrate(active).catch((errorArg) => this.options.onError?.(errorArg));
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
active.authoritative = {
|
|
217
|
+
text: updateArg.text ?? active.authoritative.text,
|
|
218
|
+
attachments: updateArg.attachments === undefined
|
|
219
|
+
? active.authoritative.attachments
|
|
220
|
+
: cloneAttachments(updateArg.attachments),
|
|
221
|
+
revision: updateArg.revision,
|
|
222
|
+
};
|
|
223
|
+
if (!active.dirtyText && updateArg.text !== undefined) active.desiredText = updateArg.text;
|
|
224
|
+
if (!active.dirtyAttachments && updateArg.attachments !== undefined) {
|
|
225
|
+
active.desiredAttachments = cloneAttachments(updateArg.attachments);
|
|
226
|
+
}
|
|
227
|
+
active.dirtyText = active.desiredText !== active.authoritative.text;
|
|
228
|
+
active.dirtyAttachments = !attachmentsEqual(
|
|
229
|
+
active.desiredAttachments,
|
|
230
|
+
active.authoritative.attachments,
|
|
231
|
+
);
|
|
232
|
+
this.emit(active);
|
|
233
|
+
if (active.dirtyText || active.dirtyAttachments) this.schedule(active);
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
public dispose(): void {
|
|
238
|
+
if (this.disposed) return;
|
|
239
|
+
this.disposed = true;
|
|
240
|
+
this.deactivate();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private async hydrate(activeArg: IActiveDraft): Promise<void> {
|
|
244
|
+
if (activeArg.hydrateTask) return activeArg.hydrateTask;
|
|
245
|
+
let hydrateTask!: Promise<void>;
|
|
246
|
+
hydrateTask = (async () => {
|
|
247
|
+
activeArg.loading = true;
|
|
248
|
+
this.emit(activeArg);
|
|
249
|
+
try {
|
|
250
|
+
const draft = await this.options.transport.getDraft(activeArg.projectId, activeArg.sessionId);
|
|
251
|
+
if (!this.canSynchronize(activeArg)) return;
|
|
252
|
+
activeArg.authoritative = cloneDraft(draft);
|
|
253
|
+
if (!activeArg.dirtyText) activeArg.desiredText = draft.text;
|
|
254
|
+
if (!activeArg.dirtyAttachments) {
|
|
255
|
+
activeArg.desiredAttachments = cloneAttachments(draft.attachments);
|
|
256
|
+
}
|
|
257
|
+
activeArg.dirtyText = activeArg.desiredText !== draft.text;
|
|
258
|
+
activeArg.dirtyAttachments = !attachmentsEqual(
|
|
259
|
+
activeArg.desiredAttachments,
|
|
260
|
+
draft.attachments,
|
|
261
|
+
);
|
|
262
|
+
activeArg.error = '';
|
|
263
|
+
} catch (errorArg) {
|
|
264
|
+
if (this.isCurrent(activeArg)) activeArg.error = this.errorMessage(errorArg);
|
|
265
|
+
throw errorArg;
|
|
266
|
+
} finally {
|
|
267
|
+
activeArg.loading = false;
|
|
268
|
+
this.emit(activeArg);
|
|
269
|
+
}
|
|
270
|
+
})();
|
|
271
|
+
activeArg.hydrateTask = hydrateTask;
|
|
272
|
+
try {
|
|
273
|
+
await hydrateTask;
|
|
274
|
+
} finally {
|
|
275
|
+
if (activeArg.hydrateTask === hydrateTask) activeArg.hydrateTask = undefined;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private async flushActive(activeArg: IActiveDraft): Promise<IControllerSessionDraft> {
|
|
280
|
+
activeArg.saving = true;
|
|
281
|
+
this.emit(activeArg);
|
|
282
|
+
let conflictAttempts = 0;
|
|
283
|
+
try {
|
|
284
|
+
while (
|
|
285
|
+
this.canSynchronize(activeArg)
|
|
286
|
+
&& (activeArg.dirtyText || activeArg.dirtyAttachments)
|
|
287
|
+
) {
|
|
288
|
+
const sentText = activeArg.desiredText;
|
|
289
|
+
const sentAttachments = cloneAttachments(activeArg.desiredAttachments);
|
|
290
|
+
const sentTextField = activeArg.dirtyText;
|
|
291
|
+
const sentAttachmentField = activeArg.dirtyAttachments;
|
|
292
|
+
const expectedRevision = activeArg.authoritative.revision;
|
|
293
|
+
let response: IControllerSessionDraft;
|
|
294
|
+
try {
|
|
295
|
+
response = await this.options.transport.updateDraft(
|
|
296
|
+
activeArg.projectId,
|
|
297
|
+
activeArg.sessionId,
|
|
298
|
+
expectedRevision,
|
|
299
|
+
{
|
|
300
|
+
...(sentTextField ? { text: sentText } : {}),
|
|
301
|
+
...(sentAttachmentField ? { attachments: sentAttachments } : {}),
|
|
302
|
+
},
|
|
303
|
+
);
|
|
304
|
+
} catch (errorArg) {
|
|
305
|
+
if (
|
|
306
|
+
this.options.transport.isConflict(errorArg)
|
|
307
|
+
&& conflictAttempts < maximumConflictRetries
|
|
308
|
+
) {
|
|
309
|
+
conflictAttempts += 1;
|
|
310
|
+
await this.hydrate(activeArg);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
throw errorArg;
|
|
314
|
+
}
|
|
315
|
+
if (!this.canSynchronize(activeArg)) break;
|
|
316
|
+
conflictAttempts = 0;
|
|
317
|
+
if (response.revision >= activeArg.authoritative.revision) {
|
|
318
|
+
activeArg.authoritative = cloneDraft(response);
|
|
319
|
+
if (sentTextField && activeArg.desiredText === sentText) activeArg.dirtyText = false;
|
|
320
|
+
if (
|
|
321
|
+
sentAttachmentField
|
|
322
|
+
&& attachmentsEqual(activeArg.desiredAttachments, sentAttachments)
|
|
323
|
+
) activeArg.dirtyAttachments = false;
|
|
324
|
+
}
|
|
325
|
+
activeArg.dirtyText = activeArg.desiredText !== activeArg.authoritative.text;
|
|
326
|
+
activeArg.dirtyAttachments = !attachmentsEqual(
|
|
327
|
+
activeArg.desiredAttachments,
|
|
328
|
+
activeArg.authoritative.attachments,
|
|
329
|
+
);
|
|
330
|
+
activeArg.error = '';
|
|
331
|
+
this.emit(activeArg);
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
text: activeArg.desiredText,
|
|
335
|
+
attachments: cloneAttachments(activeArg.desiredAttachments),
|
|
336
|
+
revision: activeArg.authoritative.revision,
|
|
337
|
+
};
|
|
338
|
+
} catch (errorArg) {
|
|
339
|
+
if (this.isCurrent(activeArg)) {
|
|
340
|
+
activeArg.error = this.errorMessage(errorArg);
|
|
341
|
+
this.emit(activeArg);
|
|
342
|
+
}
|
|
343
|
+
throw errorArg;
|
|
344
|
+
} finally {
|
|
345
|
+
activeArg.saving = false;
|
|
346
|
+
this.emit(activeArg);
|
|
347
|
+
activeArg.drainAfterDeactivate = false;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private schedule(activeArg: IActiveDraft): void {
|
|
352
|
+
if (!this.isCurrent(activeArg) || activeArg.loading) return;
|
|
353
|
+
if (activeArg.debounceTimer) clearTimeout(activeArg.debounceTimer);
|
|
354
|
+
activeArg.debounceTimer = setTimeout(() => {
|
|
355
|
+
activeArg.debounceTimer = undefined;
|
|
356
|
+
void this.flush().catch((errorArg) => this.options.onError?.(errorArg));
|
|
357
|
+
}, this.debounceMs);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
private isCurrent(activeArg: IActiveDraft): boolean {
|
|
361
|
+
return !this.disposed
|
|
362
|
+
&& this.active === activeArg
|
|
363
|
+
&& this.generation === activeArg.generation;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private canSynchronize(activeArg: IActiveDraft): boolean {
|
|
367
|
+
return this.isCurrent(activeArg) || activeArg.drainAfterDeactivate;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
private emit(activeArg: IActiveDraft): void {
|
|
371
|
+
if (this.isCurrent(activeArg)) this.options.onState(this.publicState(activeArg));
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private publicState(activeArg: IActiveDraft): ISessionDraftSyncState {
|
|
375
|
+
return {
|
|
376
|
+
projectId: activeArg.projectId,
|
|
377
|
+
sessionId: { ...activeArg.sessionId },
|
|
378
|
+
text: activeArg.desiredText,
|
|
379
|
+
attachments: cloneAttachments(activeArg.desiredAttachments),
|
|
380
|
+
revision: activeArg.authoritative.revision,
|
|
381
|
+
loading: activeArg.loading,
|
|
382
|
+
saving: activeArg.saving,
|
|
383
|
+
error: activeArg.error,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
private errorMessage(errorArg: unknown): string {
|
|
388
|
+
return errorArg instanceof Error ? errorArg.message : 'Draft synchronization failed.';
|
|
389
|
+
}
|
|
390
|
+
}
|