@symbiotejs/symbiote 3.9.0 → 3.10.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/README.md +12 -0
- package/core/AppRouter.js +97 -58
- package/core/PubSub.js +42 -11
- package/core/Symbiote.js +70 -46
- package/core/itemizeProcessor-keyed.js +88 -188
- package/core/itemizeProcessor.js +1 -12
- package/core/itemizeSetup.js +26 -3
- package/core/ownElements.js +4 -7
- package/core/resolveTemplate.js +12 -0
- package/core/tpl-processors.js +4 -2
- package/docs/README.md +2 -0
- package/docs/html-app-vs-htmx.md +55 -0
- package/docs/lifecycle.md +2 -0
- package/docs/list-rendering.md +10 -2
- package/docs/pubsub.md +3 -0
- package/docs/routing.md +15 -4
- package/html-app/BaseClass.js +203 -0
- package/html-app/README.md +369 -0
- package/html-app/action-data.js +168 -0
- package/html-app/actions.js +366 -0
- package/html-app/collection-actions.js +104 -0
- package/html-app/collection-data.js +234 -0
- package/html-app/collection-items.js +45 -0
- package/html-app/conditions.js +42 -0
- package/html-app/context-data.js +185 -0
- package/html-app/context-requests.js +374 -0
- package/html-app/context-storage.js +376 -0
- package/html-app/declarations.js +55 -0
- package/html-app/examples/assets/actions.html +56 -0
- package/html-app/examples/assets/collections.html +72 -0
- package/html-app/examples/assets/components.html +25 -0
- package/html-app/examples/assets/conditional.html +29 -0
- package/html-app/examples/assets/data.html +31 -0
- package/html-app/examples/assets/guide.html +4 -0
- package/html-app/examples/assets/imports.html +11 -0
- package/html-app/examples/assets/not-found.html +4 -0
- package/html-app/examples/assets/routes.json +58 -0
- package/html-app/examples/assets/routing.html +27 -0
- package/html-app/examples/assets/state.html +32 -0
- package/html-app/examples/assets/storage.html +28 -0
- package/html-app/examples/assets/tasks.json +25 -0
- package/html-app/examples/full-app.html +147 -0
- package/html-app/examples/todo-ref.html +37 -0
- package/html-app/fetch-source.js +147 -0
- package/html-app/fragment-content.js +29 -0
- package/html-app/fragment-source.js +169 -0
- package/html-app/index.js +34 -0
- package/html-app/initial-data.js +88 -0
- package/html-app/pub-map.js +93 -0
- package/html-app/router-data.js +50 -0
- package/html-app/router-navigation.js +70 -0
- package/html-app/storage-revisions.js +130 -0
- package/html-app/sym-collection.js +142 -0
- package/html-app/sym-component.js +20 -0
- package/html-app/sym-conditional.js +141 -0
- package/html-app/sym-context.js +176 -0
- package/html-app/sym-import.js +218 -0
- package/html-app/sym-router.js +107 -0
- package/package.json +18 -6
- package/scripts/check-html-app-package.js +117 -0
- package/scripts/check-html-app-size.js +91 -0
- package/scripts/update-exports.js +4 -0
- package/skills/symbiote/SKILL.md +64 -15
- package/skills/symbiote/agents/openai.yaml +1 -1
- package/types/core/AppRouter.d.ts +13 -4
- package/types/core/AppRouter.d.ts.map +1 -1
- package/types/core/PubSub.d.ts +1 -1
- package/types/core/PubSub.d.ts.map +1 -1
- package/types/core/Symbiote.d.ts +4 -1
- package/types/core/Symbiote.d.ts.map +1 -1
- package/types/core/itemizeProcessor-keyed.d.ts.map +1 -1
- package/types/core/itemizeProcessor.d.ts.map +1 -1
- package/types/core/itemizeSetup.d.ts +1 -0
- package/types/core/itemizeSetup.d.ts.map +1 -1
- package/types/core/ownElements.d.ts +1 -1
- package/types/core/ownElements.d.ts.map +1 -1
- package/types/core/resolveTemplate.d.ts +2 -0
- package/types/core/resolveTemplate.d.ts.map +1 -0
- package/types/core/tpl-processors.d.ts.map +1 -1
- package/types/html-app/BaseClass.d.ts +24 -0
- package/types/html-app/BaseClass.d.ts.map +1 -0
- package/types/html-app/action-data.d.ts +15 -0
- package/types/html-app/action-data.d.ts.map +1 -0
- package/types/html-app/actions.d.ts +104 -0
- package/types/html-app/actions.d.ts.map +1 -0
- package/types/html-app/collection-actions.d.ts +12 -0
- package/types/html-app/collection-actions.d.ts.map +1 -0
- package/types/html-app/collection-data.d.ts +13 -0
- package/types/html-app/collection-data.d.ts.map +1 -0
- package/types/html-app/collection-items.d.ts +7 -0
- package/types/html-app/collection-items.d.ts.map +1 -0
- package/types/html-app/conditions.d.ts +5 -0
- package/types/html-app/conditions.d.ts.map +1 -0
- package/types/html-app/context-data.d.ts +31 -0
- package/types/html-app/context-data.d.ts.map +1 -0
- package/types/html-app/context-requests.d.ts +97 -0
- package/types/html-app/context-requests.d.ts.map +1 -0
- package/types/html-app/context-storage.d.ts +102 -0
- package/types/html-app/context-storage.d.ts.map +1 -0
- package/types/html-app/declarations.d.ts +3 -0
- package/types/html-app/declarations.d.ts.map +1 -0
- package/types/html-app/fetch-source.d.ts +23 -0
- package/types/html-app/fetch-source.d.ts.map +1 -0
- package/types/html-app/fragment-content.d.ts +6 -0
- package/types/html-app/fragment-content.d.ts.map +1 -0
- package/types/html-app/fragment-source.d.ts +14 -0
- package/types/html-app/fragment-source.d.ts.map +1 -0
- package/types/html-app/index.d.ts +9 -0
- package/types/html-app/index.d.ts.map +1 -0
- package/types/html-app/initial-data.d.ts +8 -0
- package/types/html-app/initial-data.d.ts.map +1 -0
- package/types/html-app/pub-map.d.ts +19 -0
- package/types/html-app/pub-map.d.ts.map +1 -0
- package/types/html-app/router-data.d.ts +4 -0
- package/types/html-app/router-data.d.ts.map +1 -0
- package/types/html-app/router-navigation.d.ts +3 -0
- package/types/html-app/router-navigation.d.ts.map +1 -0
- package/types/html-app/storage-revisions.d.ts +11 -0
- package/types/html-app/storage-revisions.d.ts.map +1 -0
- package/types/html-app/sym-collection.d.ts +16 -0
- package/types/html-app/sym-collection.d.ts.map +1 -0
- package/types/html-app/sym-component.d.ts +5 -0
- package/types/html-app/sym-component.d.ts.map +1 -0
- package/types/html-app/sym-conditional.d.ts +10 -0
- package/types/html-app/sym-conditional.d.ts.map +1 -0
- package/types/html-app/sym-context.d.ts +16 -0
- package/types/html-app/sym-context.d.ts.map +1 -0
- package/types/html-app/sym-import.d.ts +19 -0
- package/types/html-app/sym-import.d.ts.map +1 -0
- package/types/html-app/sym-router.d.ts +12 -0
- package/types/html-app/sym-router.d.ts.map +1 -0
- package/types/utils/setNestedProp.d.ts.map +1 -1
- package/utils/setNestedProp.js +8 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { isDataKey } from './initial-data.js';
|
|
2
|
+
import { validateValue } from './action-data.js';
|
|
3
|
+
|
|
4
|
+
/** @typedef {Record<string, any>} RecordData */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Validate JSON values without dropping unsupported fields during cloning.
|
|
8
|
+
* @param {unknown} value
|
|
9
|
+
* @param {Set<object>} [ancestors]
|
|
10
|
+
*/
|
|
11
|
+
export function validateJSON(value, ancestors = new Set()) {
|
|
12
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean'
|
|
13
|
+
|| (typeof value === 'number' && Number.isFinite(value))) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (!value || typeof value !== 'object' || ancestors.has(value)
|
|
17
|
+
|| (!Array.isArray(value) && ![
|
|
18
|
+
Object.prototype,
|
|
19
|
+
null,
|
|
20
|
+
].includes(Object.getPrototypeOf(value)))) {
|
|
21
|
+
throw new TypeError('Collection data must contain JSON values only.');
|
|
22
|
+
}
|
|
23
|
+
if (Object.getOwnPropertySymbols(value).length) {
|
|
24
|
+
throw new TypeError('Collection data cannot contain symbol fields.');
|
|
25
|
+
}
|
|
26
|
+
ancestors.add(value);
|
|
27
|
+
for (let key of Object.keys(value)) {
|
|
28
|
+
if (!Array.isArray(value) && !isDataKey(key)) {
|
|
29
|
+
throw new TypeError('Collection data contains an unsafe field.');
|
|
30
|
+
}
|
|
31
|
+
validateJSON(value[key], ancestors);
|
|
32
|
+
}
|
|
33
|
+
if (Array.isArray(value) && (Object.keys(value).length !== value.length
|
|
34
|
+
|| Object.keys(value).some((key, index) => key !== String(index)))) {
|
|
35
|
+
throw new TypeError('Collection arrays must not contain holes or extra properties.');
|
|
36
|
+
}
|
|
37
|
+
ancestors.delete(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @param {unknown} value
|
|
41
|
+
* @returns {RecordData}
|
|
42
|
+
*/
|
|
43
|
+
export function recordData(value) {
|
|
44
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
45
|
+
throw new TypeError('Expected a JSON record.');
|
|
46
|
+
}
|
|
47
|
+
validateJSON(value);
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @param {unknown} value
|
|
52
|
+
* @returns {RecordData[]}
|
|
53
|
+
*/
|
|
54
|
+
export function collectionData(value) {
|
|
55
|
+
if (!Array.isArray(value)) {
|
|
56
|
+
throw new TypeError('Expected an array of records.');
|
|
57
|
+
}
|
|
58
|
+
let keys = new Set();
|
|
59
|
+
for (let item of value) {
|
|
60
|
+
recordData(item);
|
|
61
|
+
if (typeof item.id !== 'string' || !item.id || keys.has(item.id)) {
|
|
62
|
+
throw new TypeError('Collection records require unique nonempty string IDs.');
|
|
63
|
+
}
|
|
64
|
+
keys.add(item.id);
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @template T
|
|
71
|
+
* @param {T} value
|
|
72
|
+
* @returns {T}
|
|
73
|
+
*/
|
|
74
|
+
export function cloneData(value) {
|
|
75
|
+
validateJSON(value);
|
|
76
|
+
return structuredClone(value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {RecordData} item
|
|
81
|
+
* @param {RecordData} match
|
|
82
|
+
*/
|
|
83
|
+
export function matches(item, match) {
|
|
84
|
+
return Object.entries(match).every(([
|
|
85
|
+
key,
|
|
86
|
+
value,
|
|
87
|
+
]) => Object.hasOwn(item, key) && Object.is(item[key], value));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** @param {unknown} value */
|
|
91
|
+
export function matchData(value) {
|
|
92
|
+
let match = recordData(value);
|
|
93
|
+
if (Object.values(match).some((value) => value !== null && typeof value === 'object')) {
|
|
94
|
+
throw new TypeError('Matching accepts JSON scalar values only.');
|
|
95
|
+
}
|
|
96
|
+
return match;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param {unknown} source
|
|
101
|
+
* @param {unknown} draft
|
|
102
|
+
* @param {boolean} atStart
|
|
103
|
+
*/
|
|
104
|
+
function insertRecord(source, draft, atStart) {
|
|
105
|
+
let items = collectionData(source);
|
|
106
|
+
let item = cloneData(recordData(draft));
|
|
107
|
+
if (!Object.hasOwn(item, 'id')) {
|
|
108
|
+
item.id = crypto.randomUUID();
|
|
109
|
+
}
|
|
110
|
+
let next = atStart ? [
|
|
111
|
+
item,
|
|
112
|
+
...items,
|
|
113
|
+
] : [
|
|
114
|
+
...items,
|
|
115
|
+
item,
|
|
116
|
+
];
|
|
117
|
+
collectionData(next);
|
|
118
|
+
return next;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {unknown} source
|
|
123
|
+
* @param {unknown} draft
|
|
124
|
+
*/
|
|
125
|
+
export function appendRecord(source, draft) {
|
|
126
|
+
return insertRecord(source, draft, false);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {unknown} source
|
|
131
|
+
* @param {unknown} draft
|
|
132
|
+
*/
|
|
133
|
+
export function prependRecord(source, draft) {
|
|
134
|
+
return insertRecord(source, draft, true);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {RecordData} item
|
|
139
|
+
* @param {RecordData} patch
|
|
140
|
+
*/
|
|
141
|
+
function patchedRecord(item, patch) {
|
|
142
|
+
if (Object.hasOwn(patch, 'id') && patch.id !== item.id) {
|
|
143
|
+
throw new TypeError('A collection record ID cannot be changed.');
|
|
144
|
+
}
|
|
145
|
+
for (let key of Object.keys(patch)) {
|
|
146
|
+
if (Object.hasOwn(item, key)) {
|
|
147
|
+
validateValue(item[key], patch[key]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
...item,
|
|
152
|
+
...cloneData(patch),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {unknown} source
|
|
158
|
+
* @param {string} id
|
|
159
|
+
* @param {RecordData | null} patch Null removes the record.
|
|
160
|
+
*/
|
|
161
|
+
export function updateRecord(source, id, patch) {
|
|
162
|
+
let items = collectionData(source);
|
|
163
|
+
if (!items.some((item) => item.id === id)) {
|
|
164
|
+
throw new TypeError('The collection record no longer exists.');
|
|
165
|
+
}
|
|
166
|
+
if (patch === null) {
|
|
167
|
+
return items.filter((item) => item.id !== id);
|
|
168
|
+
}
|
|
169
|
+
recordData(patch);
|
|
170
|
+
return items.map((item) => item.id === id ? patchedRecord(item, patch) : item);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @param {unknown} source
|
|
175
|
+
* @param {unknown} match
|
|
176
|
+
* @param {unknown} [patch] Omit to remove matching records.
|
|
177
|
+
*/
|
|
178
|
+
export function updateMatching(source, match, patch) {
|
|
179
|
+
let items = collectionData(source);
|
|
180
|
+
let filter = matchData(match);
|
|
181
|
+
if (patch === undefined) {
|
|
182
|
+
if (!Object.keys(filter).length) {
|
|
183
|
+
throw new TypeError('Removing records requires a nonempty match.');
|
|
184
|
+
}
|
|
185
|
+
return items.filter((item) => !matches(item, filter));
|
|
186
|
+
}
|
|
187
|
+
let changes = recordData(patch);
|
|
188
|
+
if (Object.hasOwn(changes, 'id')) {
|
|
189
|
+
throw new TypeError('Bulk patches cannot change record IDs.');
|
|
190
|
+
}
|
|
191
|
+
return items.map((item) => matches(item, filter) ? patchedRecord(item, changes) : item);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param {unknown} source
|
|
196
|
+
* @param {unknown} [filter]
|
|
197
|
+
* @param {unknown} [search]
|
|
198
|
+
* @param {string[]} [fields]
|
|
199
|
+
* @param {unknown} [sort]
|
|
200
|
+
*/
|
|
201
|
+
export function projectCollection(source, filter = {}, search = '', fields = [], sort = null) {
|
|
202
|
+
let items = collectionData(source);
|
|
203
|
+
let match = matchData(filter);
|
|
204
|
+
if (typeof search !== 'string') {
|
|
205
|
+
throw new TypeError('Collection search must be a string.');
|
|
206
|
+
}
|
|
207
|
+
if (fields.some((field) => !isDataKey(field))) {
|
|
208
|
+
throw new TypeError('Search fields must be local data keys.');
|
|
209
|
+
}
|
|
210
|
+
let query = search.trim().toLocaleLowerCase();
|
|
211
|
+
if (query && !fields.length) {
|
|
212
|
+
throw new TypeError('Collection search requires explicit search-fields.');
|
|
213
|
+
}
|
|
214
|
+
let visible = items.filter((item) => matches(item, match)
|
|
215
|
+
&& (!query || fields.some((field) => String(item[field] ?? '').toLocaleLowerCase().includes(query))));
|
|
216
|
+
if (sort !== null) {
|
|
217
|
+
let order = recordData(sort);
|
|
218
|
+
if (typeof order.field !== 'string' || !isDataKey(order.field)
|
|
219
|
+
|| ![
|
|
220
|
+
'asc',
|
|
221
|
+
'desc',
|
|
222
|
+
].includes(order.direction)) {
|
|
223
|
+
throw new TypeError('Collection sort requires field and asc/desc direction.');
|
|
224
|
+
}
|
|
225
|
+
visible.sort((left, right) => {
|
|
226
|
+
let a = left[order.field];
|
|
227
|
+
let b = right[order.field];
|
|
228
|
+
let result = typeof a === 'number' && typeof b === 'number' ? a - b
|
|
229
|
+
: String(a ?? '').localeCompare(String(b ?? ''));
|
|
230
|
+
return order.direction === 'desc' ? -result : result;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
return visible;
|
|
234
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ownElements } from '../core/ownElements.js';
|
|
2
|
+
import { itemizeProcessor } from '../core/itemizeProcessor-keyed.js';
|
|
3
|
+
import { DICT } from '../core/dictionary.js';
|
|
4
|
+
|
|
5
|
+
/** @type {WeakMap<Element, import('./sym-collection.js').SymCollection>} */
|
|
6
|
+
let containers = new WeakMap();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Associate only this controller's own Itemize containers with its source.
|
|
10
|
+
* @param {Element | DocumentFragment} root
|
|
11
|
+
* @param {import('./sym-collection.js').SymCollection} owner
|
|
12
|
+
*/
|
|
13
|
+
export function collectionItemsProcessor(root, owner) {
|
|
14
|
+
for (let element of ownElements(root, '[itemize]')) {
|
|
15
|
+
let reference = owner.Symbiote.parseProp(element.getAttribute('itemize'), owner);
|
|
16
|
+
if (reference?.ctx === owner.localCtx && reference.name === 'items') {
|
|
17
|
+
containers.set(element, owner);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
itemizeProcessor(/** @type {DocumentFragment} */ (root), owner);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @param {Element} owner */
|
|
24
|
+
export function findCollectionItem(owner) {
|
|
25
|
+
let item = owner;
|
|
26
|
+
while (item) {
|
|
27
|
+
let collection = containers.get(item.parentElement);
|
|
28
|
+
if (collection) {
|
|
29
|
+
if (!collection.isConnected) {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
let id = Reflect.get(item, DICT.MCP_ITEM_KEY_KEY);
|
|
33
|
+
if (typeof id !== 'string') {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
collection,
|
|
38
|
+
item,
|
|
39
|
+
id,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
item = item.parentElement;
|
|
43
|
+
}
|
|
44
|
+
throw new TypeError('Item actions require an item in a sym-collection view.');
|
|
45
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
let behaviors = Object.freeze({
|
|
2
|
+
show: {
|
|
3
|
+
dom: false,
|
|
4
|
+
inverse: false,
|
|
5
|
+
},
|
|
6
|
+
hide: {
|
|
7
|
+
dom: false,
|
|
8
|
+
inverse: true,
|
|
9
|
+
},
|
|
10
|
+
'dom-add': {
|
|
11
|
+
dom: true,
|
|
12
|
+
inverse: false,
|
|
13
|
+
},
|
|
14
|
+
'dom-remove': {
|
|
15
|
+
dom: true,
|
|
16
|
+
inverse: true,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Conditions compare data values, never expressions.
|
|
22
|
+
* @param {unknown} value
|
|
23
|
+
* @param {string | null} expected
|
|
24
|
+
* @param {string} behavior
|
|
25
|
+
* @param {string | null} type
|
|
26
|
+
*/
|
|
27
|
+
export function conditionState(value, expected, behavior = 'show', type = null) {
|
|
28
|
+
let mode = Object.hasOwn(behaviors, behavior) ? behaviors[behavior] : undefined;
|
|
29
|
+
if (!mode) {
|
|
30
|
+
throw new TypeError('Conditional behavior must be show, hide, dom-add or dom-remove.');
|
|
31
|
+
}
|
|
32
|
+
if (type !== null && (type !== 'string' || expected === null)) {
|
|
33
|
+
throw new TypeError('is-type supports string with an explicit is value.');
|
|
34
|
+
}
|
|
35
|
+
let match = expected === null ? !!value
|
|
36
|
+
: Object.is(value, type === 'string' ? expected
|
|
37
|
+
: expected === 'true' ? true : expected === 'false' ? false : expected);
|
|
38
|
+
return {
|
|
39
|
+
visible: mode.inverse ? !match : match,
|
|
40
|
+
dom: mode.dom,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { isDataKey, initialDefaults } from './initial-data.js';
|
|
2
|
+
import { cloneData, validateJSON } from './collection-data.js';
|
|
3
|
+
import { SourceError } from './fetch-source.js';
|
|
4
|
+
import { validateValue } from './action-data.js';
|
|
5
|
+
|
|
6
|
+
export class ContextData {
|
|
7
|
+
keys = new Set();
|
|
8
|
+
bound = new Set();
|
|
9
|
+
edited = new Set();
|
|
10
|
+
/** @type {Map<string, Set<object>>} */
|
|
11
|
+
pending = new Map();
|
|
12
|
+
/** @type {Map<string, number>} */
|
|
13
|
+
versions = new Map();
|
|
14
|
+
revision = 0;
|
|
15
|
+
/** @type {number | null} */
|
|
16
|
+
savedRevision = null;
|
|
17
|
+
applying = false;
|
|
18
|
+
|
|
19
|
+
/** @param {import('./sym-context.js').SymContext} owner */
|
|
20
|
+
constructor(owner) {
|
|
21
|
+
this.owner = owner;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
setup() {
|
|
25
|
+
for (let [
|
|
26
|
+
key,
|
|
27
|
+
value,
|
|
28
|
+
] of Object.entries({
|
|
29
|
+
...this.owner.init$,
|
|
30
|
+
...initialDefaults.get(this.owner.localCtx),
|
|
31
|
+
})) {
|
|
32
|
+
if (isDataKey(key)) {
|
|
33
|
+
try {
|
|
34
|
+
validateJSON(value);
|
|
35
|
+
this.keys.add(key);
|
|
36
|
+
} catch {
|
|
37
|
+
// Native handlers and descriptors are controls, not public JSON data.
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @param {Record<string, unknown>} values */
|
|
44
|
+
incoming(values) {
|
|
45
|
+
for (let [
|
|
46
|
+
key,
|
|
47
|
+
value,
|
|
48
|
+
] of Object.entries(values)) {
|
|
49
|
+
if (!isDataKey(key)) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!this.applying) {
|
|
53
|
+
this.bound.add(key);
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
validateJSON(value);
|
|
57
|
+
this.keys.add(key);
|
|
58
|
+
} catch {
|
|
59
|
+
// Existing public fields remain selected so invalid writes fail visibly.
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
selected() {
|
|
65
|
+
let fields = this.owner.getAttribute('sync-keys');
|
|
66
|
+
return fields === null ? [
|
|
67
|
+
...this.keys,
|
|
68
|
+
] : fields.split(',').map((key) => key.trim()).filter(Boolean);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
dirty() {
|
|
72
|
+
return this.selected().some((key) => this.edited.has(key) || this.pending.has(key));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {string} key
|
|
77
|
+
* @param {unknown} previous
|
|
78
|
+
* @param {unknown} value
|
|
79
|
+
*/
|
|
80
|
+
changed(key, previous, value) {
|
|
81
|
+
if (!isDataKey(key) || Object.is(previous, value)) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (!this.keys.has(key)) {
|
|
85
|
+
try {
|
|
86
|
+
validateJSON(value);
|
|
87
|
+
this.keys.add(key);
|
|
88
|
+
} catch {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
this.versions.set(key, (this.versions.get(key) ?? 0) + 1);
|
|
93
|
+
if (!this.applying) {
|
|
94
|
+
this.edited.add(key);
|
|
95
|
+
}
|
|
96
|
+
if (this.selected().includes(key)) {
|
|
97
|
+
this.revision++;
|
|
98
|
+
}
|
|
99
|
+
this.owner.requests.publish();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Protect a captured edit without publishing it before its debounce expires.
|
|
104
|
+
* @param {string} key
|
|
105
|
+
* @param {unknown} value
|
|
106
|
+
*/
|
|
107
|
+
intent(key, value) {
|
|
108
|
+
validateJSON(value);
|
|
109
|
+
this.keys.add(key);
|
|
110
|
+
this.versions.set(key, (this.versions.get(key) ?? 0) + 1);
|
|
111
|
+
let token = {};
|
|
112
|
+
let pending = this.pending.get(key) ?? new Set();
|
|
113
|
+
pending.add(token);
|
|
114
|
+
this.pending.set(key, pending);
|
|
115
|
+
this.owner.requests.publish();
|
|
116
|
+
return {
|
|
117
|
+
remove: () => {
|
|
118
|
+
pending.delete(token);
|
|
119
|
+
if (!pending.size) {
|
|
120
|
+
this.pending.delete(key);
|
|
121
|
+
}
|
|
122
|
+
this.owner.requests.publish();
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** @param {string[]} [keys] */
|
|
128
|
+
snapshot(keys = this.selected()) {
|
|
129
|
+
if (!keys.length || new Set(keys).size !== keys.length
|
|
130
|
+
|| keys.some((key) => !isDataKey(key) || !this.keys.has(key))) {
|
|
131
|
+
throw new SourceError('INVALID_DATA', 'Selected fields must be known public data fields.');
|
|
132
|
+
}
|
|
133
|
+
let data = Object.fromEntries(keys.map((key) => [
|
|
134
|
+
key,
|
|
135
|
+
this.owner.localCtx.read(key),
|
|
136
|
+
]));
|
|
137
|
+
return {
|
|
138
|
+
data: cloneData(data),
|
|
139
|
+
revision: this.revision,
|
|
140
|
+
versions: new Map(keys.map((key) => [
|
|
141
|
+
key,
|
|
142
|
+
this.versions.get(key) ?? 0,
|
|
143
|
+
])),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {number} revision
|
|
149
|
+
* @param {Map<string, number>} versions
|
|
150
|
+
*/
|
|
151
|
+
acknowledge(revision, versions) {
|
|
152
|
+
this.savedRevision = revision;
|
|
153
|
+
for (let [
|
|
154
|
+
key,
|
|
155
|
+
version,
|
|
156
|
+
] of versions) {
|
|
157
|
+
if ((this.versions.get(key) ?? 0) === version) {
|
|
158
|
+
this.edited.delete(key);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
this.owner.requests.publish();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** @param {Record<string, unknown>} data */
|
|
165
|
+
validate(data) {
|
|
166
|
+
for (let [
|
|
167
|
+
key,
|
|
168
|
+
value,
|
|
169
|
+
] of Object.entries(data)) {
|
|
170
|
+
if (!isDataKey(key)) {
|
|
171
|
+
throw new SourceError('INVALID_DATA', 'Source keys must be public data fields.');
|
|
172
|
+
}
|
|
173
|
+
if (this.owner.localCtx.has(key)) {
|
|
174
|
+
let current = this.owner.localCtx.read(key);
|
|
175
|
+
validateJSON(current);
|
|
176
|
+
validateValue(current, value);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
invalidate() {
|
|
182
|
+
this.savedRevision = null;
|
|
183
|
+
this.revision++;
|
|
184
|
+
}
|
|
185
|
+
}
|