mettle-utils-signals 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Vam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # mettle-utils-signals
@@ -0,0 +1,159 @@
1
+ declare const BRAND_SYMBOL: unique symbol;
2
+ type Node = {
3
+ _source: Signal;
4
+ _prevSource?: Node;
5
+ _nextSource?: Node;
6
+ _target: Computed | Effect;
7
+ _prevTarget?: Node;
8
+ _nextTarget?: Node;
9
+ _version: number;
10
+ _rollbackNode?: Node;
11
+ };
12
+ /**
13
+ * Combine multiple value updates into one "commit" at the end of the provided callback.
14
+ *
15
+ * Batches can be nested and changes are only flushed once the outermost batch callback
16
+ * completes.
17
+ *
18
+ * Accessing a signal that has been modified within a batch will reflect its updated
19
+ * value.
20
+ *
21
+ * @param fn The callback function.
22
+ * @returns The value returned by the callback.
23
+ */
24
+ declare function batch<T>(fn: () => T): T;
25
+ /**
26
+ * Run a callback function that can access signal values without
27
+ * subscribing to the signal updates.
28
+ *
29
+ * @param fn The callback function.
30
+ * @returns The value returned by the callback.
31
+ */
32
+ declare function untracked<T>(fn: () => T): T;
33
+ /**
34
+ * The base class for plain and computed signals.
35
+ */
36
+ declare class Signal<T = any> {
37
+ /** @internal */
38
+ _value: unknown;
39
+ /**
40
+ * @internal
41
+ * Version numbers should always be >= 0, because the special value -1 is used
42
+ * by Nodes to signify potentially unused but recyclable nodes.
43
+ */
44
+ _version: number;
45
+ /** @internal */
46
+ _node?: Node;
47
+ /** @internal */
48
+ _targets?: Node;
49
+ constructor(value?: T, options?: SignalOptions<T>);
50
+ /** @internal */
51
+ _refresh(): boolean;
52
+ /** @internal */
53
+ _subscribe(node: Node): void;
54
+ /** @internal */
55
+ _unsubscribe(node: Node): void;
56
+ /** @internal */
57
+ _watched?(this: Signal<T>): void;
58
+ /** @internal */
59
+ _unwatched?(this: Signal<T>): void;
60
+ subscribe(fn: (value: T) => void): () => void;
61
+ name?: string;
62
+ valueOf(): T;
63
+ toString(): string;
64
+ toJSON(): T;
65
+ peek(): T;
66
+ brand: typeof BRAND_SYMBOL;
67
+ get value(): T;
68
+ set value(value: T);
69
+ }
70
+ export interface SignalOptions<T = any> {
71
+ watched?: (this: Signal<T>) => void;
72
+ unwatched?: (this: Signal<T>) => void;
73
+ name?: string;
74
+ }
75
+ /** @internal */
76
+ declare function Signal(this: Signal, value?: unknown, options?: SignalOptions): void;
77
+ /**
78
+ * Create a new plain signal.
79
+ *
80
+ * @param value The initial value for the signal.
81
+ * @returns A new signal.
82
+ */
83
+ export declare function signal<T>(value: T, options?: SignalOptions<T>): Signal<T>;
84
+ export declare function signal<T = undefined>(): Signal<T | undefined>;
85
+ /** @internal */
86
+ declare class Computed<T = any> extends Signal<T> {
87
+ _fn: () => T;
88
+ _sources?: Node;
89
+ _globalVersion: number;
90
+ _flags: number;
91
+ constructor(fn: () => T, options?: SignalOptions<T>);
92
+ _notify(): void;
93
+ get value(): T;
94
+ }
95
+ /** @internal */
96
+ declare function Computed(this: Computed, fn: () => unknown, options?: SignalOptions): void;
97
+ declare namespace Computed {
98
+ var prototype: Computed<any>;
99
+ }
100
+ /**
101
+ * An interface for read-only signals.
102
+ */
103
+ interface ReadonlySignal<T = any> {
104
+ readonly value: T;
105
+ peek(): T;
106
+ subscribe(fn: (value: T) => void): () => void;
107
+ valueOf(): T;
108
+ toString(): string;
109
+ toJSON(): T;
110
+ brand: typeof BRAND_SYMBOL;
111
+ }
112
+ /**
113
+ * Create a new signal that is computed based on the values of other signals.
114
+ *
115
+ * The returned computed signal is read-only, and its value is automatically
116
+ * updated when any signals accessed from within the callback function change.
117
+ *
118
+ * @param fn The effect callback.
119
+ * @returns A new read-only signal.
120
+ */
121
+ declare function computed<T>(fn: () => T, options?: SignalOptions<T>): ReadonlySignal<T>;
122
+ type EffectFn = ((this: {
123
+ dispose: () => void;
124
+ }) => void | (() => void)) | (() => void | (() => void));
125
+ /** @internal */
126
+ declare class Effect {
127
+ _fn?: EffectFn;
128
+ _cleanup?: () => void;
129
+ _sources?: Node;
130
+ _nextBatchedEffect?: Effect;
131
+ _flags: number;
132
+ name?: string;
133
+ constructor(fn: EffectFn, options?: EffectOptions);
134
+ _callback(): void;
135
+ _start(): () => void;
136
+ _notify(): void;
137
+ _dispose(): void;
138
+ dispose(): void;
139
+ }
140
+ export interface EffectOptions {
141
+ name?: string;
142
+ }
143
+ /** @internal */
144
+ declare function Effect(this: Effect, fn: EffectFn, options?: EffectOptions): void;
145
+ /**
146
+ * Create an effect to run arbitrary code in response to signal changes.
147
+ *
148
+ * An effect tracks which signals are accessed within the given callback
149
+ * function `fn`, and re-runs the callback when those signals change.
150
+ *
151
+ * The callback may return a cleanup function. The cleanup function gets
152
+ * run once, either when the callback is next called or when the effect
153
+ * gets disposed, whichever happens first.
154
+ *
155
+ * @param fn The effect callback.
156
+ * @returns A function for disposing the effect.
157
+ */
158
+ declare function effect(fn: EffectFn, options?: EffectOptions): () => void;
159
+ export { computed, effect, batch, untracked, Signal, ReadonlySignal, Effect, Computed };
@@ -0,0 +1 @@
1
+ var t=Symbol.for("mettle-signals");function e(){if(s>1)s--;else{for(var t,e=!1;void 0!==i;){var r=i;for(i=void 0,n++;void 0!==r;){var o=r._nextBatchedEffect;if(r._nextBatchedEffect=void 0,r._flags&=-3,!(8&r._flags)&&c(r))try{r._callback()}catch(r){e||(t=r,e=!0)}r=o}}if(n=0,s--,e)throw t}}var r=void 0;function o(t){var e=r;r=void 0;try{return t()}finally{r=e}}var i=void 0,s=0,n=0,_=0;function a(t){if(void 0!==r){var e=t._node;return void 0===e||e._target!==r?(e={_version:0,_source:t,_prevSource:r._sources,_nextSource:void 0,_target:r,_prevTarget:void 0,_nextTarget:void 0,_rollbackNode:e},void 0!==r._sources&&(r._sources._nextSource=e),r._sources=e,t._node=e,32&r._flags&&t._subscribe(e),e):-1===e._version?(e._version=0,void 0!==e._nextSource&&(e._nextSource._prevSource=e._prevSource,void 0!==e._prevSource&&(e._prevSource._nextSource=e._nextSource),e._prevSource=r._sources,e._nextSource=void 0,r._sources._nextSource=e,r._sources=e),e):void 0}}function u(t,e){this._value=t,this._version=0,this._node=void 0,this._targets=void 0,this._watched=null==e?void 0:e.watched,this._unwatched=null==e?void 0:e.unwatched,this.name=null==e?void 0:e.name}function c(t){for(var e=t._sources;void 0!==e;e=e._nextSource)if(e._source._version!==e._version||!e._source._refresh()||e._source._version!==e._version)return!0;return!1}function v(t){for(var e=t._sources;void 0!==e;e=e._nextSource){var r=e._source._node;if(void 0!==r&&(e._rollbackNode=r),e._source._node=e,e._version=-1,void 0===e._nextSource){t._sources=e;break}}}function f(t){for(var e=t._sources,r=void 0;void 0!==e;){var o=e._prevSource;-1===e._version?(e._source._unsubscribe(e),void 0!==o&&(o._nextSource=e._nextSource),void 0!==e._nextSource&&(e._nextSource._prevSource=o)):r=e,e._source._node=e._rollbackNode,void 0!==e._rollbackNode&&(e._rollbackNode=void 0),e=o}t._sources=r}function h(t,e){u.call(this,void 0),this._fn=t,this._sources=void 0,this._globalVersion=_-1,this._flags=4,this._watched=null==e?void 0:e.watched,this._unwatched=null==e?void 0:e.unwatched,this.name=null==e?void 0:e.name}function l(t){var o=t._cleanup;if(t._cleanup=void 0,"function"==typeof o){s++;var i=r;r=void 0;try{o()}catch(e){throw t._flags&=-2,t._flags|=8,d(t),e}finally{r=i,e()}}}function d(t){for(var e=t._sources;void 0!==e;e=e._nextSource)e._source._unsubscribe(e);t._fn=void 0,t._sources=void 0,l(t)}function p(t){if(r!==this)throw new Error("Out-of-order effect");f(this),r=t,this._flags&=-2,8&this._flags&&d(this),e()}function g(t,e){this._fn=t,this._cleanup=void 0,this._sources=void 0,this._nextBatchedEffect=void 0,this._flags=32,this.name=null==e?void 0:e.name}function y(t,e){var r=new g(t,e);try{r._callback()}catch(t){throw r._dispose(),t}var o=r._dispose.bind(r);return o[Symbol.dispose]=o,o}u.prototype.brand=t,u.prototype._refresh=function(){return!0},u.prototype._subscribe=function(t){var e=this,r=this._targets;r!==t&&void 0===t._prevTarget&&(t._nextTarget=r,this._targets=t,void 0!==r?r._prevTarget=t:o(function(){var t;null==(t=e._watched)||t.call(e)}))},u.prototype._unsubscribe=function(t){var e=this;if(void 0!==this._targets){var r=t._prevTarget,i=t._nextTarget;void 0!==r&&(r._nextTarget=i,t._prevTarget=void 0),void 0!==i&&(i._prevTarget=r,t._nextTarget=void 0),t===this._targets&&(this._targets=i,void 0===i&&o(function(){var t;null==(t=e._unwatched)||t.call(e)}))}},u.prototype.subscribe=function(t){var e=this;return y(function(){var o=e.value,i=r;r=void 0;try{t(o)}finally{r=i}},{name:"sub"})},u.prototype.valueOf=function(){return this.value},u.prototype.toString=function(){return this.value+""},u.prototype.toJSON=function(){return this.value},u.prototype.peek=function(){var t=r;r=void 0;try{return this.value}finally{r=t}},Object.defineProperty(u.prototype,"value",{get:function(){var t=a(this);return void 0!==t&&(t._version=this._version),this._value},set:function(t){if(t!==this._value){if(n>100)throw new Error("Cycle detected");this._value=t,this._version++,_++,s++;try{for(var r=this._targets;void 0!==r;r=r._nextTarget)r._target._notify()}finally{e()}}}}),(h.prototype=new u)._refresh=function(){if(this._flags&=-3,1&this._flags)return!1;if(32==(36&this._flags))return!0;if(this._flags&=-5,this._globalVersion===_)return!0;if(this._globalVersion=_,this._flags|=1,this._version>0&&!c(this))return this._flags&=-2,!0;var t=r;try{v(this),r=this;var e=this._fn();(16&this._flags||this._value!==e||0===this._version)&&(this._value=e,this._flags&=-17,this._version++)}catch(t){this._value=t,this._flags|=16,this._version++}return r=t,f(this),this._flags&=-2,!0},h.prototype._subscribe=function(t){if(void 0===this._targets){this._flags|=36;for(var e=this._sources;void 0!==e;e=e._nextSource)e._source._subscribe(e)}u.prototype._subscribe.call(this,t)},h.prototype._unsubscribe=function(t){if(void 0!==this._targets&&(u.prototype._unsubscribe.call(this,t),void 0===this._targets)){this._flags&=-33;for(var e=this._sources;void 0!==e;e=e._nextSource)e._source._unsubscribe(e)}},h.prototype._notify=function(){if(!(2&this._flags)){this._flags|=6;for(var t=this._targets;void 0!==t;t=t._nextTarget)t._target._notify()}},Object.defineProperty(h.prototype,"value",{get:function(){if(1&this._flags)throw new Error("Cycle detected");var t=a(this);if(this._refresh(),void 0!==t&&(t._version=this._version),16&this._flags)throw this._value;return this._value}}),g.prototype._callback=function(){var t=this._start();try{if(8&this._flags)return;if(void 0===this._fn)return;var e=this._fn();"function"==typeof e&&(this._cleanup=e)}finally{t()}},g.prototype._start=function(){if(1&this._flags)throw new Error("Cycle detected");this._flags|=1,this._flags&=-9,l(this),v(this),s++;var t=r;return r=this,p.bind(this,t)},g.prototype._notify=function(){2&this._flags||(this._flags|=2,this._nextBatchedEffect=i,i=this)},g.prototype._dispose=function(){this._flags|=8,1&this._flags||d(this)},g.prototype.dispose=function(){this._dispose()},exports.Computed=h,exports.Effect=g,exports.Signal=u,exports.batch=function(t){if(s>0)return t();/*@__INLINE__**/s++;try{return t()}finally{e()}},exports.computed=function(t,e){return new h(t,e)},exports.effect=y,exports.signal=function(t,e){return new u(t,e)},exports.untracked=o;
@@ -0,0 +1 @@
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).mettleUtilsSignals={})}(this,function(t){var e=Symbol.for("mettle-signals");function i(){if(n>1)n--;else{for(var t,e=!1;void 0!==s;){var i=s;for(s=void 0,_++;void 0!==i;){var r=i._nextBatchedEffect;if(i._nextBatchedEffect=void 0,i._flags&=-3,!(8&i._flags)&&f(i))try{i._callback()}catch(i){e||(t=i,e=!0)}i=r}}if(_=0,n--,e)throw t}}var r=void 0;function o(t){var e=r;r=void 0;try{return t()}finally{r=e}}var s=void 0,n=0,_=0,u=0;function a(t){if(void 0!==r){var e=t._node;return void 0===e||e._target!==r?(e={_version:0,_source:t,_prevSource:r._sources,_nextSource:void 0,_target:r,_prevTarget:void 0,_nextTarget:void 0,_rollbackNode:e},void 0!==r._sources&&(r._sources._nextSource=e),r._sources=e,t._node=e,32&r._flags&&t._subscribe(e),e):-1===e._version?(e._version=0,void 0!==e._nextSource&&(e._nextSource._prevSource=e._prevSource,void 0!==e._prevSource&&(e._prevSource._nextSource=e._nextSource),e._prevSource=r._sources,e._nextSource=void 0,r._sources._nextSource=e,r._sources=e),e):void 0}}function c(t,e){this._value=t,this._version=0,this._node=void 0,this._targets=void 0,this._watched=null==e?void 0:e.watched,this._unwatched=null==e?void 0:e.unwatched,this.name=null==e?void 0:e.name}function f(t){for(var e=t._sources;void 0!==e;e=e._nextSource)if(e._source._version!==e._version||!e._source._refresh()||e._source._version!==e._version)return!0;return!1}function v(t){for(var e=t._sources;void 0!==e;e=e._nextSource){var i=e._source._node;if(void 0!==i&&(e._rollbackNode=i),e._source._node=e,e._version=-1,void 0===e._nextSource){t._sources=e;break}}}function h(t){for(var e=t._sources,i=void 0;void 0!==e;){var r=e._prevSource;-1===e._version?(e._source._unsubscribe(e),void 0!==r&&(r._nextSource=e._nextSource),void 0!==e._nextSource&&(e._nextSource._prevSource=r)):i=e,e._source._node=e._rollbackNode,void 0!==e._rollbackNode&&(e._rollbackNode=void 0),e=r}t._sources=i}function l(t,e){c.call(this,void 0),this._fn=t,this._sources=void 0,this._globalVersion=u-1,this._flags=4,this._watched=null==e?void 0:e.watched,this._unwatched=null==e?void 0:e.unwatched,this.name=null==e?void 0:e.name}function d(t){var e=t._cleanup;if(t._cleanup=void 0,"function"==typeof e){n++;var o=r;r=void 0;try{e()}catch(e){throw t._flags&=-2,t._flags|=8,p(t),e}finally{r=o,i()}}}function p(t){for(var e=t._sources;void 0!==e;e=e._nextSource)e._source._unsubscribe(e);t._fn=void 0,t._sources=void 0,d(t)}function g(t){if(r!==this)throw new Error("Out-of-order effect");h(this),r=t,this._flags&=-2,8&this._flags&&p(this),i()}function y(t,e){this._fn=t,this._cleanup=void 0,this._sources=void 0,this._nextBatchedEffect=void 0,this._flags=32,this.name=null==e?void 0:e.name}function b(t,e){var i=new y(t,e);try{i._callback()}catch(t){throw i._dispose(),t}var r=i._dispose.bind(i);return r[Symbol.dispose]=r,r}c.prototype.brand=e,c.prototype._refresh=function(){return!0},c.prototype._subscribe=function(t){var e=this,i=this._targets;i!==t&&void 0===t._prevTarget&&(t._nextTarget=i,this._targets=t,void 0!==i?i._prevTarget=t:o(function(){var t;null==(t=e._watched)||t.call(e)}))},c.prototype._unsubscribe=function(t){var e=this;if(void 0!==this._targets){var i=t._prevTarget,r=t._nextTarget;void 0!==i&&(i._nextTarget=r,t._prevTarget=void 0),void 0!==r&&(r._prevTarget=i,t._nextTarget=void 0),t===this._targets&&(this._targets=r,void 0===r&&o(function(){var t;null==(t=e._unwatched)||t.call(e)}))}},c.prototype.subscribe=function(t){var e=this;return b(function(){var i=e.value,o=r;r=void 0;try{t(i)}finally{r=o}},{name:"sub"})},c.prototype.valueOf=function(){return this.value},c.prototype.toString=function(){return this.value+""},c.prototype.toJSON=function(){return this.value},c.prototype.peek=function(){var t=r;r=void 0;try{return this.value}finally{r=t}},Object.defineProperty(c.prototype,"value",{get:function(){var t=a(this);return void 0!==t&&(t._version=this._version),this._value},set:function(t){if(t!==this._value){if(_>100)throw new Error("Cycle detected");this._value=t,this._version++,u++,n++;try{for(var e=this._targets;void 0!==e;e=e._nextTarget)e._target._notify()}finally{i()}}}}),(l.prototype=new c)._refresh=function(){if(this._flags&=-3,1&this._flags)return!1;if(32==(36&this._flags))return!0;if(this._flags&=-5,this._globalVersion===u)return!0;if(this._globalVersion=u,this._flags|=1,this._version>0&&!f(this))return this._flags&=-2,!0;var t=r;try{v(this),r=this;var e=this._fn();(16&this._flags||this._value!==e||0===this._version)&&(this._value=e,this._flags&=-17,this._version++)}catch(t){this._value=t,this._flags|=16,this._version++}return r=t,h(this),this._flags&=-2,!0},l.prototype._subscribe=function(t){if(void 0===this._targets){this._flags|=36;for(var e=this._sources;void 0!==e;e=e._nextSource)e._source._subscribe(e)}c.prototype._subscribe.call(this,t)},l.prototype._unsubscribe=function(t){if(void 0!==this._targets&&(c.prototype._unsubscribe.call(this,t),void 0===this._targets)){this._flags&=-33;for(var e=this._sources;void 0!==e;e=e._nextSource)e._source._unsubscribe(e)}},l.prototype._notify=function(){if(!(2&this._flags)){this._flags|=6;for(var t=this._targets;void 0!==t;t=t._nextTarget)t._target._notify()}},Object.defineProperty(l.prototype,"value",{get:function(){if(1&this._flags)throw new Error("Cycle detected");var t=a(this);if(this._refresh(),void 0!==t&&(t._version=this._version),16&this._flags)throw this._value;return this._value}}),y.prototype._callback=function(){var t=this._start();try{if(8&this._flags)return;if(void 0===this._fn)return;var e=this._fn();"function"==typeof e&&(this._cleanup=e)}finally{t()}},y.prototype._start=function(){if(1&this._flags)throw new Error("Cycle detected");this._flags|=1,this._flags&=-9,d(this),v(this),n++;var t=r;return r=this,g.bind(this,t)},y.prototype._notify=function(){2&this._flags||(this._flags|=2,this._nextBatchedEffect=s,s=this)},y.prototype._dispose=function(){this._flags|=8,1&this._flags||p(this)},y.prototype.dispose=function(){this._dispose()},t.Computed=l,t.Effect=y,t.Signal=c,t.batch=function(t){if(n>0)return t();/*@__INLINE__**/n++;try{return t()}finally{i()}},t.computed=function(t,e){return new l(t,e)},t.effect=b,t.signal=function(t,e){return new c(t,e)},t.untracked=o});
@@ -0,0 +1 @@
1
+ const t=Symbol.for("mettle-signals");function e(){if(n>1)return void n--;let t,e=!1;for(;void 0!==s;){let o=s;for(s=void 0,_++;void 0!==o;){const i=o._nextBatchedEffect;if(o._nextBatchedEffect=void 0,o._flags&=-3,!(8&o._flags)&&h(o))try{o._callback()}catch(o){e||(t=o,e=!0)}o=i}}if(_=0,n--,e)throw t}function o(t){if(n>0)return t();/*@__INLINE__**/n++;try{return t()}finally{e()}}let i,s;function r(t){const e=i;i=void 0;try{return t()}finally{i=e}}let n=0,_=0,c=0;function u(t){if(void 0===i)return;let e=t._node;return void 0===e||e._target!==i?(e={_version:0,_source:t,_prevSource:i._sources,_nextSource:void 0,_target:i,_prevTarget:void 0,_nextTarget:void 0,_rollbackNode:e},void 0!==i._sources&&(i._sources._nextSource=e),i._sources=e,t._node=e,32&i._flags&&t._subscribe(e),e):-1===e._version?(e._version=0,void 0!==e._nextSource&&(e._nextSource._prevSource=e._prevSource,void 0!==e._prevSource&&(e._prevSource._nextSource=e._nextSource),e._prevSource=i._sources,e._nextSource=void 0,i._sources._nextSource=e,i._sources=e),e):void 0}function a(t,e){this._value=t,this._version=0,this._node=void 0,this._targets=void 0,this._watched=null==e?void 0:e.watched,this._unwatched=null==e?void 0:e.unwatched,this.name=null==e?void 0:e.name}function f(t,e){return new a(t,e)}function h(t){for(let e=t._sources;void 0!==e;e=e._nextSource)if(e._source._version!==e._version||!e._source._refresh()||e._source._version!==e._version)return!0;return!1}function l(t){for(let e=t._sources;void 0!==e;e=e._nextSource){const o=e._source._node;if(void 0!==o&&(e._rollbackNode=o),e._source._node=e,e._version=-1,void 0===e._nextSource){t._sources=e;break}}}function v(t){let e,o=t._sources;for(;void 0!==o;){const t=o._prevSource;-1===o._version?(o._source._unsubscribe(o),void 0!==t&&(t._nextSource=o._nextSource),void 0!==o._nextSource&&(o._nextSource._prevSource=t)):e=o,o._source._node=o._rollbackNode,void 0!==o._rollbackNode&&(o._rollbackNode=void 0),o=t}t._sources=e}function d(t,e){a.call(this,void 0),this._fn=t,this._sources=void 0,this._globalVersion=c-1,this._flags=4,this._watched=null==e?void 0:e.watched,this._unwatched=null==e?void 0:e.unwatched,this.name=null==e?void 0:e.name}function p(t,e){return new d(t,e)}function g(t){const o=t._cleanup;if(t._cleanup=void 0,"function"==typeof o){n++;const s=i;i=void 0;try{o()}catch(e){throw t._flags&=-2,t._flags|=8,y(t),e}finally{i=s,e()}}}function y(t){for(let e=t._sources;void 0!==e;e=e._nextSource)e._source._unsubscribe(e);t._fn=void 0,t._sources=void 0,g(t)}function b(t){if(i!==this)throw new Error("Out-of-order effect");v(this),i=t,this._flags&=-2,8&this._flags&&y(this),e()}function x(t,e){this._fn=t,this._cleanup=void 0,this._sources=void 0,this._nextBatchedEffect=void 0,this._flags=32,this.name=null==e?void 0:e.name}function S(t,e){const o=new x(t,e);try{o._callback()}catch(t){throw o._dispose(),t}const i=o._dispose.bind(o);return i[Symbol.dispose]=i,i}a.prototype.brand=t,a.prototype._refresh=function(){return!0},a.prototype._subscribe=function(t){const e=this._targets;e!==t&&void 0===t._prevTarget&&(t._nextTarget=e,this._targets=t,void 0!==e?e._prevTarget=t:r(()=>{var t;null==(t=this._watched)||t.call(this)}))},a.prototype._unsubscribe=function(t){if(void 0!==this._targets){const e=t._prevTarget,o=t._nextTarget;void 0!==e&&(e._nextTarget=o,t._prevTarget=void 0),void 0!==o&&(o._prevTarget=e,t._nextTarget=void 0),t===this._targets&&(this._targets=o,void 0===o&&r(()=>{var t;null==(t=this._unwatched)||t.call(this)}))}},a.prototype.subscribe=function(t){return S(()=>{const e=this.value,o=i;i=void 0;try{t(e)}finally{i=o}},{name:"sub"})},a.prototype.valueOf=function(){return this.value},a.prototype.toString=function(){return this.value+""},a.prototype.toJSON=function(){return this.value},a.prototype.peek=function(){const t=i;i=void 0;try{return this.value}finally{i=t}},Object.defineProperty(a.prototype,"value",{get(){const t=u(this);return void 0!==t&&(t._version=this._version),this._value},set(t){if(t!==this._value){if(_>100)throw new Error("Cycle detected");this._value=t,this._version++,c++,n++;try{for(let t=this._targets;void 0!==t;t=t._nextTarget)t._target._notify()}finally{e()}}}}),(d.prototype=new a)._refresh=function(){if(this._flags&=-3,1&this._flags)return!1;if(32==(36&this._flags))return!0;if(this._flags&=-5,this._globalVersion===c)return!0;if(this._globalVersion=c,this._flags|=1,this._version>0&&!h(this))return this._flags&=-2,!0;const t=i;try{l(this),i=this;const t=this._fn();(16&this._flags||this._value!==t||0===this._version)&&(this._value=t,this._flags&=-17,this._version++)}catch(t){this._value=t,this._flags|=16,this._version++}return i=t,v(this),this._flags&=-2,!0},d.prototype._subscribe=function(t){if(void 0===this._targets){this._flags|=36;for(let t=this._sources;void 0!==t;t=t._nextSource)t._source._subscribe(t)}a.prototype._subscribe.call(this,t)},d.prototype._unsubscribe=function(t){if(void 0!==this._targets&&(a.prototype._unsubscribe.call(this,t),void 0===this._targets)){this._flags&=-33;for(let t=this._sources;void 0!==t;t=t._nextSource)t._source._unsubscribe(t)}},d.prototype._notify=function(){if(!(2&this._flags)){this._flags|=6;for(let t=this._targets;void 0!==t;t=t._nextTarget)t._target._notify()}},Object.defineProperty(d.prototype,"value",{get(){if(1&this._flags)throw new Error("Cycle detected");const t=u(this);if(this._refresh(),void 0!==t&&(t._version=this._version),16&this._flags)throw this._value;return this._value}}),x.prototype._callback=function(){const t=this._start();try{if(8&this._flags)return;if(void 0===this._fn)return;const t=this._fn();"function"==typeof t&&(this._cleanup=t)}finally{t()}},x.prototype._start=function(){if(1&this._flags)throw new Error("Cycle detected");this._flags|=1,this._flags&=-9,g(this),l(this),n++;const t=i;return i=this,b.bind(this,t)},x.prototype._notify=function(){2&this._flags||(this._flags|=2,this._nextBatchedEffect=s,s=this)},x.prototype._dispose=function(){this._flags|=8,1&this._flags||y(this)},x.prototype.dispose=function(){this._dispose()};export{d as Computed,x as Effect,a as Signal,o as batch,p as computed,S as effect,f as signal,r as untracked};
@@ -0,0 +1 @@
1
+ var t=Symbol.for("mettle-signals");function e(){if(n>1)n--;else{for(var t,e=!1;void 0!==s;){var r=s;for(s=void 0,_++;void 0!==r;){var i=r._nextBatchedEffect;if(r._nextBatchedEffect=void 0,r._flags&=-3,!(8&r._flags)&&f(r))try{r._callback()}catch(r){e||(t=r,e=!0)}r=i}}if(_=0,n--,e)throw t}}function r(t){if(n>0)return t();/*@__INLINE__**/n++;try{return t()}finally{e()}}var i=void 0;function o(t){var e=i;i=void 0;try{return t()}finally{i=e}}var s=void 0,n=0,_=0,u=0;function a(t){if(void 0!==i){var e=t._node;return void 0===e||e._target!==i?(e={_version:0,_source:t,_prevSource:i._sources,_nextSource:void 0,_target:i,_prevTarget:void 0,_nextTarget:void 0,_rollbackNode:e},void 0!==i._sources&&(i._sources._nextSource=e),i._sources=e,t._node=e,32&i._flags&&t._subscribe(e),e):-1===e._version?(e._version=0,void 0!==e._nextSource&&(e._nextSource._prevSource=e._prevSource,void 0!==e._prevSource&&(e._prevSource._nextSource=e._nextSource),e._prevSource=i._sources,e._nextSource=void 0,i._sources._nextSource=e,i._sources=e),e):void 0}}function c(t,e){this._value=t,this._version=0,this._node=void 0,this._targets=void 0,this._watched=null==e?void 0:e.watched,this._unwatched=null==e?void 0:e.unwatched,this.name=null==e?void 0:e.name}function v(t,e){return new c(t,e)}function f(t){for(var e=t._sources;void 0!==e;e=e._nextSource)if(e._source._version!==e._version||!e._source._refresh()||e._source._version!==e._version)return!0;return!1}function h(t){for(var e=t._sources;void 0!==e;e=e._nextSource){var r=e._source._node;if(void 0!==r&&(e._rollbackNode=r),e._source._node=e,e._version=-1,void 0===e._nextSource){t._sources=e;break}}}function l(t){for(var e=t._sources,r=void 0;void 0!==e;){var i=e._prevSource;-1===e._version?(e._source._unsubscribe(e),void 0!==i&&(i._nextSource=e._nextSource),void 0!==e._nextSource&&(e._nextSource._prevSource=i)):r=e,e._source._node=e._rollbackNode,void 0!==e._rollbackNode&&(e._rollbackNode=void 0),e=i}t._sources=r}function d(t,e){c.call(this,void 0),this._fn=t,this._sources=void 0,this._globalVersion=u-1,this._flags=4,this._watched=null==e?void 0:e.watched,this._unwatched=null==e?void 0:e.unwatched,this.name=null==e?void 0:e.name}function p(t,e){return new d(t,e)}function g(t){var r=t._cleanup;if(t._cleanup=void 0,"function"==typeof r){n++;var o=i;i=void 0;try{r()}catch(e){throw t._flags&=-2,t._flags|=8,y(t),e}finally{i=o,e()}}}function y(t){for(var e=t._sources;void 0!==e;e=e._nextSource)e._source._unsubscribe(e);t._fn=void 0,t._sources=void 0,g(t)}function b(t){if(i!==this)throw new Error("Out-of-order effect");l(this),i=t,this._flags&=-2,8&this._flags&&y(this),e()}function x(t,e){this._fn=t,this._cleanup=void 0,this._sources=void 0,this._nextBatchedEffect=void 0,this._flags=32,this.name=null==e?void 0:e.name}function S(t,e){var r=new x(t,e);try{r._callback()}catch(t){throw r._dispose(),t}var i=r._dispose.bind(r);return i[Symbol.dispose]=i,i}c.prototype.brand=t,c.prototype._refresh=function(){return!0},c.prototype._subscribe=function(t){var e=this,r=this._targets;r!==t&&void 0===t._prevTarget&&(t._nextTarget=r,this._targets=t,void 0!==r?r._prevTarget=t:o(function(){var t;null==(t=e._watched)||t.call(e)}))},c.prototype._unsubscribe=function(t){var e=this;if(void 0!==this._targets){var r=t._prevTarget,i=t._nextTarget;void 0!==r&&(r._nextTarget=i,t._prevTarget=void 0),void 0!==i&&(i._prevTarget=r,t._nextTarget=void 0),t===this._targets&&(this._targets=i,void 0===i&&o(function(){var t;null==(t=e._unwatched)||t.call(e)}))}},c.prototype.subscribe=function(t){var e=this;return S(function(){var r=e.value,o=i;i=void 0;try{t(r)}finally{i=o}},{name:"sub"})},c.prototype.valueOf=function(){return this.value},c.prototype.toString=function(){return this.value+""},c.prototype.toJSON=function(){return this.value},c.prototype.peek=function(){var t=i;i=void 0;try{return this.value}finally{i=t}},Object.defineProperty(c.prototype,"value",{get:function(){var t=a(this);return void 0!==t&&(t._version=this._version),this._value},set:function(t){if(t!==this._value){if(_>100)throw new Error("Cycle detected");this._value=t,this._version++,u++,n++;try{for(var r=this._targets;void 0!==r;r=r._nextTarget)r._target._notify()}finally{e()}}}}),(d.prototype=new c)._refresh=function(){if(this._flags&=-3,1&this._flags)return!1;if(32==(36&this._flags))return!0;if(this._flags&=-5,this._globalVersion===u)return!0;if(this._globalVersion=u,this._flags|=1,this._version>0&&!f(this))return this._flags&=-2,!0;var t=i;try{h(this),i=this;var e=this._fn();(16&this._flags||this._value!==e||0===this._version)&&(this._value=e,this._flags&=-17,this._version++)}catch(t){this._value=t,this._flags|=16,this._version++}return i=t,l(this),this._flags&=-2,!0},d.prototype._subscribe=function(t){if(void 0===this._targets){this._flags|=36;for(var e=this._sources;void 0!==e;e=e._nextSource)e._source._subscribe(e)}c.prototype._subscribe.call(this,t)},d.prototype._unsubscribe=function(t){if(void 0!==this._targets&&(c.prototype._unsubscribe.call(this,t),void 0===this._targets)){this._flags&=-33;for(var e=this._sources;void 0!==e;e=e._nextSource)e._source._unsubscribe(e)}},d.prototype._notify=function(){if(!(2&this._flags)){this._flags|=6;for(var t=this._targets;void 0!==t;t=t._nextTarget)t._target._notify()}},Object.defineProperty(d.prototype,"value",{get:function(){if(1&this._flags)throw new Error("Cycle detected");var t=a(this);if(this._refresh(),void 0!==t&&(t._version=this._version),16&this._flags)throw this._value;return this._value}}),x.prototype._callback=function(){var t=this._start();try{if(8&this._flags)return;if(void 0===this._fn)return;var e=this._fn();"function"==typeof e&&(this._cleanup=e)}finally{t()}},x.prototype._start=function(){if(1&this._flags)throw new Error("Cycle detected");this._flags|=1,this._flags&=-9,g(this),h(this),n++;var t=i;return i=this,b.bind(this,t)},x.prototype._notify=function(){2&this._flags||(this._flags|=2,this._nextBatchedEffect=s,s=this)},x.prototype._dispose=function(){this._flags|=8,1&this._flags||y(this)},x.prototype.dispose=function(){this._dispose()};export{d as Computed,x as Effect,c as Signal,r as batch,p as computed,S as effect,v as signal,o as untracked};
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "mettle-utils-signals",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "private": false,
6
+ "main": "dist/mettle-utils-signals.js",
7
+ "module": "dist/mettle-utils-signals.module.js",
8
+ "unpkg": "dist/mettle-utils-signals.min.js",
9
+ "types": "dist/mettle-utils-signals.d.ts",
10
+ "source": "src/index.ts",
11
+ "scripts": {
12
+ "tsc":"tsc",
13
+ "_build": "microbundle --no-sourcemap",
14
+ "build": "tsc && pnpm _build --cwd"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/mettle-utils-signals.d.ts",
19
+ "browser": "./dist/mettle-utils-signals.module.js",
20
+ "import": "./dist/mettle-utils-signals.mjs",
21
+ "require": "./dist/mettle-utils-signals.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "keywords": [],
28
+ "license": "MIT",
29
+ "devDependencies": {
30
+ "@types/node": "^18.19.103",
31
+ "microbundle": "^0.15.1",
32
+ "typescript": "~5.8.3"
33
+ }
34
+ }