Observable state
Classes
- ObservableState ⇐
Observable
Functions
- effect(effectFn) ⇒
function
This function sets up an effect that is run when the observable changes
ObservableState ⇐ Observable
Kind: global class
Extends: Observable
- ObservableState ⇐
Observable
- new ObservableState(initialValue, subscriber, options)
- .value() ⇒
any
- .value(newValue)
- .assign(obj)
- .set(key, value)
- .delete(key)
- .clear()
- .push(...elements)
- .pop()
- .shift()
- .splice(start, deleteCount, ...items)
- .unshift(...elements)
- .reverse()
- .sort([compareFunction])
- .fill(value, [start], [end])
- .copyWithin(target, start, [end])
- .update(updater)
- .complete()
new ObservableState(initialValue, subscriber, options)
This class extends the Observable class and adds methods for updating the value of the observable.
Param | Type | Default | Description |
---|---|---|---|
initialValue | any |
|
The initial value of the observable |
subscriber | Subscriber |
|
The subscriber to the observable |
options | Object |
Additional options for the observable | |
options.last | boolean |
Whether the subscriber is the last observer |
Example
import { ObservableState } from 'cami-js';
const observable = new ObservableState(10);
console.log(observable.value); // 10
observableState.value() ⇒ any
Kind: instance method of ObservableState
Returns: any
- The current value of the observable
Example
observableState.value(newValue)
This method sets a new value for the observable by calling the update method with the new value.
Kind: instance method of ObservableState
Param | Type | Description |
---|---|---|
newValue | any |
The new value to set for the observable |
Example
observableState.assign(obj)
Merges properties from the provided object into the observable's value
Kind: instance method of ObservableState
Param | Type | Description |
---|---|---|
obj | Object |
The object whose properties to merge |
Example
observableState.set(key, value)
Sets a new value for a specific key in the observable's value. If the key is nested, it should be provided as a string with keys separated by dots.
Kind: instance method of ObservableState
Throws:
- Will throw an error if the observable's value is not an object
Param | Type | Description |
---|---|---|
key | string |
The key to set the new value for |
value | any |
The new value to set |
Example
observableState.delete(key)
Deletes a specific key from the observable's value. If the key is nested, it should be provided as a string with keys separated by dots.
Kind: instance method of ObservableState
Throws:
- Will throw an error if the observable's value is not an object
Param | Type | Description |
---|---|---|
key | string |
The key to delete |
Example
observableState.clear()
Removes all key/value pairs from the observable's value
Kind: instance method of ObservableState
Example
observableState.push(...elements)
Adds one or more elements to the end of the observable's value
Kind: instance method of ObservableState
Param | Type | Description |
---|---|---|
...elements | any |
The elements to add |
Example
observableState.pop()
Removes the last element from the observable's value
Kind: instance method of ObservableState
Example
observableState.shift()
Removes the first element from the observable's value
Kind: instance method of ObservableState
Example
observableState.splice(start, deleteCount, ...items)
Changes the contents of the observable's value by removing, replacing, or adding elements
Kind: instance method of ObservableState
Param | Type | Description |
---|---|---|
start | number |
The index at which to start changing the array |
deleteCount | number |
The number of elements to remove |
...items | any |
The elements to add to the array |
Example
observableState.unshift(...elements)
Adds one or more elements to the beginning of the observable's value
Kind: instance method of ObservableState
Param | Type | Description |
---|---|---|
...elements | any |
The elements to add |
Example
observableState.reverse()
Reverses the order of the elements in the observable's value
Kind: instance method of ObservableState
Example
observableState.sort([compareFunction])
Sorts the elements in the observable's value
Kind: instance method of ObservableState
Param | Type | Description |
---|---|---|
[compareFunction] | function |
The function used to determine the order of the elements |
Example
observableState.fill(value, [start], [end])
Changes all elements in the observable's value to a static value
Kind: instance method of ObservableState
Param | Type | Default | Description |
---|---|---|---|
value | any |
The value to fill the array with | |
[start] | number |
0 |
The index to start filling at |
[end] | number |
this.__value.length |
The index to stop filling at |
Example
observableState.copyWithin(target, start, [end])
Shallow copies part of the observable's value to another location in the same array
Kind: instance method of ObservableState
Param | Type | Default | Description |
---|---|---|---|
target | number |
The index to copy the elements to | |
start | number |
The start index to begin copying elements from | |
[end] | number |
this.__value.length |
The end index to stop copying elements from |
Example
observableState.update(updater)
This method adds the updater function to the pending updates queue. It uses a synchronous approach to schedule the updates, ensuring the whole state is consistent at each tick. This is done to batch multiple updates together and avoid unnecessary re-renders.
Kind: instance method of ObservableState
Param | Type | Description |
---|---|---|
updater | function |
The function to update the value |
Example
observableState.complete()
Calls the complete method of all observers.
Kind: instance method of ObservableState
Example
effect(effectFn) ⇒ function
This function sets up an effect that is run when the observable changes
Kind: global function
Returns: function
- A function that when called, unsubscribes from all dependencies and runs cleanup function
Param | Type | Description |
---|---|---|
effectFn | function |
The function to call for the effect |
Example
// Assuming `effectFn` is a function that is called when the observable changes
const effectFunction = effect(effectFn);
- effect(effectFn) ⇒
function
- ~tracker
- ~_runEffect()
- ~dispose() ⇒
void
effect~tracker
The tracker object is used to keep track of dependencies for the effect function. It provides a method to add a dependency (an observable) to the dependencies set. If the observable is not already a dependency, it is added to the set and a subscription is created to run the effect function whenever the observable's value changes. This mechanism allows the effect function to respond to state changes in its dependencies.
Kind: inner constant of effect
effect~_runEffect()
The _runEffect function is responsible for running the effect function and managing its dependencies. Before the effect function is run, any cleanup from the previous run is performed and the current tracker is set to this tracker. This allows the effect function to add dependencies via the tracker while it is running. After the effect function has run, the current tracker is set back to null to prevent further dependencies from being added outside of the effect function. The effect function is expected to return a cleanup function, which is saved for the next run. The cleanup function, initially empty, is replaced by the one returned from effectFn (run by the observable) before each new run and on effect disposal.
Kind: inner method of effect
effect~dispose() ⇒ void
Unsubscribes from all dependencies and runs cleanup function
Kind: inner method of effect
Example