Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Machy8 committed Nov 17, 2023
1 parent 3e49070 commit 77429e5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 46 deletions.
23 changes: 6 additions & 17 deletions packages/signalizejs/src/plugins/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,16 @@ export default (pluginOptions?: PluginOptions): SignalizePlugin => {
const canCompile = mode === 'init';

const elementClosestScope = element.closest(`[${$.scopeAttribute}]`);
let scopeInitPromise = null;

if (elementClosestScope && typeof elementClosestScope[$.scopeKey] === 'undefined') {
scopeInitPromise = new Promise((resolve) => {
on('scope:inited', ({ detail }) => {
if (detail.element === elementClosestScope) {
resolve(true);
}
})
})
}

if (scopeInitPromise instanceof Promise) {
await scopeInitPromise;
return element;
}

let elementScope;
let elementScope = scope(element);

/* if (mode === 'reinit') {
if (mode === 'reinit') {
elementScope.cleanup();
} */
}

const compiledDirectives = elementScope?.directives ?? new Map();

Expand All @@ -121,13 +110,14 @@ export default (pluginOptions?: PluginOptions): SignalizePlugin => {
if (attribute.name in processedAttributes) {
continue;
}

const matcherReturn = matcher({ element, attribute });

if (matcherReturn === undefined) {
continue;
}

const matches = matcherReturn.exec(attribute.name);
const matches = new RegExp(`^${matcherReturn.source}$`).exec(attribute.name);
if (matches === null) {
continue;
}
Expand Down Expand Up @@ -187,7 +177,6 @@ export default (pluginOptions?: PluginOptions): SignalizePlugin => {
const processDirectives = async (options?: ProcessDirectiveOptions = {}): Promise<Element | Document | DocumentFragment> => {
let { root = $.root, directiveName, mode = 'init' } = options;
const directivesToProcess = directiveName === undefined ? Object.keys(directives) : [directiveName];

await $.traverseDom({
root,
nodeTypes: [1],
Expand Down
44 changes: 17 additions & 27 deletions packages/signalizejs/src/plugins/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,39 +192,29 @@ export default ($: Signalize): void => {
on('dom:ready', async (event) => {
await traverseDomTree($.root);

on('scope:defined', () => {
let selector = `${$.scopeAttribute}`
if (event !== undefined && event.detail?.name === undefined) {
selector += `="${event.detail.name}"`;
}
selector = `[${selector}]`;

for (const element of $.selectAll(selector)) {
if (element.parentNode.closest(selecto)) {
continue;
}
on('scope:defined', (event) => {
let selector = `[${$.scopeAttribute}="${event.detail.name}"]`;
for (const element of $.selectAll(selector)) {
if (element.parentNode.closest(selector)) {
continue;
}
traverseDomTree(element);
}
});

on('dom:mutation:node:removed', (event) => {
scope(event.detail)?.cleanup();
});

traverseDomTree(element);
on('dom:mutation:node:added' as keyof CustomEventListener, $.root, ({ detail }: { detail: Node }): void => {
if (!(detail instanceof Element) || scope(detail) !== undefined) {
return;
}
void traverseDomTree(detail);
});
});
});

on('dom:mutation:node:removed', (event) => {
/* if () {
return;
} */

scope(event.detail)?.cleanup();
});

on('dom:mutation:node:added' as keyof CustomEventListener, $.root, ({ detail }: { detail: Node }): void => {
if (!(detail instanceof Element) || scope(detail) !== undefined) {
return;
}
void traverseDomTree(detail);
});

$.Scope = ElementScope;
$.scope = scope;
}
1 change: 1 addition & 0 deletions packages/signalizejs/src/plugins/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default ($: Signalize): void => {
}

this.value = newValue;
console.trace(newValue);

clearTimeout(this.#setWatchersTimeout);
this.#setWatchersTimeout = setTimeout(() => {
Expand Down
9 changes: 7 additions & 2 deletions packages/signalizejs/src/plugins/traverse-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ export interface TraverseDomOptions {
export default ($: Signalize): void => {
$.traverseDom = async <T>(options: TraverseDomOptions): Promise<T> => {
const { root, callback, nodeTypes = [] } = options
const canProcess = (node) => nodeTypes.includes(node.nodeType) || nodeTypes.length === 0;
const processNode = async (node: Node): Promise<void> => {
if (nodeTypes.includes(node.nodeType) || nodeTypes.length === 0) {
node = node instanceof Document ? node.documentElement : node;
if (canProcess(node)) {
await callback(node);
}

for (const child of [...node.childNodes]) {
const childPromises = [];
for (const child of node.childNodes) {
await processNode(child);
}

await Promise.all(childPromises);
}

await processNode(root);
Expand Down

0 comments on commit 77429e5

Please sign in to comment.