Skip to content

Commit

Permalink
Add in computed
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Dec 27, 2024
1 parent 30a196b commit 53dcfe1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,19 +537,21 @@ declare class Computed<T = any> extends Signal<T> {
_globalVersion: number;
_flags: number;

constructor(fn: () => T);
constructor(fn: () => T, options?: SignalOptions<T>);

_notify(): void;
get value(): T;
}

function Computed(this: Computed, fn: () => unknown) {
function Computed(this: Computed, fn: () => unknown, options?: SignalOptions) {
Signal.call(this, undefined);

this._fn = fn;
this._sources = undefined;
this._globalVersion = globalVersion - 1;
this._flags = OUTDATED;
this._watched = options?.watched;
this._unwatched = options?.unwatched;
}

Computed.prototype = new Signal() as Computed;
Expand Down Expand Up @@ -626,9 +628,8 @@ Computed.prototype._subscribe = function (node) {

Computed.prototype._unsubscribe = function (node) {
// Only run the unsubscribe step if the computed signal has any subscribers.
Signal.prototype._unsubscribe.call(this, node);
if (this._targets !== undefined) {
Signal.prototype._unsubscribe.call(this, node);

// Computed signal unsubscribes from its dependencies when it loses its last subscriber.
// This makes it possible for unreferences subgraphs of computed signals to get garbage collected.
if (this._targets === undefined) {
Expand Down

0 comments on commit 53dcfe1

Please sign in to comment.