-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more docs, changed trigger method name
- Loading branch information
Vitaliy Berekchiyan
committed
Dec 12, 2023
1 parent
bc2e821
commit aebaa6e
Showing
20 changed files
with
285 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# AbstractEntity | ||
|
||
The base entity. | ||
You cannot create an instance of it, but every entity in `svitore` inherits its behavior | ||
|
||
## subscribe | ||
|
||
Subscribe to entity | ||
|
||
**Interface:** | ||
|
||
```ts | ||
subscribe(subscriber: (data: T) => void): () => void | ||
``` | ||
|
||
## unsubscribe | ||
|
||
Unsubscribe from entity | ||
|
||
**Interface:** | ||
|
||
```ts | ||
unsubscribe(subscriber: ((data: T) => void) | Entity<any>): void | ||
``` | ||
|
||
## release | ||
|
||
Remove all subscribers | ||
|
||
**Interface:** | ||
|
||
```ts | ||
release(): void | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ComputedState | ||
|
||
Derived state entity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# PersistState | ||
|
||
Entity to store a state and save the value in the storage | ||
|
||
_Has the same api as [State](/entities/state)_ | ||
|
||
## constructor | ||
|
||
```ts | ||
constructor( | ||
state: T, | ||
storageKey: string, | ||
storage?: Storage, // globalThis.Storage | ||
): PersistState<T> | ||
``` | ||
|
||
**Example:** | ||
|
||
```ts | ||
const $m = new SvitoreModule(); | ||
|
||
const firstName = $m.PersistState( | ||
"Alex", | ||
"firstName", | ||
globalThis.sessionStorage | ||
); | ||
``` | ||
|
||
This code means to create a state with a default value of `"Alex"` and store the value in the storage by the key `"firstName"` | ||
|
||
::: info | ||
By default `localStorage` is used, but you can use any storage, including your own custom storage by implementing the `Storage` interface | ||
::: | ||
|
||
## clear | ||
|
||
Delete state from storage | ||
|
||
**Interface:** | ||
|
||
```ts | ||
clear(): void | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,133 @@ | ||
# State | ||
|
||
## Description | ||
Entity to store the state | ||
|
||
_Has the same api as [AbstractEntity](/entities/abstract-entity)_ | ||
|
||
## constructor | ||
|
||
```ts | ||
constructor(state: T): State<T> | ||
``` | ||
|
||
## get | ||
|
||
Return current state | ||
|
||
**Interface:** | ||
|
||
```ts | ||
get(): T | ||
``` | ||
|
||
**Example:** | ||
|
||
```ts | ||
const $m = new SvitoreModule(); | ||
|
||
const counter = $m.State(0); | ||
|
||
counter.get(); // 0 | ||
``` | ||
|
||
## changeOn | ||
|
||
Method to change the state using an event | ||
|
||
**Interface:** | ||
|
||
```ts | ||
changeOn<Payload extends T>(event: Event<Payload>): this; | ||
``` | ||
```ts | ||
changeOn<Payload>( | ||
event: Event<Payload>, | ||
selector: (payload: Payload, state: T, prevState: T) => T | ||
): this; | ||
``` | ||
**Example:** | ||
```ts | ||
const $m = new SvitoreModule(); | ||
|
||
const counter = $m.State(0); | ||
const counterChanged = $m.Event<number>(); | ||
|
||
counter.changeOn(counterChanged); | ||
``` | ||
|
||
You can use in several ways | ||
|
||
if the event type matches the state type - short form: | ||
|
||
```ts | ||
counter.changeOn(counterChanged); | ||
``` | ||
|
||
if you want to transform the value | ||
|
||
```ts | ||
counter.changeOn(counterChanged, (payload) => payload * 10); | ||
``` | ||
|
||
if you want to calculate a value based on the state | ||
|
||
```ts | ||
counter.changeOn(counterChanged, (payload, state) => state + payload); | ||
``` | ||
|
||
## resetOn | ||
|
||
Reset state to default value | ||
|
||
**Interface:** | ||
|
||
```ts | ||
resetOn(event: Event<any>): this | ||
``` | ||
|
||
**Example:** | ||
|
||
```ts | ||
const $m = new SvitoreModule(); | ||
|
||
const counter = $m.State(5); | ||
const counterChanged = $m.Event<number>(); | ||
const reset = $m.Event(); | ||
|
||
counter.changeOn(counterChanged).resetOn(reset); | ||
|
||
counterChanged.dispatch(10); | ||
|
||
reset.dispatch(); | ||
|
||
counter.get(); // 5 | ||
``` | ||
|
||
## getPrev | ||
|
||
Return prev state | ||
|
||
**Interface:** | ||
|
||
```ts | ||
getPrev(): T | ||
``` | ||
|
||
**Example:** | ||
|
||
```ts | ||
const $m = new SvitoreModule(); | ||
|
||
const counter = $m.State(0); | ||
const counterChanged = $m.Event<number>(); | ||
|
||
counter.changeOn(counterChanged); | ||
|
||
counterChanged.dispatch(10); | ||
|
||
counter.getPrev(); // 0 | ||
counter.get(); // 10 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.