ui-router-devtools 0.0.1-alpha.0 → 0.1.0
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/LICENSE +9 -0
- package/README.md +161 -0
- package/dist/console.d.ts +5 -0
- package/dist/console.d.ts.map +1 -0
- package/dist/console.js +47 -0
- package/dist/console.js.map +1 -0
- package/dist/define.d.ts +8 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +4 -0
- package/dist/define.js.map +1 -0
- package/dist/edges.d.ts +25 -0
- package/dist/edges.d.ts.map +1 -0
- package/dist/edges.js +28 -0
- package/dist/edges.js.map +1 -0
- package/dist/element/chips.d.ts +17 -0
- package/dist/element/chips.d.ts.map +1 -0
- package/dist/element/chips.js +134 -0
- package/dist/element/chips.js.map +1 -0
- package/dist/element/glyphs.d.ts +34 -0
- package/dist/element/glyphs.d.ts.map +1 -0
- package/dist/element/glyphs.js +107 -0
- package/dist/element/glyphs.js.map +1 -0
- package/dist/element/inspector.d.ts +7 -0
- package/dist/element/inspector.d.ts.map +1 -0
- package/dist/element/inspector.js +183 -0
- package/dist/element/inspector.js.map +1 -0
- package/dist/element/links.d.ts +4 -0
- package/dist/element/links.d.ts.map +1 -0
- package/dist/element/links.js +87 -0
- package/dist/element/links.js.map +1 -0
- package/dist/element/log.d.ts +12 -0
- package/dist/element/log.d.ts.map +1 -0
- package/dist/element/log.js +272 -0
- package/dist/element/log.js.map +1 -0
- package/dist/element/map.d.ts +5 -0
- package/dist/element/map.d.ts.map +1 -0
- package/dist/element/map.js +5 -0
- package/dist/element/map.js.map +1 -0
- package/dist/element/mapview.d.ts +17 -0
- package/dist/element/mapview.d.ts.map +1 -0
- package/dist/element/mapview.js +158 -0
- package/dist/element/mapview.js.map +1 -0
- package/dist/element/measure.d.ts +3 -0
- package/dist/element/measure.d.ts.map +1 -0
- package/dist/element/measure.js +9 -0
- package/dist/element/measure.js.map +1 -0
- package/dist/element/nested.d.ts +45 -0
- package/dist/element/nested.d.ts.map +1 -0
- package/dist/element/nested.js +686 -0
- package/dist/element/nested.js.map +1 -0
- package/dist/element/pack.d.ts +75 -0
- package/dist/element/pack.d.ts.map +1 -0
- package/dist/element/pack.js +281 -0
- package/dist/element/pack.js.map +1 -0
- package/dist/element/panel.d.ts +159 -0
- package/dist/element/panel.d.ts.map +1 -0
- package/dist/element/panel.js +1542 -0
- package/dist/element/panel.js.map +1 -0
- package/dist/element/popover.d.ts +12 -0
- package/dist/element/popover.d.ts.map +1 -0
- package/dist/element/popover.js +38 -0
- package/dist/element/popover.js.map +1 -0
- package/dist/element/route.d.ts +75 -0
- package/dist/element/route.d.ts.map +1 -0
- package/dist/element/route.js +883 -0
- package/dist/element/route.js.map +1 -0
- package/dist/element/scrubber.d.ts +4 -0
- package/dist/element/scrubber.d.ts.map +1 -0
- package/dist/element/scrubber.js +67 -0
- package/dist/element/scrubber.js.map +1 -0
- package/dist/element/styles.d.ts +3 -0
- package/dist/element/styles.d.ts.map +1 -0
- package/dist/element/styles.js +2084 -0
- package/dist/element/styles.js.map +1 -0
- package/dist/element/timeline-styles.d.ts +3 -0
- package/dist/element/timeline-styles.d.ts.map +1 -0
- package/dist/element/timeline-styles.js +286 -0
- package/dist/element/timeline-styles.js.map +1 -0
- package/dist/element/timeline.d.ts +9 -0
- package/dist/element/timeline.d.ts.map +1 -0
- package/dist/element/timeline.js +225 -0
- package/dist/element/timeline.js.map +1 -0
- package/dist/element/tree.d.ts +24 -0
- package/dist/element/tree.d.ts.map +1 -0
- package/dist/element/tree.js +149 -0
- package/dist/element/tree.js.map +1 -0
- package/dist/element/viewmap-styles.d.ts +3 -0
- package/dist/element/viewmap-styles.d.ts.map +1 -0
- package/dist/element/viewmap-styles.js +282 -0
- package/dist/element/viewmap-styles.js.map +1 -0
- package/dist/element/viewmap.d.ts +39 -0
- package/dist/element/viewmap.d.ts.map +1 -0
- package/dist/element/viewmap.js +406 -0
- package/dist/element/viewmap.js.map +1 -0
- package/dist/element/viewport.d.ts +31 -0
- package/dist/element/viewport.d.ts.map +1 -0
- package/dist/element/viewport.js +37 -0
- package/dist/element/viewport.js.map +1 -0
- package/dist/element/waterfall.d.ts +85 -0
- package/dist/element/waterfall.d.ts.map +1 -0
- package/dist/element/waterfall.js +284 -0
- package/dist/element/waterfall.js.map +1 -0
- package/dist/fold.d.ts +27 -0
- package/dist/fold.d.ts.map +1 -0
- package/dist/fold.js +104 -0
- package/dist/fold.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/lit-ui-router.d.ts +24 -0
- package/dist/lit-ui-router.d.ts.map +1 -0
- package/dist/lit-ui-router.js +102 -0
- package/dist/lit-ui-router.js.map +1 -0
- package/dist/notation.d.ts +28 -0
- package/dist/notation.d.ts.map +1 -0
- package/dist/notation.js +105 -0
- package/dist/notation.js.map +1 -0
- package/dist/performance.d.ts +5 -0
- package/dist/performance.d.ts.map +1 -0
- package/dist/performance.js +66 -0
- package/dist/performance.js.map +1 -0
- package/dist/plugin.d.ts +32 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +21 -0
- package/dist/plugin.js.map +1 -0
- package/dist/recorder.d.ts +30 -0
- package/dist/recorder.d.ts.map +1 -0
- package/dist/recorder.js +117 -0
- package/dist/recorder.js.map +1 -0
- package/dist/schema.d.ts +2 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +2 -0
- package/dist/schema.js.map +1 -0
- package/dist/session-schema.d.ts +31 -0
- package/dist/session-schema.d.ts.map +1 -0
- package/dist/session-schema.js +314 -0
- package/dist/session-schema.js.map +1 -0
- package/dist/session.schema.json +1172 -0
- package/dist/shared/event-schema.d.ts +225 -0
- package/dist/shared/event-schema.d.ts.map +1 -0
- package/dist/shared/event-schema.js +2 -0
- package/dist/shared/event-schema.js.map +1 -0
- package/dist/shared/redaction.d.ts +19 -0
- package/dist/shared/redaction.d.ts.map +1 -0
- package/dist/shared/redaction.js +56 -0
- package/dist/shared/redaction.js.map +1 -0
- package/dist/shared/resolve-timing.d.ts +6 -0
- package/dist/shared/resolve-timing.d.ts.map +1 -0
- package/dist/shared/resolve-timing.js +61 -0
- package/dist/shared/resolve-timing.js.map +1 -0
- package/dist/shared/router-events.d.ts +10 -0
- package/dist/shared/router-events.d.ts.map +1 -0
- package/dist/shared/router-events.js +296 -0
- package/dist/shared/router-events.js.map +1 -0
- package/dist/shared/thrown.d.ts +5 -0
- package/dist/shared/thrown.d.ts.map +1 -0
- package/dist/shared/thrown.js +12 -0
- package/dist/shared/thrown.js.map +1 -0
- package/dist/shared/transition-source.d.ts +4 -0
- package/dist/shared/transition-source.d.ts.map +1 -0
- package/dist/shared/transition-source.js +2 -0
- package/dist/shared/transition-source.js.map +1 -0
- package/dist/shared/views.d.ts +10 -0
- package/dist/shared/views.d.ts.map +1 -0
- package/dist/shared/views.js +67 -0
- package/dist/shared/views.js.map +1 -0
- package/dist/spans.d.ts +15 -0
- package/dist/spans.d.ts.map +1 -0
- package/dist/spans.js +24 -0
- package/dist/spans.js.map +1 -0
- package/dist/testing.d.ts +32 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +28 -0
- package/dist/testing.js.map +1 -0
- package/dist/ui-router-devtools.d.ts +36 -0
- package/dist/ui-router-devtools.d.ts.map +1 -0
- package/dist/ui-router-devtools.js +3 -0
- package/dist/ui-router-devtools.js.map +1 -0
- package/dist/views.d.ts +44 -0
- package/dist/views.d.ts.map +1 -0
- package/dist/views.js +43 -0
- package/dist/views.js.map +1 -0
- package/package.json +112 -2
- package/src/console.ts +104 -0
- package/src/define.ts +12 -0
- package/src/edges.ts +64 -0
- package/src/element/chips.ts +187 -0
- package/src/element/glyphs.ts +218 -0
- package/src/element/inspector.ts +315 -0
- package/src/element/links.ts +136 -0
- package/src/element/log.ts +410 -0
- package/src/element/map.ts +22 -0
- package/src/element/mapview.ts +224 -0
- package/src/element/measure.ts +13 -0
- package/src/element/nested.ts +1124 -0
- package/src/element/pack.ts +526 -0
- package/src/element/panel.ts +2378 -0
- package/src/element/popover.ts +64 -0
- package/src/element/route.ts +1481 -0
- package/src/element/scrubber.ts +103 -0
- package/src/element/styles.ts +2084 -0
- package/src/element/timeline-styles.ts +287 -0
- package/src/element/timeline.ts +392 -0
- package/src/element/tree.ts +271 -0
- package/src/element/viewmap-styles.ts +283 -0
- package/src/element/viewmap.ts +733 -0
- package/src/element/viewport.ts +112 -0
- package/src/element/waterfall.ts +585 -0
- package/src/fold.ts +151 -0
- package/src/index.ts +40 -0
- package/src/lit-ui-router.ts +194 -0
- package/src/notation.ts +181 -0
- package/src/performance.ts +142 -0
- package/src/plugin.ts +57 -0
- package/src/recorder.ts +228 -0
- package/src/schema.ts +32 -0
- package/src/session-schema.ts +362 -0
- package/src/spans.ts +53 -0
- package/src/testing.ts +74 -0
- package/src/types/import-meta-env.d.ts +9 -0
- package/src/ui-router-devtools.ts +39 -0
- package/src/views.ts +116 -0
package/src/spans.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Tx } from './fold.ts';
|
|
2
|
+
import type { SpanRecord } from './schema.ts';
|
|
3
|
+
|
|
4
|
+
/** How many spans the panel keeps; past it the oldest go first. */
|
|
5
|
+
export const SPAN_LIMIT = 2000;
|
|
6
|
+
|
|
7
|
+
/** Adds `span` to `spans` in place, replacing the record with its `spanId`, so a started span is replaced when it ends. */
|
|
8
|
+
export const upsertSpan = (
|
|
9
|
+
spans: Map<string, SpanRecord>,
|
|
10
|
+
span: SpanRecord,
|
|
11
|
+
limit: number = SPAN_LIMIT,
|
|
12
|
+
): void => {
|
|
13
|
+
spans.delete(span.spanId);
|
|
14
|
+
spans.set(span.spanId, span);
|
|
15
|
+
|
|
16
|
+
for (const key of spans.keys()) {
|
|
17
|
+
if (spans.size <= limit) break;
|
|
18
|
+
spans.delete(key);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export interface SpanWindow {
|
|
23
|
+
start: number;
|
|
24
|
+
end: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Where a span sits on the event clock, from its epoch times; a running span ends at `clock`. */
|
|
28
|
+
export const spanWindow = (
|
|
29
|
+
span: SpanRecord,
|
|
30
|
+
timeOrigin: number,
|
|
31
|
+
clock: number,
|
|
32
|
+
): SpanWindow => ({
|
|
33
|
+
start: span.startTime - timeOrigin,
|
|
34
|
+
end: span.endTime === undefined ? clock : span.endTime - timeOrigin,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/** The spans that overlap any transition's run, which is what a session file keeps of them. */
|
|
38
|
+
export const spansDuring = (
|
|
39
|
+
spans: readonly SpanRecord[],
|
|
40
|
+
txs: readonly Tx[],
|
|
41
|
+
timeOrigin: number,
|
|
42
|
+
clock: number,
|
|
43
|
+
): SpanRecord[] => {
|
|
44
|
+
const runs = txs.flatMap((tx) =>
|
|
45
|
+
tx.id < 0 ? [] : [{ start: tx.t, end: tx.result?.t ?? clock }],
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return spans.filter((span) => {
|
|
49
|
+
const { start, end } = spanWindow(span, timeOrigin, clock);
|
|
50
|
+
|
|
51
|
+
return runs.some((run) => start <= run.end && end >= run.start);
|
|
52
|
+
});
|
|
53
|
+
};
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test helpers: a snapshot-stable list of the recorded transitions, and a promise that waits for them to settle.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { fold } from './fold.ts';
|
|
7
|
+
import type { Recorder } from './recorder.ts';
|
|
8
|
+
import type { Outcome, Params, Session } from './schema.ts';
|
|
9
|
+
|
|
10
|
+
/** A recorded transition without its times, from {@link transitions}. */
|
|
11
|
+
export interface NormalisedTransition {
|
|
12
|
+
/** Null for a target that names no state, which core never makes a Transition. */
|
|
13
|
+
id: number | null;
|
|
14
|
+
/** The state the transition left; `''` is the root. */
|
|
15
|
+
from: string;
|
|
16
|
+
/** The state the transition targets. */
|
|
17
|
+
to: string;
|
|
18
|
+
/** How the transition settled, or `running` if it has not. */
|
|
19
|
+
outcome: Outcome | 'running';
|
|
20
|
+
/** The transition that took over: a redirect's successor, or the one that superseded this one. */
|
|
21
|
+
by: number | null;
|
|
22
|
+
/** The result's message, such as a rejection's; null when it has none. */
|
|
23
|
+
message: string | null;
|
|
24
|
+
/** The transition this one redirected from; null when it is not a redirect. */
|
|
25
|
+
redirectedFrom: number | null;
|
|
26
|
+
/** The states the transition enters. */
|
|
27
|
+
entering: string[];
|
|
28
|
+
/** The states the transition exits. */
|
|
29
|
+
exiting: string[];
|
|
30
|
+
/** The states the transition keeps. */
|
|
31
|
+
retained: string[];
|
|
32
|
+
/** The target's params. */
|
|
33
|
+
params: Params;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Transitions in start order with times and durations removed, so the list is snapshot-stable; a recorder's params keep their values. */
|
|
37
|
+
export const transitions = (
|
|
38
|
+
source: Session | Recorder,
|
|
39
|
+
): NormalisedTransition[] => {
|
|
40
|
+
const events =
|
|
41
|
+
'session' in source
|
|
42
|
+
? source.session({ params: 'all' }).events
|
|
43
|
+
: source.events;
|
|
44
|
+
|
|
45
|
+
return fold(events).txs.map((tx) => ({
|
|
46
|
+
id: tx.id < 0 ? null : tx.id,
|
|
47
|
+
from: tx.from,
|
|
48
|
+
to: tx.to,
|
|
49
|
+
outcome: tx.result?.outcome ?? 'running',
|
|
50
|
+
by: tx.result?.by ?? null,
|
|
51
|
+
message: tx.result?.message ?? null,
|
|
52
|
+
redirectedFrom: tx.redirectedFrom,
|
|
53
|
+
entering: tx.entering,
|
|
54
|
+
exiting: tx.exiting,
|
|
55
|
+
retained: tx.retained,
|
|
56
|
+
params: tx.toParams,
|
|
57
|
+
}));
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const running = (recorder: Recorder): number =>
|
|
61
|
+
fold(recorder.events).txs.filter((tx) => !tx.result).length;
|
|
62
|
+
|
|
63
|
+
/** Resolves once no recorded transition is running; a redirect's successor starts before its origin settles. */
|
|
64
|
+
export const settled = (recorder: Recorder): Promise<void> =>
|
|
65
|
+
new Promise((resolve) => {
|
|
66
|
+
const check = () => {
|
|
67
|
+
if (running(recorder)) return;
|
|
68
|
+
off();
|
|
69
|
+
resolve();
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const off = recorder.subscribe(() => queueMicrotask(check));
|
|
73
|
+
queueMicrotask(check);
|
|
74
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Not `vite/client` (TS 6 `types` defaults to `[]`); never published, since `define` folds every `import.meta.env` read out of dist/.
|
|
2
|
+
interface ImportMetaEnv {
|
|
3
|
+
/** This package's manifest version, defined by oxc-emit-js, by vitest's `test.env` and by the demo's vite config. */
|
|
4
|
+
readonly PACKAGE_VERSION: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface ImportMeta {
|
|
8
|
+
readonly env: ImportMetaEnv;
|
|
9
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { LitElement } from 'lit';
|
|
2
|
+
import type { UIRouter } from '@uirouter/core';
|
|
3
|
+
|
|
4
|
+
import { DevtoolsPanel } from './element/panel.ts';
|
|
5
|
+
import type { Recorder } from './recorder.ts';
|
|
6
|
+
import type { Redaction, Session, SpanRecord } from './schema.ts';
|
|
7
|
+
import type { ViewLocator } from './views.ts';
|
|
8
|
+
|
|
9
|
+
/** <code><ui-router-devtools></code>: a pill that opens a panel of the router's states, transitions and config. */
|
|
10
|
+
export interface UIRouterDevtools extends LitElement {
|
|
11
|
+
/** The router to record. */
|
|
12
|
+
router: UIRouter | undefined;
|
|
13
|
+
/** How many transitions the log keeps; the attribute is `history`. */
|
|
14
|
+
history: number;
|
|
15
|
+
/** Time resolves by wrapping their `resolveFn`; read when the router attaches. The attribute is `time-resolves`. */
|
|
16
|
+
timeResolves: boolean;
|
|
17
|
+
/** What Export scrubs unless the panel's toggles say otherwise: param keys without their values and messages kept by default. */
|
|
18
|
+
redaction: Redaction;
|
|
19
|
+
/** The recorder behind the panel, for reporters that should share it. */
|
|
20
|
+
readonly recorder: Recorder | undefined;
|
|
21
|
+
/** The session the panel shows, live or imported, scrubbed by `redaction`, which defaults to the element's. */
|
|
22
|
+
session(redaction?: Partial<Redaction>): Session | undefined;
|
|
23
|
+
/** Shows `session` instead of the live router until the panel's Live button is pressed. */
|
|
24
|
+
importSession(session: Session): void;
|
|
25
|
+
/** Spans from a tracer, which the Timeline joins to transitions; setting it replaces them all. It holds the latest 2000. */
|
|
26
|
+
spans: readonly SpanRecord[];
|
|
27
|
+
/** Adds a span, or replaces the one with its `spanId`: a span can be sent when it starts, without `endTime`, and again when it ends. */
|
|
28
|
+
addSpan(span: SpanRecord): void;
|
|
29
|
+
/** Finds a view's element on the page for the Views, such as `litViewLocator` from `ui-router-devtools/lit-ui-router`; without one the Views draw the router's data only. */
|
|
30
|
+
viewLocator: ViewLocator | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The `<ui-router-devtools>` class; `ui-router-devtools/define` registers it. */
|
|
34
|
+
export const UIRouterDevtools: {
|
|
35
|
+
/** Creates an element; `document.createElement('ui-router-devtools')` does too once it is registered. */
|
|
36
|
+
new (): UIRouterDevtools;
|
|
37
|
+
/** The prototype every `<ui-router-devtools>` shares. */
|
|
38
|
+
readonly prototype: UIRouterDevtools;
|
|
39
|
+
} = DevtoolsPanel;
|
package/src/views.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { ViewFill, ViewInfo } from './schema.ts';
|
|
2
|
+
|
|
3
|
+
/** A `ui-view` in the nesting `fqn` describes, with the views registered inside it. */
|
|
4
|
+
export interface ViewNode {
|
|
5
|
+
/** The view as the snapshot records it. */
|
|
6
|
+
view: ViewInfo;
|
|
7
|
+
/** The enclosing view's id; null for a view at the top. */
|
|
8
|
+
parent: number | null;
|
|
9
|
+
/** From 1 at the top. */
|
|
10
|
+
depth: number;
|
|
11
|
+
/** The views registered inside this one. */
|
|
12
|
+
children: ViewNode[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* What a renderer adds to the panel's Views: where a view is on the page. The panel calls it when one is set, and
|
|
17
|
+
* only for the live router, never for an imported session.
|
|
18
|
+
*/
|
|
19
|
+
export interface ViewLocator {
|
|
20
|
+
/** Marks the view's place on the page; null takes the mark away. */
|
|
21
|
+
highlight(view: ViewInfo | null): void;
|
|
22
|
+
/** Where the page shows the view, in words; null when the page has no element for it. */
|
|
23
|
+
describe?(view: ViewInfo): string | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const parentFqn = (fqn: string): string | null => {
|
|
27
|
+
const dot = fqn.lastIndexOf('.');
|
|
28
|
+
|
|
29
|
+
return dot === -1 ? null : fqn.slice(0, dot);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Two views can share an fqn; the one filled by the state a view was created in is its parent.
|
|
33
|
+
const parentOf = (
|
|
34
|
+
view: ViewInfo,
|
|
35
|
+
byFqn: ReadonlyMap<string, readonly ViewInfo[]>,
|
|
36
|
+
): ViewInfo | undefined => {
|
|
37
|
+
const fqn = parentFqn(view.fqn);
|
|
38
|
+
const candidates = fqn === null ? [] : (byFqn.get(fqn) ?? []);
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
candidates.find((candidate) => candidate.fill?.state === view.context) ??
|
|
42
|
+
candidates[0]
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Nests views by their `fqn`: a view sits inside the view named by its `fqn` without the last segment. A view whose
|
|
48
|
+
* enclosing view is not registered is drawn at the top. Siblings keep the order they are given in.
|
|
49
|
+
*/
|
|
50
|
+
export const viewTree = (views: readonly ViewInfo[]): ViewNode[] => {
|
|
51
|
+
const byFqn = new Map<string, ViewInfo[]>();
|
|
52
|
+
|
|
53
|
+
for (const view of views)
|
|
54
|
+
byFqn.set(view.fqn, [...(byFqn.get(view.fqn) ?? []), view]);
|
|
55
|
+
|
|
56
|
+
const parents = new Map(
|
|
57
|
+
views.map((view) => [view.id, parentOf(view, byFqn)?.id ?? null]),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const build = (parent: number | null, depth: number): ViewNode[] =>
|
|
61
|
+
views.flatMap((view) =>
|
|
62
|
+
parents.get(view.id) === parent
|
|
63
|
+
? [{ view, parent, depth, children: build(view.id, depth + 1) }]
|
|
64
|
+
: [],
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// A cycle cannot form: a parent's fqn is always shorter than its child's.
|
|
68
|
+
return build(null, 1);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** A view config's target as a state's `views` can write it, `name@anchor`; the root's anchor is empty. */
|
|
72
|
+
export const targetText = ({ target }: ViewFill): string =>
|
|
73
|
+
`${target.name}@${target.anchor}`;
|
|
74
|
+
|
|
75
|
+
/** The views a state fills, inside the views that hold them, and how many other views the snapshot has. */
|
|
76
|
+
export interface ViewPath {
|
|
77
|
+
/** The state the views are filled by: the one asked for, or the nearest of its ancestors that fills one. */
|
|
78
|
+
state: string;
|
|
79
|
+
/** The view tree pruned to the state's views and the views that hold them, in snapshot order. */
|
|
80
|
+
nodes: ViewNode[];
|
|
81
|
+
/** Views on no branch of `nodes`. */
|
|
82
|
+
others: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const prune = (nodes: readonly ViewNode[], state: string): ViewNode[] =>
|
|
86
|
+
nodes.flatMap((node) => {
|
|
87
|
+
const children = prune(node.children, state);
|
|
88
|
+
|
|
89
|
+
return node.view.fill?.state === state || children.length
|
|
90
|
+
? [{ ...node, children }]
|
|
91
|
+
: [];
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const count = (nodes: readonly ViewNode[]): number =>
|
|
95
|
+
nodes.reduce((total, node) => total + 1 + count(node.children), 0);
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The view path of `state`: each view it fills and the views it sits inside. A state that fills none gives the path
|
|
99
|
+
* of the first of `ancestors`, nearest first, that does; null when none of them fills a view.
|
|
100
|
+
*/
|
|
101
|
+
export const viewPath = (
|
|
102
|
+
views: readonly ViewInfo[],
|
|
103
|
+
state: string,
|
|
104
|
+
ancestors: readonly string[] = [],
|
|
105
|
+
): ViewPath | null => {
|
|
106
|
+
const tree = viewTree(views);
|
|
107
|
+
|
|
108
|
+
for (const name of [state, ...ancestors]) {
|
|
109
|
+
const nodes = prune(tree, name);
|
|
110
|
+
|
|
111
|
+
if (nodes.length)
|
|
112
|
+
return { state: name, nodes, others: views.length - count(nodes) };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return null;
|
|
116
|
+
};
|