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 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 `Regulator` class is accessed via `context.actions.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 either globally or by type.
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.actions.regulator.policy.disallow.matching(
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.actions.regulator.policy.allow.matching(
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
- context.actions.regulator.abort.matching(Actions.Fetch);
686
- context.actions.regulator.abort.all();
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
- - `add(action: Action, controller: AbortController): void` — Registers an AbortController for a given action.
692
- - `controller(action: Action): AbortController` — Creates and registers an AbortController for an action, aborting immediately if disallowed by policy.
693
- - `abort.all(): void` — Aborts all controllers and removes them.
694
- - `abort.matching(...actions: Action[]): void` — Aborts controllers for specific actions and removes them.
695
- - `policy.allow.all(...actions: Action[]): void` — Allows one or more actions.
696
- - `policy.allow.matching(...actions: Action[]): void` — Allows specific actions.
697
- - `policy.disallow.all(...actions: Action[]): void` — Disallows one or more actions.
698
- - `policy.disallow.matching(...actions: Action[]): void` — Disallows specific actions.
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 &ndash; 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
- The `Regulator` class 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.
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.
@@ -1,4 +1,5 @@
1
- import { Action, Payload } from '../types/index.ts';
1
+ import { Payload } from '../types/index.ts';
2
+ import { Action } from '../regulator/types.ts';
2
3
  /**
3
4
  * Defines a new action with a given payload type.
4
5
  *