Skip to content

Commit

Permalink
fix: untrack - infer return type
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisergeX committed Oct 28, 2024
1 parent 6f5b1a1 commit aa1523b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-rats-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kaiverse/signal': patch
---

fix `unTrack` infer return type
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div align="center">
<h1>Signal Proxy</h1>

<p>A simple reactive system for your Javascript application.<br/>Zero dependency, TypeScript fully supported.</p>
<p>A lightweight, simple reactive system for your Javascript application.<br/>Zero dependencies, TypeScript fully supported.</p>
</div>

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.
Expand Down
2 changes: 1 addition & 1 deletion packages/signal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div align="center">
<h1>Signal Proxy</h1>

<p>A simple reactive system for your Javascript application.<br/>Zero dependency, TypeScript fully supported.</p>
<p>A lightweight, simple reactive system for your Javascript application.<br/>Zero dependencies, TypeScript fully supported.</p>
</div>

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.
Expand Down
2 changes: 1 addition & 1 deletion packages/signal/jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kaiverse/signal",
"version": "0.2.2",
"version": "0.2.3",
"exports": "./src/index.ts",
"publish": {
"exclude": [
Expand Down
1 change: 1 addition & 0 deletions packages/signal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ export type SignalFactoryReturnType<T> = Readonly<[get: Signal<T>, set: SignalSe
export type SignalEffect = () => void;
export type EffectTracking = {execute: SignalEffect; deps: Set<Set<EffectTracking>>};
export type CleanupEffectFn = () => void;
export type SignalUntrackFn<T> = () => T;
17 changes: 10 additions & 7 deletions packages/signal/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(untrackFn: SignalUntrackFn<T>): T {
const prevEffectTracking = effectTrackingCache;
effectTrackingCache = null;
const untrackEffectExecute = untrackEffectCb();
const untrackReturnValue = untrackFn();
effectTrackingCache = prevEffectTracking;
return untrackEffectExecute;
return untrackReturnValue;
}

/**
Expand Down Expand Up @@ -84,7 +86,7 @@ export function createSignal<T>(
}

/**
* @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 {
Expand All @@ -98,6 +100,7 @@ export function createEffect(effect: SignalEffect): CleanupEffectFn {
deps: new Set(),
};

// const weakEffectDetailRef = new WeakRef(effectDetail);
effectDetail.execute();

// if (import.meta.hot) {
Expand All @@ -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.
*
Expand Down

0 comments on commit aa1523b

Please sign in to comment.