@jxsuite/server 9.2.1 → 9.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.
- package/package.json +24 -18
- package/src/collab.ts +390 -18
- package/src/jxn-response.ts +41 -0
- package/src/net-guard.ts +4 -3
- package/src/project-events.ts +622 -0
- package/src/project-server.ts +102 -3
- package/src/project-state/collections.ts +108 -0
- package/src/project-state/git-monitor.ts +302 -0
- package/src/project-state/graph.ts +651 -0
- package/src/project-state/host-deps.ts +224 -0
- package/src/project-state/host.ts +2113 -0
- package/src/project-state/overlays.ts +231 -0
- package/src/project-state/rename.ts +473 -0
- package/src/project-state/settle.ts +38 -0
- package/src/refactor/apply.ts +177 -67
- package/src/refactor/find-refs.ts +0 -0
- package/src/refactor/index.ts +2 -1
- package/src/refactor/paths.ts +3 -325
- package/src/refactor/refs.ts +5 -388
- package/src/refactor/scan.ts +17 -4
- package/src/refactor/watcher.ts +47 -2
- package/src/serve.node.ts +1 -0
- package/src/serve.ts +7 -5
- package/src/server.ts +40 -1
- package/src/sse.ts +1 -1
- package/src/studio-api.ts +76 -9
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host's dependency graph (`specs/project-state.md` §8): for each document, the documents whose
|
|
3
|
+
* rendering depends on it, by edge kind (§8.1), and the `dependentsInvalidated` events a change to
|
|
4
|
+
* it causes.
|
|
5
|
+
*
|
|
6
|
+
* The module performs no I/O. A host parses a document, hands the parsed value to
|
|
7
|
+
* {@link extractRefs}, and gives the result to the graph with the document's id and path; what the
|
|
8
|
+
* graph needs from the project configuration and the collection loader arrives through
|
|
9
|
+
* {@link DependencyGraph.configure}. That keeps the same module bundleable for every host: Bun
|
|
10
|
+
* imports it as source, and a Durable Object, a browser or the desktop backend's Node bundle bundle
|
|
11
|
+
* it unchanged (`packages/server/tests/refs-export.test.ts` proves its module graph has no
|
|
12
|
+
* filesystem, process or watcher import).
|
|
13
|
+
*
|
|
14
|
+
* References come from `walkDocRefs` and tag uses from the walk behind `countTagUses`, imported
|
|
15
|
+
* from the pure `refs.ts` module and never from the `./refactor` index, so the graph cannot
|
|
16
|
+
* disagree with the rename refactor or the usage query (§12).
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { isJsonObject, isString } from "@jxsuite/schema/guards";
|
|
22
|
+
import { PROJECT_FILE_NAMES } from "@jxsuite/schema/project-file";
|
|
23
|
+
import type { AssetMount } from "@jxsuite/schema/asset-paths";
|
|
24
|
+
import type { JsonValue } from "@jxsuite/schema/types";
|
|
25
|
+
import { DEPENDENCY_KIND_ORDER } from "@jxsuite/protocol/project-state";
|
|
26
|
+
import type {
|
|
27
|
+
DependencyKind,
|
|
28
|
+
DocId,
|
|
29
|
+
DocKind,
|
|
30
|
+
EventOrigin,
|
|
31
|
+
ProjectEventInit,
|
|
32
|
+
} from "@jxsuite/protocol/project-state";
|
|
33
|
+
import { classifyRef, joinPosix, projectPathsForRootedRef } from "../refactor/paths.ts";
|
|
34
|
+
import { collectTagUses, walkDocRefs } from "../refactor/refs.ts";
|
|
35
|
+
|
|
36
|
+
// ─── What a document references ───────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
/** One reference-bearing string `walkDocRefs` offered, exactly as authored. */
|
|
39
|
+
export interface ExtractedRef {
|
|
40
|
+
value: string;
|
|
41
|
+
/** The key that carried it (`walkDocRefs`' `refType`). */
|
|
42
|
+
refType: string;
|
|
43
|
+
/** Whether a bare value resolves against the project root rather than the document. */
|
|
44
|
+
rootRelativeBare: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A document's outgoing references, unresolved. The graph resolves them against the document's
|
|
49
|
+
* current path and configuration, so a relabel or a configuration change re-resolves them without
|
|
50
|
+
* the document being parsed again.
|
|
51
|
+
*/
|
|
52
|
+
export interface DocumentRefs {
|
|
53
|
+
/** Every file reference in the document (`walkDocRefs`). */
|
|
54
|
+
refs: readonly ExtractedRef[];
|
|
55
|
+
/** The custom-element tags the document instantiates, its own definition's excluded. */
|
|
56
|
+
tags: readonly string[];
|
|
57
|
+
/**
|
|
58
|
+
* How many nodes carry each `tagName`, every tag and the document's own root included: the tally
|
|
59
|
+
* the usage query reads one tag of (`countTagUses`, §12).
|
|
60
|
+
*/
|
|
61
|
+
tagCounts: Readonly<Record<string, number>>;
|
|
62
|
+
/** The tag a component definition declares, `null` for every other document. */
|
|
63
|
+
defines: string | null;
|
|
64
|
+
/** Whether the document is a page that names no layout, so it wraps in `defaults.layout`. */
|
|
65
|
+
implicitLayout: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* The content types the document queries: `ContentCollection` and `ContentEntry` state in any
|
|
68
|
+
* scope, and every `contentType` its `$paths` names, `each` included.
|
|
69
|
+
*/
|
|
70
|
+
contentTypes: readonly string[];
|
|
71
|
+
/**
|
|
72
|
+
* The `$prototype` names the document uses that its own `imports` do not map, so the project
|
|
73
|
+
* configuration's `imports` resolve them (`imports.md` §1.1).
|
|
74
|
+
*/
|
|
75
|
+
prototypes: readonly string[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The references of a document that has none, such as a media file or a stylesheet. */
|
|
79
|
+
export const NO_REFS: DocumentRefs = Object.freeze({
|
|
80
|
+
contentTypes: [],
|
|
81
|
+
defines: null,
|
|
82
|
+
implicitLayout: false,
|
|
83
|
+
prototypes: [],
|
|
84
|
+
refs: [],
|
|
85
|
+
tagCounts: {},
|
|
86
|
+
tags: [],
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/** The state prototypes that read a content collection (`site-architecture.md` §6.4). */
|
|
90
|
+
const CONTENT_PROTOTYPES = new Set(["ContentCollection", "ContentEntry"]);
|
|
91
|
+
|
|
92
|
+
/** Add `value`'s content type names, one or a list, to `into`. */
|
|
93
|
+
function addTypes(value: JsonValue | undefined, into: Set<string>): void {
|
|
94
|
+
for (const type of Array.isArray(value) ? value : [value]) {
|
|
95
|
+
if (isString(type)) {
|
|
96
|
+
into.add(type);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Every `contentType` under a `$paths` source, `each` and nested sources included. */
|
|
102
|
+
function pathsContentTypes(node: JsonValue, into: Set<string>): void {
|
|
103
|
+
if (Array.isArray(node)) {
|
|
104
|
+
for (const item of node) {
|
|
105
|
+
pathsContentTypes(item, into);
|
|
106
|
+
}
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (!isJsonObject(node)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
for (const [key, value] of Object.entries(node)) {
|
|
113
|
+
if (key === "contentType") {
|
|
114
|
+
addTypes(value, into);
|
|
115
|
+
} else {
|
|
116
|
+
pathsContentTypes(value, into);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The content types a document queries and the prototype names it uses, from every scope: a nested
|
|
123
|
+
* element's `state` lists a collection as the root's does (`spec.md` §23.3), and a `$paths` source
|
|
124
|
+
* names its type (`site-architecture.md` §4.3).
|
|
125
|
+
*/
|
|
126
|
+
function queriesOf(doc: JsonValue) {
|
|
127
|
+
const types = new Set<string>();
|
|
128
|
+
const prototypes = new Set<string>();
|
|
129
|
+
const visit = (node: JsonValue): void => {
|
|
130
|
+
if (Array.isArray(node)) {
|
|
131
|
+
for (const item of node) {
|
|
132
|
+
visit(item);
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (!isJsonObject(node)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (isString(node.$prototype)) {
|
|
140
|
+
prototypes.add(node.$prototype);
|
|
141
|
+
if (CONTENT_PROTOTYPES.has(node.$prototype)) {
|
|
142
|
+
addTypes(node.contentType, types);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
for (const [key, value] of Object.entries(node)) {
|
|
146
|
+
if (key === "$paths") {
|
|
147
|
+
pathsContentTypes(value, types);
|
|
148
|
+
} else {
|
|
149
|
+
visit(value);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
visit(doc);
|
|
154
|
+
const root = isJsonObject(doc) ? doc : null;
|
|
155
|
+
const own = root !== null && isJsonObject(root.imports) ? root.imports : {};
|
|
156
|
+
return {
|
|
157
|
+
contentTypes: [...types].toSorted(),
|
|
158
|
+
prototypes: [...prototypes].filter((name) => !Object.hasOwn(own, name)).toSorted(),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Extract a parsed document's references for the graph. Pure: `doc` is read, never written.
|
|
164
|
+
*
|
|
165
|
+
* @param doc - The parsed document (a JSON document, or what a format parser produced)
|
|
166
|
+
* @param kind - The document's kind in the index (§2.2)
|
|
167
|
+
*/
|
|
168
|
+
export function extractRefs(doc: JsonValue, kind: DocKind): DocumentRefs {
|
|
169
|
+
const refs: ExtractedRef[] = [];
|
|
170
|
+
walkDocRefs(doc, (value, refType, rootRelativeBare) => {
|
|
171
|
+
refs.push({ refType, rootRelativeBare, value });
|
|
172
|
+
return null;
|
|
173
|
+
});
|
|
174
|
+
const root = isJsonObject(doc) ? doc : null;
|
|
175
|
+
const rootTag = root !== null && isString(root.tagName) ? root.tagName : null;
|
|
176
|
+
const defines = kind === "component" && rootTag?.includes("-") === true ? rootTag : null;
|
|
177
|
+
const uses = collectTagUses(doc);
|
|
178
|
+
const tags = [...uses.keys()].filter((tag) => tag.includes("-") && tag !== defines).toSorted();
|
|
179
|
+
const { contentTypes, prototypes } = queriesOf(doc);
|
|
180
|
+
return {
|
|
181
|
+
contentTypes,
|
|
182
|
+
defines,
|
|
183
|
+
implicitLayout: kind === "page" && root !== null && root.$layout === undefined,
|
|
184
|
+
prototypes,
|
|
185
|
+
refs,
|
|
186
|
+
tagCounts: Object.fromEntries(uses),
|
|
187
|
+
tags,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ─── The graph ────────────────────────────────────────────────────────────────────────────────
|
|
192
|
+
|
|
193
|
+
/** What the graph reads from the project configuration and the collection loader. */
|
|
194
|
+
export interface DependencyGraphConfig {
|
|
195
|
+
/**
|
|
196
|
+
* The configuration's `defaults.layout` as authored (`./layouts/base.json`), `null` when it names
|
|
197
|
+
* none. The graph resolves it against the project root, as it does a page's `$layout`.
|
|
198
|
+
*/
|
|
199
|
+
defaultLayout?: string | null;
|
|
200
|
+
/**
|
|
201
|
+
* The configuration's `imports` as authored, name to path (`imports.md` §1.1). They cascade into
|
|
202
|
+
* every document, so the file an import names is a `data-consumer` dependency of each document
|
|
203
|
+
* whose `$prototype` uses the name and whose own `imports` do not map it.
|
|
204
|
+
*/
|
|
205
|
+
imports?: Readonly<Record<string, string>>;
|
|
206
|
+
/** Project-relative asset mounts, through which a rooted reference resolves. */
|
|
207
|
+
mounts?: readonly AssetMount[];
|
|
208
|
+
/**
|
|
209
|
+
* Each content type's entry files, project-relative, as the collection loader resolved them. The
|
|
210
|
+
* loader's list rather than the type's `source`, because a `{locale}` source is not a glob and
|
|
211
|
+
* `exclude` and `where` leave files out. Between configuration changes the host keeps it current
|
|
212
|
+
* entry by entry with {@link DependencyGraph.setEntryTypes}.
|
|
213
|
+
*/
|
|
214
|
+
collections?: Readonly<Record<string, readonly string[]>>;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The documents one change invalidates through one edge kind. */
|
|
218
|
+
export interface Invalidation {
|
|
219
|
+
kind: DependencyKind;
|
|
220
|
+
/** Every dependent, transitive, once each, in a stable order; never the cause itself. */
|
|
221
|
+
docIds: DocId[];
|
|
222
|
+
/** The component tags the cause declared before and after the change. */
|
|
223
|
+
tags: string[];
|
|
224
|
+
/** `false` while a document whose references are unknown could be among the dependents (§8.2). */
|
|
225
|
+
complete: boolean;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export interface DependencyGraphOptions {
|
|
229
|
+
/**
|
|
230
|
+
* Paths whose change invalidates every document (`project-config`): the project configuration and
|
|
231
|
+
* the package manifest. Defaults to both spellings of the project file and `package.json`.
|
|
232
|
+
*/
|
|
233
|
+
configPaths?: readonly string[];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface DependencyGraph {
|
|
237
|
+
/**
|
|
238
|
+
* Record a document's references, `null` for a document the host could not parse (unindexed,
|
|
239
|
+
* §8.2), and answer the invalidations its change causes. Call it when a document's bytes change
|
|
240
|
+
* or it is added, including at a path documents already reference.
|
|
241
|
+
*/
|
|
242
|
+
update: (docId: DocId, path: string, refs: DocumentRefs | null) => Invalidation[];
|
|
243
|
+
/** Forget a removed document and answer the invalidations of its now dangling referrers. */
|
|
244
|
+
remove: (docId: DocId) => Invalidation[];
|
|
245
|
+
/** Move a document to `to`, re-resolving its references, and answer both paths' referrers. */
|
|
246
|
+
relabel: (docId: DocId, to: string) => Invalidation[];
|
|
247
|
+
/** Replace what the configuration and the collection loader say; on `projectConfigChanged`. */
|
|
248
|
+
configure: (config: DependencyGraphConfig) => void;
|
|
249
|
+
/**
|
|
250
|
+
* Record the content types whose resolved entry list now includes the file at `path` (`[]` for
|
|
251
|
+
* none), after the collection loader re-resolved them for a file event under a collection's
|
|
252
|
+
* source. Call it BEFORE the `update`, `remove` or `relabel` of that event: a type the entry
|
|
253
|
+
* joined reaches its readers through that change, and a type it left is remembered until then, so
|
|
254
|
+
* the readers of both appear in the same one event per kind (§8) rather than in two.
|
|
255
|
+
*/
|
|
256
|
+
setEntryTypes: (path: string, types: readonly string[]) => void;
|
|
257
|
+
/** The document's dependents by the kind of the first edge that reaches them. */
|
|
258
|
+
dependentsOf: (docId: DocId) => Map<DependencyKind, DocId[]>;
|
|
259
|
+
/** The dependents of whatever is, or will be, at `path`. */
|
|
260
|
+
dependentsAt: (path: string) => Map<DependencyKind, DocId[]>;
|
|
261
|
+
/** The documents recorded with unknown references. */
|
|
262
|
+
unindexed: () => DocId[];
|
|
263
|
+
/**
|
|
264
|
+
* Every document the graph holds, with its current path and the references it was given (`null`
|
|
265
|
+
* for one whose references are unknown, §8.2): what the usage query answers from (§12).
|
|
266
|
+
*/
|
|
267
|
+
documents: () => GraphDocument[];
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** One document as the graph holds it. */
|
|
271
|
+
export interface GraphDocument {
|
|
272
|
+
docId: DocId;
|
|
273
|
+
path: string;
|
|
274
|
+
refs: DocumentRefs | null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** The edge a reference makes, by the key that carried it; `null` for none. */
|
|
278
|
+
function edgeKindOf(refType: string): DependencyKind | null {
|
|
279
|
+
switch (refType) {
|
|
280
|
+
case "$layout": {
|
|
281
|
+
return "layout-page";
|
|
282
|
+
}
|
|
283
|
+
case "$src":
|
|
284
|
+
case "imports": {
|
|
285
|
+
// A page's own class import is read as its `$src` is (`imports.md` §1.1).
|
|
286
|
+
return "data-consumer";
|
|
287
|
+
}
|
|
288
|
+
case "$ref":
|
|
289
|
+
case "$elements": {
|
|
290
|
+
return "component-instance";
|
|
291
|
+
}
|
|
292
|
+
case "source": {
|
|
293
|
+
// A collection's source: its entries reach their readers through the loader's list.
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
default: {
|
|
297
|
+
return "media-referrer";
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const dirOf = (path: string): string => path.slice(0, Math.max(0, path.lastIndexOf("/")));
|
|
303
|
+
|
|
304
|
+
/** A configuration path (`./layouts/base.json`, `/layouts/base.json`) as a project path. */
|
|
305
|
+
function projectPath(value: string): string | null {
|
|
306
|
+
const cls = classifyRef(value);
|
|
307
|
+
if (cls.kind !== "path") {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
const resolved = joinPosix("/", cls.core).slice(1);
|
|
311
|
+
return resolved === "" ? null : resolved;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Every project path a reference in the document at `from` could name. */
|
|
315
|
+
function resolveRef(from: string, ref: ExtractedRef, mounts: readonly AssetMount[]): string[] {
|
|
316
|
+
const cls = classifyRef(ref.value);
|
|
317
|
+
if (cls.kind !== "path") {
|
|
318
|
+
return [];
|
|
319
|
+
}
|
|
320
|
+
if (cls.rooted) {
|
|
321
|
+
return projectPathsForRootedRef(cls.core, mounts);
|
|
322
|
+
}
|
|
323
|
+
const base = ref.rootRelativeBare ? "/" : `/${dirOf(from)}`;
|
|
324
|
+
const resolved = joinPosix(base, cls.core).slice(1);
|
|
325
|
+
return resolved === "" ? [] : [resolved];
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
interface GraphNode {
|
|
329
|
+
path: string;
|
|
330
|
+
refs: DocumentRefs | null;
|
|
331
|
+
/** The resolved outgoing path edges. */
|
|
332
|
+
edges: { kind: DependencyKind; path: string }[];
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const addTo = <K, V>(map: Map<K, Set<V>>, key: K, value: V): void => {
|
|
336
|
+
const set = map.get(key) ?? new Set<V>();
|
|
337
|
+
set.add(value);
|
|
338
|
+
map.set(key, set);
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const deleteFrom = <K, V>(map: Map<K, Set<V>>, key: K, value: V): void => {
|
|
342
|
+
const set = map.get(key);
|
|
343
|
+
set?.delete(value);
|
|
344
|
+
if (set?.size === 0) {
|
|
345
|
+
map.delete(key);
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
/** Create an empty graph. */
|
|
350
|
+
export function createDependencyGraph(options: DependencyGraphOptions = {}): DependencyGraph {
|
|
351
|
+
const configPaths = new Set(options.configPaths ?? [...PROJECT_FILE_NAMES, "package.json"]);
|
|
352
|
+
const nodes = new Map<DocId, GraphNode>();
|
|
353
|
+
/** Target path → the documents referencing it, each with the edge kind it does so by. */
|
|
354
|
+
const referrers = new Map<string, Map<DocId, Set<DependencyKind>>>();
|
|
355
|
+
const tagUsers = new Map<string, Set<DocId>>();
|
|
356
|
+
const typeReaders = new Map<string, Set<DocId>>();
|
|
357
|
+
const prototypeUsers = new Map<string, Set<DocId>>();
|
|
358
|
+
/** Project path → the configuration import names that map to it. */
|
|
359
|
+
let importsAt = new Map<string, Set<string>>();
|
|
360
|
+
/** Entry path → the content types it left since its last change (`setEntryTypes`). */
|
|
361
|
+
const departed = new Map<string, Set<string>>();
|
|
362
|
+
const implicitPages = new Set<DocId>();
|
|
363
|
+
const unknown = new Set<DocId>();
|
|
364
|
+
let defaultLayout: string | null = null;
|
|
365
|
+
let mounts: readonly AssetMount[] = [];
|
|
366
|
+
/** Entry path → the content types that include it. */
|
|
367
|
+
let entryTypes = new Map<string, Set<string>>();
|
|
368
|
+
|
|
369
|
+
const unlink = (docId: DocId, node: GraphNode): void => {
|
|
370
|
+
for (const edge of node.edges) {
|
|
371
|
+
const byDoc = referrers.get(edge.path);
|
|
372
|
+
byDoc?.delete(docId);
|
|
373
|
+
if (byDoc?.size === 0) {
|
|
374
|
+
referrers.delete(edge.path);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
const refs = node.refs ?? NO_REFS;
|
|
378
|
+
for (const tag of refs.tags) {
|
|
379
|
+
deleteFrom(tagUsers, tag, docId);
|
|
380
|
+
}
|
|
381
|
+
for (const type of refs.contentTypes) {
|
|
382
|
+
deleteFrom(typeReaders, type, docId);
|
|
383
|
+
}
|
|
384
|
+
for (const name of refs.prototypes) {
|
|
385
|
+
deleteFrom(prototypeUsers, name, docId);
|
|
386
|
+
}
|
|
387
|
+
implicitPages.delete(docId);
|
|
388
|
+
unknown.delete(docId);
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const link = (docId: DocId, path: string, refs: DocumentRefs | null): void => {
|
|
392
|
+
const edges: GraphNode["edges"] = [];
|
|
393
|
+
/*
|
|
394
|
+
* The configuration renders nothing, and its own references (`defaults.layout`, `imports`,
|
|
395
|
+
* `copy`) reach the graph through `configure`. Recording them as edges would make every layout it names
|
|
396
|
+
* invalidate the configuration, and through it every document.
|
|
397
|
+
*/
|
|
398
|
+
const outgoing = configPaths.has(path) ? [] : (refs?.refs ?? []);
|
|
399
|
+
for (const ref of outgoing) {
|
|
400
|
+
const kind = edgeKindOf(ref.refType);
|
|
401
|
+
if (kind === null) {
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
for (const target of resolveRef(path, ref, mounts)) {
|
|
405
|
+
if (target === path) {
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
edges.push({ kind, path: target });
|
|
409
|
+
const byDoc = referrers.get(target) ?? new Map<DocId, Set<DependencyKind>>();
|
|
410
|
+
addTo(byDoc, docId, kind);
|
|
411
|
+
referrers.set(target, byDoc);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
nodes.set(docId, { edges, path, refs });
|
|
415
|
+
if (refs === null) {
|
|
416
|
+
unknown.add(docId);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
for (const tag of refs.tags) {
|
|
420
|
+
addTo(tagUsers, tag, docId);
|
|
421
|
+
}
|
|
422
|
+
for (const type of refs.contentTypes) {
|
|
423
|
+
addTo(typeReaders, type, docId);
|
|
424
|
+
}
|
|
425
|
+
for (const name of refs.prototypes) {
|
|
426
|
+
addTo(prototypeUsers, name, docId);
|
|
427
|
+
}
|
|
428
|
+
if (refs.implicitLayout) {
|
|
429
|
+
implicitPages.add(docId);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
/** The documents one edge away from `path`, and from the definitions of `tags`. */
|
|
434
|
+
const direct = (path: string, tags: Iterable<string>): Map<DependencyKind, Set<DocId>> => {
|
|
435
|
+
const out = new Map<DependencyKind, Set<DocId>>();
|
|
436
|
+
for (const [docId, kinds] of referrers.get(path) ?? []) {
|
|
437
|
+
for (const kind of kinds) {
|
|
438
|
+
addTo(out, kind, docId);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
for (const tag of tags) {
|
|
442
|
+
for (const docId of tagUsers.get(tag) ?? []) {
|
|
443
|
+
addTo(out, "component-instance", docId);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (defaultLayout !== null && path === defaultLayout) {
|
|
447
|
+
for (const docId of implicitPages) {
|
|
448
|
+
addTo(out, "layout-page", docId);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
for (const type of entryTypes.get(path) ?? []) {
|
|
452
|
+
for (const docId of typeReaders.get(type) ?? []) {
|
|
453
|
+
addTo(out, "content-collection", docId);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
for (const name of importsAt.get(path) ?? []) {
|
|
457
|
+
for (const docId of prototypeUsers.get(name) ?? []) {
|
|
458
|
+
addTo(out, "data-consumer", docId);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (configPaths.has(path)) {
|
|
462
|
+
for (const docId of nodes.keys()) {
|
|
463
|
+
addTo(out, "project-config", docId);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return out;
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
/** The tags a document declares, for the dependents of its definition. */
|
|
470
|
+
const definedBy = (docId: DocId): string[] => {
|
|
471
|
+
const defines = nodes.get(docId)?.refs?.defines;
|
|
472
|
+
return defines === null || defines === undefined ? [] : [defines];
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* The dependents of `paths` and `tags` by first-hop kind, each group closed transitively over
|
|
477
|
+
* every kind and never containing `cause`.
|
|
478
|
+
*/
|
|
479
|
+
const reach = (
|
|
480
|
+
paths: readonly string[],
|
|
481
|
+
tags: readonly string[],
|
|
482
|
+
cause: DocId | null,
|
|
483
|
+
first = new Map<DependencyKind, Set<DocId>>(),
|
|
484
|
+
): Map<DependencyKind, DocId[]> => {
|
|
485
|
+
for (const path of paths) {
|
|
486
|
+
for (const [kind, ids] of direct(path, tags)) {
|
|
487
|
+
for (const id of ids) {
|
|
488
|
+
addTo(first, kind, id);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
const out = new Map<DependencyKind, DocId[]>();
|
|
493
|
+
for (const kind of DEPENDENCY_KIND_ORDER) {
|
|
494
|
+
const seen = new Set(first.get(kind));
|
|
495
|
+
if (cause !== null) {
|
|
496
|
+
seen.delete(cause);
|
|
497
|
+
}
|
|
498
|
+
const queue = [...seen];
|
|
499
|
+
while (queue.length > 0) {
|
|
500
|
+
const id = queue.pop()!;
|
|
501
|
+
const node = nodes.get(id)!;
|
|
502
|
+
for (const next of direct(node.path, definedBy(id)).values()) {
|
|
503
|
+
for (const dependent of next) {
|
|
504
|
+
if (dependent !== cause && !seen.has(dependent)) {
|
|
505
|
+
seen.add(dependent);
|
|
506
|
+
queue.push(dependent);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (seen.size > 0) {
|
|
512
|
+
out.set(kind, [...seen].toSorted());
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return out;
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
const invalidate = (
|
|
519
|
+
cause: DocId,
|
|
520
|
+
paths: readonly string[],
|
|
521
|
+
tags: readonly string[],
|
|
522
|
+
): Invalidation[] => {
|
|
523
|
+
const tagList = [...new Set(tags)].toSorted();
|
|
524
|
+
const complete = [...unknown].every((id) => id === cause);
|
|
525
|
+
// The readers of a collection the entry left still listed it, so they are invalidated too.
|
|
526
|
+
const left = new Map<DependencyKind, Set<DocId>>();
|
|
527
|
+
for (const path of paths) {
|
|
528
|
+
for (const type of departed.get(path) ?? []) {
|
|
529
|
+
for (const docId of typeReaders.get(type) ?? []) {
|
|
530
|
+
addTo(left, "content-collection", docId);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
departed.delete(path);
|
|
534
|
+
}
|
|
535
|
+
return [...reach([...new Set(paths)], tagList, cause, left)].map(([kind, docIds]) => ({
|
|
536
|
+
complete,
|
|
537
|
+
docIds,
|
|
538
|
+
kind,
|
|
539
|
+
tags: tagList,
|
|
540
|
+
}));
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
const rebuild = (): void => {
|
|
544
|
+
const all = [...nodes];
|
|
545
|
+
for (const [docId, node] of all) {
|
|
546
|
+
unlink(docId, node);
|
|
547
|
+
}
|
|
548
|
+
for (const [docId, node] of all) {
|
|
549
|
+
link(docId, node.path, node.refs);
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
return {
|
|
554
|
+
configure(config) {
|
|
555
|
+
defaultLayout = isString(config.defaultLayout) ? projectPath(config.defaultLayout) : null;
|
|
556
|
+
mounts = config.mounts ?? [];
|
|
557
|
+
importsAt = new Map();
|
|
558
|
+
for (const [name, value] of Object.entries(config.imports ?? {})) {
|
|
559
|
+
const path = projectPath(value);
|
|
560
|
+
if (path !== null) {
|
|
561
|
+
addTo(importsAt, path, name);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
// A configuration change invalidates every document, so no departure is still owed.
|
|
565
|
+
departed.clear();
|
|
566
|
+
entryTypes = new Map();
|
|
567
|
+
for (const [type, entries] of Object.entries(config.collections ?? {})) {
|
|
568
|
+
for (const entry of entries) {
|
|
569
|
+
addTo(entryTypes, entry, type);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
rebuild();
|
|
573
|
+
},
|
|
574
|
+
dependentsAt: (path) => reach([path], [], null),
|
|
575
|
+
dependentsOf(docId) {
|
|
576
|
+
const node = nodes.get(docId);
|
|
577
|
+
return node === undefined ? new Map() : reach([node.path], definedBy(docId), docId);
|
|
578
|
+
},
|
|
579
|
+
setEntryTypes(path, types) {
|
|
580
|
+
const next = new Set(types);
|
|
581
|
+
for (const type of entryTypes.get(path) ?? []) {
|
|
582
|
+
if (!next.has(type)) {
|
|
583
|
+
addTo(departed, path, type);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
for (const type of next) {
|
|
587
|
+
deleteFrom(departed, path, type);
|
|
588
|
+
}
|
|
589
|
+
if (next.size === 0) {
|
|
590
|
+
entryTypes.delete(path);
|
|
591
|
+
} else {
|
|
592
|
+
entryTypes.set(path, next);
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
relabel(docId, to) {
|
|
596
|
+
const node = nodes.get(docId);
|
|
597
|
+
if (node === undefined) {
|
|
598
|
+
return [];
|
|
599
|
+
}
|
|
600
|
+
unlink(docId, node);
|
|
601
|
+
link(docId, to, node.refs);
|
|
602
|
+
return invalidate(docId, [node.path, to], definedBy(docId));
|
|
603
|
+
},
|
|
604
|
+
remove(docId) {
|
|
605
|
+
const node = nodes.get(docId);
|
|
606
|
+
if (node === undefined) {
|
|
607
|
+
return [];
|
|
608
|
+
}
|
|
609
|
+
const tags = definedBy(docId);
|
|
610
|
+
unlink(docId, node);
|
|
611
|
+
nodes.delete(docId);
|
|
612
|
+
return invalidate(docId, [node.path], tags);
|
|
613
|
+
},
|
|
614
|
+
unindexed: () => [...unknown].toSorted(),
|
|
615
|
+
documents: () =>
|
|
616
|
+
[...nodes].map(([docId, node]) => ({ docId, path: node.path, refs: node.refs })),
|
|
617
|
+
update(docId, path, refs) {
|
|
618
|
+
const before = nodes.get(docId);
|
|
619
|
+
const tags = definedBy(docId);
|
|
620
|
+
if (before !== undefined) {
|
|
621
|
+
unlink(docId, before);
|
|
622
|
+
}
|
|
623
|
+
link(docId, path, refs);
|
|
624
|
+
const paths = before === undefined ? [path] : [before.path, path];
|
|
625
|
+
return invalidate(docId, paths, [...tags, ...definedBy(docId)]);
|
|
626
|
+
},
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// ─── Events ───────────────────────────────────────────────────────────────────────────────────
|
|
631
|
+
|
|
632
|
+
type DependentsInvalidatedInit = Extract<ProjectEventInit, { type: "dependentsInvalidated" }>;
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* One `dependentsInvalidated` per kind that reaches a dependent (§8), for the host to append after
|
|
636
|
+
* the cause's file event and its `docChanged`.
|
|
637
|
+
*/
|
|
638
|
+
export function invalidationEvents(
|
|
639
|
+
cause: DocId,
|
|
640
|
+
invalidations: readonly Invalidation[],
|
|
641
|
+
origin: EventOrigin,
|
|
642
|
+
): DependentsInvalidatedInit[] {
|
|
643
|
+
return invalidations.map((invalidation) => ({
|
|
644
|
+
cause: { docId: cause, kind: invalidation.kind },
|
|
645
|
+
complete: invalidation.complete,
|
|
646
|
+
docIds: [...invalidation.docIds],
|
|
647
|
+
origin,
|
|
648
|
+
tags: [...invalidation.tags],
|
|
649
|
+
type: "dependentsInvalidated",
|
|
650
|
+
}));
|
|
651
|
+
}
|