Skip to content

feat: fine-grained readonly markers #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions workspace/extension/src/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ function clone(value, seen = new Map()) {
/** @type {Record<string, any>} */
const o = {};
seen.set(value, o);

const descriptors = Object.getOwnPropertyDescriptors(value);
for (const [key, v] of Object.entries(value)) {
o[key] = clone(v, seen);
const { get, writable } = descriptors[key];
const readonly = !writable || get !== undefined;
o[key] = { key, value: clone(v, seen), readonly };
}
return o;
}
Expand All @@ -37,13 +41,13 @@ export function serialize(node) {
switch (node.type) {
case 'component': {
const { $$: internal = {} } = node.detail;
const ctx = clone(node.detail.$capture_state?.() || {});
const captured = node.detail.$capture_state?.() || {};
const bindings = Object.values(internal.bound || {}).map(
/** @param {Function} f */ (f) => f.name,
);
const props = Object.keys(internal.props || {}).flatMap((key) => {
const value = ctx[key];
delete ctx[key]; // deduplicate for ctx
const value = clone(captured[key]);
delete captured[key]; // deduplicate for ctx
if (value === undefined) return [];

const bounded = bindings.some((f) => f.includes(key));
Expand All @@ -55,7 +59,7 @@ export function serialize(node) {
listeners: Object.entries(internal.callbacks || {}).flatMap(([event, value]) =>
value.map(/** @param {Function} f */ (f) => ({ event, handler: f.toString() })),
),
ctx: Object.entries(ctx).map(([key, value]) => ({ key, value })),
ctx: Object.entries(captured).map(([key, v]) => ({ key, value: clone(v) })),
};
break;
}
Expand Down
6 changes: 1 addition & 5 deletions workspace/extension/src/lib/panel/PropertyList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@
<span class="object">Object &lbrace;&hellip;&rbrace;</span>

{#if expanded[key]}
{@const entries = Object.entries(value).map(([key, v]) => {
return { key, value: v, readonly };
})}

<PropertyList {entries} {keys} />
<PropertyList entries={Object.values(value)} {keys} />
{/if}
{:else}
<span class="object">Object &lbrace; &rbrace;</span>
Expand Down