@simpleplatform/sdk 3.0.4 → 3.1.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.
- package/README.md +20 -4
- package/dist/space/browser-transport.d.ts +1 -1
- package/dist/space/browser-transport.js +81 -3
- package/dist/space/core.d.ts +9 -0
- package/dist/space/core.js +29 -2
- package/dist/space/index.js +5 -4
- package/dist/space/record-form.d.ts +15 -1
- package/dist/space/record-form.js +74 -10
- package/dist/space/record.js +23 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -183,8 +183,8 @@ cannot be opened again as a secondary session (`already_current`).
|
|
|
183
183
|
|
|
184
184
|
### Managed record UI and header actions (pre-release)
|
|
185
185
|
|
|
186
|
-
The companion UI Kit
|
|
187
|
-
|
|
186
|
+
The companion UI Kit provides pre-release managed React `RecordForm` and
|
|
187
|
+
`RecordActivity` components for record handles:
|
|
188
188
|
|
|
189
189
|
```tsx
|
|
190
190
|
<RecordForm record={record} />
|
|
@@ -195,6 +195,12 @@ registries, document/reference/secret internals, or Record Behavior timing as
|
|
|
195
195
|
public APIs. When a Space needs fully custom presentation, keep using the
|
|
196
196
|
record handle's `snapshot()`, `update()`, and `submit()` commands.
|
|
197
197
|
|
|
198
|
+
For collections, the UI Kit also provides database-backed `ListView`,
|
|
199
|
+
`DataGrid`, and `Board`. Their private bridge requires the planned SDK
|
|
200
|
+
`^3.2.0` release, which is not yet published. Inbox remains platform-only;
|
|
201
|
+
standalone filter-builder components are private. See the
|
|
202
|
+
[UI Kit collection contracts](../space-ui/README.md#managed-database-backed-views).
|
|
203
|
+
|
|
198
204
|
Record Spaces can replace the platform main actions in the header while
|
|
199
205
|
mounted:
|
|
200
206
|
|
|
@@ -345,11 +351,21 @@ switch (simple.context.kind) {
|
|
|
345
351
|
}
|
|
346
352
|
```
|
|
347
353
|
|
|
348
|
-
The two
|
|
349
|
-
`{ kind: 'record', applicationId, tableName, recordId }`. There is no
|
|
354
|
+
The two context forms are `{ kind: 'standalone', user?, tenant? }` and
|
|
355
|
+
`{ kind: 'record', applicationId, tableName, recordId, user?, tenant? }`. There is no
|
|
350
356
|
`unknown` context variant. A missing or malformed context rejects
|
|
351
357
|
`connect({ targetOrigin })` with `SpaceProtocolError` code `invalid_response`.
|
|
352
358
|
|
|
359
|
+
`simple.context.user` and `simple.context.tenant` name the signed-in user and
|
|
360
|
+
the tenant exactly as an action's context names them, with the SDK's own `User`
|
|
361
|
+
and `Tenant` types: `user.id`, and `tenant.id`, `tenant.name` and `tenant.host`.
|
|
362
|
+
They are present from the host version that adds them to initialization
|
|
363
|
+
context. Older hosts omit them, so a Space handles `undefined` explicitly before
|
|
364
|
+
using `user.id`, for example to filter the tasks assigned to the viewer. The
|
|
365
|
+
SDK never invents them or derives them from `recordId`. Supplied text must be
|
|
366
|
+
non-blank; otherwise the context is malformed. This is descriptive identity,
|
|
367
|
+
not an authorization grant: the host and server still enforce access.
|
|
368
|
+
|
|
353
369
|
`connect({ targetOrigin })` works in any embedded Space, including standalone dashboards
|
|
354
370
|
and tools. In a non-record Space, `simple.data` remains available and
|
|
355
371
|
`simple.records.open()` succeeds whenever the records capability was negotiated,
|
|
@@ -16,4 +16,4 @@ export interface BrowserSpaceTransport extends SpaceDataTransport, SpaceHeaderSt
|
|
|
16
16
|
capabilities: RecordFormCapabilities;
|
|
17
17
|
subscribeFormModel: (listener: (form: unknown) => void) => () => void;
|
|
18
18
|
}
|
|
19
|
-
export declare function createMessagePortTransport(port: MessagePortLike, targetOrigin: string, onThemeChanged?: (theme: SpaceThemeSnapshot) => void): BrowserSpaceTransport;
|
|
19
|
+
export declare function createMessagePortTransport(port: MessagePortLike, targetOrigin: string, onThemeChanged?: (theme: SpaceThemeSnapshot) => void, formLifecycleEnabled?: boolean): BrowserSpaceTransport;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { SpaceDataError, SpaceProtocolError, } from './protocol.js';
|
|
1
|
+
import { isObjectRecord, SpaceDataError, SpaceProtocolError, } from './protocol.js';
|
|
2
2
|
import { createFormModelSubscriptionManager, createRecordFormCapabilities, } from './record-form.js';
|
|
3
3
|
import { parseThemeSnapshot } from './theme.js';
|
|
4
|
-
export function createMessagePortTransport(port, targetOrigin, onThemeChanged) {
|
|
4
|
+
export function createMessagePortTransport(port, targetOrigin, onThemeChanged, formLifecycleEnabled = false) {
|
|
5
5
|
const headerCallbacks = new Map();
|
|
6
6
|
const formModelManager = createFormModelSubscriptionManager();
|
|
7
|
+
const formLifecycles = new Map();
|
|
8
|
+
const activeFormRequests = new Set();
|
|
7
9
|
const pendingData = new Map();
|
|
8
10
|
const pending = new Map();
|
|
9
11
|
const pendingCapabilities = new Map();
|
|
@@ -12,6 +14,8 @@ export function createMessagePortTransport(port, targetOrigin, onThemeChanged) {
|
|
|
12
14
|
if (typeof port.addEventListener === 'function') {
|
|
13
15
|
port.addEventListener('close', () => {
|
|
14
16
|
isPortClosed = true;
|
|
17
|
+
formLifecycles.clear();
|
|
18
|
+
activeFormRequests.clear();
|
|
15
19
|
});
|
|
16
20
|
}
|
|
17
21
|
port.onmessage = (event) => {
|
|
@@ -23,6 +27,49 @@ export function createMessagePortTransport(port, targetOrigin, onThemeChanged) {
|
|
|
23
27
|
formModelManager.handleUpdate(envelope.form);
|
|
24
28
|
return;
|
|
25
29
|
}
|
|
30
|
+
if (envelope.type === 'SPACE_RECORD_FORM_REQUEST' && formLifecycleEnabled) {
|
|
31
|
+
const request = message;
|
|
32
|
+
if (typeof request.id !== 'string' || !request.id || typeof request.sessionId !== 'string' || !request.sessionId)
|
|
33
|
+
return;
|
|
34
|
+
const { id, sessionId } = request;
|
|
35
|
+
Promise.resolve().then(async () => {
|
|
36
|
+
const handlers = formLifecycles.get(sessionId);
|
|
37
|
+
if (!handlers?.size)
|
|
38
|
+
throw new Error('The managed record session is unavailable.');
|
|
39
|
+
const check = () => {
|
|
40
|
+
const states = [...handlers].map(handler => handler.check());
|
|
41
|
+
return { busy: activeFormRequests.has(sessionId) || states.some(state => state.busy), dirty: states.some(state => state.dirty) };
|
|
42
|
+
};
|
|
43
|
+
if (request.operation === 'check')
|
|
44
|
+
return check();
|
|
45
|
+
if (request.operation !== 'prepare' && request.operation !== 'discard')
|
|
46
|
+
throw new Error('The managed record form operation is unsupported.');
|
|
47
|
+
if (activeFormRequests.has(sessionId) || [...handlers].some(handler => handler.check().busy))
|
|
48
|
+
throw new Error('The record is still being prepared or saved.');
|
|
49
|
+
if (request.operation === 'discard' && !isObjectRecord(request.values))
|
|
50
|
+
throw new Error('Discard requires the original record values.');
|
|
51
|
+
activeFormRequests.add(sessionId);
|
|
52
|
+
try {
|
|
53
|
+
for (const handler of handlers) {
|
|
54
|
+
if (request.operation === 'prepare')
|
|
55
|
+
await handler.prepare();
|
|
56
|
+
else
|
|
57
|
+
await handler.discard(request.values);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
activeFormRequests.delete(sessionId);
|
|
62
|
+
}
|
|
63
|
+
return check();
|
|
64
|
+
}).then((state) => {
|
|
65
|
+
if (!isPortClosed)
|
|
66
|
+
port.postMessage({ ...state, id, ok: true, sessionId, type: 'SPACE_RECORD_FORM_RESULT' });
|
|
67
|
+
}).catch((error) => {
|
|
68
|
+
if (!isPortClosed)
|
|
69
|
+
port.postMessage({ error: error instanceof Error ? error.message : 'The managed record form request failed.', id, ok: false, sessionId, type: 'SPACE_RECORD_FORM_RESULT' });
|
|
70
|
+
});
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
26
73
|
if (envelope.type === 'THEME_CHANGED') {
|
|
27
74
|
if (onThemeChanged) {
|
|
28
75
|
const snapshot = parseThemeSnapshot(envelope.theme);
|
|
@@ -136,7 +183,38 @@ export function createMessagePortTransport(port, targetOrigin, onThemeChanged) {
|
|
|
136
183
|
});
|
|
137
184
|
};
|
|
138
185
|
return {
|
|
139
|
-
capabilities:
|
|
186
|
+
capabilities: {
|
|
187
|
+
...createRecordFormCapabilities(requestCapability, msg => port.postMessage(msg), targetOrigin),
|
|
188
|
+
...(formLifecycleEnabled
|
|
189
|
+
? {
|
|
190
|
+
notifyFormDraft: (sessionId) => {
|
|
191
|
+
if (isPortClosed)
|
|
192
|
+
return;
|
|
193
|
+
let dirty = true;
|
|
194
|
+
try {
|
|
195
|
+
dirty = [...formLifecycles.get(sessionId) ?? []].some(handler => handler.check().dirty);
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
// A broken draft getter must not let navigation discard edits.
|
|
199
|
+
}
|
|
200
|
+
port.postMessage({ dirty, sessionId, type: 'SPACE_RECORD_FORM_DRAFT' });
|
|
201
|
+
},
|
|
202
|
+
registerFormLifecycle: (sessionId, lifecycle) => {
|
|
203
|
+
let handlers = formLifecycles.get(sessionId);
|
|
204
|
+
if (!handlers) {
|
|
205
|
+
handlers = new Set();
|
|
206
|
+
formLifecycles.set(sessionId, handlers);
|
|
207
|
+
}
|
|
208
|
+
handlers.add(lifecycle);
|
|
209
|
+
return () => {
|
|
210
|
+
handlers.delete(lifecycle);
|
|
211
|
+
if (!handlers.size)
|
|
212
|
+
formLifecycles.delete(sessionId);
|
|
213
|
+
};
|
|
214
|
+
},
|
|
215
|
+
}
|
|
216
|
+
: {}),
|
|
217
|
+
},
|
|
140
218
|
execute: (document, variables) => {
|
|
141
219
|
return new Promise((resolve, reject) => {
|
|
142
220
|
const id = createDataRequestId();
|
package/dist/space/core.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Tenant, User } from '../types.js';
|
|
1
2
|
import type { SimpleActionsClient } from './actions.js';
|
|
2
3
|
import type { SimpleDocumentsClient } from './documents.js';
|
|
3
4
|
import type { SimpleNavigationClient, SpaceNavigationTransport } from './navigation.js';
|
|
@@ -23,8 +24,16 @@ export type SpaceContext = {
|
|
|
23
24
|
kind: 'record';
|
|
24
25
|
recordId: string;
|
|
25
26
|
tableName: string;
|
|
27
|
+
/** The tenant the Space runs in, named as an action's context names it. Absent on older hosts. */
|
|
28
|
+
tenant?: Tenant;
|
|
29
|
+
/** The signed-in user, named as an action's context names it. Absent on older hosts. */
|
|
30
|
+
user?: User;
|
|
26
31
|
} | {
|
|
27
32
|
kind: 'standalone';
|
|
33
|
+
/** The tenant the Space runs in, named as an action's context names it. Absent on older hosts. */
|
|
34
|
+
tenant?: Tenant;
|
|
35
|
+
/** The signed-in user, named as an action's context names it. Absent on older hosts. */
|
|
36
|
+
user?: User;
|
|
28
37
|
};
|
|
29
38
|
export type GraphQLVariables = Readonly<Record<string, unknown>>;
|
|
30
39
|
export interface SpaceDataTransport {
|
package/dist/space/core.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createActionsClient } from './actions.js';
|
|
2
2
|
import { createDocumentsClient } from './documents.js';
|
|
3
3
|
import { createNavigationClient } from './navigation.js';
|
|
4
|
-
import { deepFreeze, invalidResponse, isObjectRecord, PROTOCOL_VERSION, readResponse, SpaceDataError, SpaceProtocolError } from './protocol.js';
|
|
4
|
+
import { deepFreeze, invalidResponse, isNonBlankString, isObjectRecord, PROTOCOL_VERSION, readResponse, SpaceDataError, SpaceProtocolError } from './protocol.js';
|
|
5
5
|
import { createRecordsClient, isCurrentRecordResult, ProtocolRecordHandle } from './record.js';
|
|
6
6
|
import { createTasksClient } from './tasks.js';
|
|
7
7
|
import { createHeaderClient, createTabsClient, createToastClient, validateSpaceToastOptions } from './ui.js';
|
|
@@ -9,6 +9,7 @@ export { PROTOCOL_VERSION, SpaceDataError, SpaceProtocolError } from './protocol
|
|
|
9
9
|
export { MANAGED_RECORD_FORM_PROTOCOL_VERSION } from './record-form.js';
|
|
10
10
|
export { RECORDS_PROTOCOL_VERSION } from './record.js';
|
|
11
11
|
export { DEFAULT_TABS_TIMEOUT_MS, HEADER_ACTIONS_PROTOCOL_VERSION, HEADER_STATUS_PROTOCOL_VERSION, LUCIDE_ICON_NAME, TAB_ID, TABS_PROTOCOL_VERSION, TOAST_PROTOCOL_VERSION, validateHeaderStatus, validateTabs } from './ui.js';
|
|
12
|
+
const MANAGED_UI_SYMBOL = Symbol.for('@simpleplatform/sdk/space/managed-ui/v1');
|
|
12
13
|
/** Composes Space capabilities around the host transports supplied by `connect`. */
|
|
13
14
|
export function createSimpleClient({ actionTransport, capabilities, context = { kind: 'standalone' }, dataTransport, documentTransport, formModelTransport, headerStatusTransport, headerTransport, hostOrigin, navigationTransport, nextRequestId = createRequestId, recordsTransport, runtime, tabsTransport, taskTransport, toastTransport, transport, }) {
|
|
14
15
|
const immutableContext = deepFreeze(structuredClone(context));
|
|
@@ -51,7 +52,7 @@ export function createSimpleClient({ actionTransport, capabilities, context = {
|
|
|
51
52
|
recordsTransport,
|
|
52
53
|
runtime,
|
|
53
54
|
});
|
|
54
|
-
|
|
55
|
+
const client = {
|
|
55
56
|
actions: createActionsClient(actionTransport, nextRequestId),
|
|
56
57
|
context: immutableContext,
|
|
57
58
|
data: {
|
|
@@ -68,10 +69,36 @@ export function createSimpleClient({ actionTransport, capabilities, context = {
|
|
|
68
69
|
toast: createToastClient(toastTransport),
|
|
69
70
|
},
|
|
70
71
|
};
|
|
72
|
+
const uiBridge = Object.freeze({
|
|
73
|
+
graphql: (document, variables) => executeData(dataTransport, document, variables),
|
|
74
|
+
runtime: runtime ? Object.freeze({ url: runtime.url, version: runtime.version }) : undefined,
|
|
75
|
+
});
|
|
76
|
+
Object.defineProperty(client, MANAGED_UI_SYMBOL, {
|
|
77
|
+
configurable: false,
|
|
78
|
+
enumerable: false,
|
|
79
|
+
value: uiBridge,
|
|
80
|
+
writable: false,
|
|
81
|
+
});
|
|
82
|
+
return client;
|
|
83
|
+
}
|
|
84
|
+
/** An action's context names the user by an optional ID; a supplied ID is non-blank. */
|
|
85
|
+
function isContextUser(value) {
|
|
86
|
+
return isObjectRecord(value) && (value.id === undefined || isNonBlankString(value.id));
|
|
87
|
+
}
|
|
88
|
+
/** An action's context names the tenant, with an optional ID and host; supplied text is non-blank. */
|
|
89
|
+
function isContextTenant(value) {
|
|
90
|
+
return isObjectRecord(value)
|
|
91
|
+
&& isNonBlankString(value.name)
|
|
92
|
+
&& (value.id === undefined || isNonBlankString(value.id))
|
|
93
|
+
&& (value.host === undefined || isNonBlankString(value.host));
|
|
71
94
|
}
|
|
72
95
|
export function isSpaceContext(value) {
|
|
73
96
|
if (!isObjectRecord(value))
|
|
74
97
|
return false;
|
|
98
|
+
if (value.user !== undefined && !isContextUser(value.user))
|
|
99
|
+
return false;
|
|
100
|
+
if (value.tenant !== undefined && !isContextTenant(value.tenant))
|
|
101
|
+
return false;
|
|
75
102
|
if (value.kind === 'standalone')
|
|
76
103
|
return true;
|
|
77
104
|
return value.kind === 'record'
|
package/dist/space/index.js
CHANGED
|
@@ -65,8 +65,8 @@ export function connect({ targetOrigin }) {
|
|
|
65
65
|
applyThemeSnapshot(window.document, appliedThemeTokens, snapshot, fontState);
|
|
66
66
|
}
|
|
67
67
|
: undefined;
|
|
68
|
-
const transport = createMessagePortTransport(port, targetOrigin, onThemeChanged);
|
|
69
68
|
const protocols = event.data.protocols;
|
|
69
|
+
const transport = createMessagePortTransport(port, targetOrigin, onThemeChanged, protocols?.form === MANAGED_RECORD_FORM_PROTOCOL_VERSION);
|
|
70
70
|
const hasRecord = protocols?.record === PROTOCOL_VERSION;
|
|
71
71
|
const hasRecords = protocols?.records === RECORDS_PROTOCOL_VERSION;
|
|
72
72
|
resolve(createSimpleClient({
|
|
@@ -75,7 +75,7 @@ export function connect({ targetOrigin }) {
|
|
|
75
75
|
context: event.data.context,
|
|
76
76
|
dataTransport: transport,
|
|
77
77
|
documentTransport: protocols?.document === PROTOCOL_VERSION ? transport : undefined,
|
|
78
|
-
formModelTransport: protocols?.form === MANAGED_RECORD_FORM_PROTOCOL_VERSION ? transport : undefined,
|
|
78
|
+
formModelTransport: protocols?.form === MANAGED_RECORD_FORM_PROTOCOL_VERSION || protocols?.form === 2 ? transport : undefined,
|
|
79
79
|
headerStatusTransport: hasRecord && protocols?.headerStatus === HEADER_STATUS_PROTOCOL_VERSION ? transport : undefined,
|
|
80
80
|
headerTransport: hasRecord && protocols?.header === HEADER_ACTIONS_PROTOCOL_VERSION ? transport : undefined,
|
|
81
81
|
hostOrigin: targetOrigin,
|
|
@@ -93,7 +93,7 @@ export function connect({ targetOrigin }) {
|
|
|
93
93
|
protocols: {
|
|
94
94
|
action: [PROTOCOL_VERSION],
|
|
95
95
|
document: [PROTOCOL_VERSION],
|
|
96
|
-
form: [MANAGED_RECORD_FORM_PROTOCOL_VERSION],
|
|
96
|
+
form: [MANAGED_RECORD_FORM_PROTOCOL_VERSION, 2],
|
|
97
97
|
header: [HEADER_ACTIONS_PROTOCOL_VERSION],
|
|
98
98
|
headerStatus: [HEADER_STATUS_PROTOCOL_VERSION],
|
|
99
99
|
record: [PROTOCOL_VERSION],
|
|
@@ -134,7 +134,8 @@ function isUiRuntimeDescriptor(value) {
|
|
|
134
134
|
if (typeof url !== 'string' || !url || typeof version !== 'string' || !version)
|
|
135
135
|
return false;
|
|
136
136
|
try {
|
|
137
|
-
const
|
|
137
|
+
const runtimeUrl = new URL(url);
|
|
138
|
+
const protocol = runtimeUrl.protocol;
|
|
138
139
|
return protocol === 'http:' || protocol === 'https:';
|
|
139
140
|
}
|
|
140
141
|
catch {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { RecordHandle, RecordSnapshot, RecordUiBridge } from './record.js';
|
|
2
2
|
import type { SpaceToastOptions } from './ui.js';
|
|
3
|
-
export declare const MANAGED_RECORD_FORM_PROTOCOL_VERSION:
|
|
3
|
+
export declare const MANAGED_RECORD_FORM_PROTOCOL_VERSION: 3;
|
|
4
4
|
export interface UiRuntimeDescriptor {
|
|
5
5
|
readonly url: string;
|
|
6
6
|
readonly version: string;
|
|
@@ -26,7 +26,17 @@ export interface ManagedRecordFormModel {
|
|
|
26
26
|
}
|
|
27
27
|
export type ManagedRecordFormSnapshotListener = (snapshot: RecordSnapshot) => void;
|
|
28
28
|
export type RecordFormSubmitPreparation = () => Promise<Readonly<Record<string, unknown>> | undefined>;
|
|
29
|
+
export interface RecordFormHostLifecycle {
|
|
30
|
+
check: () => {
|
|
31
|
+
busy: boolean;
|
|
32
|
+
dirty: boolean;
|
|
33
|
+
};
|
|
34
|
+
discard: (values: Readonly<Record<string, unknown>>) => Promise<void>;
|
|
35
|
+
prepare: () => Promise<void>;
|
|
36
|
+
}
|
|
29
37
|
export interface RecordFormCapabilities {
|
|
38
|
+
notifyFormDraft?: (sessionId: string) => void;
|
|
39
|
+
registerFormLifecycle?: (sessionId: string, lifecycle: RecordFormHostLifecycle) => () => void;
|
|
30
40
|
createDocumentHandle: (request: {
|
|
31
41
|
bytes: ArrayBuffer;
|
|
32
42
|
lifecycle?: 'pending' | 'record' | 'staged';
|
|
@@ -62,6 +72,9 @@ export interface RecordFormBridge extends RecordUiBridge {
|
|
|
62
72
|
blurField: (fieldName: string) => Promise<void>;
|
|
63
73
|
focusField: (fieldName: string) => Promise<void>;
|
|
64
74
|
form: unknown;
|
|
75
|
+
onDraftChange: () => void;
|
|
76
|
+
registerDiscard: (discard: (values: Readonly<Record<string, unknown>>) => Promise<void>) => () => void;
|
|
77
|
+
registerDraftState: (getDirty: () => boolean) => () => void;
|
|
65
78
|
registerSubmitPreparation: (preparation: RecordFormSubmitPreparation) => () => void;
|
|
66
79
|
subscribeFormModel: (listener: (form: unknown) => void) => () => void;
|
|
67
80
|
subscribe: (listener: ManagedRecordFormSnapshotListener) => () => void;
|
|
@@ -70,6 +83,7 @@ export interface RecordFormModelTransport {
|
|
|
70
83
|
subscribeFormModel: (listener: (form: unknown) => void) => () => void;
|
|
71
84
|
}
|
|
72
85
|
export interface RecordFormLifecycle {
|
|
86
|
+
beginSubmit: () => () => void;
|
|
73
87
|
notifySnapshot: (snapshot: RecordSnapshot) => void;
|
|
74
88
|
prepareSubmit: () => Promise<Record<string, unknown>>;
|
|
75
89
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { deepFreeze, invalidResponse, isObjectRecord } from './protocol.js';
|
|
2
|
-
export const MANAGED_RECORD_FORM_PROTOCOL_VERSION =
|
|
2
|
+
export const MANAGED_RECORD_FORM_PROTOCOL_VERSION = 3;
|
|
3
3
|
const MANAGED_RECORD_UI_SYMBOL = Symbol.for('@simpleplatform/sdk/space/managed-record-ui/v1');
|
|
4
4
|
export function isManagedRecordFormModel(value) {
|
|
5
5
|
if (!isObjectRecord(value) || !Array.isArray(value.fields))
|
|
@@ -49,7 +49,56 @@ export function initRecordFormLifecycle(record, options) {
|
|
|
49
49
|
const metadataListeners = new Set();
|
|
50
50
|
const snapshotListeners = new Set();
|
|
51
51
|
const submitPreparations = new Set();
|
|
52
|
+
const draftStates = new Set();
|
|
53
|
+
const discardHandlers = new Set();
|
|
54
|
+
let submissions = 0;
|
|
55
|
+
let formOperationPending = false;
|
|
52
56
|
let currentMetadata = currentForm ? recordUiMetadata(currentForm) : undefined;
|
|
57
|
+
const onDraftChange = () => capabilities?.notifyFormDraft?.(record.id);
|
|
58
|
+
const check = () => ({ busy: submissions > 0 || formOperationPending, dirty: [...draftStates].some(getDirty => getDirty()) });
|
|
59
|
+
const prepareSubmit = async () => {
|
|
60
|
+
const values = {};
|
|
61
|
+
for (const preparation of submitPreparations) {
|
|
62
|
+
const preparedValues = await preparation();
|
|
63
|
+
if (preparedValues)
|
|
64
|
+
Object.assign(values, preparedValues);
|
|
65
|
+
}
|
|
66
|
+
return values;
|
|
67
|
+
};
|
|
68
|
+
capabilities?.registerFormLifecycle?.(record.id, {
|
|
69
|
+
check,
|
|
70
|
+
discard: async (values) => {
|
|
71
|
+
if (check().busy)
|
|
72
|
+
throw new Error('The record is still being prepared or saved.');
|
|
73
|
+
formOperationPending = true;
|
|
74
|
+
try {
|
|
75
|
+
for (const discard of discardHandlers)
|
|
76
|
+
await discard(values);
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
formOperationPending = false;
|
|
80
|
+
onDraftChange();
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
prepare: async () => {
|
|
84
|
+
if (check().busy)
|
|
85
|
+
throw new Error('The record is still being prepared or saved.');
|
|
86
|
+
formOperationPending = true;
|
|
87
|
+
onDraftChange();
|
|
88
|
+
try {
|
|
89
|
+
const values = await prepareSubmit();
|
|
90
|
+
if (Object.keys(values).length > 0) {
|
|
91
|
+
const result = await record.update(values);
|
|
92
|
+
if (!result.ok)
|
|
93
|
+
throw new Error('Record validation rejected the prepared fields.');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
formOperationPending = false;
|
|
98
|
+
onDraftChange();
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
});
|
|
53
102
|
const publishFormModel = (updatedForm) => {
|
|
54
103
|
if (!isManagedRecordFormModel(updatedForm))
|
|
55
104
|
return;
|
|
@@ -112,6 +161,19 @@ export function initRecordFormLifecycle(record, options) {
|
|
|
112
161
|
get metadata() {
|
|
113
162
|
return currentMetadata;
|
|
114
163
|
},
|
|
164
|
+
onDraftChange,
|
|
165
|
+
registerDiscard: (discard) => {
|
|
166
|
+
discardHandlers.add(discard);
|
|
167
|
+
return () => discardHandlers.delete(discard);
|
|
168
|
+
},
|
|
169
|
+
registerDraftState: (getDirty) => {
|
|
170
|
+
draftStates.add(getDirty);
|
|
171
|
+
onDraftChange();
|
|
172
|
+
return () => {
|
|
173
|
+
draftStates.delete(getDirty);
|
|
174
|
+
onDraftChange();
|
|
175
|
+
};
|
|
176
|
+
},
|
|
115
177
|
registerSubmitPreparation: (preparation) => {
|
|
116
178
|
submitPreparations.add(preparation);
|
|
117
179
|
return () => submitPreparations.delete(preparation);
|
|
@@ -140,19 +202,21 @@ export function initRecordFormLifecycle(record, options) {
|
|
|
140
202
|
};
|
|
141
203
|
Object.defineProperty(record, MANAGED_RECORD_UI_SYMBOL, { value: bridge });
|
|
142
204
|
return {
|
|
205
|
+
beginSubmit: () => {
|
|
206
|
+
if (formOperationPending)
|
|
207
|
+
throw new Error('The record is still being prepared or discarded.');
|
|
208
|
+
submissions++;
|
|
209
|
+
onDraftChange();
|
|
210
|
+
return () => {
|
|
211
|
+
submissions--;
|
|
212
|
+
onDraftChange();
|
|
213
|
+
};
|
|
214
|
+
},
|
|
143
215
|
notifySnapshot: (snapshot) => {
|
|
144
216
|
for (const listener of snapshotListeners)
|
|
145
217
|
listener(snapshot);
|
|
146
218
|
},
|
|
147
|
-
prepareSubmit
|
|
148
|
-
const values = {};
|
|
149
|
-
for (const preparation of submitPreparations) {
|
|
150
|
-
const preparedValues = await preparation();
|
|
151
|
-
if (preparedValues)
|
|
152
|
-
Object.assign(values, preparedValues);
|
|
153
|
-
}
|
|
154
|
-
return values;
|
|
155
|
-
},
|
|
219
|
+
prepareSubmit,
|
|
156
220
|
};
|
|
157
221
|
}
|
|
158
222
|
function recordUiMetadata(form) {
|
package/dist/space/record.js
CHANGED
|
@@ -157,23 +157,29 @@ export class ProtocolRecordHandle {
|
|
|
157
157
|
return { ok: result.ok, snapshot };
|
|
158
158
|
}
|
|
159
159
|
async submit() {
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
160
|
+
const endSubmission = __classPrivateFieldGet(this, _ProtocolRecordHandle_formLifecycle, "f")?.beginSubmit();
|
|
161
|
+
try {
|
|
162
|
+
const values = await __classPrivateFieldGet(this, _ProtocolRecordHandle_formLifecycle, "f")?.prepareSubmit() ?? {};
|
|
163
|
+
const request = {
|
|
164
|
+
operation: 'record.submit',
|
|
165
|
+
payload: {
|
|
166
|
+
sessionId: this.id,
|
|
167
|
+
...(Object.keys(values).length > 0 ? { values } : {}),
|
|
168
|
+
},
|
|
169
|
+
protocol: PROTOCOL_VERSION,
|
|
170
|
+
requestId: __classPrivateFieldGet(this, _ProtocolRecordHandle_nextRequestId, "f").call(this),
|
|
171
|
+
};
|
|
172
|
+
const response = await __classPrivateFieldGet(this, _ProtocolRecordHandle_transport, "f").request(request);
|
|
173
|
+
const result = readResponse(response, request);
|
|
174
|
+
if (!isRecordSubmitResult(result))
|
|
175
|
+
throw invalidResponse('The record-submit response is malformed.');
|
|
176
|
+
const snapshot = immutableSnapshot(result.snapshot);
|
|
177
|
+
this.setSnapshot(snapshot);
|
|
178
|
+
return { ok: result.ok, snapshot };
|
|
179
|
+
}
|
|
180
|
+
finally {
|
|
181
|
+
endSubmission?.();
|
|
182
|
+
}
|
|
177
183
|
}
|
|
178
184
|
}
|
|
179
185
|
_ProtocolRecordHandle_nextRequestId = new WeakMap(), _ProtocolRecordHandle_transport = new WeakMap(), _ProtocolRecordHandle_formLifecycle = new WeakMap(), _ProtocolRecordHandle_snapshot = new WeakMap();
|