Skip to content

Commit db60c27

Browse files
committed
~ Update docs with Keys and State types
1 parent f3b3bf7 commit db60c27

File tree

1 file changed

+80
-11
lines changed

1 file changed

+80
-11
lines changed

README.md

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class-based store.
1212

1313
Key features include:
1414

15-
- Action/event based state flow.
15+
- Action/event based state flow (like Redux or Zustand).
1616
- Reset entire state or specific fields.
1717
- Type-safe state management.
1818
- Minimal boilerplate.
@@ -144,7 +144,8 @@ class Store extends TwoAndEight {
144144
```
145145

146146
> [!WARNING]
147-
> The class methods must use arrow functions in order to bind `this` to the class, allowing you to access your state and actions.
147+
> The class methods must use arrow functions in order to bind the store's `this`
148+
> to the action, allowing you to access your state and actions.
148149
149150
Generate your React hook:
150151

@@ -685,20 +686,21 @@ const Component = () => {
685686
#### `useStore.get`
686687

687688
```ts
688-
useStore.get(field: Field): Store[Field]
689+
useStore.get(key: Key): Store[Key]
689690
```
690691

691-
Get any field or action of your store, useful in subscribers where hooks are not
692-
available.
692+
Get any state field, derived state, or action of your store, useful in
693+
subscribers where hooks are not available.
693694

694695
#### `useStore.subscribe`
695696

696697
```ts
697698
useStore.subscribe(field: Field, callback: () => void): () => void
698699
```
699700

700-
Subscribes to state updates for a particular field or getter; registers a
701-
callback that fires whenever an action emits that affects the chosen field.
701+
Subscribes to state updates for a particular state field or derived state;
702+
registers a callback that fires whenever an action emits that affects the chosen
703+
field.
702704

703705
```ts
704706
useStore.subscribe('counter', () => {
@@ -718,11 +720,11 @@ need this if you are creating other, non-React, integrations with a 2n8 store.
718720
#### `store.get`
719721

720722
```ts
721-
useStore.get(field: Field): Store[Field]
723+
useStore.get(key: Key): Store[Key]
722724
```
723725

724-
Get any field or action of your store, useful in subscribers where hooks are not
725-
available.
726+
Get any state field, derived state, or action of your store, useful in
727+
subscribers where hooks are not available.
726728

727729
#### `store.subscribe`
728730

@@ -731,4 +733,71 @@ store.subscribe(field: Field, callback: () => void): () => void
731733
```
732734

733735
Subscribes to state updates; registers a callback that fires whenever an action
734-
emits and the selected field is changed.
736+
emits and the selected state field or derived state is changed.
737+
738+
### Types
739+
740+
#### `Keys`
741+
742+
```ts
743+
type T = Keys<Store>
744+
```
745+
746+
Given your `Store` class, this will extract all state field names, derived state
747+
name, and action names.
748+
749+
e.g.
750+
751+
```ts
752+
class Store extends TwoAndEight {
753+
count = 0
754+
755+
get countPlusOne() {
756+
return this.count + 1
757+
}
758+
759+
increment = () => {
760+
this.count++
761+
}
762+
763+
$noEmitAction = () => {
764+
this.count++
765+
}
766+
}
767+
768+
type T = Keys<Store>
769+
// "count" | "countPlusOne" | "increment"
770+
```
771+
772+
#### `State`
773+
774+
```ts
775+
type T = State<Store>
776+
```
777+
778+
Given your `Store`, this will extract the type of your state object.
779+
780+
e.g.
781+
782+
```ts
783+
class Store extends TwoAndEight {
784+
count = 0
785+
786+
get countPlusOne() {
787+
return this.count + 1
788+
}
789+
790+
increment = () => {
791+
this.count++
792+
}
793+
794+
$noEmitAction = () => {
795+
this.count++
796+
}
797+
}
798+
799+
type T = State<Store>
800+
// {
801+
// count: number;
802+
// }
803+
```

0 commit comments

Comments
 (0)