From aa1523bb49c6b583c0bc042121bdd6f57216cf89 Mon Sep 17 00:00:00 2001 From: kaisergeX Date: Mon, 28 Oct 2024 12:26:03 +0700 Subject: [PATCH] fix: untrack - infer return type --- .changeset/soft-rats-love.md | 5 +++++ README.md | 2 +- packages/signal/README.md | 2 +- packages/signal/jsr.json | 2 +- packages/signal/src/types.ts | 1 + packages/signal/src/utils.ts | 17 ++++++++++------- 6 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 .changeset/soft-rats-love.md diff --git a/.changeset/soft-rats-love.md b/.changeset/soft-rats-love.md new file mode 100644 index 0000000..d7033f0 --- /dev/null +++ b/.changeset/soft-rats-love.md @@ -0,0 +1,5 @@ +--- +'@kaiverse/signal': patch +--- + +fix `unTrack` infer return type diff --git a/README.md b/README.md index a9a18ab..f602fe3 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@

Signal Proxy

-

A simple reactive system for your Javascript application.
Zero dependency, TypeScript fully supported.

+

A lightweight, simple reactive system for your Javascript application.
Zero dependencies, TypeScript fully supported.

This package draws strong inspiration from [KnockoutJS](https://github.com/knockout/knockout)'s concepts and [SolidJS](https://github.com/solidjs)'s Signal, enabling us to use Signals in vanilla JavaScript. [JS Signals proposal](https://github.com/tc39/proposal-signals) is currently in Stage 1. diff --git a/packages/signal/README.md b/packages/signal/README.md index c5415ab..8ab5039 100644 --- a/packages/signal/README.md +++ b/packages/signal/README.md @@ -3,7 +3,7 @@

Signal Proxy

-

A simple reactive system for your Javascript application.
Zero dependency, TypeScript fully supported.

+

A lightweight, simple reactive system for your Javascript application.
Zero dependencies, TypeScript fully supported.

This package draws strong inspiration from [KnockoutJS](https://github.com/knockout/knockout)'s concepts and [SolidJS](https://github.com/solidjs)'s Signal, enabling us to use Signals in vanilla JavaScript. [JS Signals proposal](https://github.com/tc39/proposal-signals) is currently in Stage 1. diff --git a/packages/signal/jsr.json b/packages/signal/jsr.json index 6fa5887..6c7a1c5 100644 --- a/packages/signal/jsr.json +++ b/packages/signal/jsr.json @@ -1,6 +1,6 @@ { "name": "@kaiverse/signal", - "version": "0.2.2", + "version": "0.2.3", "exports": "./src/index.ts", "publish": { "exclude": [ diff --git a/packages/signal/src/types.ts b/packages/signal/src/types.ts index a736dbf..bb891ff 100644 --- a/packages/signal/src/types.ts +++ b/packages/signal/src/types.ts @@ -40,3 +40,4 @@ export type SignalFactoryReturnType = Readonly<[get: Signal, set: SignalSe export type SignalEffect = () => void; export type EffectTracking = {execute: SignalEffect; deps: Set>}; export type CleanupEffectFn = () => void; +export type SignalUntrackFn = () => T; diff --git a/packages/signal/src/utils.ts b/packages/signal/src/utils.ts index 30e91ae..e06e553 100644 --- a/packages/signal/src/utils.ts +++ b/packages/signal/src/utils.ts @@ -9,21 +9,23 @@ import type { SignalOptions, SignalSetter, CleanupEffectFn, + SignalUntrackFn, } from './types'; let effectTrackingCache: EffectTracking | null = null; /** - * Ignores tracking any of the dependencies in the `untrackEffectCb` and returns its value + * Ignores tracking any of the dependencies inside the `untrackFn` scope. * - * @param untrackEffectCb + * @param untrackFn the executing code block. + * @returns the return value of `untrackFn`. */ -export function unTrack(untrackEffectCb: SignalEffect): void { +export function unTrack(untrackFn: SignalUntrackFn): T { const prevEffectTracking = effectTrackingCache; effectTrackingCache = null; - const untrackEffectExecute = untrackEffectCb(); + const untrackReturnValue = untrackFn(); effectTrackingCache = prevEffectTracking; - return untrackEffectExecute; + return untrackReturnValue; } /** @@ -84,7 +86,7 @@ export function createSignal( } /** - * @param effectCb Imperative function that will run whenever dependencies change. Dependencies are Signals that are used inside the Effect itself + * @param effect Imperative function that will run whenever dependencies change. Dependencies are Signals that are used inside the Effect itself. * @returns a cleanup function. It will stop related Effect. */ export function createEffect(effect: SignalEffect): CleanupEffectFn { @@ -98,6 +100,7 @@ export function createEffect(effect: SignalEffect): CleanupEffectFn { deps: new Set(), }; + // const weakEffectDetailRef = new WeakRef(effectDetail); effectDetail.execute(); // if (import.meta.hot) { @@ -110,7 +113,7 @@ export function createEffect(effect: SignalEffect): CleanupEffectFn { } /** - * `createComputed` creates a readonly reactive value equal to the return value of the given function and this function only gets executed when its dependencies change. + * Creates a readonly reactive value equal to the return value of the given function and this function only gets executed when its dependencies change. * * Dependencies are all Signals that are used inside the Computed function. *