chizu 0.2.42 → 0.2.43
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/README.md +62 -20
- package/dist/action/index.d.ts +2 -1
- package/dist/chizu.js +1336 -1290
- package/dist/chizu.umd.cjs +1 -1
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/utils.d.ts +61 -1
- package/dist/index.d.ts +1 -0
- package/dist/regulator/index.d.ts +13 -80
- package/dist/regulator/types.d.ts +7 -0
- package/dist/regulator/utils.d.ts +103 -0
- package/dist/types/index.d.ts +12 -11
- package/dist/use/index.d.ts +1 -1
- package/dist/use/utils.d.ts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/utils.d.ts +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -22,6 +22,7 @@ Strongly typed React framework using generators and efficiently updated views al
|
|
|
22
22
|
1. [Utility functions](#utility-functions)
|
|
23
23
|
1. [Referential equality](#referential-equality)
|
|
24
24
|
1. [Action regulator](#action-regulator)
|
|
25
|
+
1. [Context providers](#context-providers)
|
|
25
26
|
|
|
26
27
|
## Benefits
|
|
27
28
|
|
|
@@ -650,7 +651,7 @@ function useSearchActions(props: Props) {
|
|
|
650
651
|
|
|
651
652
|
## Action regulator
|
|
652
653
|
|
|
653
|
-
The
|
|
654
|
+
The regulator is accessed via `context.regulator` inside your action handlers. It provides fine-grained control over asynchronous actions by managing `AbortController` instances and action policies. You can programmatically allow or disallow actions, and abort running actions across all components in the context.
|
|
654
655
|
|
|
655
656
|
### Usage
|
|
656
657
|
|
|
@@ -658,10 +659,7 @@ The `Regulator` class is accessed via `context.actions.regulator` inside your ac
|
|
|
658
659
|
const fetchAction = useAction<Model, typeof Actions, "Fetch">(
|
|
659
660
|
async (context) => {
|
|
660
661
|
// Disallow future dispatches of these actions
|
|
661
|
-
context.
|
|
662
|
-
Actions.Fetch,
|
|
663
|
-
Actions.Save,
|
|
664
|
-
);
|
|
662
|
+
context.regulator.policy.disallow.matching(Actions.Fetch, Actions.Save);
|
|
665
663
|
|
|
666
664
|
// Future dispatches via useAction will be aborted immediately
|
|
667
665
|
// and the error handler will receive Reason.Disallowed
|
|
@@ -671,10 +669,7 @@ const fetchAction = useAction<Model, typeof Actions, "Fetch">(
|
|
|
671
669
|
const resetAction = useAction<Model, typeof Actions, "Reset">(
|
|
672
670
|
async (context) => {
|
|
673
671
|
// Allow the actions again
|
|
674
|
-
context.
|
|
675
|
-
Actions.Fetch,
|
|
676
|
-
Actions.Save,
|
|
677
|
-
);
|
|
672
|
+
context.regulator.policy.allow.matching(Actions.Fetch, Actions.Save);
|
|
678
673
|
},
|
|
679
674
|
);
|
|
680
675
|
```
|
|
@@ -682,19 +677,66 @@ const resetAction = useAction<Model, typeof Actions, "Reset">(
|
|
|
682
677
|
You can also abort running actions:
|
|
683
678
|
|
|
684
679
|
```ts
|
|
685
|
-
|
|
686
|
-
context.
|
|
680
|
+
// Abort specific actions across all components
|
|
681
|
+
context.regulator.abort.matching(Actions.Fetch);
|
|
682
|
+
|
|
683
|
+
// Abort all actions across all components
|
|
684
|
+
context.regulator.abort.all();
|
|
685
|
+
|
|
686
|
+
// Abort only the current action instance
|
|
687
|
+
context.regulator.abort.self();
|
|
687
688
|
```
|
|
688
689
|
|
|
689
690
|
### API
|
|
690
691
|
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
- `abort.all()
|
|
694
|
-
- `abort.matching(...actions
|
|
695
|
-
- `
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
692
|
+
**Abort methods:**
|
|
693
|
+
|
|
694
|
+
- `abort.all()` — Aborts all running actions across all components in the context.
|
|
695
|
+
- `abort.matching(...actions)` — Aborts specific actions across all components.
|
|
696
|
+
- `abort.self()` — Aborts only the current action instance.
|
|
697
|
+
|
|
698
|
+
**Allow methods:**
|
|
699
|
+
|
|
700
|
+
- `policy.allow.all()` — Clears all disallow policies across all components.
|
|
701
|
+
- `policy.allow.matching(...actions)` — Allows specific actions across all components.
|
|
702
|
+
- `policy.allow.self()` — Allows the current action.
|
|
703
|
+
|
|
704
|
+
**Disallow methods:**
|
|
705
|
+
|
|
706
|
+
- `policy.disallow.all()` — Clears all allow policies across all components.
|
|
707
|
+
- `policy.disallow.matching(...actions)` — Disallows specific actions across all components.
|
|
708
|
+
- `policy.disallow.self()` — Disallows the current action.
|
|
709
|
+
|
|
710
|
+
The regulator is useful for advanced scenarios where you need to centrally manage cancellation and permission of asynchronous actions, such as rate limiting, feature toggling, or global aborts.
|
|
711
|
+
|
|
712
|
+
## Context providers
|
|
713
|
+
|
|
714
|
+
Chizu provides context providers for advanced use cases where you need isolated contexts. These are edge cases – most applications don't need them.
|
|
715
|
+
|
|
716
|
+
### `Broadcaster`
|
|
717
|
+
|
|
718
|
+
Creates an isolated broadcast context for distributed actions. Useful for libraries that want their own broadcast context without interfering with the host application:
|
|
719
|
+
|
|
720
|
+
```tsx
|
|
721
|
+
import { Broadcaster } from "chizu";
|
|
722
|
+
|
|
723
|
+
function MyLibraryRoot({ children }) {
|
|
724
|
+
return <Broadcaster>{children}</Broadcaster>;
|
|
725
|
+
}
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
Components inside `<Broadcaster>` have their own isolated broadcast channel. Distributed actions dispatched inside won't reach components outside, and vice versa.
|
|
729
|
+
|
|
730
|
+
### `Regulators`
|
|
731
|
+
|
|
732
|
+
Creates an isolated regulator context. All regulator operations (`abort.all()`, `policy.disallow.matching()`, etc.) only affect components within the same `Regulators` provider:
|
|
733
|
+
|
|
734
|
+
```tsx
|
|
735
|
+
import { Regulators } from "chizu";
|
|
736
|
+
|
|
737
|
+
function Example({ children }) {
|
|
738
|
+
return <Regulators>{children}</Regulators>;
|
|
739
|
+
}
|
|
740
|
+
```
|
|
699
741
|
|
|
700
|
-
|
|
742
|
+
This is useful for libraries that need action control without affecting the host application's actions. An `abort.all()` inside the provider won't abort actions outside it.
|
package/dist/action/index.d.ts
CHANGED