@@ -22,275 +22,16 @@ export interface SignalOptions<T> {
2222 cloneFunction ?: < V > ( value : V ) => V ;
2323 }
2424
25- /**
26- * A Signal represents a reactive value that automatically triggers updates
27- * when modified. It can store any type of value and provides methods for
28- * safely mutating that value while maintaining reactivity.
29- */
30- export class Signal < T > {
25+ /**
26+ * A Signal represents a reactive value that automatically triggers updates
27+ * when modified. It can store any type of value and provides methods for
28+ * safely mutating that value while maintaining reactivity.
29+ */
30+ export class Signal < T > {
3131 /**
3232 * Creates a new Signal with an initial value
3333 * @param initialValue - The initial value to store in the signal
3434 * @param options - Configuration options for the signal's behavior
3535 */
3636 constructor ( initialValue : T , options ?: SignalOptions < T > ) ;
37- /**
38- * Creates a new Signal without an initial value
39- */
40- constructor ( ) ;
41-
42- /**
43- * Gets the current value, establishing a reactive dependency.
44- * When accessed within a reactive context (like a Reaction),
45- * any changes to this Signal will cause the reactive context to re-run.
46- */
47- get value ( ) : T ;
48-
49- /**
50- * Sets a new value, triggering updates if the value has changed.
51- * Notifies all reactive contexts that depend on this Signal to re-run.
52- */
53- set value ( newValue : T ) ;
54-
55- /**
56- * Gets the current value and establishes a reactive dependency.
57- * This is an alias for `value` getter.
58- */
59- get ( ) : T ;
60-
61- /**
62- * Sets a new value and triggers updates if the value has changed.
63- * This is an alias for `value` setter.
64- * @param newValue - The new value to set
65- */
66- set ( newValue : T ) : void ;
67-
68- /**
69- * Returns the current value without establishing a reactive dependency.
70- * Accessing the value with `peek()` will not cause any reactive context to depend on this Signal.
71- */
72- peek ( ) : T ;
73-
74- /**
75- * Sets the signal's value to undefined.
76- * Triggers updates if the value was not already undefined.
77- */
78- clear ( ) : void ;
79-
80- /**
81- * Subscribes to changes in the signal's value.
82- * @param callback - Function called whenever the value changes.
83- * Receives the new value and a computation object with a `stop` method.
84- * @returns An object with a `stop` method that can be called to unsubscribe,
85- * preventing further calls to the callback.
86- */
87- subscribe ( callback : ( value : T , computation : { stop : ( ) => void } ) => void ) : { stop : ( ) => void } ;
88-
89- // Array-specific methods (only available when T is or extends any[])
90- // These methods are available when the Signal's value is an array, providing
91- // convenient ways to manipulate array values reactively.
92- /**
93- * Adds elements to the end of the array.
94- * This method is only available when `T` is or extends `any[]`.
95- * @param items - Elements to add
96- */
97- push < U extends any [ ] > ( this : Signal < U > , ...items : U [ number ] [ ] ) : void ;
98-
99- /**
100- * Adds elements to the beginning of the array.
101- * This method is only available when `T` is or extends `any[]`.
102- * @param items - Elements to add
103- */
104- unshift < U extends any [ ] > ( this : Signal < U > , ...items : U [ number ] [ ] ) : void ;
105-
106- /**
107- * Changes the contents of the array by removing or replacing existing elements.
108- * This method is only available when `T` is or extends `any[]`.
109- * @param start - Index at which to start modifying the array
110- * @param deleteCount - Number of elements to remove
111- * @param items - Elements to insert
112- */
113- splice < U extends any [ ] > ( this : Signal < U > , start : number , deleteCount ?: number , ...items : U [ number ] [ ] ) : void ;
114-
115- /**
116- * Gets the element at the specified index.
117- * This method is only available when `T` is or extends `any[]`.
118- * @param index - Index of the element to retrieve
119- */
120- getIndex < U extends any [ ] > ( this : Signal < U > , index : number ) : U [ number ] ;
121-
122- /**
123- * Sets the element at the specified index.
124- * This method is only available when `T` is or extends `any[]`.
125- * @param index - Index to set
126- * @param value - Value to set at the index
127- */
128- setIndex < U extends any [ ] > ( this : Signal < U > , index : number , value : U [ number ] ) : void ;
129-
130- /**
131- * Removes the element at the specified index.
132- * This method is only available when `T` is or extends `any[]`.
133- * @param index - Index of the element to remove
134- */
135- removeIndex < U extends any [ ] > ( this : Signal < U > , index : number ) : void ;
136-
137- // Object array methods (only available when T is or extends Record<string, any>[])
138- // These methods are for Signals holding arrays of objects, allowing for reactive updates
139- // to properties within those objects.
140- /**
141- * Sets a property on an object in the array at a specific index.
142- * This method is only available when `T` is or extends `Record<string, any>[]`.
143- * @param index - Index of object to modify
144- * @param property - Property name to set
145- * @param value - Value to set for the property
146- */
147- setArrayProperty < U extends Record < string , any > [ ] > (
148- this : Signal < U > ,
149- index : number ,
150- property : keyof U [ number ] ,
151- value : U [ number ] [ keyof U [ number ] ]
152- ) : void ;
153-
154- /**
155- * Sets a property on all objects in the array.
156- * This method is only available when `T` is or extends `Record<string, any>[]`.
157- * @param property - Property name to set on all objects
158- * @param value - Value to set for the property
159- */
160- setArrayProperty < U extends Record < string , any > [ ] > (
161- this : Signal < U > ,
162- property : keyof U [ number ] ,
163- value : U [ number ] [ keyof U [ number ] ]
164- ) : void ;
165-
166- // Boolean methods (only available when T is or extends boolean)
167- // These methods are available when the Signal's value is a boolean.
168- /**
169- * Toggles a boolean value between true and false.
170- * This method is only available when `T` is or extends `boolean`.
171- */
172- toggle ( this : Signal < boolean > ) : void ;
173-
174- // Numeric methods (only available when T is or extends number)
175- // These methods are available when the Signal's value is a number.
176- /**
177- * Increments the numeric value.
178- * This method is only available when `T` is or extends `number`.
179- * @param amount - Amount to increment by
180- * @default 1
181- */
182- increment ( this : Signal < number > , amount ?: number ) : void ;
183-
184- /**
185- * Decrements the numeric value.
186- * This method is only available when `T` is or extends `number`.
187- * @param amount - Amount to decrement by
188- * @default 1
189- */
190- decrement ( this : Signal < number > , amount ?: number ) : void ;
191-
192- // Date methods (only available when T is or extends Date)
193- // These methods are available when the Signal's value is a Date object.
194- /**
195- * Sets the value to the current date/time.
196- * This method is only available when `T` is or extends `Date`.
197- */
198- now ( this : Signal < Date > ) : void ;
199-
200- // Object ID related methods (for arrays or Signal values that are objects with IDs)
201- // These methods facilitate working with objects that have identifier properties
202- // such as `_id`, `id`, `hash`, or `key`.
203- /**
204- * Gets all possible ID values from an object.
205- * Checks for properties: `_id`, `id`, `hash`, `key`.
206- * @param item - Object to get IDs from
207- * @returns Array of ID values found in the object.
208- */
209- getIDs ( item : { _id ?: string ; id ?: string ; hash ?: string ; key ?: string } ) : string [ ] ;
210- /**
211- * Gets the ID from a string directly (returns the string in an array).
212- * @param item - string to get ID from
213- * @returns Array containing the input string.
214- */
215- getIDs ( item : string ) : [ string ] ;
216-
217- /**
218- * Gets the first available ID from an object.
219- * Checks for properties in order: `_id`, `id`, `hash`, `key`.
220- * @param item - Object to get ID from
221- * @returns The first available ID found, or undefined if no ID property is present.
222- */
223- getID ( item : { _id ?: string ; id ?: string ; hash ?: string ; key ?: string } ) : string | undefined ;
224- /**
225- * Gets the ID from a string directly (returns the string).
226- * @param item - string to get ID from
227- * @returns The input string as the ID.
228- */
229- getID ( item : string ) : string ;
230-
231- /**
232- * Checks if an object or string has a specific ID.
233- * @param item - Object or string to check
234- * @param id - ID to look for
235- * @returns `true` if the object or string's ID matches the provided `id`, `false` otherwise.
236- */
237- hasID ( item : { _id ?: string ; id ?: string ; hash ?: string ; key ?: string } | string , id : string ) : boolean ;
238-
239- /**
240- * Gets the index of an object with the specified ID within the Signal's array value.
241- * Assumes the Signal's value is an array of objects.
242- * @param id - ID to look for
243- * @returns Index of the matching object in the array, or -1 if not found.
244- */
245- getItem ( id : string ) : number ;
246-
247- /**
248- * Sets a property on an object with the specified ID within the Signal's array value.
249- * Assumes the Signal's value is an array of objects.
250- * @param id - ID of object to modify
251- * @param property - Property name
252- * @param value - Value to set
253- */
254- setProperty < K extends keyof T > ( id : string , property : K , value : T [ K ] ) : void ;
255- /**
256- * Sets a property directly on the Signal's value (if it's an object).
257- * Assumes the Signal's value is an object.
258- * @param property - Property name
259- * @param value - Value to set
260- */
261- setProperty < K extends keyof T > ( property : K , value : T [ K ] ) : void ;
262-
263- /**
264- * Replaces an object with the specified ID in the Signal's array value.
265- * Assumes the Signal's value is an array of objects.
266- * @param id - ID of object to replace
267- * @param item - New object to insert
268- */
269- replaceItem < U extends any [ ] > ( this : Signal < U > , id : string , item : U [ number ] ) : void ;
270-
271- /**
272- * Removes an object with the specified ID from the Signal's array value.
273- * Assumes the Signal's value is an array of objects.
274- * @param id - ID of object to remove
275- */
276- removeItem ( id : string ) : void ;
277-
278- // Array transformation methods (only available when T is or extends any[])
279- // These methods transform the array value of the Signal and update the Signal with the new array.
280- // Note: These methods modify the Signal's value in place.
281- /**
282- * Transforms each element in the array using the provided mapping function.
283- * This method is only available when `T` is or extends `any[]`.
284- * Note: This method modifies the Signal's value in place.
285- * @param callback - Function to transform each element
286- */
287- map < U extends any [ ] , V > ( this : Signal < U > , callback : ( value : U [ number ] , index : number , array : U ) => V ) : void ;
288-
289- /**
290- * Filters the array to only include elements that pass the test.
291- * This method is only available when `T` is or extends `any[]`.
292- * Note: This method modifies the Signal's value in place.
293- * @param predicate - Function to test each element
294- */
295- filter < U extends any [ ] > ( this : Signal < U > , predicate : ( value : U [ number ] , index : number , array : U ) => boolean ) : void ;
296- }
37+ }
0 commit comments