diff --git a/src/runtime/index.ts b/src/runtime/index.ts index 002e8b8c..94f4cf18 100644 --- a/src/runtime/index.ts +++ b/src/runtime/index.ts @@ -39,7 +39,7 @@ export { Component } from "./component"; export type { ComponentConstructor } from "./component"; export { useComponent, useState } from "./component_node"; export { status } from "./status"; -export { reactive, markRaw, toRaw } from "./reactivity"; +export { reactive, markRaw, toRaw, effect } from "./reactivity"; export { useEffect, useEnv, useExternalListener, useRef, useChildSubEnv, useSubEnv } from "./hooks"; export { batched, EventBus, htmlEscape, whenReady, loadFile, markup } from "./utils"; export { diff --git a/src/runtime/reactivity.ts b/src/runtime/reactivity.ts index f3ac48df..7e793391 100644 --- a/src/runtime/reactivity.ts +++ b/src/runtime/reactivity.ts @@ -131,6 +131,11 @@ function processSignals() { [...scheduledSignals.values()].map((s) => [...s.executionContexts]).flat() ); + // schedule before context.update in case there is write operations during update + // todo: add a test in case there is write operations during update the test + // will break is scheduledSignals.clear(); is called after context.update(); + // that writes + scheduledSignals.clear(); for (const ctx of [...scheduledContexts]) { removeSignalsFromContext(ctx); // custom unsubscribe depending on the context. @@ -146,7 +151,6 @@ function processSignals() { popExecutionContext(); } } - scheduledSignals.clear(); } /** @@ -263,7 +267,11 @@ function unsubscribeChildEffect( executionContext.meta.children.length = 0; } export function effect(fn: Function) { - const parent = getExecutionContext(); + let parent = getExecutionContext(); + // todo: is it useful? + if (parent && !parent?.meta.children) { + parent = undefined!; + } const executionContext: ExecutionContext = { unsubcribe: (scheduledContexts: Set) => { unsubscribeChildEffect(executionContext, scheduledContexts); @@ -277,12 +285,13 @@ export function effect(fn: Function) { }, signals: new Set(), meta: { - parent: getExecutionContext(), + parent: parent, children: [], }, }; if (parent) { - parent.meta.children.push(executionContext); + // todo: is it useful? + parent.meta.children?.push?.(executionContext); } pushExecutionContext(executionContext); try {