Skip to content

Commit

Permalink
Tunning
Browse files Browse the repository at this point in the history
  • Loading branch information
Machy8 committed Nov 6, 2023
1 parent 8b3952e commit ea772dc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
55 changes: 27 additions & 28 deletions packages/signalizejs/directives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Signalize, Scope } from 'signalizejs'
declare module '..' {
interface Signalize {
directive: (name: string, data: Directive) => void
createDirectiveFunction: (options: CreateFunctionOptions) => () => Promise<any>
AsyncFunction: () => Promise<any>
}

Expand Down Expand Up @@ -113,10 +114,10 @@ export default ($: Signalize): void => {
const directive = directives[directiveName];
const elementScope = scope(element, (elementScope) => {
if (!('directives' in elementScope)) {
elementScope.directives = [];
elementScope.directives = new Set();
}

elementScope.directives.push(directiveName);
elementScope.directives.add(directiveName);
});

countdown --;
Expand Down Expand Up @@ -262,20 +263,26 @@ export default ($: Signalize): void => {
const newContextVariables: string[] = argumentsMatch[1].replace(/[[({})\]\s]/g, '').split(',');

let unwatchSignalCallbacks = [];
const signalsToWatch = [];

const process = async (): Promise<void> => {
const signalsToWatch = [];
for (const signal of Object.values(data)) {
if (typeof signal !== 'function') {
continue;
}

for (const signal of Object.values(data)) {
if (typeof signal !== 'function') {
continue;
}
const unwatch = signal.watch(() => {
signalsToWatch.push(signal);
unwatch();
}, { execution: 'onGet' })
}

const unwatch = signal.watch(() => {
signalsToWatch.push(signal);
unwatch();
}, { execution: 'onGet' })
}
const stackFn = createFunction({
functionString: `return typeof ${argumentsMatch[3]} === 'function' ? ${argumentsMatch[3]}() : ${argumentsMatch[3]};`,
context: data,
element
});

const process = async (): Promise<void> => {
const directivesProcessingPromises = [];
const processScope = async (scopeToProcess) => {
const templateFragment = element.cloneNode(true).content;
Expand All @@ -289,11 +296,6 @@ export default ($: Signalize): void => {
}
}

const stackFn = createFunction({
functionString: `return typeof ${argumentsMatch[3]} === 'function' ? ${argumentsMatch[3]}() : ${argumentsMatch[3]};`,
context: data,
element
});
let stack = await stackFn(data);

if (typeof stack === 'number') {
Expand Down Expand Up @@ -364,7 +366,7 @@ export default ($: Signalize): void => {
if (elementScope !== undefined && elementScope?.directives !== undefined) {
elementScope.cleanup();
const directivesQueue = elementScope.directives;
elementScope.directives = [];
elementScope.directives.clear();
processElement(element, directivesQueue);
}

Expand Down Expand Up @@ -461,19 +463,15 @@ export default ($: Signalize): void => {
nextElementSibling = nextElementToRemove;
removeId++;
}

for (const unwatch of unwatchSignalCallbacks) {
unwatch();
}

unwatchSignalCallbacks = [];
for (const signalToWatch of signalsToWatch) {
unwatchSignalCallbacks.push(signalToWatch.watch(process))
}
}

await process();

unwatchSignalCallbacks = [];
for (const signalToWatch of signalsToWatch) {
unwatchSignalCallbacks.push(signalToWatch.watch(process))
}

scope(element).cleanup(() => {
for (const unwatch of unwatchSignalCallbacks) {
unwatch();
Expand Down Expand Up @@ -695,6 +693,7 @@ export default ($: Signalize): void => {
})

$.AsyncFunction = AsyncFunction;
$.createDirectiveFunction = createFunction;
$.directive = directive;
})
}
10 changes: 4 additions & 6 deletions packages/signalizejs/src/plugins/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare module '..' {
}

export default ($: Signalize): void => {
const { scope, Signal, on, off, task } = $;
const { scope, Signal, on, off } = $;

const reactiveInputAttributes = ['value', 'checked'];
const numericInputAttributes = ['range', 'number'];
Expand Down Expand Up @@ -88,7 +88,7 @@ export default ($: Signalize): void => {
}

if (attributeBinderIsSignal === true) {
signalsToWatch.push(attributeBinder);
//signalsToWatch.push(attributeBinder);
}

if (attributeBinderIsSignal === true || signalsToWatch.length === 1) {
Expand Down Expand Up @@ -119,10 +119,8 @@ export default ($: Signalize): void => {
};

on('input', element, () => {
task(() => {
inputListener()
});
});
inputListener()
}, { passive: true });

if (elementScope instanceof Scope) {
elementScope.cleanup(() => {
Expand Down
45 changes: 25 additions & 20 deletions packages/signalizejs/src/plugins/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class Signal<T = any> extends Function {
onGet: new Set()
};

setTimeout = undefined;

constructor (defaultValue: T) {
super()
this.value = defaultValue;
Expand All @@ -51,32 +53,35 @@ export class Signal<T = any> extends Function {
}

set = (newValue: T): void => {
const oldValue = this.value;
clearTimeout(this.setTimeout);
this.setTimeout = setTimeout(() => {
const oldValue = this.value;

if (['string', 'number'].includes(typeof newValue) && newValue === oldValue) {
return;
}
if (['string', 'number'].includes(typeof newValue) && newValue === oldValue) {
return;
}

let settable = true;
for (const watcher of this.watchers.beforeSet) {
const watcherData = watcher({ newValue, oldValue });
if (typeof watcherData !== 'undefined') {
settable = watcherData.settable ?? settable;
newValue = watcherData.value;
let settable = true;
for (const watcher of this.watchers.beforeSet) {
const watcherData = watcher({ newValue, oldValue });
if (typeof watcherData !== 'undefined') {
settable = watcherData.settable ?? settable;
newValue = watcherData.value;
}
if (!settable) {
break;
}
}

if (!settable) {
break;
return;
}
}

if (!settable) {
return;
}

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

watch = (listener: BeforeSetSignalWatcher<T> | AfterSetSignalWatcher<T>, options: SignalWatcherOptions = {}): Unwatch => {
Expand Down

0 comments on commit ea772dc

Please sign in to comment.