Skip to content

Commit

Permalink
fix: lifecycle events and slots changes detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ncpa0cpl committed Oct 12, 2023
1 parent a2caaef commit 17b9fab
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 106 deletions.
4 changes: 3 additions & 1 deletion scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const p = (...fpath) => path.resolve(__dirname, "..", ...fpath);

const isDev = process.argv.includes("--dev");
const watch = process.argv.includes("--watch");
const noSourceMap = process.argv.includes("--no-source-map");

async function main() {
/**
Expand All @@ -18,11 +19,12 @@ async function main() {
tsconfig: p("tsconfig.json"),
entryPoints: [p("src/index.ts")],
outdir: p("dist"),
external: ["jsxte"],
bundle: true,
keepNames: true,
treeShaking: !isDev,
minify: !isDev,
sourcemap: isDev ? "inline" : false,
sourcemap: noSourceMap ? false : isDev ? "inline" : false,
};

if (watch) {
Expand Down
25 changes: 19 additions & 6 deletions src/element/decorator-slotted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type SlotChanges = {
added: WcSlot[];
removed: WcSlot[];
updated: WcSlot[];
changed: WcSlot[];
};

export type OnSlotChangeCallback = (changes: SlotChanges) => void;
Expand Down Expand Up @@ -109,17 +110,29 @@ export function Slotted(opts: SlottedOptions = {}) {
this.lifecycle.dispatchEvent(
new ElementSlotDidChangeEvent(context.name as string),
);
} else if (changes.changed.length > 0) {
accessor.set.call(this, [...accessor.get.call(this)]);
this.requestUpdate();
this.lifecycle.dispatchEvent(
new ElementSlotDidChangeEvent(context.name as string),
);
}
});

this.lifecycle.once(ElementLifecycleEvent.DidMount, () => {
const initValues = Array.from(this.children).filter(
(elem): elem is S => {
return WcSlot.isSlot(elem) && matches(elem as S);
},
);
const children = Array.from(this.children);

const initValue: S[] = [];
for (let i = 0; i < children.length; i++) {
const elem = children[i]!;

if (WcSlot.isSlot(elem) && matches(elem as S)) {
initValue.push(elem as S);
this.connectToWcSlot(elem);
}
}

accessor.set.call(this, initValues);
accessor.set.call(this, initValue);

this.requestUpdate();
this.lifecycle.dispatchEvent(
Expand Down
Loading

0 comments on commit 17b9fab

Please sign in to comment.