Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Machy8 committed Oct 27, 2023
1 parent 5117c88 commit b0f7553
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 65 deletions.
44 changes: 11 additions & 33 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@
}
</style>

<div $text="" $count="5" cloak>
<!-- <div $text="" $count="1000" cloak>
<input :value="text">
<button @click="count.set(count() + 1)">Přidej<span :text="count"></span></button>
<button @click="count.set(count() - 1)">Odeber<span :text="count"></span></button><br>
<ul>
<template :for="i of count()">
<div>
<li :kei="i">
<template :if="i % 2 !== 0">
<span :text="'Odd' + i + ' - ' + text"></span>
</template>
</div>
<div>
<template :if="i % 2 === 0">
<span :text="'Even' + i + ' - ' + text" :style="text().length > 2 ? 'color:red' : 'color:blue'"></span>
</template>
</div>
</li>
</template>
</ul>
</div>
</div> -->

<!-- <form $items="{}" $text="" @submit="
event.preventDefault();
Expand All @@ -54,12 +52,12 @@
items.set(newItems);
"
>
<span :text="item"></span> - X
<span :text="item"></span> - X
</a>
</li>
</template>
</ul>
</form> -->
</form> -->

<!-- <template :for="[name, surname] of Object.entries({name: 'vladimir', surname: 'Macháček'})">
<li :text="name + ' ' + surname"></li>
Expand All @@ -79,16 +77,15 @@
</template>
</div> -->

<!-- <br><br>
<div $items="[]">
<!-- <div $items="[]">
<button @click="items.set([items().length, ...items()])" :text="'+' + items().length"></button>
<button @click="items.set(items().slice(1))" :text="'-' + items().length"></button>
<template :for="i of items()">
<div $text="i"><input :value="text"><span :text="text"></span></div>
<div $text="i" :key="i"><input :value="text"><span :text="text"></span></div>
</template>
</div>
-->
</div> -->

<!-- <template :for="i in 1100">
<div :text="i"></div>
</template> -->
Expand Down Expand Up @@ -140,32 +137,13 @@
</div>
-->

<!-- <div $renderForm>
<template :if="renderForm">
<div>Text</div>
</template>
<template :if="!renderForm">
</template>
</div>
-->

<!-- <div $count="1">
<button @click="count.set(count() + 1)">+</button>
<button @click="count.set(count() - 1)">-</button>
<template template="text" :if="count() > 0">
Ahoj
</template> -->
<!-- template -->
<!-- Ahoj -->
<!-- /template -->
<!-- </div> -->

<script type="module">
import Signalize from 'signalizejs';
import Directives from 'signalizejs/directives';

const { use } = new Signalize();

use(Directives);
</script>

Expand Down
5 changes: 3 additions & 2 deletions packages/signalizejs/ajax/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export interface AjaxOptions extends RequestInit {
}

export default (signalize: Signalize): void => {
const { dispatch } = signalize;
const { dispatch, config } = signalize;
const requestedWithHeader = config.ajaxRequestedWithHeader ?? 'XMLHttpRequest';

signalize.ajax = async (options: AjaxOptions): Promise<AjaxReturn> => {
let response: Response | null = null;
Expand All @@ -50,7 +51,7 @@ export default (signalize: Signalize): void => {
const url = options.url;

const requestInit = { ...options };
requestInit.headers = { ...{ 'X-Requested-With': 'XMLHttpRequest' }, ...requestInit.headers ?? {} }
requestInit.headers = { ...{ 'X-Requested-With': requestedWithHeader }, ...requestInit.headers ?? {} }

delete requestInit.url;

Expand Down
50 changes: 24 additions & 26 deletions packages/signalizejs/directives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,22 +507,10 @@ export default (signalize: Signalize): void => {

let nextSibling = element.nextSibling;
const nextSiblingScope = scope(nextSibling);
const rendered = signal(nextSiblingScope?.template === element)
let rendered = nextSiblingScope?.template === element;
const renderedElements = [];

const bindElementToRenderedSignal = (root: ChildNode) => {
scope(root, (rootScope) => {
rootScope.template = element;

const unwatch = rendered.watch(({ newValue }) => {
if (newValue === false) {
unwatch();
root.remove();
}
})
});
}

if (rendered() === false) {
if (rendered === false) {
let renderedTemplateSibling = element.nextSibling;
let renderedTemplateOpenned = false;

Expand All @@ -534,36 +522,43 @@ export default (signalize: Signalize): void => {
if (renderedTemplateSibling.nodeType === Node.COMMENT_NODE) {
const content = renderedTemplateSibling.textContent?.trim();
if (content === renderedTemplateStartComment) {
rendered.set(true);
bindElementToRenderedSignal(renderedTemplateSibling)
rendered = true;
renderedElements.push(renderedTemplateSibling);
renderedTemplateOpenned = true;
renderedTemplateSibling = renderedTemplateSibling.nextSibling;
continue;
} else if (content === renderedTemplateEndComment) {
bindElementToRenderedSignal(renderedTemplateSibling)
renderedElements.push(renderedTemplateSibling);
renderedTemplateOpenned = false;
break;
}
}

if (renderedTemplateOpenned) {
bindElementToRenderedSignal(renderedTemplateSibling);
renderedElements.push(renderedTemplateSibling);
}

renderedTemplateSibling = renderedTemplateSibling.nextSibling;
}
}

const cleanElements = () => {
while (renderedElements.length) {
renderedElements.pop().remove()
}
}

const render = async (): Promise<void> => {
const conditionResult = await fn(data());
let lastInsertPoint = element;

if (conditionResult === true && rendered() === true) {
if (conditionResult === true && rendered === true) {
return;
}

if (conditionResult !== true) {
rendered.set(false);
cleanElements();
rendered = false;
return;
}

Expand All @@ -577,24 +572,27 @@ export default (signalize: Signalize): void => {

fragment = await processDirectives({ root: fragment });
const children = [...fragment.childNodes];

while (children.length > 0) {
const root = children.shift();
bindElementToRenderedSignal(root)
renderedElements.push(root);
lastInsertPoint.after(root);
lastInsertPoint = root;
}
rendered.set(true);

rendered = true;
}

await render();

const unwatchSignalCallbacks = [];

for (const signalToWatch of signalsToWatch) {
unwatchSignalCallbacks.push(signalToWatch.watch(render));
while (signalsToWatch.length) {
unwatchSignalCallbacks.push(signalsToWatch.pop().watch(render));
}

scope(element).cleanup(() => {
cleanElements();
for (const unwatch of unwatchSignalCallbacks) {
unwatch();
}
Expand All @@ -621,7 +619,7 @@ export default (signalize: Signalize): void => {
context: currentData,
element
})
const signalsToWatch = [];
let signalsToWatch = [];

for (const signal of Object.values(currentData)) {
if (!(signal instanceof Signal)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/signalizejs/src/plugins/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export default (signalize: Signalize): void => {
if (attributeBinderIsSignal === true || signalsToWatch.length === 1) {
getListener = () => attributeBinder();
setListener = (value) => signalsToWatch[0].set(value);
} else if (typeof attributeBinder === 'function') {
getListener = () => attributeBinder();
} else {
getListener = typeof attributeBinder.get === 'function' ? () => attributeBinder.get() : null;
setListener = typeof attributeBinder.set === 'function' ? (value) => attributeBinder.set(value) : null
Expand Down
3 changes: 2 additions & 1 deletion packages/signalizejs/src/plugins/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ interface Options {
}

export default (signalize: Signalize): void => {
const { config } = signalize;
signalize.dispatch = (eventName: string, eventData: any = undefined, options: Options = {}): boolean => {
return (options?.target ?? document).dispatchEvent(
return (options?.target ?? config.root).dispatchEvent(
new window.CustomEvent(eventName, {
detail: eventData,
cancelable: options?.cancelable ?? true,
Expand Down
2 changes: 1 addition & 1 deletion packages/signalizejs/src/plugins/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default (signalize: Signalize): void => {

if (Array.isArray(value)) {
mergedObject[key] = [...mergedObject[key], ...value];
} else if (value !== null && valueType === 'object') {
} else if (value !== null && !(value instanceof Element) && !(value instanceof Document) && !(value instanceof DocumentFragment) && valueType === 'object') {
mergedObject[key] = merge(mergedObject[key], value as T);
} else {
mergedObject[key] = value;
Expand Down
1 change: 0 additions & 1 deletion packages/signalizejs/src/plugins/on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export interface CustomEventListeners extends HTMLElementEventMap {

export default (signalize: Signalize): void => {
const { config, selectorToIterable } = signalize;
const domMutationRemoveListeners = new Set();

const customEventListeners: Record<string, CustomEventListener> = {
clickOutside: (target: HTMLElement | string, listener: CallableFunction, options: AddEventListenerOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/signalizejs/src/plugins/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Signal<T = any> extends Function {
this.value = newValue;

for (const watcher of this.watchers.afterSet) {
setTimeout(() => watcher({ newValue, oldValue }), 0)
watcher({ newValue, oldValue });
}
}

Expand Down

0 comments on commit b0f7553

Please sign in to comment.