chizu 0.2.49 → 0.2.50
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 +2 -0
- package/dist/boundary/components/cache/index.d.ts +14 -0
- package/dist/boundary/components/cache/types.d.ts +24 -0
- package/dist/boundary/components/cache/utils.d.ts +12 -0
- package/dist/boundary/index.d.ts +1 -1
- package/dist/cache/index.d.ts +29 -0
- package/dist/cache/utils.d.ts +48 -0
- package/dist/chizu.js +8 -7
- package/dist/chizu.umd.cjs +1 -1
- package/dist/hooks/utils.d.ts +4 -1
- package/dist/index.d.ts +1 -0
- package/dist/types/index.d.ts +124 -0
- package/dist/utils/index.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -247,3 +247,5 @@ actions.dispatch(Actions.Multicast.Update, 42, { scope: "TeamA" });
|
|
|
247
247
|
```
|
|
248
248
|
|
|
249
249
|
Unlike broadcast which reaches all components, multicast is scoped to the named boundary – perfect for isolated widget groups, form sections, or distinct UI regions. See the [multicast recipe](./recipes/multicast-actions.md) for more details.
|
|
250
|
+
|
|
251
|
+
For caching async results across components, define cache operations with `Cache()` and use `context.actions.cache.put()` inside handlers – on cache hit the value is returned synchronously. Use `context.actions.cache.get()` to read cached values and `context.actions.cache.delete()` to clear entries with partial channel matching. See the [cache recipe](./recipes/cache.md) for details.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Props } from './types.ts';
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
export { useCache } from './utils.ts';
|
|
4
|
+
export type { CacheEntry, CacheStore } from './types.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new cache context for caching async results within action handlers.
|
|
7
|
+
* Automatically included in `<Boundary>`. Only needed directly if you want to
|
|
8
|
+
* isolate a cache context, useful for libraries that want their own cache
|
|
9
|
+
* without interfering with the app's cache.
|
|
10
|
+
*
|
|
11
|
+
* @param props.children - The children to render within the cache context.
|
|
12
|
+
* @returns The children wrapped in a cache context provider.
|
|
13
|
+
*/
|
|
14
|
+
export declare function CacheProvider({ children }: Props): React.ReactNode;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Filter } from '../../../types/index.ts';
|
|
2
|
+
import type * as React from "react";
|
|
3
|
+
/**
|
|
4
|
+
* A single cache entry with its stored value, expiration timestamp, and
|
|
5
|
+
* original channel for partial-match invalidation.
|
|
6
|
+
*/
|
|
7
|
+
export type CacheEntry = {
|
|
8
|
+
value: unknown;
|
|
9
|
+
expiresAt: number;
|
|
10
|
+
channel: Filter | undefined;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The cache store: outer Map keyed by operation symbol, inner Map keyed
|
|
14
|
+
* by serialised channel string.
|
|
15
|
+
*
|
|
16
|
+
* For unchanneled cache operations, the inner key is an empty string "".
|
|
17
|
+
*/
|
|
18
|
+
export type CacheStore = Map<symbol, Map<string, CacheEntry>>;
|
|
19
|
+
/**
|
|
20
|
+
* Props for the CacheProvider component.
|
|
21
|
+
*/
|
|
22
|
+
export type Props = {
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CacheStore } from './types.ts';
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
/**
|
|
4
|
+
* React context for the shared cache store.
|
|
5
|
+
*/
|
|
6
|
+
export declare const Context: React.Context<CacheStore>;
|
|
7
|
+
/**
|
|
8
|
+
* Hook to access the cache store from context.
|
|
9
|
+
*
|
|
10
|
+
* @returns The cache store Map from the current context.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useCache(): CacheStore;
|
package/dist/boundary/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Props } from './types.ts';
|
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
/**
|
|
4
4
|
* Creates a unified context boundary for all Chizu features.
|
|
5
|
-
* Wraps children with Broadcaster, Consumer, and Tasks providers.
|
|
5
|
+
* Wraps children with Broadcaster, Consumer, Cache, and Tasks providers.
|
|
6
6
|
*
|
|
7
7
|
* Use this at the root of your application or to create isolated context boundaries
|
|
8
8
|
* for libraries that need their own Chizu context.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CacheFactory } from '../types/index.ts';
|
|
2
|
+
export { getCacheSymbol, getCacheTtl, isCacheOperation, isChanneledCache, serializeChannel, matchesCacheChannel, } from './utils.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new cache operation with a TTL (time-to-live) in milliseconds.
|
|
5
|
+
*
|
|
6
|
+
* Cache operations are identifiers used with `context.actions.cache.put()` and
|
|
7
|
+
* `context.actions.cache.delete()` to manage cached async results. They follow the
|
|
8
|
+
* same channel pattern as actions.
|
|
9
|
+
*
|
|
10
|
+
* @template C - The channel type for keyed cache entries (defaults to never).
|
|
11
|
+
* @param ttl - Time-to-live in milliseconds.
|
|
12
|
+
* @returns A typed cache operation object.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* export class CacheStore {
|
|
17
|
+
* static User = Cache<{ UserId: number }>(30_000); // 30s TTL, channeled
|
|
18
|
+
* static Config = Cache(60_000); // 60s TTL, unchanneled
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // Channeled usage
|
|
22
|
+
* context.actions.cache.put(CacheStore.User({ UserId: 5 }), () => fetchUser(5));
|
|
23
|
+
*
|
|
24
|
+
* // Deletion (partial channel match)
|
|
25
|
+
* context.actions.cache.delete(CacheStore.User({ UserId: 5 }));
|
|
26
|
+
* context.actions.cache.delete(CacheStore.User); // clears all
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare const Cache: CacheFactory;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Filter } from '../types/index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts the underlying symbol from a cache operation or channeled cache.
|
|
4
|
+
*
|
|
5
|
+
* @param operation The cache operation to extract the symbol from.
|
|
6
|
+
* @returns The underlying symbol.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getCacheSymbol(operation: unknown): symbol;
|
|
9
|
+
/**
|
|
10
|
+
* Extracts the TTL from a cache operation or channeled cache.
|
|
11
|
+
*
|
|
12
|
+
* @param operation The cache operation to extract the TTL from.
|
|
13
|
+
* @returns The TTL in milliseconds.
|
|
14
|
+
*/
|
|
15
|
+
export declare function getCacheTtl(operation: unknown): number;
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether a value is a cache operation.
|
|
18
|
+
*
|
|
19
|
+
* @param value The value to check.
|
|
20
|
+
* @returns True if the value is a cache operation.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isCacheOperation(value: unknown): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Checks whether a value is a channeled cache operation.
|
|
25
|
+
*
|
|
26
|
+
* @param value The value to check.
|
|
27
|
+
* @returns True if the value is a channeled cache operation with a channel property.
|
|
28
|
+
*/
|
|
29
|
+
export declare function isChanneledCache(value: unknown): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Serialises a channel object into a deterministic string key for cache lookup.
|
|
32
|
+
* Keys are sorted alphabetically for deterministic output.
|
|
33
|
+
* Returns an empty string for unchanneled operations.
|
|
34
|
+
*
|
|
35
|
+
* @param channel The channel object to serialise.
|
|
36
|
+
* @returns A deterministic string representation of the channel.
|
|
37
|
+
*/
|
|
38
|
+
export declare function serializeChannel(channel?: Filter): string;
|
|
39
|
+
/**
|
|
40
|
+
* Checks whether an invalidation channel partially matches a stored channel.
|
|
41
|
+
* Returns true if ALL keys in the invalidation channel exist in the stored
|
|
42
|
+
* channel with matching values.
|
|
43
|
+
*
|
|
44
|
+
* @param invalidateChannel The channel from the invalidation call.
|
|
45
|
+
* @param storedChannel The channel stored with the cache entry.
|
|
46
|
+
* @returns True if the invalidation channel is a subset of the stored channel.
|
|
47
|
+
*/
|
|
48
|
+
export declare function matchesCacheChannel(invalidateChannel: Filter, storedChannel: Filter): boolean;
|
package/dist/chizu.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import{G as e,A as t}from"@mobily/ts-belt";import{jsx as n}from"react/jsx-runtime";import*as r from"react";import{createContext as o,useContext as
|
|
2
|
-
return n(
|
|
3
|
-
return n(
|
|
4
|
-
return n(
|
|
5
|
-
return n(
|
|
6
|
-
return n(
|
|
7
|
-
return n(
|
|
1
|
+
import{G as e,A as t}from"@mobily/ts-belt";import{jsx as n}from"react/jsx-runtime";import*as r from"react";import{createContext as o,useContext as c}from"react";import{immerable as i,enablePatches as s,Immer as a}from"immer";class u{static Payload=/* @__PURE__ */Symbol("chizu.brand/Payload");static Broadcast=/* @__PURE__ */Symbol("chizu.brand/Broadcast");static Multicast=/* @__PURE__ */Symbol("chizu.brand/Multicast");static Action=/* @__PURE__ */Symbol("chizu.brand/Action");static Channel=/* @__PURE__ */Symbol("chizu.brand/Channel");static Node=/* @__PURE__ */Symbol("chizu.action.lifecycle/Node");static Cache=/* @__PURE__ */Symbol("chizu.brand/Cache");static TTL=/* @__PURE__ */Symbol("chizu.brand/TTL")}class l{static Mount=/* @__PURE__ */Symbol("chizu.action.lifecycle/Mount");static Unmount=/* @__PURE__ */Symbol("chizu.action.lifecycle/Unmount");static Error=/* @__PURE__ */Symbol("chizu.action.lifecycle/Error");static Update=/* @__PURE__ */Symbol("chizu.action.lifecycle/Update");static Node=(()=>{const e=u.Node,t=function(t){return{[u.Action]:e,[u.Payload]:void 0,[u.Channel]:t,channel:t}};return Object.defineProperty(t,u.Action,{value:e,enumerable:!1}),Object.defineProperty(t,u.Payload,{value:void 0,enumerable:!1}),t})()}var f=/* @__PURE__ */(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(f||{}),d=/* @__PURE__ */(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(d||{}),h=/* @__PURE__ */(e=>(e[e.Timedout=0]="Timedout",e[e.Supplanted=1]="Supplanted",e[e.Disallowed=2]="Disallowed",e[e.Errored=3]="Errored",e[e.Unmounted=4]="Unmounted",e))(h||{});class p extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const m={actionPrefix:"chizu.action/",broadcastActionPrefix:"chizu.action/broadcast/",multicastActionPrefix:"chizu.action/multicast/",channelPrefix:"chizu.channel/",cachePrefix:"chizu.cache/"};function y(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new p);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new p)},{once:!0})})}function b(e){return e?Boolean(e&&"symbol"!=typeof e):/* @__PURE__ */Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const v=/* @__PURE__ */Object.freeze(/* @__PURE__ */Object.defineProperty({__proto__:null,config:m,pk:b,sleep:y,"ζ":y,"κ":b},Symbol.toStringTag,{value:"Module"})),g=e=>"symbol"==typeof e;function w(t){return e.isString(t)||g(t)?t:(e.isObject(t)||e.isFunction(t))&&u.Action in t?t[u.Action]:t}function x(t){if(e.isString(t))return t.startsWith(m.broadcastActionPrefix);if(g(t))return t.description?.startsWith(m.broadcastActionPrefix)??!1;if(e.isObject(t)||e.isFunction(t)){if(u.Broadcast in t&&t[u.Broadcast])return!0;if(u.Action in t){const e=t[u.Action];return e.description?.startsWith(m.broadcastActionPrefix)??!1}}return!1}function P(t){const n=w(t),r=e.isString(n)?n:n.description??"";return r.startsWith(m.actionPrefix)&&r.slice(r.lastIndexOf("/")+1)||"unknown"}function O(t){return e.isObject(t)&&u.Channel in t&&"channel"in t}function S(t){if(e.isString(t))return t.startsWith(m.multicastActionPrefix);if(g(t))return t.description?.startsWith(m.multicastActionPrefix)??!1;if(e.isObject(t)||e.isFunction(t)){if(u.Multicast in t&&t[u.Multicast])return!0;if(u.Action in t){const e=t[u.Action];return e.description?.startsWith(m.multicastActionPrefix)??!1}}return!1}const A=(e,t=f.Unicast)=>{const n=t===f.Broadcast?/* @__PURE__ */Symbol(`${m.broadcastActionPrefix}${e}`):t===f.Multicast?/* @__PURE__ */Symbol(`${m.multicastActionPrefix}${e}`):/* @__PURE__ */Symbol(`${m.actionPrefix}${e}`),r=function(e){return{[u.Action]:n,[u.Payload]:void 0,[u.Channel]:e,channel:e}};return Object.defineProperty(r,u.Action,{value:n,enumerable:!1}),Object.defineProperty(r,u.Payload,{value:void 0,enumerable:!1}),t===f.Broadcast&&Object.defineProperty(r,u.Broadcast,{value:!0,enumerable:!1}),t===f.Multicast&&Object.defineProperty(r,u.Multicast,{value:!0,enumerable:!1}),r};function E(t){if((e.isObject(t)||e.isFunction(t))&&u.Action in t)return t[u.Action];throw new Error("Invalid cache operation")}function j(t){return function(t){return(e.isObject(t)||e.isFunction(t))&&u.Cache in t&&!0===t[u.Cache]}(t)&&e.isObject(t)&&u.Channel in t&&"channel"in t}function M(e){return e?[...Object.keys(e)].toSorted().map(t=>`${t}=${String(e[t])}`).join("&"):""}function C(e,t){return Object.keys(e).every(n=>t[n]===e[n])}const _=e=>{const t=/* @__PURE__ */Symbol(`${m.cachePrefix}${e}`),n=function(n){return{[u.Action]:t,[u.Cache]:!0,[u.TTL]:e,[u.Channel]:n,channel:n}};return Object.defineProperty(n,u.Action,{value:t,enumerable:!1}),Object.defineProperty(n,u.Cache,{value:!0,enumerable:!1}),Object.defineProperty(n,u.TTL,{value:e,enumerable:!1}),n};function N(e){if(e instanceof Error){if("TimeoutError"===e.name)return h.Timedout;if("AbortError"===e.name)return h.Supplanted}return h.Errored}function k(e){return e instanceof Error?e:new Error(String(e))}const R=o(void 0);function z({handler:e,children:t}){/* @__PURE__ */
|
|
2
|
+
return n(R.Provider,{value:e,children:t})}let L=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var T=/* @__PURE__ */(e=>(e[e.Add=1]="Add",e[e.Remove=2]="Remove",e[e.Update=4]="Update",e[e.Move=8]="Move",e[e.Replace=16]="Replace",e[e.Sort=32]="Sort",e))(T||{}),U=/* @__PURE__ */(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(U||{}),$=/* @__PURE__ */(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))($||{});class B{[i]=!0;static keys=new Set(Object.values($));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new B(this.value,this.operation);return n.property=e,n.process=t,n}}class F{static immer=(()=>{s();const e=new a;return e.setAutoFreeze(!1),e})();static tag="κ";static id=L}function W(e,t){const n="string"==typeof t?""===t?[]:t.split("."):t;let r=e;for(const o of n){if(null==r)return;r=r[o]}return r}function D(t){if(e.isNullable(t)||I(t))return t;if(e.isArray(t))return t.map(e=>D(e));if(e.isObject(t)){const e=Object.entries(t).map(([e,t])=>[e,D(t)]);return{...Object.fromEntries(e),[F.tag]:t[F.tag]??F.id()}}return t}function H(e){if(Array.isArray(e))return e.filter(e=>F.tag in e).map(e=>e[F.tag]??"").join(",");const t=e[F.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function I(t){return e.isNullable(t)||e.isString(t)||e.isNumber(t)||e.isBoolean(t)||"symbol"==typeof t||"bigint"==typeof t}function G(t,n,r,o,c,i){return function s(a,u=n.path){if(a instanceof B){const n=W(r,u.join("."));if(Object.entries(a).filter(([e,t])=>!B.keys.has(e)&&t instanceof B).forEach(([e,t])=>s(t,u.concat(e))),I(a.value)){if(t===U.Hydrate)return a.value;const s=u.slice(0,-1),l=s.length>0?W(r,s.join(".")):r;return e.isNullable(l)||V(l,a,u.at(-1),o,c,i),n??a.value}if(t===U.Hydrate){const e=D(s(a.value,u));return V(e,a,null,o,c,i),e}const l=n??D(a.value);return V(l,a,null,o,c,i),e.isNullable(n)?l:(s(a.value,u),n)}if(e.isArray(a))return a.map((e,t)=>s(e,u.concat(t)));if(e.isObject(a)){const e=Object.entries(a).map(([e,t])=>[e,s(t,u.concat(e))]),n=Object.fromEntries(e);if(t===U.Hydrate){const e=D(n);return Object.entries(a).forEach(([t,n])=>{n instanceof B&&I(n.value)&&V(e,n,t,o,c,i)}),e}return n}return a}(n.value)}function V(e,t,n,r,o,c){const i=c(e),s=o.get(i)??[];o.set(i,[t.assign(n,r),...s])}class q{#e={};#t;#n=/* @__PURE__ */new Map;#r=/* @__PURE__ */new Set;#o=!1;constructor(e=H){this.#t=e}static pk(){return L()}static"κ"=q.pk;annotate(e,t){return new B(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(n,r,o,c,i){function s(c){const i=c.at(-1),s=W(n(),c),a=c.slice(0,-1),u=t.isNotEmpty(a)?W(n(),a):n();return[...e.isObject(s)||e.isArray(s)?r.get(o(s))?.filter(t=>e.isNullable(t.property))??[]:[],...e.isObject(u)?r.get(o(u))?.filter(e=>e.property===i)??[]:[]]}return function e(r){return new Proxy(()=>{},{get:(o,a)=>"pending"===a?()=>!t.isEmpty(s(r)):"remaining"===a?()=>t.length(s(r)):"box"===a?()=>({value:W(n(),r),inspect:e(r)}):"is"===a?e=>s(r).some(t=>0!==(t.operation&e)):"draft"===a?()=>t.head(s(r))?.value??W(n(),r):"settled"===a?()=>new Promise(e=>{if(t.isEmpty(s(r)))return e(W(n(),r));const o=()=>{t.isEmpty(s(r))&&(i(o),e(W(n(),r)))};c(o)}):e([...r,String(a)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#c(U.Hydrate,t=>Object.assign(t,e))}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#c(U.Produce,e)}#c(e,t){const n=/* @__PURE__ */Symbol("process"),[,r]=F.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>F.immer.applyPatches(t,[{...r,value:G(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=D(this.#e),this.#i(),n}prune(e){this.#n.forEach((n,r)=>{const o=n.filter(t=>t.process!==e);t.isEmpty(o)?this.#n.delete(r):this.#n.set(r,o)}),this.#i()}#i(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}function J(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var K,Q={exports:{}};const X=/* @__PURE__ */J((K||(K=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function c(e,t,r,c,i){if("function"!=typeof r)throw new TypeError("The listener must be a function");var s=new o(r,c||e,i),a=n?n+t:t;return e._events[a]?e._events[a].fn?e._events[a]=[e._events[a],s]:e._events[a].push(s):(e._events[a]=s,e._eventsCount++),e}function i(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function s(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=/* @__PURE__ */Object.create(null),(new r).__proto__||(n=!1)),s.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},s.prototype.listeners=function(e){var t=this._events[n?n+e:e];if(!t)return[];if(t.fn)return[t.fn];for(var r=0,o=t.length,c=new Array(o);r<o;r++)c[r]=t[r].fn;return c},s.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},s.prototype.emit=function(e,t,r,o,c,i){var s=n?n+e:e;if(!this._events[s])return!1;var a,u,l=this._events[s],f=arguments.length;if(l.fn){switch(l.once&&this.removeListener(e,l.fn,void 0,!0),f){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,t),!0;case 3:return l.fn.call(l.context,t,r),!0;case 4:return l.fn.call(l.context,t,r,o),!0;case 5:return l.fn.call(l.context,t,r,o,c),!0;case 6:return l.fn.call(l.context,t,r,o,c,i),!0}for(u=1,a=new Array(f-1);u<f;u++)a[u-1]=arguments[u];l.fn.apply(l.context,a)}else{var d,h=l.length;for(u=0;u<h;u++)switch(l[u].once&&this.removeListener(e,l[u].fn,void 0,!0),f){case 1:l[u].fn.call(l[u].context);break;case 2:l[u].fn.call(l[u].context,t);break;case 3:l[u].fn.call(l[u].context,t,r);break;case 4:l[u].fn.call(l[u].context,t,r,o);break;default:if(!a)for(d=1,a=new Array(f-1);d<f;d++)a[d-1]=arguments[d];l[u].fn.apply(l[u].context,a)}}return!0},s.prototype.on=function(e,t,n){return c(this,e,t,n,!1)},s.prototype.once=function(e,t,n){return c(this,e,t,n,!0)},s.prototype.removeListener=function(e,t,r,o){var c=n?n+e:e;if(!this._events[c])return this;if(!t)return i(this,c),this;var s=this._events[c];if(s.fn)s.fn!==t||o&&!s.once||r&&s.context!==r||i(this,c);else{for(var a=0,u=[],l=s.length;a<l;a++)(s[a].fn!==t||o&&!s[a].once||r&&s[a].context!==r)&&u.push(s[a]);u.length?this._events[c]=1===u.length?u[0]:u:i(this,c)}return this},s.prototype.removeAllListeners=function(e){var t;return e?this._events[t=n?n+e:e]&&i(this,t):(this._events=new r,this._eventsCount=0),this},s.prototype.off=s.prototype.removeListener,s.prototype.addListener=s.prototype.on,s.prefixed=n,s.EventEmitter=s,e.exports=s}(Q)),Q.exports)),Y=r.createContext(new X);function Z(){return r.useContext(Y)}function ee({children:e}){const t=r.useMemo(()=>new X,[]);/* @__PURE__ */
|
|
3
|
+
return n(Y.Provider,{value:t,children:e})}const te=r.createContext(/* @__PURE__ */new Map);function ne(){return r.useContext(te)}function re(){const[,e]=r.useReducer(e=>e+1,0);return e}function oe({action:t,renderer:n}){const o=Z(),c=ne(),i=re(),s=r.useMemo(()=>{const e=c.get(t);if(e)return e;const n={state:new q,listeners:/* @__PURE__ */new Set};return c.set(t,n),n},[t,c]);r.useLayoutEffect(()=>{function e(e){s.state.hydrate({value:e}),s.listeners.forEach(e=>e())}return s.listeners.add(i),o.on(t,e),()=>{s.listeners.delete(i),o.off(t,e)}},[t,o,s]);const a=s.state.model?.value;return e.isNullable(a)?null:n({value:a,inspect:s.state.inspect.value})}function ce({children:e}){const t=r.useMemo(()=>/* @__PURE__ */new Map,[]);/* @__PURE__ */
|
|
4
|
+
return n(te.Provider,{value:t,children:e})}const ie=r.createContext(/* @__PURE__ */new Map);function se({children:e}){const t=r.useMemo(()=>/* @__PURE__ */new Map,[]);/* @__PURE__ */
|
|
5
|
+
return n(ie.Provider,{value:t,children:e})}const ae=r.createContext(/* @__PURE__ */new Set);function ue({children:e}){const t=r.useMemo(()=>/* @__PURE__ */new Set,[]);/* @__PURE__ */
|
|
6
|
+
return n(ae.Provider,{value:t,children:e})}function le({children:e}){/* @__PURE__ */
|
|
7
|
+
return n(ee,{children:/* @__PURE__ */n(ce,{children:/* @__PURE__ */n(se,{children:/* @__PURE__ */n(ue,{children:e})})})})}const fe=r.createContext(null);function de(){return r.useContext(fe)}function he(e,t){return e?.get(t)??null}function pe({action:t,scopeName:n,renderer:o}){const c=de(),i=re(),s=r.useMemo(()=>he(c,n),[c,n]),a=r.useMemo(()=>s?function(e,t){const n=e.store.get(t);if(n)return n;const r={state:new q,listeners:/* @__PURE__ */new Set};return e.store.set(t,r),r}(s,t):null,[t,s]);if(r.useLayoutEffect(()=>{if(s&&a)return a.listeners.add(i),s.emitter.on(t,e),()=>{a.listeners.delete(i),s.emitter.off(t,e)};function e(e){a&&(a.state.hydrate({value:e}),a.listeners.forEach(e=>e()))}},[t,s,a,i]),!a)return null;const u=a.state.model?.value;return e.isNullable(u)?null:o({value:u,inspect:a.state.inspect.value})}function me({name:e,children:t}){const o=de(),c=r.useMemo(()=>({name:e,emitter:new X,store:/* @__PURE__ */new Map,listeners:/* @__PURE__ */new Map}),[]),i=r.useMemo(()=>{const t=new Map(o??[]);return t.set(e,c),t},[o,e,c]);/* @__PURE__ */
|
|
8
|
+
return n(fe.Provider,{value:i,children:t})}function ye(e){return(t,n)=>{t.actions.produce(t=>{t.model[e]=n})}}function be(n,o=()=>({})){const i=Z(),s=de(),a=c(R),f=r.useContext(ae),h=r.useContext(ie),p=r.useMemo(()=>({...n}),[]),[m,y]=r.useState(p),b=re(),v=r.useRef(null),g=r.useRef((()=>{const e=new q;return v.current=e.hydrate(p),e})()),A=function(e){const t=r.useRef(e);return r.useLayoutEffect(()=>{t.current=e},[e]),r.useMemo(()=>{return n=t,Object.keys(e).reduce((e,t)=>(Object.defineProperty(e,t,{get:()=>n.current[t],enumerable:!0}),e),{});var n},[e])}(o()),_=r.useMemo(()=>new X,[]),z=r.useRef({handlers:/* @__PURE__ */new Map}),L=function(){const e=r.useRef(/* @__PURE__ */new Set),t=r.useRef(/* @__PURE__ */new Set);return r.useMemo(()=>({broadcast:e.current,multicast:t.current}),[])}(),T=r.useRef(d.Mounting),U=function(){const e=r.useRef({}),t=r.useRef(/* @__PURE__ */new Map),n=r.useRef(/* @__PURE__ */new Map);return r.useMemo(()=>({refs:e,pending:t,emitted:n}),[])}(),$=r.useRef(/* @__PURE__ */new Set),B=r.useRef(0),F=r.useCallback((t,n,r)=>{const o=new AbortController,c={controller:o,action:t,payload:n};return f.add(c),$.current.add(c),{model:m,get phase(){return T.current},task:c,data:A,tasks:f,nodes:U.refs.current,actions:{produce(e){if(o.signal.aborted)return;const t=g.current.produce(t=>e({model:t,inspect:g.current.inspect}));y(g.current.model),r.processes.add(t),v.current&&(r.processes.add(v.current),v.current=null)},dispatch(e,t,n){if(o.signal.aborted)return;const r=w(e),c=O(e)?e.channel:void 0;if(S(e)&&n?.scope){const e=he(s,n.scope);return void(e&&e.emitter.emit(r,t,c))}(x(e)?i:_).emit(r,t,c)},annotate:(e,t)=>g.current.annotate(e,t),cache:{put(t,n){const r=E(t),o=function(t){if((e.isObject(t)||e.isFunction(t))&&u.TTL in t)return t[u.TTL];throw new Error("Invalid cache operation")}(t),c=j(t)?M(t.channel):"",i=h.get(r);if(i){const e=i.get(c);if(e&&e.expiresAt>Date.now())return e.value}return n(e=>(h.has(r)||h.set(r,/* @__PURE__ */new Map),h.get(r).set(c,{value:e,expiresAt:Date.now()+o,channel:j(t)?t.channel:void 0}),e))},get(e){const t=E(e),n=j(e)?M(e.channel):"",r=h.get(t)?.get(n);return r&&r.expiresAt>Date.now()?r.value:void 0},delete(e){const t=E(e);if(j(e)){const n=e.channel,r=h.get(t);if(r)for(const[e,t]of r)t.channel&&C(n,t.channel)&&r.delete(e)}else h.delete(t)}}}}},[m]);r.useLayoutEffect(()=>{function t(t,n,r){return async function(o,c){const i=r();if(e.isNotNullable(c)&&e.isNotNullable(i)&&!function(e,t){for(const n of Object.keys(e))if(t[n]!==e[n])return!1;return!0}(c,i))return;const s={processes:/* @__PURE__ */new Set},u=Promise.withResolvers(),d=F(t,o,s);try{await n(d,o)}catch(h){const e=z.current.handlers.has(l.Error),n={reason:N(h),error:k(h),action:P(t),handled:e,tasks:f};a?.(n),e&&_.emit(l.Error,n)}finally{for(const e of f)if(e===d.task){f.delete(e),$.current.delete(e);break}s.processes.forEach(e=>g.current.prune(e)),s.processes.size>0&&b(),u.resolve()}}}B.current++;const n=/* @__PURE__ */new Set;return z.current.handlers.forEach((e,r)=>{for(const{getChannel:o,handler:c}of e){const e=t(r,c,o);if(S(r)){if(s)for(const t of s.values()){const o=t.emitter;o.on(r,e),n.add(()=>o.off(r,e))}_.on(r,e),L.multicast.add(r),n.add(()=>_.off(r,e))}else x(r)?(i.on(r,e),_.on(r,e),L.broadcast.add(r),n.add(()=>{i.off(r,e),_.off(r,e)})):(_.on(r,e),n.add(()=>_.off(r,e)))}}),()=>{const e=++B.current,t=new Set(n);queueMicrotask(()=>{if(B.current===e){for(const e of $.current)e.controller.abort(),f.delete(e);$.current.clear(),T.current=d.Unmounting,_.emit(l.Unmount),T.current=d.Unmounted;for(const e of t)e()}else for(const e of t)e()})}},[_]),r.useLayoutEffect(()=>{for(const[e,t]of U.pending.current)U.emitted.current.get(e)!==t&&(U.emitted.current.set(e,t),_.emit(u.Node,t,{Name:e}));U.pending.current.clear()}),function({unicast:n,broadcastActions:o,phase:c,data:i}){const s=ne(),a=r.useRef(null),u=r.useRef(!1);r.useLayoutEffect(()=>{u.current||(u.current=!0,n.emit(l.Mount),o.forEach(t=>{const r=s.get(t),o=r?.state.model?.value;e.isNullable(o)||n.emit(t,o)}),c.current=d.Mounted)},[]),r.useLayoutEffect(()=>{if(e.isNotNullable(a.current)){const e=function(e,t){return Object.keys(t).reduce((n,r)=>e[r]!==t[r]?{...n,[r]:t[r]}:n,{})}(a.current,i);t.isNotEmpty(Object.keys(e))&&n.emit(l.Update,e)}a.current=i},[i,n])}({unicast:_,broadcastActions:L.broadcast,phase:T,data:o()});const W=r.useMemo(()=>[m,{dispatch(e,t,n){const r=w(e),o=O(e)?e.channel:void 0;if(S(e)&&n?.scope){const e=he(s,n.scope);return void(e&&e.emitter.emit(r,t,o))}(x(e)?i:_).emit(r,t,o)},consume:(e,t,n)=>S(e)&&n?.scope?r.createElement(pe,{action:w(e),scopeName:n.scope,renderer:t}):r.createElement(oe,{action:w(e),renderer:t}),get inspect(){return g.current.inspect},get nodes(){return U.refs.current},node(e,t){U.refs.current[e]=t,U.pending.current.set(e,t)}}],[m,_]);return W.useAction=(e,t)=>{!function(e,t,n){const o=r.useRef(n);r.useLayoutEffect(()=>{o.current=n});const c=r.useRef(t);r.useLayoutEffect(()=>{c.current=t});const i=r.useCallback(async(e,t)=>{const n=o.current;if("GeneratorFunction"===n.constructor.name||"AsyncGeneratorFunction"===n.constructor.name){const r=n(e,t);for await(const e of r);}else await n(e,t)},[]),s=r.useCallback(()=>O(c.current)?c.current.channel:void 0,[]),a=w(t),u=e.current.handlers.get(a)??/* @__PURE__ */new Set;0===u.size&&e.current.handlers.set(a,u),u.add({getChannel:s,handler:i})}(z,e,t)},W}export{A as Action,le as Boundary,_ as Cache,f as Distribution,z as Error,l as Lifecycle,T as Op,T as Operation,h as Reason,me as Scope,q as State,ye as With,be as useActions,v as utils};
|
package/dist/chizu.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var global,factory;global=this,factory=function(e,t,n,r,o){"use strict";function i(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e)for(const n in e)if("default"!==n){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}return t.default=e,Object.freeze(t)}const s=i(r);class c{static Payload=Symbol("chizu.brand/Payload");static Broadcast=Symbol("chizu.brand/Broadcast");static Multicast=Symbol("chizu.brand/Multicast");static Action=Symbol("chizu.brand/Action");static Channel=Symbol("chizu.brand/Channel");static Node=Symbol("chizu.action.lifecycle/Node")}class a{static Mount=Symbol("chizu.action.lifecycle/Mount");static Unmount=Symbol("chizu.action.lifecycle/Unmount");static Error=Symbol("chizu.action.lifecycle/Error");static Update=Symbol("chizu.action.lifecycle/Update");static Node=(()=>{const e=c.Node,t=function(t){return{[c.Action]:e,[c.Payload]:void 0,[c.Channel]:t,channel:t}};return Object.defineProperty(t,c.Action,{value:e,enumerable:!1}),Object.defineProperty(t,c.Payload,{value:void 0,enumerable:!1}),t})()}var u=(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(u||{}),l=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(l||{}),f=(e=>(e[e.Timedout=0]="Timedout",e[e.Supplanted=1]="Supplanted",e[e.Disallowed=2]="Disallowed",e[e.Errored=3]="Errored",e[e.Unmounted=4]="Unmounted",e))(f||{});class d extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const p={actionPrefix:"chizu.action/",broadcastActionPrefix:"chizu.action/broadcast/",multicastActionPrefix:"chizu.action/multicast/",channelPrefix:"chizu.channel/"};function h(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new d);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new d)},{once:!0})})}function m(e){return e?Boolean(e&&"symbol"!=typeof e):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const y=Object.freeze(Object.defineProperty({__proto__:null,config:p,pk:m,sleep:h,"ζ":h,"κ":m},Symbol.toStringTag,{value:"Module"})),b=e=>"symbol"==typeof e;function v(e){return t.G.isString(e)||b(e)?e:(t.G.isObject(e)||t.G.isFunction(e))&&c.Action in e?e[c.Action]:e}function g(e){if(t.G.isString(e))return e.startsWith(p.broadcastActionPrefix);if(b(e))return e.description?.startsWith(p.broadcastActionPrefix)??!1;if(t.G.isObject(e)||t.G.isFunction(e)){if(c.Broadcast in e&&e[c.Broadcast])return!0;if(c.Action in e){const t=e[c.Action];return t.description?.startsWith(p.broadcastActionPrefix)??!1}}return!1}function w(e){const n=v(e),r=t.G.isString(n)?n:n.description??"";return r.startsWith(p.actionPrefix)&&r.slice(r.lastIndexOf("/")+1)||"unknown"}function x(e){return t.G.isObject(e)&&c.Channel in e&&"channel"in e}function j(e){if(t.G.isString(e))return e.startsWith(p.multicastActionPrefix);if(b(e))return e.description?.startsWith(p.multicastActionPrefix)??!1;if(t.G.isObject(e)||t.G.isFunction(e)){if(c.Multicast in e&&e[c.Multicast])return!0;if(c.Action in e){const t=e[c.Action];return t.description?.startsWith(p.multicastActionPrefix)??!1}}return!1}function A(e){if(e instanceof Error){if("TimeoutError"===e.name)return f.Timedout;if("AbortError"===e.name)return f.Supplanted}return f.Errored}function P(e){return e instanceof Error?e:new Error(String(e))}const S=r.createContext(void 0);let O=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var E=(e=>(e[e.Add=1]="Add",e[e.Remove=2]="Remove",e[e.Update=4]="Update",e[e.Move=8]="Move",e[e.Replace=16]="Replace",e[e.Sort=32]="Sort",e))(E||{}),M=(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(M||{}),G=(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(G||{});class _{[o.immerable]=!0;static keys=new Set(Object.values(G));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new _(this.value,this.operation);return n.property=e,n.process=t,n}}class C{static immer=(()=>{o.enablePatches();const e=new o.Immer;return e.setAutoFreeze(!1),e})();static tag="κ";static id=O}function N(e,t){const n="string"==typeof t?""===t?[]:t.split("."):t;let r=e;for(const o of n){if(null==r)return;r=r[o]}return r}function k(e){if(t.G.isNullable(e)||z(e))return e;if(t.G.isArray(e))return e.map(e=>k(e));if(t.G.isObject(e)){const t=Object.entries(e).map(([e,t])=>[e,k(t)]);return{...Object.fromEntries(t),[C.tag]:e[C.tag]??C.id()}}return e}function R(e){if(Array.isArray(e))return e.filter(e=>C.tag in e).map(e=>e[C.tag]??"").join(",");const t=e[C.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function z(e){return t.G.isNullable(e)||t.G.isString(e)||t.G.isNumber(e)||t.G.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function U(e,n,r,o,i,s){return function c(a,u=n.path){if(a instanceof _){const n=N(r,u.join("."));if(Object.entries(a).filter(([e,t])=>!_.keys.has(e)&&t instanceof _).forEach(([e,t])=>c(t,u.concat(e))),z(a.value)){if(e===M.Hydrate)return a.value;const c=u.slice(0,-1),l=c.length>0?N(r,c.join(".")):r;return t.G.isNullable(l)||L(l,a,u.at(-1),o,i,s),n??a.value}if(e===M.Hydrate){const e=k(c(a.value,u));return L(e,a,null,o,i,s),e}const l=n??k(a.value);return L(l,a,null,o,i,s),t.G.isNullable(n)?l:(c(a.value,u),n)}if(t.G.isArray(a))return a.map((e,t)=>c(e,u.concat(t)));if(t.G.isObject(a)){const t=Object.entries(a).map(([e,t])=>[e,c(t,u.concat(e))]),n=Object.fromEntries(t);if(e===M.Hydrate){const e=k(n);return Object.entries(a).forEach(([t,n])=>{n instanceof _&&z(n.value)&&L(e,n,t,o,i,s)}),e}return n}return a}(n.value)}function L(e,t,n,r,o,i){const s=i(e),c=o.get(s)??[];o.set(s,[t.assign(n,r),...c])}class T{#e={};#t;#n=new Map;#r=new Set;#o=!1;constructor(e=R){this.#t=e}static pk(){return O()}static"κ"=T.pk;annotate(e,t){return new _(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(e,n,r,o,i){function s(o){const i=o.at(-1),s=N(e(),o),c=o.slice(0,-1),a=t.A.isNotEmpty(c)?N(e(),c):e();return[...t.G.isObject(s)||t.G.isArray(s)?n.get(r(s))?.filter(e=>t.G.isNullable(e.property))??[]:[],...t.G.isObject(a)?n.get(r(a))?.filter(e=>e.property===i)??[]:[]]}return function n(r){return new Proxy(()=>{},{get:(c,a)=>"pending"===a?()=>!t.A.isEmpty(s(r)):"remaining"===a?()=>t.A.length(s(r)):"box"===a?()=>({value:N(e(),r),inspect:n(r)}):"is"===a?e=>s(r).some(t=>0!==(t.operation&e)):"draft"===a?()=>t.A.head(s(r))?.value??N(e(),r):"settled"===a?()=>new Promise(n=>{if(t.A.isEmpty(s(r)))return n(N(e(),r));const c=()=>{t.A.isEmpty(s(r))&&(i(c),n(N(e(),r)))};o(c)}):n([...r,String(a)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#i(M.Hydrate,t=>Object.assign(t,e))}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#i(M.Produce,e)}#i(e,t){const n=Symbol("process"),[,r]=C.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>C.immer.applyPatches(t,[{...r,value:U(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=k(this.#e),this.#s(),n}prune(e){this.#n.forEach((n,r)=>{const o=n.filter(t=>t.process!==e);t.A.isEmpty(o)?this.#n.delete(r):this.#n.set(r,o)}),this.#s()}#s(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}function B(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var W,$={exports:{}},D=(W||(W=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function i(e,t,r,i,s){if("function"!=typeof r)throw new TypeError("The listener must be a function");var c=new o(r,i||e,s),a=n?n+t:t;return e._events[a]?e._events[a].fn?e._events[a]=[e._events[a],c]:e._events[a].push(c):(e._events[a]=c,e._eventsCount++),e}function s(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function c(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),c.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},c.prototype.listeners=function(e){var t=this._events[n?n+e:e];if(!t)return[];if(t.fn)return[t.fn];for(var r=0,o=t.length,i=new Array(o);r<o;r++)i[r]=t[r].fn;return i},c.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},c.prototype.emit=function(e,t,r,o,i,s){var c=n?n+e:e;if(!this._events[c])return!1;var a,u,l=this._events[c],f=arguments.length;if(l.fn){switch(l.once&&this.removeListener(e,l.fn,void 0,!0),f){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,t),!0;case 3:return l.fn.call(l.context,t,r),!0;case 4:return l.fn.call(l.context,t,r,o),!0;case 5:return l.fn.call(l.context,t,r,o,i),!0;case 6:return l.fn.call(l.context,t,r,o,i,s),!0}for(u=1,a=new Array(f-1);u<f;u++)a[u-1]=arguments[u];l.fn.apply(l.context,a)}else{var d,p=l.length;for(u=0;u<p;u++)switch(l[u].once&&this.removeListener(e,l[u].fn,void 0,!0),f){case 1:l[u].fn.call(l[u].context);break;case 2:l[u].fn.call(l[u].context,t);break;case 3:l[u].fn.call(l[u].context,t,r);break;case 4:l[u].fn.call(l[u].context,t,r,o);break;default:if(!a)for(d=1,a=new Array(f-1);d<f;d++)a[d-1]=arguments[d];l[u].fn.apply(l[u].context,a)}}return!0},c.prototype.on=function(e,t,n){return i(this,e,t,n,!1)},c.prototype.once=function(e,t,n){return i(this,e,t,n,!0)},c.prototype.removeListener=function(e,t,r,o){var i=n?n+e:e;if(!this._events[i])return this;if(!t)return s(this,i),this;var c=this._events[i];if(c.fn)c.fn!==t||o&&!c.once||r&&c.context!==r||s(this,i);else{for(var a=0,u=[],l=c.length;a<l;a++)(c[a].fn!==t||o&&!c[a].once||r&&c[a].context!==r)&&u.push(c[a]);u.length?this._events[i]=1===u.length?u[0]:u:s(this,i)}return this},c.prototype.removeAllListeners=function(e){var t;return e?this._events[t=n?n+e:e]&&s(this,t):(this._events=new r,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=n,c.EventEmitter=c,e.exports=c}($)),$.exports);const F=B(D),H=s.createContext(new F);function q(){return s.useContext(H)}function I({children:e}){const t=s.useMemo(()=>new F,[]);return n.jsx(H.Provider,{value:t,children:e})}const V=s.createContext(new Map);function J(){return s.useContext(V)}function K(){const[,e]=s.useReducer(e=>e+1,0);return e}function Q({action:e,renderer:n}){const r=q(),o=J(),i=K(),c=s.useMemo(()=>{const t=o.get(e);if(t)return t;const n={state:new T,listeners:new Set};return o.set(e,n),n},[e,o]);s.useLayoutEffect(()=>{function t(e){c.state.hydrate({value:e}),c.listeners.forEach(e=>e())}return c.listeners.add(i),r.on(e,t),()=>{c.listeners.delete(i),r.off(e,t)}},[e,r,c]);const a=c.state.model?.value;return t.G.isNullable(a)?null:n({value:a,inspect:c.state.inspect.value})}function X({children:e}){const t=s.useMemo(()=>new Map,[]);return n.jsx(V.Provider,{value:t,children:e})}const Y=s.createContext(new Set);function Z({children:e}){const t=s.useMemo(()=>new Set,[]);return n.jsx(Y.Provider,{value:t,children:e})}const ee=s.createContext(null);function te(){return s.useContext(ee)}function ne(e,t){return e?.get(t)??null}function re({action:e,scopeName:n,renderer:r}){const o=te(),i=K(),c=s.useMemo(()=>ne(o,n),[o,n]),a=s.useMemo(()=>c?function(e,t){const n=e.store.get(t);if(n)return n;const r={state:new T,listeners:new Set};return e.store.set(t,r),r}(c,e):null,[e,c]);if(s.useLayoutEffect(()=>{if(c&&a)return a.listeners.add(i),c.emitter.on(e,t),()=>{a.listeners.delete(i),c.emitter.off(e,t)};function t(e){a&&(a.state.hydrate({value:e}),a.listeners.forEach(e=>e()))}},[e,c,a,i]),!a)return null;const u=a.state.model?.value;return t.G.isNullable(u)?null:r({value:u,inspect:a.state.inspect.value})}e.Action=(e,t=u.Unicast)=>{const n=t===u.Broadcast?Symbol(`${p.broadcastActionPrefix}${e}`):t===u.Multicast?Symbol(`${p.multicastActionPrefix}${e}`):Symbol(`${p.actionPrefix}${e}`),r=function(e){return{[c.Action]:n,[c.Payload]:void 0,[c.Channel]:e,channel:e}};return Object.defineProperty(r,c.Action,{value:n,enumerable:!1}),Object.defineProperty(r,c.Payload,{value:void 0,enumerable:!1}),t===u.Broadcast&&Object.defineProperty(r,c.Broadcast,{value:!0,enumerable:!1}),t===u.Multicast&&Object.defineProperty(r,c.Multicast,{value:!0,enumerable:!1}),r},e.Boundary=function({children:e}){return n.jsx(I,{children:n.jsx(X,{children:n.jsx(Z,{children:e})})})},e.Distribution=u,e.Error=function({handler:e,children:t}){return n.jsx(S.Provider,{value:e,children:t})},e.Lifecycle=a,e.Op=E,e.Operation=E,e.Reason=f,e.Scope=function({name:e,children:t}){const r=te(),o=s.useMemo(()=>({name:e,emitter:new F,store:new Map,listeners:new Map}),[]),i=s.useMemo(()=>{const t=new Map(r??[]);return t.set(e,o),t},[r,e,o]);return n.jsx(ee.Provider,{value:i,children:t})},e.State=T,e.With=function(e){return(t,n)=>{t.actions.produce(t=>{t.model[e]=n})}},e.useActions=function(e,n=()=>({})){const o=q(),i=te(),u=r.useContext(S),f=s.useContext(Y),[d,p]=s.useState(e),h=K(),m=s.useRef(null),y=s.useRef((()=>{const t=new T;return m.current=t.hydrate(e),t})()),b=function(e){const t=s.useRef(e);return s.useLayoutEffect(()=>{t.current=e},[e]),s.useMemo(()=>{return n=t,Object.keys(e).reduce((e,t)=>(Object.defineProperty(e,t,{get:()=>n.current[t],enumerable:!0}),e),{});var n},[e])}(n()),O=s.useMemo(()=>new F,[]),E=s.useRef({handlers:new Map}),M=function(){const e=s.useRef(new Set),t=s.useRef(new Set);return s.useMemo(()=>({broadcast:e.current,multicast:t.current}),[])}(),G=s.useRef(l.Mounting),_=function(){const e=s.useRef({}),t=s.useRef(new Map),n=s.useRef(new Map);return s.useMemo(()=>({refs:e,pending:t,emitted:n}),[])}(),C=s.useCallback((e,t,n)=>{const r=new AbortController,s={controller:r,action:e,payload:t};return f.add(s),{model:d,get phase(){return G.current},task:s,data:b,tasks:f,nodes:_.refs.current,actions:{produce(e){if(r.signal.aborted)return;const t=y.current.produce(t=>e({model:t,inspect:y.current.inspect}));p(y.current.model),n.processes.add(t),m.current&&(n.processes.add(m.current),m.current=null)},dispatch(e,t,n){if(r.signal.aborted)return;const s=v(e),c=x(e)?e.channel:void 0;if(j(e)&&n?.scope){const e=ne(i,n.scope);return void(e&&e.emitter.emit(s,t,c))}(g(e)?o:O).emit(s,t,c)},annotate:(e,t)=>y.current.annotate(e,t)}}},[d]);s.useLayoutEffect(()=>{function e(e,n,r){return async function(o,i){const s=r();if(t.G.isNotNullable(i)&&t.G.isNotNullable(s)&&!function(e,t){for(const n of Object.keys(e))if(t[n]!==e[n])return!1;return!0}(i,s))return;const c={processes:new Set},l=Promise.withResolvers(),d=C(e,o,c);try{await n(d,o)}catch(p){const t=E.current.handlers.has(a.Error),n={reason:A(p),error:P(p),action:w(e),handled:t,tasks:f};u?.(n),t&&O.emit(a.Error,n)}finally{for(const e of f)if(e===d.task){f.delete(e);break}c.processes.forEach(e=>y.current.prune(e)),c.processes.size>0&&h(),l.resolve()}}}const n=new Set;return E.current.handlers.forEach((t,r)=>{for(const{getChannel:s,handler:c}of t){const t=e(r,c,s);if(j(r)){if(i)for(const e of i.values()){const o=e.emitter;o.on(r,t),n.add(()=>o.off(r,t))}O.on(r,t),M.multicast.add(r),n.add(()=>O.off(r,t))}else g(r)?(o.on(r,t),O.on(r,t),M.broadcast.add(r),n.add(()=>{o.off(r,t),O.off(r,t)})):(O.on(r,t),n.add(()=>O.off(r,t)))}}),()=>{G.current=l.Unmounting,O.emit(a.Unmount),G.current=l.Unmounted;for(const e of n)e()}},[O]),s.useLayoutEffect(()=>{for(const[e,t]of _.pending.current)_.emitted.current.get(e)!==t&&(_.emitted.current.set(e,t),O.emit(c.Node,t,{Name:e}));_.pending.current.clear()}),function({unicast:e,broadcastActions:n,phase:r,data:o}){const i=J(),c=s.useRef(null);s.useLayoutEffect(()=>{e.emit(a.Mount),n.forEach(n=>{const r=i.get(n),o=r?.state.model?.value;t.G.isNullable(o)||e.emit(n,o)}),r.current=l.Mounted},[]),s.useLayoutEffect(()=>{if(t.G.isNotNullable(c.current)){const n=function(e,t){return Object.keys(t).reduce((n,r)=>e[r]!==t[r]?{...n,[r]:t[r]}:n,{})}(c.current,o);t.A.isNotEmpty(Object.keys(n))&&e.emit(a.Update,n)}c.current=o},[o,e])}({unicast:O,broadcastActions:M.broadcast,phase:G,data:n()});const N=s.useMemo(()=>[d,{dispatch(e,t,n){const r=v(e),s=x(e)?e.channel:void 0;if(j(e)&&n?.scope){const e=ne(i,n.scope);return void(e&&e.emitter.emit(r,t,s))}(g(e)?o:O).emit(r,t,s)},consume:(e,t,n)=>j(e)&&n?.scope?s.createElement(re,{action:v(e),scopeName:n.scope,renderer:t}):s.createElement(Q,{action:v(e),renderer:t}),get inspect(){return y.current.inspect},get nodes(){return _.refs.current},node(e,t){_.refs.current[e]=t,_.pending.current.set(e,t)}}],[d,O]);return N.useAction=(e,t)=>{!function(e,t,n){const r=s.useRef(n);s.useLayoutEffect(()=>{r.current=n});const o=s.useRef(t);s.useLayoutEffect(()=>{o.current=t});const i=s.useCallback(async(e,t)=>{const n=r.current;if("GeneratorFunction"===n.constructor.name||"AsyncGeneratorFunction"===n.constructor.name){const r=n(e,t);for await(const e of r);}else await n(e,t)},[]),c=s.useCallback(()=>x(o.current)?o.current.channel:void 0,[]),a=v(t),u=e.current.handlers.get(a)??new Set;0===u.size&&e.current.handlers.set(a,u),u.add({getChannel:c,handler:i})}(E,e,t)},N},e.utils=y,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})},"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("@mobily/ts-belt"),require("react/jsx-runtime"),require("react"),require("immer")):"function"==typeof define&&define.amd?define(["exports","@mobily/ts-belt","react/jsx-runtime","react","immer"],factory):factory((global="undefined"!=typeof globalThis?globalThis:global||self).Chizu={},global.TsBelt,global.jsxRuntime,global.React,global.Immer);
|
|
1
|
+
var global,factory;global=this,factory=function(e,t,n,r,o){"use strict";function c(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e)for(const n in e)if("default"!==n){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}return t.default=e,Object.freeze(t)}const i=c(r);class s{static Payload=Symbol("chizu.brand/Payload");static Broadcast=Symbol("chizu.brand/Broadcast");static Multicast=Symbol("chizu.brand/Multicast");static Action=Symbol("chizu.brand/Action");static Channel=Symbol("chizu.brand/Channel");static Node=Symbol("chizu.action.lifecycle/Node");static Cache=Symbol("chizu.brand/Cache");static TTL=Symbol("chizu.brand/TTL")}class a{static Mount=Symbol("chizu.action.lifecycle/Mount");static Unmount=Symbol("chizu.action.lifecycle/Unmount");static Error=Symbol("chizu.action.lifecycle/Error");static Update=Symbol("chizu.action.lifecycle/Update");static Node=(()=>{const e=s.Node,t=function(t){return{[s.Action]:e,[s.Payload]:void 0,[s.Channel]:t,channel:t}};return Object.defineProperty(t,s.Action,{value:e,enumerable:!1}),Object.defineProperty(t,s.Payload,{value:void 0,enumerable:!1}),t})()}var u=(e=>(e.Unicast="unicast",e.Broadcast="broadcast",e.Multicast="multicast",e))(u||{}),l=(e=>(e.Mounting="mounting",e.Mounted="mounted",e.Unmounting="unmounting",e.Unmounted="unmounted",e))(l||{}),f=(e=>(e[e.Timedout=0]="Timedout",e[e.Supplanted=1]="Supplanted",e[e.Disallowed=2]="Disallowed",e[e.Errored=3]="Errored",e[e.Unmounted=4]="Unmounted",e))(f||{});class d extends Error{name="AbortError";constructor(e="Aborted"){super(e)}}const h={actionPrefix:"chizu.action/",broadcastActionPrefix:"chizu.action/broadcast/",multicastActionPrefix:"chizu.action/multicast/",channelPrefix:"chizu.channel/",cachePrefix:"chizu.cache/"};function p(e,t){return new Promise((n,r)=>{if(t?.aborted)return void r(new d);const o=setTimeout(n,e);t?.addEventListener("abort",()=>{clearTimeout(o),r(new d)},{once:!0})})}function m(e){return e?Boolean(e&&"symbol"!=typeof e):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}const b=Object.freeze(Object.defineProperty({__proto__:null,config:h,pk:m,sleep:p,"ζ":p,"κ":m},Symbol.toStringTag,{value:"Module"})),y=e=>"symbol"==typeof e;function v(e){return t.G.isString(e)||y(e)?e:(t.G.isObject(e)||t.G.isFunction(e))&&s.Action in e?e[s.Action]:e}function g(e){if(t.G.isString(e))return e.startsWith(h.broadcastActionPrefix);if(y(e))return e.description?.startsWith(h.broadcastActionPrefix)??!1;if(t.G.isObject(e)||t.G.isFunction(e)){if(s.Broadcast in e&&e[s.Broadcast])return!0;if(s.Action in e){const t=e[s.Action];return t.description?.startsWith(h.broadcastActionPrefix)??!1}}return!1}function w(e){const n=v(e),r=t.G.isString(n)?n:n.description??"";return r.startsWith(h.actionPrefix)&&r.slice(r.lastIndexOf("/")+1)||"unknown"}function x(e){return t.G.isObject(e)&&s.Channel in e&&"channel"in e}function j(e){if(t.G.isString(e))return e.startsWith(h.multicastActionPrefix);if(y(e))return e.description?.startsWith(h.multicastActionPrefix)??!1;if(t.G.isObject(e)||t.G.isFunction(e)){if(s.Multicast in e&&e[s.Multicast])return!0;if(s.Action in e){const t=e[s.Action];return t.description?.startsWith(h.multicastActionPrefix)??!1}}return!1}function A(e){if((t.G.isObject(e)||t.G.isFunction(e))&&s.Action in e)return e[s.Action];throw new Error("Invalid cache operation")}function O(e){return function(e){return(t.G.isObject(e)||t.G.isFunction(e))&&s.Cache in e&&!0===e[s.Cache]}(e)&&t.G.isObject(e)&&s.Channel in e&&"channel"in e}function P(e){return e?[...Object.keys(e)].toSorted().map(t=>`${t}=${String(e[t])}`).join("&"):""}function S(e,t){return Object.keys(e).every(n=>t[n]===e[n])}function E(e){if(e instanceof Error){if("TimeoutError"===e.name)return f.Timedout;if("AbortError"===e.name)return f.Supplanted}return f.Errored}function M(e){return e instanceof Error?e:new Error(String(e))}const G=r.createContext(void 0);let C=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[63&n[e]];return t};var _=(e=>(e[e.Add=1]="Add",e[e.Remove=2]="Remove",e[e.Update=4]="Update",e[e.Move=8]="Move",e[e.Replace=16]="Replace",e[e.Sort=32]="Sort",e))(_||{}),N=(e=>(e[e.Produce=0]="Produce",e[e.Hydrate=1]="Hydrate",e))(N||{}),k=(e=>(e.Property="property",e.Process="process",e.Value="value",e.Operation="operation",e))(k||{});class R{[o.immerable]=!0;static keys=new Set(Object.values(k));property=null;process=null;value;operation;constructor(e,t){this.value=e,this.operation=t}assign(e,t){const n=new R(this.value,this.operation);return n.property=e,n.process=t,n}}class T{static immer=(()=>{o.enablePatches();const e=new o.Immer;return e.setAutoFreeze(!1),e})();static tag="κ";static id=C}function z(e,t){const n="string"==typeof t?""===t?[]:t.split("."):t;let r=e;for(const o of n){if(null==r)return;r=r[o]}return r}function L(e){if(t.G.isNullable(e)||B(e))return e;if(t.G.isArray(e))return e.map(e=>L(e));if(t.G.isObject(e)){const t=Object.entries(e).map(([e,t])=>[e,L(t)]);return{...Object.fromEntries(t),[T.tag]:e[T.tag]??T.id()}}return e}function U(e){if(Array.isArray(e))return e.filter(e=>T.tag in e).map(e=>e[T.tag]??"").join(",");const t=e[T.tag];if(t)return t;try{return JSON.stringify(e)}catch{return`[unserializable:${typeof e}]`}}function B(e){return t.G.isNullable(e)||t.G.isString(e)||t.G.isNumber(e)||t.G.isBoolean(e)||"symbol"==typeof e||"bigint"==typeof e}function $(e,n,r,o,c,i){return function s(a,u=n.path){if(a instanceof R){const n=z(r,u.join("."));if(Object.entries(a).filter(([e,t])=>!R.keys.has(e)&&t instanceof R).forEach(([e,t])=>s(t,u.concat(e))),B(a.value)){if(e===N.Hydrate)return a.value;const s=u.slice(0,-1),l=s.length>0?z(r,s.join(".")):r;return t.G.isNullable(l)||D(l,a,u.at(-1),o,c,i),n??a.value}if(e===N.Hydrate){const e=L(s(a.value,u));return D(e,a,null,o,c,i),e}const l=n??L(a.value);return D(l,a,null,o,c,i),t.G.isNullable(n)?l:(s(a.value,u),n)}if(t.G.isArray(a))return a.map((e,t)=>s(e,u.concat(t)));if(t.G.isObject(a)){const t=Object.entries(a).map(([e,t])=>[e,s(t,u.concat(e))]),n=Object.fromEntries(t);if(e===N.Hydrate){const e=L(n);return Object.entries(a).forEach(([t,n])=>{n instanceof R&&B(n.value)&&D(e,n,t,o,c,i)}),e}return n}return a}(n.value)}function D(e,t,n,r,o,c){const i=c(e),s=o.get(i)??[];o.set(i,[t.assign(n,r),...s])}class F{#e={};#t;#n=new Map;#r=new Set;#o=!1;constructor(e=U){this.#t=e}static pk(){return C()}static"κ"=F.pk;annotate(e,t){return new R(t,e)}"δ"=this.annotate;get model(){return this.#e}get inspect(){return function(e,n,r,o,c){function i(o){const c=o.at(-1),i=z(e(),o),s=o.slice(0,-1),a=t.A.isNotEmpty(s)?z(e(),s):e();return[...t.G.isObject(i)||t.G.isArray(i)?n.get(r(i))?.filter(e=>t.G.isNullable(e.property))??[]:[],...t.G.isObject(a)?n.get(r(a))?.filter(e=>e.property===c)??[]:[]]}return function n(r){return new Proxy(()=>{},{get:(s,a)=>"pending"===a?()=>!t.A.isEmpty(i(r)):"remaining"===a?()=>t.A.length(i(r)):"box"===a?()=>({value:z(e(),r),inspect:n(r)}):"is"===a?e=>i(r).some(t=>0!==(t.operation&e)):"draft"===a?()=>t.A.head(i(r))?.value??z(e(),r):"settled"===a?()=>new Promise(n=>{if(t.A.isEmpty(i(r)))return n(z(e(),r));const s=()=>{t.A.isEmpty(i(r))&&(c(s),n(z(e(),r)))};o(s)}):n([...r,String(a)])})}([])}(()=>this.#e,this.#n,this.#t,e=>this.#r.add(e),e=>this.#r.delete(e))}hydrate(e){return this.#o=!0,this.#c(N.Hydrate,t=>Object.assign(t,e))}produce(e){if(!this.#o)throw new Error("State must be hydrated using hydrate() before calling produce()");return this.#c(N.Produce,e)}#c(e,t){const n=Symbol("process"),[,r]=T.immer.produceWithPatches(this.#e,t);return this.#e=r.reduce((t,r)=>T.immer.applyPatches(t,[{...r,value:$(e,r,t,n,this.#n,this.#t)}]),this.#e),this.#e=L(this.#e),this.#i(),n}prune(e){this.#n.forEach((n,r)=>{const o=n.filter(t=>t.process!==e);t.A.isEmpty(o)?this.#n.delete(r):this.#n.set(r,o)}),this.#i()}#i(){this.#r.forEach(e=>e())}observe(e){const t=()=>e(this.#e);return this.#r.add(t),()=>this.#r.delete(t)}}function W(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var H,I={exports:{}},q=(H||(H=1,function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function c(e,t,r,c,i){if("function"!=typeof r)throw new TypeError("The listener must be a function");var s=new o(r,c||e,i),a=n?n+t:t;return e._events[a]?e._events[a].fn?e._events[a]=[e._events[a],s]:e._events[a].push(s):(e._events[a]=s,e._eventsCount++),e}function i(e,t){0===--e._eventsCount?e._events=new r:delete e._events[t]}function s(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),s.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},s.prototype.listeners=function(e){var t=this._events[n?n+e:e];if(!t)return[];if(t.fn)return[t.fn];for(var r=0,o=t.length,c=new Array(o);r<o;r++)c[r]=t[r].fn;return c},s.prototype.listenerCount=function(e){var t=this._events[n?n+e:e];return t?t.fn?1:t.length:0},s.prototype.emit=function(e,t,r,o,c,i){var s=n?n+e:e;if(!this._events[s])return!1;var a,u,l=this._events[s],f=arguments.length;if(l.fn){switch(l.once&&this.removeListener(e,l.fn,void 0,!0),f){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,t),!0;case 3:return l.fn.call(l.context,t,r),!0;case 4:return l.fn.call(l.context,t,r,o),!0;case 5:return l.fn.call(l.context,t,r,o,c),!0;case 6:return l.fn.call(l.context,t,r,o,c,i),!0}for(u=1,a=new Array(f-1);u<f;u++)a[u-1]=arguments[u];l.fn.apply(l.context,a)}else{var d,h=l.length;for(u=0;u<h;u++)switch(l[u].once&&this.removeListener(e,l[u].fn,void 0,!0),f){case 1:l[u].fn.call(l[u].context);break;case 2:l[u].fn.call(l[u].context,t);break;case 3:l[u].fn.call(l[u].context,t,r);break;case 4:l[u].fn.call(l[u].context,t,r,o);break;default:if(!a)for(d=1,a=new Array(f-1);d<f;d++)a[d-1]=arguments[d];l[u].fn.apply(l[u].context,a)}}return!0},s.prototype.on=function(e,t,n){return c(this,e,t,n,!1)},s.prototype.once=function(e,t,n){return c(this,e,t,n,!0)},s.prototype.removeListener=function(e,t,r,o){var c=n?n+e:e;if(!this._events[c])return this;if(!t)return i(this,c),this;var s=this._events[c];if(s.fn)s.fn!==t||o&&!s.once||r&&s.context!==r||i(this,c);else{for(var a=0,u=[],l=s.length;a<l;a++)(s[a].fn!==t||o&&!s[a].once||r&&s[a].context!==r)&&u.push(s[a]);u.length?this._events[c]=1===u.length?u[0]:u:i(this,c)}return this},s.prototype.removeAllListeners=function(e){var t;return e?this._events[t=n?n+e:e]&&i(this,t):(this._events=new r,this._eventsCount=0),this},s.prototype.off=s.prototype.removeListener,s.prototype.addListener=s.prototype.on,s.prefixed=n,s.EventEmitter=s,e.exports=s}(I)),I.exports);const V=W(q),J=i.createContext(new V);function K(){return i.useContext(J)}function Q({children:e}){const t=i.useMemo(()=>new V,[]);return n.jsx(J.Provider,{value:t,children:e})}const X=i.createContext(new Map);function Y(){return i.useContext(X)}function Z(){const[,e]=i.useReducer(e=>e+1,0);return e}function ee({action:e,renderer:n}){const r=K(),o=Y(),c=Z(),s=i.useMemo(()=>{const t=o.get(e);if(t)return t;const n={state:new F,listeners:new Set};return o.set(e,n),n},[e,o]);i.useLayoutEffect(()=>{function t(e){s.state.hydrate({value:e}),s.listeners.forEach(e=>e())}return s.listeners.add(c),r.on(e,t),()=>{s.listeners.delete(c),r.off(e,t)}},[e,r,s]);const a=s.state.model?.value;return t.G.isNullable(a)?null:n({value:a,inspect:s.state.inspect.value})}function te({children:e}){const t=i.useMemo(()=>new Map,[]);return n.jsx(X.Provider,{value:t,children:e})}const ne=i.createContext(new Map);function re({children:e}){const t=i.useMemo(()=>new Map,[]);return n.jsx(ne.Provider,{value:t,children:e})}const oe=i.createContext(new Set);function ce({children:e}){const t=i.useMemo(()=>new Set,[]);return n.jsx(oe.Provider,{value:t,children:e})}const ie=i.createContext(null);function se(){return i.useContext(ie)}function ae(e,t){return e?.get(t)??null}function ue({action:e,scopeName:n,renderer:r}){const o=se(),c=Z(),s=i.useMemo(()=>ae(o,n),[o,n]),a=i.useMemo(()=>s?function(e,t){const n=e.store.get(t);if(n)return n;const r={state:new F,listeners:new Set};return e.store.set(t,r),r}(s,e):null,[e,s]);if(i.useLayoutEffect(()=>{if(s&&a)return a.listeners.add(c),s.emitter.on(e,t),()=>{a.listeners.delete(c),s.emitter.off(e,t)};function t(e){a&&(a.state.hydrate({value:e}),a.listeners.forEach(e=>e()))}},[e,s,a,c]),!a)return null;const u=a.state.model?.value;return t.G.isNullable(u)?null:r({value:u,inspect:a.state.inspect.value})}e.Action=(e,t=u.Unicast)=>{const n=t===u.Broadcast?Symbol(`${h.broadcastActionPrefix}${e}`):t===u.Multicast?Symbol(`${h.multicastActionPrefix}${e}`):Symbol(`${h.actionPrefix}${e}`),r=function(e){return{[s.Action]:n,[s.Payload]:void 0,[s.Channel]:e,channel:e}};return Object.defineProperty(r,s.Action,{value:n,enumerable:!1}),Object.defineProperty(r,s.Payload,{value:void 0,enumerable:!1}),t===u.Broadcast&&Object.defineProperty(r,s.Broadcast,{value:!0,enumerable:!1}),t===u.Multicast&&Object.defineProperty(r,s.Multicast,{value:!0,enumerable:!1}),r},e.Boundary=function({children:e}){return n.jsx(Q,{children:n.jsx(te,{children:n.jsx(re,{children:n.jsx(ce,{children:e})})})})},e.Cache=e=>{const t=Symbol(`${h.cachePrefix}${e}`),n=function(n){return{[s.Action]:t,[s.Cache]:!0,[s.TTL]:e,[s.Channel]:n,channel:n}};return Object.defineProperty(n,s.Action,{value:t,enumerable:!1}),Object.defineProperty(n,s.Cache,{value:!0,enumerable:!1}),Object.defineProperty(n,s.TTL,{value:e,enumerable:!1}),n},e.Distribution=u,e.Error=function({handler:e,children:t}){return n.jsx(G.Provider,{value:e,children:t})},e.Lifecycle=a,e.Op=_,e.Operation=_,e.Reason=f,e.Scope=function({name:e,children:t}){const r=se(),o=i.useMemo(()=>({name:e,emitter:new V,store:new Map,listeners:new Map}),[]),c=i.useMemo(()=>{const t=new Map(r??[]);return t.set(e,o),t},[r,e,o]);return n.jsx(ie.Provider,{value:c,children:t})},e.State=F,e.With=function(e){return(t,n)=>{t.actions.produce(t=>{t.model[e]=n})}},e.useActions=function(e,n=()=>({})){const o=K(),c=se(),u=r.useContext(G),f=i.useContext(oe),d=i.useContext(ne),h=i.useMemo(()=>({...e}),[]),[p,m]=i.useState(h),b=Z(),y=i.useRef(null),C=i.useRef((()=>{const e=new F;return y.current=e.hydrate(h),e})()),_=function(e){const t=i.useRef(e);return i.useLayoutEffect(()=>{t.current=e},[e]),i.useMemo(()=>{return n=t,Object.keys(e).reduce((e,t)=>(Object.defineProperty(e,t,{get:()=>n.current[t],enumerable:!0}),e),{});var n},[e])}(n()),N=i.useMemo(()=>new V,[]),k=i.useRef({handlers:new Map}),R=function(){const e=i.useRef(new Set),t=i.useRef(new Set);return i.useMemo(()=>({broadcast:e.current,multicast:t.current}),[])}(),T=i.useRef(l.Mounting),z=function(){const e=i.useRef({}),t=i.useRef(new Map),n=i.useRef(new Map);return i.useMemo(()=>({refs:e,pending:t,emitted:n}),[])}(),L=i.useRef(new Set),U=i.useRef(0),B=i.useCallback((e,n,r)=>{const i=new AbortController,a={controller:i,action:e,payload:n};return f.add(a),L.current.add(a),{model:p,get phase(){return T.current},task:a,data:_,tasks:f,nodes:z.refs.current,actions:{produce(e){if(i.signal.aborted)return;const t=C.current.produce(t=>e({model:t,inspect:C.current.inspect}));m(C.current.model),r.processes.add(t),y.current&&(r.processes.add(y.current),y.current=null)},dispatch(e,t,n){if(i.signal.aborted)return;const r=v(e),s=x(e)?e.channel:void 0;if(j(e)&&n?.scope){const e=ae(c,n.scope);return void(e&&e.emitter.emit(r,t,s))}(g(e)?o:N).emit(r,t,s)},annotate:(e,t)=>C.current.annotate(e,t),cache:{put(e,n){const r=A(e),o=function(e){if((t.G.isObject(e)||t.G.isFunction(e))&&s.TTL in e)return e[s.TTL];throw new Error("Invalid cache operation")}(e),c=O(e)?P(e.channel):"",i=d.get(r);if(i){const e=i.get(c);if(e&&e.expiresAt>Date.now())return e.value}return n(t=>(d.has(r)||d.set(r,new Map),d.get(r).set(c,{value:t,expiresAt:Date.now()+o,channel:O(e)?e.channel:void 0}),t))},get(e){const t=A(e),n=O(e)?P(e.channel):"",r=d.get(t)?.get(n);return r&&r.expiresAt>Date.now()?r.value:void 0},delete(e){const t=A(e);if(O(e)){const n=e.channel,r=d.get(t);if(r)for(const[e,t]of r)t.channel&&S(n,t.channel)&&r.delete(e)}else d.delete(t)}}}}},[p]);i.useLayoutEffect(()=>{function e(e,n,r){return async function(o,c){const i=r();if(t.G.isNotNullable(c)&&t.G.isNotNullable(i)&&!function(e,t){for(const n of Object.keys(e))if(t[n]!==e[n])return!1;return!0}(c,i))return;const s={processes:new Set},l=Promise.withResolvers(),d=B(e,o,s);try{await n(d,o)}catch(h){const t=k.current.handlers.has(a.Error),n={reason:E(h),error:M(h),action:w(e),handled:t,tasks:f};u?.(n),t&&N.emit(a.Error,n)}finally{for(const e of f)if(e===d.task){f.delete(e),L.current.delete(e);break}s.processes.forEach(e=>C.current.prune(e)),s.processes.size>0&&b(),l.resolve()}}}U.current++;const n=new Set;return k.current.handlers.forEach((t,r)=>{for(const{getChannel:i,handler:s}of t){const t=e(r,s,i);if(j(r)){if(c)for(const e of c.values()){const o=e.emitter;o.on(r,t),n.add(()=>o.off(r,t))}N.on(r,t),R.multicast.add(r),n.add(()=>N.off(r,t))}else g(r)?(o.on(r,t),N.on(r,t),R.broadcast.add(r),n.add(()=>{o.off(r,t),N.off(r,t)})):(N.on(r,t),n.add(()=>N.off(r,t)))}}),()=>{const e=++U.current,t=new Set(n);queueMicrotask(()=>{if(U.current===e){for(const e of L.current)e.controller.abort(),f.delete(e);L.current.clear(),T.current=l.Unmounting,N.emit(a.Unmount),T.current=l.Unmounted;for(const e of t)e()}else for(const e of t)e()})}},[N]),i.useLayoutEffect(()=>{for(const[e,t]of z.pending.current)z.emitted.current.get(e)!==t&&(z.emitted.current.set(e,t),N.emit(s.Node,t,{Name:e}));z.pending.current.clear()}),function({unicast:e,broadcastActions:n,phase:r,data:o}){const c=Y(),s=i.useRef(null),u=i.useRef(!1);i.useLayoutEffect(()=>{u.current||(u.current=!0,e.emit(a.Mount),n.forEach(n=>{const r=c.get(n),o=r?.state.model?.value;t.G.isNullable(o)||e.emit(n,o)}),r.current=l.Mounted)},[]),i.useLayoutEffect(()=>{if(t.G.isNotNullable(s.current)){const n=function(e,t){return Object.keys(t).reduce((n,r)=>e[r]!==t[r]?{...n,[r]:t[r]}:n,{})}(s.current,o);t.A.isNotEmpty(Object.keys(n))&&e.emit(a.Update,n)}s.current=o},[o,e])}({unicast:N,broadcastActions:R.broadcast,phase:T,data:n()});const $=i.useMemo(()=>[p,{dispatch(e,t,n){const r=v(e),i=x(e)?e.channel:void 0;if(j(e)&&n?.scope){const e=ae(c,n.scope);return void(e&&e.emitter.emit(r,t,i))}(g(e)?o:N).emit(r,t,i)},consume:(e,t,n)=>j(e)&&n?.scope?i.createElement(ue,{action:v(e),scopeName:n.scope,renderer:t}):i.createElement(ee,{action:v(e),renderer:t}),get inspect(){return C.current.inspect},get nodes(){return z.refs.current},node(e,t){z.refs.current[e]=t,z.pending.current.set(e,t)}}],[p,N]);return $.useAction=(e,t)=>{!function(e,t,n){const r=i.useRef(n);i.useLayoutEffect(()=>{r.current=n});const o=i.useRef(t);i.useLayoutEffect(()=>{o.current=t});const c=i.useCallback(async(e,t)=>{const n=r.current;if("GeneratorFunction"===n.constructor.name||"AsyncGeneratorFunction"===n.constructor.name){const r=n(e,t);for await(const e of r);}else await n(e,t)},[]),s=i.useCallback(()=>x(o.current)?o.current.channel:void 0,[]),a=v(t),u=e.current.handlers.get(a)??new Set;0===u.size&&e.current.handlers.set(a,u),u.add({getChannel:s,handler:c})}(k,e,t)},$},e.utils=b,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})},"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("@mobily/ts-belt"),require("react/jsx-runtime"),require("react"),require("immer")):"function"==typeof define&&define.amd?define(["exports","@mobily/ts-belt","react/jsx-runtime","react","immer"],factory):factory((global="undefined"!=typeof globalThis?globalThis:global||self).Chizu={},global.TsBelt,global.jsxRuntime,global.React,global.Immer);
|
package/dist/hooks/utils.d.ts
CHANGED
|
@@ -13,10 +13,13 @@ export declare function withGetters<P extends Props>(a: P, b: RefObject<P>): P;
|
|
|
13
13
|
*/
|
|
14
14
|
export declare function isGenerator(result: unknown): result is Generator | AsyncGenerator;
|
|
15
15
|
/**
|
|
16
|
-
* Emits lifecycle events for component mount
|
|
16
|
+
* Emits lifecycle events for component mount and DOM attachment.
|
|
17
17
|
* Also invokes broadcast action handlers with cached values on mount.
|
|
18
18
|
* Updates the phase ref to track the component's current lifecycle state.
|
|
19
19
|
*
|
|
20
|
+
* Uses a ref guard to prevent React Strict Mode from causing duplicate
|
|
21
|
+
* Mount emissions during its development-only double-invocation cycle.
|
|
22
|
+
*
|
|
20
23
|
* Note: The phase transitions are:
|
|
21
24
|
* - Mounting → (cached broadcast action values emitted here) → Mounted
|
|
22
25
|
* - Mounted → Unmounting → Unmounted
|
package/dist/index.d.ts
CHANGED
package/dist/types/index.d.ts
CHANGED
|
@@ -56,6 +56,10 @@ export declare class Brand {
|
|
|
56
56
|
static Channel: symbol;
|
|
57
57
|
/** Node capture events used by Lifecycle.Node */
|
|
58
58
|
static Node: symbol;
|
|
59
|
+
/** Identifies cache operations created with Cache() */
|
|
60
|
+
static Cache: symbol;
|
|
61
|
+
/** TTL value for cache operations */
|
|
62
|
+
static TTL: symbol;
|
|
59
63
|
}
|
|
60
64
|
/**
|
|
61
65
|
* Lifecycle actions that trigger at specific points in a component's lifecycle.
|
|
@@ -275,6 +279,56 @@ export type BroadcastPayload<P = unknown, C extends Filter = never> = HandlerPay
|
|
|
275
279
|
* ```
|
|
276
280
|
*/
|
|
277
281
|
export type MulticastPayload<P = unknown, C extends Filter = never> = HandlerPayload<P, C> & {};
|
|
282
|
+
/**
|
|
283
|
+
* Branded type for cache operations created with `Cache()`.
|
|
284
|
+
* Cache operations identify cached async results and carry a TTL for expiration.
|
|
285
|
+
*
|
|
286
|
+
* When a channel type is specified, the cache operation becomes callable to
|
|
287
|
+
* create channeled cache keys for targeted lookups.
|
|
288
|
+
*
|
|
289
|
+
* @template C - The channel type for keyed cache entries (defaults to never)
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* export class CacheStore {
|
|
294
|
+
* static User = Cache<{ UserId: number }>(30_000); // 30s TTL, channeled
|
|
295
|
+
* static Config = Cache(60_000); // 60s TTL, unchanneled
|
|
296
|
+
* }
|
|
297
|
+
*
|
|
298
|
+
* // Channeled usage
|
|
299
|
+
* context.actions.cache.put(CacheStore.User({ UserId: 5 }), () => fetchUser(5));
|
|
300
|
+
* context.actions.cache.delete(CacheStore.User({ UserId: 5 }));
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
export type CachePayload<C extends Filter = never> = {} & ([C] extends [never] ? unknown : {
|
|
304
|
+
(channel: C): ChanneledCache<C>;
|
|
305
|
+
});
|
|
306
|
+
/**
|
|
307
|
+
* Result of calling a cache operation with a channel argument.
|
|
308
|
+
* Contains the cache reference and the channel data for keyed lookup.
|
|
309
|
+
*
|
|
310
|
+
* @template C - The channel type
|
|
311
|
+
*/
|
|
312
|
+
export type ChanneledCache<C = unknown> = {
|
|
313
|
+
readonly channel: C;
|
|
314
|
+
};
|
|
315
|
+
/**
|
|
316
|
+
* Union type representing either a plain or channeled cache operation.
|
|
317
|
+
* Used in `cache.put`, `cache.get`, and `cache.delete` signatures.
|
|
318
|
+
*/
|
|
319
|
+
export type AnyCacheOperation = CachePayload<Filter> | ChanneledCache;
|
|
320
|
+
/**
|
|
321
|
+
* Interface for the Cache factory function.
|
|
322
|
+
*/
|
|
323
|
+
export type CacheFactory = {
|
|
324
|
+
/**
|
|
325
|
+
* Creates a new cache operation with a TTL in milliseconds.
|
|
326
|
+
* @template C The channel type for keyed cache entries (defaults to never).
|
|
327
|
+
* @param ttl Time-to-live in milliseconds.
|
|
328
|
+
* @returns A typed cache operation object.
|
|
329
|
+
*/
|
|
330
|
+
<C extends Filter = never>(ttl: number): CachePayload<C>;
|
|
331
|
+
};
|
|
278
332
|
/**
|
|
279
333
|
* Options for multicast dispatch.
|
|
280
334
|
* Required when dispatching a multicast action.
|
|
@@ -501,6 +555,76 @@ export type HandlerContext<M extends Model, _AC extends Actions, D extends Props
|
|
|
501
555
|
}) => void>(ƒ: F & AssertSync<F>): void;
|
|
502
556
|
dispatch(action: ActionOrChanneled, payload?: unknown, options?: MulticastOptions): void;
|
|
503
557
|
annotate<T>(operation: Operation, value: T): T;
|
|
558
|
+
/**
|
|
559
|
+
* Cache operations for reading, writing, and deleting cached values.
|
|
560
|
+
*/
|
|
561
|
+
cache: {
|
|
562
|
+
/**
|
|
563
|
+
* Retrieves a value from cache if available and within TTL,
|
|
564
|
+
* otherwise executes the async function and caches the result.
|
|
565
|
+
*
|
|
566
|
+
* Returns `T` synchronously on cache hit, or `Promise<T>` on cache miss.
|
|
567
|
+
* Using `await` handles both cases uniformly.
|
|
568
|
+
*
|
|
569
|
+
* @param operation - The cache operation (plain or channeled).
|
|
570
|
+
* @param fn - Async function receiving a `cache` callback. Call `cache(value)` to store the value; if not called, nothing is cached.
|
|
571
|
+
* @returns The cached or freshly fetched value.
|
|
572
|
+
*
|
|
573
|
+
* @example
|
|
574
|
+
* ```ts
|
|
575
|
+
* const user = await context.actions.cache.put(
|
|
576
|
+
* CacheOps.User({ UserId: 5 }),
|
|
577
|
+
* async (cache) => {
|
|
578
|
+
* const response = await fetchUser(5);
|
|
579
|
+
* return response.error ? response : cache(response);
|
|
580
|
+
* },
|
|
581
|
+
* );
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
put<T>(operation: AnyCacheOperation, fn: (cache: (value: T) => T) => Promise<T>): T | Promise<T>;
|
|
585
|
+
/**
|
|
586
|
+
* Reads a cached value for a cache operation without triggering a fetch.
|
|
587
|
+
* Returns `undefined` if no entry exists or the entry has expired.
|
|
588
|
+
*
|
|
589
|
+
* @template T - The expected value type.
|
|
590
|
+
* @param operation - The cache operation (plain or channeled).
|
|
591
|
+
* @returns The cached value, or `undefined` if not found or expired.
|
|
592
|
+
*
|
|
593
|
+
* @example
|
|
594
|
+
* ```ts
|
|
595
|
+
* const user = context.actions.cache.get<User>(CacheStore.User({ UserId: 5 }));
|
|
596
|
+
* if (user) {
|
|
597
|
+
* context.actions.produce(({ model }) => {
|
|
598
|
+
* model.user = user;
|
|
599
|
+
* });
|
|
600
|
+
* }
|
|
601
|
+
* ```
|
|
602
|
+
*/
|
|
603
|
+
get<T>(operation: AnyCacheOperation): T | undefined;
|
|
604
|
+
/**
|
|
605
|
+
* Deletes cache entries for a cache operation.
|
|
606
|
+
*
|
|
607
|
+
* With a channeled operation, deletes all entries whose channel
|
|
608
|
+
* contains the specified keys (partial match). For example,
|
|
609
|
+
* `cache.delete(CacheOps.User({ UserId: 5 }))` removes entries for
|
|
610
|
+
* `{ UserId: 5 }`, `{ UserId: 5, Role: "admin" }`, etc.
|
|
611
|
+
*
|
|
612
|
+
* With a plain (unchanneled) operation, deletes all entries
|
|
613
|
+
* for that operation regardless of channel.
|
|
614
|
+
*
|
|
615
|
+
* @param operation - The cache operation (plain or channeled).
|
|
616
|
+
*
|
|
617
|
+
* @example
|
|
618
|
+
* ```ts
|
|
619
|
+
* // Delete specific cache entry (partial channel match)
|
|
620
|
+
* context.actions.cache.delete(CacheOps.User({ UserId: 5 }));
|
|
621
|
+
*
|
|
622
|
+
* // Delete ALL user cache entries
|
|
623
|
+
* context.actions.cache.delete(CacheOps.User);
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
delete(operation: AnyCacheOperation): void;
|
|
627
|
+
};
|
|
504
628
|
};
|
|
505
629
|
};
|
|
506
630
|
/**
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export declare const config: {
|
|
|
11
11
|
multicastActionPrefix: string;
|
|
12
12
|
/** Prefix for channeled action symbols. */
|
|
13
13
|
channelPrefix: string;
|
|
14
|
+
/** Prefix for cache operation symbols. */
|
|
15
|
+
cachePrefix: string;
|
|
14
16
|
};
|
|
15
17
|
/**
|
|
16
18
|
* Returns a promise that resolves after the specified number of milliseconds.
|