Skip to content

Commit 1054a5c

Browse files
authored
chore: generate uuid for nodes (#191)
1 parent 41d229a commit 1054a5c

File tree

5 files changed

+56
-59
lines changed

5 files changed

+56
-59
lines changed

src/app.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ declare global {
3434
};
3535

3636
type SvelteBlockDetail = {
37-
id: number;
37+
id: string; // crypto.randomUUID();
3838
source: string;
3939
type:
4040
| 'anchor'

src/client/index.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { highlight } from './highlight.js';
22
import { addListener } from './listener.js';
33
// import { profiler } from './profiler.js';
4-
import { getNode } from './svelte.js';
5-
6-
// @ts-ignore - possibly find an alternative
7-
window.__svelte_devtools_inject_state = function (id, key, value) {
8-
const { detail: component } = getNode(id) || {};
9-
component && component.$inject_state({ [key]: value });
10-
};
11-
12-
// @ts-ignore - possibly find an alternative
13-
window.__svelte_devtools_select_element = function (element) {
14-
const node = getNode(element);
15-
if (node) send('inspect', { node: serialize(node) });
4+
import { nodes } from './svelte.js';
5+
6+
// @ts-ignore - for the app to call with `eval`
7+
window['#SvelteDevTools'] = {
8+
/**
9+
* @param {string} id
10+
* @param {string} key
11+
* @param {any} value
12+
*/
13+
inject(id, key, value) {
14+
const { detail: component } = nodes.map.get(id) || {};
15+
component && component.$inject_state({ [key]: value });
16+
},
1617
};
1718

1819
const previous = {
@@ -56,7 +57,7 @@ const inspect = {
5657
click(event) {
5758
event.preventDefault();
5859
document.removeEventListener('mousemove', inspect.handle, true);
59-
const node = getNode(/** @type {Node} */ (event.target));
60+
const node = nodes.map.get(/** @type {Node} */ (event.target));
6061
if (node) send('bridge::ext/inspect', { node: serialize(node) });
6162
previous.clear();
6263
},
@@ -67,11 +68,11 @@ window.addEventListener('message', ({ data, source }) => {
6768
if (source !== window || data?.source !== 'svelte-devtools') return;
6869

6970
if (data.type === 'bridge::ext/select') {
70-
const node = getNode(data.payload);
71+
const node = nodes.map.get(data.payload);
7172
// @ts-expect-error - saved for `devtools.inspect()`
7273
if (node) window.$n = node.detail;
7374
} else if (data.type === 'bridge::ext/highlight') {
74-
const node = getNode(data.payload);
75+
const node = nodes.map.get(data.payload);
7576
return highlight(node);
7677
} else if (data.type === 'bridge::ext/inspect') {
7778
switch (data.payload) {
@@ -202,8 +203,8 @@ addListener({
202203
add(node, anchor) {
203204
send('bridge::courier/node->add', {
204205
node: serialize(node),
205-
target: node.parent?.id ?? null,
206-
anchor: anchor?.id ?? null,
206+
target: node.parent?.id,
207+
anchor: anchor?.id,
207208
});
208209
},
209210

src/client/svelte.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@ import { listeners } from './listener.js';
33

44
/** @type {undefined | SvelteBlockDetail} */
55
let current_block;
6-
let pointer = 0;
76

8-
/** @param {number | Node} id */
9-
export function getNode(id) {
10-
return nodes.map.get(id);
11-
}
12-
13-
const nodes = {
14-
/** @type {SvelteBlockDetail[]} */
15-
root: [],
16-
17-
/** @type {Map<any, SvelteBlockDetail>} */
7+
export const nodes = {
8+
/** @type {Map<string | object | Node, SvelteBlockDetail>} */
189
map: new Map(),
1910

2011
/** @param {{ node: SvelteBlockDetail; target?: Node; anchor?: Node }} opts */
@@ -28,13 +19,11 @@ const nodes = {
2819
}
2920
node.parent = target;
3021

31-
const sibling = this.map.get(anchor);
22+
const sibling = anchor && this.map.get(anchor);
3223
if (target) {
3324
const index = target.children.findIndex((n) => n === sibling);
3425
if (index === -1) target.children.push(node);
3526
else target.children.splice(index, 0, node);
36-
} else {
37-
this.root.push(node);
3827
}
3928

4029
listeners.add(node, sibling);
@@ -81,7 +70,7 @@ document.addEventListener('SvelteRegisterComponent', ({ detail }) => {
8170
let last_promise;
8271
document.addEventListener('SvelteRegisterBlock', ({ detail }) => {
8372
const { type, id, block, ...rest } = detail;
84-
const current_node_id = pointer++;
73+
const current_node_id = crypto.randomUUID();
8574

8675
if (block.m) {
8776
const original = block.m;
@@ -134,7 +123,7 @@ document.addEventListener('SvelteRegisterBlock', ({ detail }) => {
134123
// @ts-expect-error - each block fallback
135124
group = /** @type {SvelteBlockDetail} */ ({
136125
version: '',
137-
id: pointer++,
126+
id: crypto.randomUUID(),
138127
type: 'block',
139128
tagName: 'each',
140129
container: parent,
@@ -214,7 +203,7 @@ document.addEventListener('SvelteDOMInsert', ({ detail }) => {
214203
target,
215204
// @ts-expect-error - missing properties are irrelevant
216205
node: {
217-
id: pointer++,
206+
id: crypto.randomUUID(),
218207
type,
219208
detail: element,
220209
tagName: element.nodeName.toLowerCase(),

src/lib/panel/PropertyList.svelte

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
import Expandable from './Expandable.svelte';
33
44
export let entries: Array<{ key: string; value: any }> = [];
5-
export let id: number;
5+
export let id: string;
66
export let readonly = false;
77
88
const errors: Record<string, string | undefined> = {};
99
function change(key: string, value: any) {
1010
chrome.devtools.inspectedWindow.eval(
11-
`__svelte_devtools_inject_state(${id}, '${key}', ${value})`,
11+
`window['#SvelteDevTools'].inject("${id}", "${key}", ${value})`,
1212
(_, error) => {
13-
errors[key] =
14-
error && error.isException
15-
? error.value.substring(0, error.value.indexOf('\n'))
16-
: undefined;
13+
errors[key] = error?.isException
14+
? error.value.slice(0, error.value.indexOf('\n'))
15+
: undefined;
1716
},
1817
);
1918
}

src/lib/runtime.ts

+26-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const background = {
1111
},
1212
};
1313

14-
const nodes = new Map<null | number, DebugNode>();
14+
const nodes = {} as { [key: string]: DebugNode };
1515

1616
function resolveEventBubble(node: any) {
1717
if (!node.detail || !node.detail.listeners) return;
@@ -42,58 +42,66 @@ port.onMessage.addListener(({ type, payload }) => {
4242
case 'bridge::ext/clear': {
4343
selected.set(undefined);
4444
hovered.set(undefined);
45-
return root.set([]);
45+
root.set([]);
46+
break;
4647
}
4748

4849
case 'bridge::ext/inspect': {
49-
if (typeof payload === 'string') return;
50-
const current = nodes.get(payload.node.id);
51-
return selected.set(current);
50+
if (typeof payload === 'string') break;
51+
const current = nodes[payload.node.id];
52+
selected.set(current);
53+
break;
5254
}
5355

5456
case 'bridge::courier/node->add': {
5557
const { node, target, anchor } = payload as {
5658
node: DebugNode;
57-
target: null | number;
58-
anchor: null | number;
59+
target: string;
60+
anchor: string;
5961
};
6062

6163
node.children = [];
6264
node.expanded = false;
6365
node.invalidate = () => {};
6466
resolveEventBubble(node);
6567

66-
const parent = nodes.get(target);
67-
nodes.set(node.id, node);
68-
if (!parent) return root.update((n) => [...n, node]);
68+
const parent = nodes[target];
69+
nodes[node.id] = node;
70+
if (!parent) {
71+
root.update((n) => [...n, node]);
72+
break;
73+
}
6974

7075
const index = parent.children.findIndex((n) => n.id === anchor);
7176
if (index === -1) parent.children.push(node);
7277
else parent.children.splice(index, 0, node);
7378

74-
return (node.parent = parent).invalidate();
79+
(node.parent = parent).invalidate();
80+
break;
7581
}
7682

7783
case 'bridge::courier/node->remove': {
7884
const node = payload.node as SvelteBlockDetail;
79-
const current = nodes.get(node.id);
80-
if (current) nodes.delete(current.id);
81-
if (!current?.parent) return;
85+
const current = nodes[node.id];
86+
if (current) delete nodes[current.id];
87+
if (!current?.parent) break;
8288

8389
const index = current.parent.children.findIndex((o) => o.id === current.id);
8490
current.parent.children.splice(index, 1);
85-
return current.parent.invalidate();
91+
current.parent.invalidate();
92+
break;
8693
}
8794

8895
case 'bridge::courier/node->update': {
8996
const node = payload.node as SvelteBlockDetail;
90-
const current = nodes.get(node.id);
91-
if (!current) return;
97+
const current = nodes[node.id];
98+
if (!current) break;
9299
Object.assign(current, payload.node);
93100
resolveEventBubble(current);
94101

95102
selected.update((o) => o);
96-
return current.invalidate();
103+
current.invalidate();
104+
break;
97105
}
98106

99107
// case 'bridge::courier/profile->update': {

0 commit comments

Comments
 (0)