@akashjs/runtime 0.2.1 → 0.2.6

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.
Files changed (87) hide show
  1. package/dist/{chunk-NVZLEJXB.cjs → chunk-2Q6SYE5O.cjs} +2 -2
  2. package/dist/{chunk-NVZLEJXB.cjs.map → chunk-2Q6SYE5O.cjs.map} +1 -1
  3. package/dist/chunk-2U643GJZ.cjs +2 -0
  4. package/dist/chunk-2U643GJZ.cjs.map +1 -0
  5. package/dist/chunk-35DJOBEO.cjs +2 -0
  6. package/dist/chunk-35DJOBEO.cjs.map +1 -0
  7. package/dist/{chunk-POLTPHUA.js → chunk-3GRR4VW2.js} +3 -3
  8. package/dist/{chunk-3AL2DVPZ.cjs.map → chunk-3GRR4VW2.js.map} +1 -1
  9. package/dist/{chunk-Z5LQV5ND.js → chunk-5A7KDBDU.js} +2 -2
  10. package/dist/{chunk-Z5LQV5ND.js.map → chunk-5A7KDBDU.js.map} +1 -1
  11. package/dist/{chunk-D6QQYZIC.js → chunk-5NHDEY2C.js} +2 -2
  12. package/dist/chunk-5NHDEY2C.js.map +1 -0
  13. package/dist/{chunk-NQVWTQ2I.cjs → chunk-6GGYM5SF.cjs} +3 -3
  14. package/dist/{chunk-NQVWTQ2I.cjs.map → chunk-6GGYM5SF.cjs.map} +1 -1
  15. package/dist/chunk-AVVJKYT3.cjs +2 -0
  16. package/dist/chunk-AVVJKYT3.cjs.map +1 -0
  17. package/dist/{chunk-3AL2DVPZ.cjs → chunk-EEILP4OL.cjs} +3 -3
  18. package/dist/chunk-EEILP4OL.cjs.map +1 -0
  19. package/dist/{chunk-BT6HNBE7.js → chunk-FTTNKDZQ.js} +3 -3
  20. package/dist/{chunk-BT6HNBE7.js.map → chunk-FTTNKDZQ.js.map} +1 -1
  21. package/dist/chunk-H2HNKYN2.js +2 -0
  22. package/dist/chunk-H2HNKYN2.js.map +1 -0
  23. package/dist/chunk-TKFJGLUO.js +2 -0
  24. package/dist/chunk-TKFJGLUO.js.map +1 -0
  25. package/dist/chunk-U53YRJNV.js +36 -0
  26. package/dist/chunk-U53YRJNV.js.map +1 -0
  27. package/dist/chunk-ZYVQQ5VR.cjs +36 -0
  28. package/dist/chunk-ZYVQQ5VR.cjs.map +1 -0
  29. package/dist/{context-2uQ6fuxu.d.ts → context-CB1mCq2h.d.cts} +1 -1
  30. package/dist/{context-2uQ6fuxu.d.cts → context-CB1mCq2h.d.ts} +1 -1
  31. package/dist/core.cjs +1 -1
  32. package/dist/core.d.cts +1 -1
  33. package/dist/core.d.ts +1 -1
  34. package/dist/core.js +1 -1
  35. package/dist/devtools-overlay-CJWKBTP4.js +57 -0
  36. package/dist/devtools-overlay-CJWKBTP4.js.map +1 -0
  37. package/dist/devtools-overlay-EQ3G755P.cjs +57 -0
  38. package/dist/devtools-overlay-EQ3G755P.cjs.map +1 -0
  39. package/dist/index.cjs +18 -18
  40. package/dist/index.cjs.map +1 -1
  41. package/dist/index.d.cts +11 -7
  42. package/dist/index.d.ts +11 -7
  43. package/dist/index.js +18 -18
  44. package/dist/index.js.map +1 -1
  45. package/dist/machine.cjs +1 -1
  46. package/dist/machine.js +1 -1
  47. package/dist/offline.cjs +1 -1
  48. package/dist/offline.d.cts +2 -0
  49. package/dist/offline.d.ts +2 -0
  50. package/dist/offline.js +1 -1
  51. package/dist/pwa.cjs +1 -1
  52. package/dist/pwa.d.cts +5 -1
  53. package/dist/pwa.d.ts +5 -1
  54. package/dist/pwa.js +1 -1
  55. package/dist/ssr.cjs +1 -1
  56. package/dist/ssr.js +1 -1
  57. package/dist/store.cjs +1 -1
  58. package/dist/store.d.cts +35 -36
  59. package/dist/store.d.ts +35 -36
  60. package/dist/store.js +1 -1
  61. package/dist/sync.cjs +1 -1
  62. package/dist/sync.d.cts +59 -4
  63. package/dist/sync.d.ts +59 -4
  64. package/dist/sync.js +1 -1
  65. package/dist/test.cjs +4 -4
  66. package/dist/test.cjs.map +1 -1
  67. package/dist/test.d.cts +71 -3
  68. package/dist/test.d.ts +71 -3
  69. package/dist/test.js +4 -4
  70. package/dist/test.js.map +1 -1
  71. package/package.json +1 -1
  72. package/dist/chunk-6NX6JRSV.cjs +0 -32
  73. package/dist/chunk-6NX6JRSV.cjs.map +0 -1
  74. package/dist/chunk-772SU4MG.js +0 -2
  75. package/dist/chunk-772SU4MG.js.map +0 -1
  76. package/dist/chunk-7LQZF3XA.cjs +0 -2
  77. package/dist/chunk-7LQZF3XA.cjs.map +0 -1
  78. package/dist/chunk-D6QQYZIC.js.map +0 -1
  79. package/dist/chunk-H4SAK7A5.cjs +0 -2
  80. package/dist/chunk-H4SAK7A5.cjs.map +0 -1
  81. package/dist/chunk-IM2VW4TK.js +0 -32
  82. package/dist/chunk-IM2VW4TK.js.map +0 -1
  83. package/dist/chunk-ODDXU5DO.js +0 -2
  84. package/dist/chunk-ODDXU5DO.js.map +0 -1
  85. package/dist/chunk-POLTPHUA.js.map +0 -1
  86. package/dist/chunk-YIB4EKVI.cjs +0 -2
  87. package/dist/chunk-YIB4EKVI.cjs.map +0 -1
@@ -1,2 +1,2 @@
1
- 'use strict';var chunk3AL2DVPZ_cjs=require('./chunk-3AL2DVPZ.cjs');function M(t){let n=chunk3AL2DVPZ_cjs.l(t.initial),a=chunk3AL2DVPZ_cjs.l(t.context??{}),d=chunk3AL2DVPZ_cjs.l([t.initial]),g=chunk3AL2DVPZ_cjs.m(()=>t.states[n()]?.type==="final"),y=chunk3AL2DVPZ_cjs.m(()=>{let e=t.states[n()];return e?.on?Object.keys(e.on):[]}),S=t.states[t.initial];S?.entry&&S.entry({data:a(),event:""});function E(e,i){if(g())return;let s=n(),o=t.states[s];if(!o?.on)return;let x=o.on[e];if(!x)return;let r,l,f;if(typeof x=="string")r=x;else {let T=x;r=T.target,l=T.guard,f=T.action;}let c={data:a(),event:e,payload:i};if(l&&!l(c))return;o.exit&&o.exit(c),f&&f(c,i),a.set(c.data),n.set(r),d.update(T=>[...T,r]);let v=t.states[r];v?.entry&&v.entry({data:a(),event:e});}function h(e){let i=t.states[n()];if(!i?.on)return false;let s=i.on[e];if(!s)return false;if(typeof s!="string"){let o=s;if(o.guard)return o.guard({data:a(),event:e})}return true}return {state:()=>n(),context:()=>a(),send:E,matches:e=>n()===e,done:g,reset(){n.set(t.initial),a.set(t.context??{}),d.set([t.initial]);},history:()=>d(),can:h,nextEvents:y}}exports.a=M;//# sourceMappingURL=chunk-NVZLEJXB.cjs.map
2
- //# sourceMappingURL=chunk-NVZLEJXB.cjs.map
1
+ 'use strict';var chunkEEILP4OL_cjs=require('./chunk-EEILP4OL.cjs');function M(t){let n=chunkEEILP4OL_cjs.l(t.initial),a=chunkEEILP4OL_cjs.l(t.context??{}),d=chunkEEILP4OL_cjs.l([t.initial]),g=chunkEEILP4OL_cjs.m(()=>t.states[n()]?.type==="final"),y=chunkEEILP4OL_cjs.m(()=>{let e=t.states[n()];return e?.on?Object.keys(e.on):[]}),S=t.states[t.initial];S?.entry&&S.entry({data:a(),event:""});function E(e,i){if(g())return;let s=n(),o=t.states[s];if(!o?.on)return;let x=o.on[e];if(!x)return;let r,l,f;if(typeof x=="string")r=x;else {let T=x;r=T.target,l=T.guard,f=T.action;}let c={data:a(),event:e,payload:i};if(l&&!l(c))return;o.exit&&o.exit(c),f&&f(c,i),a.set(c.data),n.set(r),d.update(T=>[...T,r]);let v=t.states[r];v?.entry&&v.entry({data:a(),event:e});}function h(e){let i=t.states[n()];if(!i?.on)return false;let s=i.on[e];if(!s)return false;if(typeof s!="string"){let o=s;if(o.guard)return o.guard({data:a(),event:e})}return true}return {state:()=>n(),context:()=>a(),send:E,matches:e=>n()===e,done:g,reset(){n.set(t.initial),a.set(t.context??{}),d.set([t.initial]);},history:()=>d(),can:h,nextEvents:y}}exports.a=M;//# sourceMappingURL=chunk-2Q6SYE5O.cjs.map
2
+ //# sourceMappingURL=chunk-2Q6SYE5O.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/machine.ts"],"names":["createMachine","config","currentState","signal","contextData","stateHistory","done","computed","nextEvents","stateCfg","initialStateCfg","send","event","payload","state","transition","target","guard","action","t","ctx","h","targetCfg","can"],"mappings":"mEAkGO,SAASA,CAAAA,CAIdC,CAAAA,CAAoF,CACpF,IAAMC,EAAeC,mBAAAA,CAAeF,CAAAA,CAAO,OAAO,CAAA,CAC5CG,EAAcD,mBAAAA,CAAkBF,CAAAA,CAAO,OAAA,EAAW,EAAe,CAAA,CACjEI,CAAAA,CAAeF,mBAAAA,CAAiB,CAACF,CAAAA,CAAO,OAAO,CAAC,CAAA,CAEhDK,EAAOC,mBAAAA,CAAS,IACHN,CAAAA,CAAO,MAAA,CAAOC,GAAc,CAAA,EAC5B,IAAA,GAAS,OAC3B,EAEKM,CAAAA,CAAaD,mBAAAA,CAAS,IAAgB,CAC1C,IAAME,CAAAA,CAAWR,CAAAA,CAAO,MAAA,CAAOC,GAAc,CAAA,CAC7C,OAAKO,CAAAA,EAAU,GACR,MAAA,CAAO,IAAA,CAAKA,CAAAA,CAAS,EAAE,EADJ,EAE5B,CAAC,CAAA,CAGKC,CAAAA,CAAkBT,CAAAA,CAAO,MAAA,CAAOA,CAAAA,CAAO,OAAO,CAAA,CAChDS,CAAAA,EAAiB,KAAA,EACnBA,CAAAA,CAAgB,MAAM,CAAE,IAAA,CAAMN,CAAAA,EAAY,CAAe,MAAO,EAAG,CAAC,CAAA,CAGtE,SAASO,CAAAA,CAAKC,CAAAA,CAAeC,CAAAA,CAAyB,CACpD,GAAIP,CAAAA,EAAK,CAAG,OAEZ,IAAMQ,EAAQZ,CAAAA,EAAa,CACrBO,CAAAA,CAAWR,CAAAA,CAAO,OAAOa,CAAK,CAAA,CACpC,GAAI,CAACL,CAAAA,EAAU,EAAA,CAAI,OAEnB,IAAMM,EAAaN,CAAAA,CAAS,EAAA,CAAGG,CAAK,CAAA,CACpC,GAAI,CAACG,CAAAA,CAAY,OAEjB,IAAIC,EACAC,CAAAA,CACAC,CAAAA,CAEJ,GAAI,OAAOH,C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A,CAAE,KAAA,CAAM,CAAE,IAAA,CAAMf,GAAY,CAAe,KAAA,CAAAQ,CAAM,CAAC,CAE7D,CACA,OAAO,KACT,CAEA,OAAO,CACL,KAAA,CAAO,IAAMV,GAAa,CAC1B,OAAA,CAAS,IAAME,CAAAA,GACf,IAAA,CAAAO,CAAAA,CACA,OAAA,CAAUG,CAAAA,EAAkBZ,CAAAA,EAAa,GAAMY,CAAAA,CAC/C,IAAA,CAAAR,EACA,KAAA,EAAQ,CACNJ,CAAAA,CAAa,GAAA,CAAID,EAAO,OAAO,CAAA,CAC/BG,CAAAA,CAAY,GAAA,CAAKH,EAAO,OAAA,EAAW,EAAe,CAAA,CAClDI,EAAa,GAAA,CAAI,CAACJ,CAAAA,CAAO,OAAO,CAAC,EACnC,CAAA,CACA,OAAA,CAAS,IAAMI,GAAa,CAC5B,GAAA,CAAAkB,CAAAA,CACA,UAAA,CAAAf,CACF,CACF","file":"chunk-NVZLEJXB.cjs","sourcesContent":["/**\n * State machines for complex UI flows.\n *\n * Finite state machines with typed states, events, guards,\n * actions, and reactive current state signal.\n *\n * ```ts\n * const checkout = createMachine({\n * initial: 'cart',\n * states: {\n * cart: { on: { CHECKOUT: 'shipping' } },\n * shipping: { on: { NEXT: 'payment', BACK: 'cart' } },\n * payment: { on: { PAY: 'processing', BACK: 'shipping' } },\n * processing: { on: { SUCCESS: 'complete', FAIL: 'payment' } },\n * complete: { type: 'final' },\n * },\n * });\n *\n * checkout.state(); // 'cart'\n * checkout.send('CHECKOUT');\n * checkout.state(); // 'shipping'\n * checkout.matches('shipping'); // true\n * ```\n */\n\nimport { signal, computed } from './signals.js';\nimport type { ReadonlySignal } from './signals.js';\n\n// =========================================================================\n// Types\n// =========================================================================\n\nexport interface MachineConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Initial state */\n initial: TState;\n /** Initial context data */\n context?: TContext;\n /** State definitions */\n states: Record<TState, StateConfig<TState, TEvent, TContext>>;\n}\n\nexport interface StateConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Transitions: event → target state */\n on?: Partial<Record<TEvent, TState | TransitionConfig<TState, TEvent, TContext>>>;\n /** Entry action — runs when entering this state */\n entry?: (ctx: MachineContext<TContext>) => void;\n /** Exit action — runs when leaving this state */\n exit?: (ctx: MachineContext<TContext>) => void;\n /** Final state — machine stops accepting events */\n type?: 'final';\n}\n\nexport interface TransitionConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Target state */\n target: TState;\n /** Guard — transition only if this returns true */\n guard?: (ctx: MachineContext<TContext>) => boolean;\n /** Action — runs during the transition */\n action?: (ctx: MachineContext<TContext>) => void;\n}\n\nexport interface MachineContext<TContext> {\n /** The context data (mutable) */\n data: TContext;\n /** The current event that triggered the transition */\n event: string;\n /** Optional payload passed with the event via send(event, payload) */\n payload?: unknown;\n}\n\nexport interface Machine<TState extends string, TEvent extends string, TContext = unknown> {\n /** Current state (reactive signal) */\n state: ReadonlySignal<TState>;\n /** Context data (reactive signal) */\n context: ReadonlySignal<TContext>;\n /** Send an event to the machine */\n send(event: TEvent, payload?: Record<string, unknown>): void;\n /** Check if currently in a specific state */\n matches(state: TState): boolean;\n /** Whether the machine is in a final state */\n done: ReadonlySignal<boolean>;\n /** Reset to initial state */\n reset(): void;\n /** State history */\n history: ReadonlySignal<TState[]>;\n /** Can a specific event be sent in the current state? */\n can(event: TEvent): boolean;\n /** Get all possible events from current state */\n nextEvents: ReadonlySignal<TEvent[]>;\n}\n\n// =========================================================================\n// createMachine\n// =========================================================================\n\n/**\n * Create a finite state machine.\n */\nexport function createMachine<\n TState extends string,\n TEvent extends string,\n TContext = unknown,\n>(config: MachineConfig<TState, TEvent, TContext>): Machine<TState, TEvent, TContext> {\n const currentState = signal<TState>(config.initial);\n const contextData = signal<TContext>((config.context ?? {}) as TContext);\n const stateHistory = signal<TState[]>([config.initial]);\n\n const done = computed(() => {\n const stateCfg = config.states[currentState()];\n return stateCfg?.type === 'final';\n });\n\n const nextEvents = computed((): TEvent[] => {\n const stateCfg = config.states[currentState()];\n if (!stateCfg?.on) return [];\n return Object.keys(stateCfg.on) as TEvent[];\n });\n\n // Run entry action for initial state\n const initialStateCfg = config.states[config.initial];\n if (initialStateCfg?.entry) {\n initialStateCfg.entry({ data: contextData() as TContext, event: '' });\n }\n\n function send(event: TEvent, payload?: unknown): void {\n if (done()) return; // Final state — no more transitions\n\n const state = currentState();\n const stateCfg = config.states[state];\n if (!stateCfg?.on) return;\n\n const transition = stateCfg.on[event];\n if (!transition) return;\n\n let target: TState;\n let guard: ((ctx: MachineContext<TContext>) => boolean) | undefined;\n let action: ((ctx: MachineContext<TContext>) => void) | undefined;\n\n if (typeof transition === 'string') {\n target = transition as TState;\n } else {\n const t = transition as TransitionConfig<TState, TEvent, TContext>;\n target = t.target;\n guard = t.guard;\n action = t.action;\n }\n\n const ctx: MachineContext<TContext> = {\n data: contextData() as TContext,\n event,\n payload,\n };\n\n // Check guard\n if (guard && !guard(ctx)) return;\n\n // Exit action\n if (stateCfg.exit) {\n stateCfg.exit(ctx);\n }\n\n // Transition action — pass payload as second argument for convenience\n if (action) {\n (action as Function)(ctx, payload);\n }\n\n // Update context if modified\n contextData.set(ctx.data);\n\n // Enter new state\n currentState.set(target);\n stateHistory.update((h) => [...h, target]);\n\n // Entry action for new state\n const targetCfg = config.states[target];\n if (targetCfg?.entry) {\n targetCfg.entry({ data: contextData() as TContext, event });\n }\n }\n\n function can(event: TEvent): boolean {\n const stateCfg = config.states[currentState()];\n if (!stateCfg?.on) return false;\n const transition = stateCfg.on[event];\n if (!transition) return false;\n\n if (typeof transition !== 'string') {\n const t = transition as TransitionConfig<TState, TEvent, TContext>;\n if (t.guard) {\n return t.guard({ data: contextData() as TContext, event });\n }\n }\n return true;\n }\n\n return {\n state: () => currentState(),\n context: () => contextData(),\n send,\n matches: (state: TState) => currentState() === state,\n done,\n reset() {\n currentState.set(config.initial);\n contextData.set((config.context ?? {}) as TContext);\n stateHistory.set([config.initial]);\n },\n history: () => stateHistory(),\n can,\n nextEvents,\n };\n}\n"]}
1
+ {"version":3,"sources":["../src/machine.ts"],"names":["createMachine","config","currentState","signal","contextData","stateHistory","done","computed","nextEvents","stateCfg","initialStateCfg","send","event","payload","state","transition","target","guard","action","t","ctx","h","targetCfg","can"],"mappings":"mEAkGO,SAASA,CAAAA,CAIdC,CAAAA,CAAoF,CACpF,IAAMC,EAAeC,mBAAAA,CAAeF,CAAAA,CAAO,OAAO,CAAA,CAC5CG,EAAcD,mBAAAA,CAAkBF,CAAAA,CAAO,OAAA,EAAW,EAAe,CAAA,CACjEI,CAAAA,CAAeF,mBAAAA,CAAiB,CAACF,CAAAA,CAAO,OAAO,CAAC,CAAA,CAEhDK,EAAOC,mBAAAA,CAAS,IACHN,CAAAA,CAAO,MAAA,CAAOC,GAAc,CAAA,EAC5B,IAAA,GAAS,OAC3B,EAEKM,CAAAA,CAAaD,mBAAAA,CAAS,IAAgB,CAC1C,IAAME,CAAAA,CAAWR,CAAAA,CAAO,MAAA,CAAOC,GAAc,CAAA,CAC7C,OAAKO,CAAAA,EAAU,GACR,MAAA,CAAO,IAAA,CAAKA,CAAAA,CAAS,EAAE,EADJ,EAE5B,CAAC,CAAA,CAGKC,CAAAA,CAAkBT,CAAAA,CAAO,MAAA,CAAOA,CAAAA,CAAO,OAAO,CAAA,CAChDS,CAAAA,EAAiB,KAAA,EACnBA,CAAAA,CAAgB,MAAM,CAAE,IAAA,CAAMN,CAAAA,EAAY,CAAe,MAAO,EAAG,CAAC,CAAA,CAGtE,SAASO,CAAAA,CAAKC,CAAAA,CAAeC,CAAAA,CAAyB,CACpD,GAAIP,CAAAA,EAAK,CAAG,OAEZ,IAAMQ,EAAQZ,CAAAA,EAAa,CACrBO,CAAAA,CAAWR,CAAAA,CAAO,OAAOa,CAAK,CAAA,CACpC,GAAI,CAACL,CAAAA,EAAU,EAAA,CAAI,OAEnB,IAAMM,EAAaN,CAAAA,CAAS,EAAA,CAAGG,CAAK,CAAA,CACpC,GAAI,CAACG,CAAA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IAAMI,EAAIJ,CAAAA,CACV,GAAII,CAAAA,CAAE,KAAA,CACJ,OAAOA,CAAAA,CAAE,KAAA,CAAM,CAAE,IAAA,CAAMf,GAAY,CAAe,KAAA,CAAAQ,CAAM,CAAC,CAE7D,CACA,OAAO,KACT,CAEA,OAAO,CACL,KAAA,CAAO,IAAMV,GAAa,CAC1B,OAAA,CAAS,IAAME,CAAAA,GACf,IAAA,CAAAO,CAAAA,CACA,OAAA,CAAUG,CAAAA,EAAkBZ,CAAAA,EAAa,GAAMY,CAAAA,CAC/C,IAAA,CAAAR,EACA,KAAA,EAAQ,CACNJ,CAAAA,CAAa,GAAA,CAAID,EAAO,OAAO,CAAA,CAC/BG,CAAAA,CAAY,GAAA,CAAKH,EAAO,OAAA,EAAW,EAAe,CAAA,CAClDI,EAAa,GAAA,CAAI,CAACJ,CAAAA,CAAO,OAAO,CAAC,EACnC,CAAA,CACA,OAAA,CAAS,IAAMI,GAAa,CAC5B,GAAA,CAAAkB,CAAAA,CACA,UAAA,CAAAf,CACF,CACF","file":"chunk-2Q6SYE5O.cjs","sourcesContent":["/**\n * State machines for complex UI flows.\n *\n * Finite state machines with typed states, events, guards,\n * actions, and reactive current state signal.\n *\n * ```ts\n * const checkout = createMachine({\n * initial: 'cart',\n * states: {\n * cart: { on: { CHECKOUT: 'shipping' } },\n * shipping: { on: { NEXT: 'payment', BACK: 'cart' } },\n * payment: { on: { PAY: 'processing', BACK: 'shipping' } },\n * processing: { on: { SUCCESS: 'complete', FAIL: 'payment' } },\n * complete: { type: 'final' },\n * },\n * });\n *\n * checkout.state(); // 'cart'\n * checkout.send('CHECKOUT');\n * checkout.state(); // 'shipping'\n * checkout.matches('shipping'); // true\n * ```\n */\n\nimport { signal, computed } from './signals.js';\nimport type { ReadonlySignal } from './signals.js';\n\n// =========================================================================\n// Types\n// =========================================================================\n\nexport interface MachineConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Initial state */\n initial: TState;\n /** Initial context data */\n context?: TContext;\n /** State definitions */\n states: Record<TState, StateConfig<TState, TEvent, TContext>>;\n}\n\nexport interface StateConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Transitions: event → target state */\n on?: Partial<Record<TEvent, TState | TransitionConfig<TState, TEvent, TContext>>>;\n /** Entry action — runs when entering this state */\n entry?: (ctx: MachineContext<TContext>) => void;\n /** Exit action — runs when leaving this state */\n exit?: (ctx: MachineContext<TContext>) => void;\n /** Final state — machine stops accepting events */\n type?: 'final';\n}\n\nexport interface TransitionConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Target state */\n target: TState;\n /** Guard — transition only if this returns true */\n guard?: (ctx: MachineContext<TContext>) => boolean;\n /** Action — runs during the transition */\n action?: (ctx: MachineContext<TContext>) => void;\n}\n\nexport interface MachineContext<TContext> {\n /** The context data (mutable) */\n data: TContext;\n /** The current event that triggered the transition */\n event: string;\n /** Optional payload passed with the event via send(event, payload) */\n payload?: unknown;\n}\n\nexport interface Machine<TState extends string, TEvent extends string, TContext = unknown> {\n /** Current state (reactive signal) */\n state: ReadonlySignal<TState>;\n /** Context data (reactive signal) */\n context: ReadonlySignal<TContext>;\n /** Send an event to the machine */\n send(event: TEvent, payload?: Record<string, unknown>): void;\n /** Check if currently in a specific state */\n matches(state: TState): boolean;\n /** Whether the machine is in a final state */\n done: ReadonlySignal<boolean>;\n /** Reset to initial state */\n reset(): void;\n /** State history */\n history: ReadonlySignal<TState[]>;\n /** Can a specific event be sent in the current state? */\n can(event: TEvent): boolean;\n /** Get all possible events from current state */\n nextEvents: ReadonlySignal<TEvent[]>;\n}\n\n// =========================================================================\n// createMachine\n// =========================================================================\n\n/**\n * Create a finite state machine.\n */\nexport function createMachine<\n TState extends string,\n TEvent extends string,\n TContext = unknown,\n>(config: MachineConfig<TState, TEvent, TContext>): Machine<TState, TEvent, TContext> {\n const currentState = signal<TState>(config.initial);\n const contextData = signal<TContext>((config.context ?? {}) as TContext);\n const stateHistory = signal<TState[]>([config.initial]);\n\n const done = computed(() => {\n const stateCfg = config.states[currentState()];\n return stateCfg?.type === 'final';\n });\n\n const nextEvents = computed((): TEvent[] => {\n const stateCfg = config.states[currentState()];\n if (!stateCfg?.on) return [];\n return Object.keys(stateCfg.on) as TEvent[];\n });\n\n // Run entry action for initial state\n const initialStateCfg = config.states[config.initial];\n if (initialStateCfg?.entry) {\n initialStateCfg.entry({ data: contextData() as TContext, event: '' });\n }\n\n function send(event: TEvent, payload?: unknown): void {\n if (done()) return; // Final state — no more transitions\n\n const state = currentState();\n const stateCfg = config.states[state];\n if (!stateCfg?.on) return;\n\n const transition = stateCfg.on[event];\n if (!transition) return;\n\n let target: TState;\n let guard: ((ctx: MachineContext<TContext>) => boolean) | undefined;\n let action: ((ctx: MachineContext<TContext>) => void) | undefined;\n\n if (typeof transition === 'string') {\n target = transition as TState;\n } else {\n const t = transition as TransitionConfig<TState, TEvent, TContext>;\n target = t.target;\n guard = t.guard;\n action = t.action;\n }\n\n const ctx: MachineContext<TContext> = {\n data: contextData() as TContext,\n event,\n payload,\n };\n\n // Check guard\n if (guard && !guard(ctx)) return;\n\n // Exit action\n if (stateCfg.exit) {\n stateCfg.exit(ctx);\n }\n\n // Transition action — pass payload as second argument for convenience\n if (action) {\n (action as Function)(ctx, payload);\n }\n\n // Update context if modified\n contextData.set(ctx.data);\n\n // Enter new state\n currentState.set(target);\n stateHistory.update((h) => [...h, target]);\n\n // Entry action for new state\n const targetCfg = config.states[target];\n if (targetCfg?.entry) {\n targetCfg.entry({ data: contextData() as TContext, event });\n }\n }\n\n function can(event: TEvent): boolean {\n const stateCfg = config.states[currentState()];\n if (!stateCfg?.on) return false;\n const transition = stateCfg.on[event];\n if (!transition) return false;\n\n if (typeof transition !== 'string') {\n const t = transition as TransitionConfig<TState, TEvent, TContext>;\n if (t.guard) {\n return t.guard({ data: contextData() as TContext, event });\n }\n }\n return true;\n }\n\n return {\n state: () => currentState(),\n context: () => contextData(),\n send,\n matches: (state: TState) => currentState() === state,\n done,\n reset() {\n currentState.set(config.initial);\n contextData.set((config.context ?? {}) as TContext);\n stateHistory.set([config.initial]);\n },\n history: () => stateHistory(),\n can,\n nextEvents,\n };\n}\n"]}
@@ -0,0 +1,2 @@
1
+ 'use strict';var chunkEEILP4OL_cjs=require('./chunk-EEILP4OL.cjs');async function j(a,t=1){return typeof indexedDB>"u"?Promise.reject(new Error("IndexedDB not available")):new Promise((d,r)=>{try{let n=indexedDB.open(a,t);n.onupgradeneeded=()=>{let i=n.result;i.objectStoreNames.contains("items")||i.createObjectStore("items"),i.objectStoreNames.contains("pending")||i.createObjectStore("pending",{autoIncrement:!0});},n.onsuccess=()=>d(n.result),n.onerror=()=>r(n.error??new Error("IndexedDB open failed"));}catch(n){r(n);}})}async function x(a,t){return new Promise((d,r)=>{let i=a.transaction(t,"readonly").objectStore(t).getAll();i.onsuccess=()=>d(i.result),i.onerror=()=>r(i.error);})}async function D(a,t,d,r){return new Promise((n,i)=>{let l=a.transaction(t,"readwrite");l.objectStore(t).put(r,d),l.oncomplete=()=>n(),l.onerror=()=>i(l.error);})}async function B(a,t,d){return new Promise((r,n)=>{let i=a.transaction(t,"readwrite");i.objectStore(t).delete(d),i.oncomplete=()=>r(),i.onerror=()=>n(i.error);})}async function p(a,t){return new Promise((d,r)=>{let n=a.transaction(t,"readwrite");n.objectStore(t).clear(),n.oncomplete=()=>d(),n.onerror=()=>r(n.error);})}function A(a,t={}){let d=t.keyField??"id",r=chunkEEILP4OL_cjs.l([]),n=chunkEEILP4OL_cjs.l([]),i=chunkEEILP4OL_cjs.l(false),l=chunkEEILP4OL_cjs.l(typeof navigator<"u"&&navigator.onLine!=null?!!navigator.onLine:true),b=chunkEEILP4OL_cjs.m(()=>n().length),o=null,m=null,T=false;typeof window<"u"&&(window.addEventListener("online",()=>{l.set(true),v();}),window.addEventListener("offline",()=>l.set(false)));async function O(){if(!(typeof indexedDB>"u"))try{o=await j(`akash-offline-${a}`,t.version??1);let e=await x(o,"items");r.set(e);let s=await x(o,"pending");n.set(s);}catch(e){console.error("[AkashJS] Offline store init failed:",e);}}let k=O();if(t.sync){let e=t.sync.interval??3e4;m=setInterval(()=>{l()&&b()>0&&!i()&&v();},e);}function g(e){return String(e[d])}function w(e){let s=g(e);r.update(c=>{let f=c.findIndex(u=>g(u)===s);if(f!==-1){let u=[...c];return u[f]=e,u}return [...c,e]}),S({type:"put",key:s,value:e,timestamp:Date.now()}),o&&D(o,"items",s,e);}function I(e){r.update(s=>s.filter(c=>g(c)!==e)),S({type:"delete",key:e,timestamp:Date.now()}),o&&B(o,"items",e);}function S(e){n.update(s=>[...s,e]),o&&o.transaction("pending","readwrite").objectStore("pending").add(e);}async function v(){if(await k,!t.sync||i()||T)return;let e=n();if(e.length===0)return;i.set(true);let s=t.sync.fetch??globalThis.fetch.bind(globalThis);try{if((await s(t.sync.url,{method:"POST",headers:{"Content-Type":"application/json",...t.sync.headers},body:JSON.stringify({ops:e})})).ok){n.set([]),o&&await p(o,"pending");let f=await s(t.sync.url,{headers:t.sync.headers});if(f.ok){let u=await f.json();if(r.set(u),o){await p(o,"items");for(let P of u)await D(o,"items",g(P),P);}}}}catch(c){console.warn("[AkashJS] Offline sync failed:",c);}finally{i.set(false);}}return {items:()=>r(),data:()=>r(),get(e){return r().find(s=>g(s)===e)},put:w,add:w,update(e,s){let c=r().find(f=>g(f)===e);c&&w({...c,...s});},remove:I,clear(){r.set([]),n.set([]),o&&(p(o,"items"),p(o,"pending"));},pending:b,syncing:()=>i(),online:()=>l(),sync:v,dispose(){T=true,m&&clearInterval(m),o?.close();}}}exports.a=A;//# sourceMappingURL=chunk-2U643GJZ.cjs.map
2
+ //# sourceMappingURL=chunk-2U643GJZ.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/offline.ts"],"names":["openDB","name","version","resolve","reject","request","db","err","idbGetAll","store","req","idbPut","key","value","tx","idbDelete","idbClear","createOfflineStore","options","keyField","items","signal","pendingOps","syncing","online","pending","computed","syncTimer","disposed","syncNow","init","stored","ops","initPromise","interval","get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,CACbhB,IACFU,CAAAA,CAASV,CAAAA,CAAI,OAAO,CAAA,CACpBU,CAAAA,CAASV,CAAAA,CAAI,SAAS,CAAA,EAE1B,CAAA,CACA,QAAAmB,CAAAA,CACA,OAAA,CAAS,IAAMF,CAAAA,EAAQ,CACvB,OAAQ,IAAMC,CAAAA,EAAO,CACrB,IAAA,CAAMK,CAAAA,CACN,OAAA,EAAU,CACRD,CAAAA,CAAW,IAAA,CACPD,GAAW,aAAA,CAAcA,CAAS,EACtCrB,CAAAA,EAAI,KAAA,GACN,CACF,CACF","file":"chunk-2U643GJZ.cjs","sourcesContent":["/**\n * Offline-first store with IndexedDB persistence and background sync.\n *\n * Data persists across page reloads. Changes made offline queue up\n * and sync automatically when connection returns. Conflict resolution\n * via configurable strategies (last-write-wins, merge, custom).\n *\n * ```ts\n * const todos = createOfflineStore('todos', {\n * sync: { url: '/api/todos', strategy: 'last-write-wins' },\n * });\n *\n * todos.add({ id: '1', text: 'Buy milk', done: false });\n * todos.items(); // reactive list\n * todos.syncing(); // boolean\n * todos.pending(); // number of unsynced changes\n * ```\n */\n\nimport { signal, computed } from './signals.js';\nimport type { Signal, ReadonlySignal } from './signals.js';\n\n// =========================================================================\n// Types\n// =========================================================================\n\nexport type ConflictStrategy = 'last-write-wins' | 'client-wins' | 'server-wins' | 'manual';\n\nexport interface OfflineStoreOptions<T> {\n /** Sync configuration */\n sync?: {\n /** Server URL for sync */\n url: string;\n /** Conflict resolution strategy (default: 'last-write-wins') */\n strategy?: ConflictStrategy;\n /** Sync interval in ms (default: 30000) */\n interval?: number;\n /** Custom fetch */\n fetch?: typeof globalThis.fetch;\n /** Auth headers */\n headers?: Record<string, string>;\n };\n /** Key field for items (default: 'id') */\n keyField?: string;\n /** Version for schema migrations */\n version?: number;\n}\n\nexport interface OfflineStore<T extends Record<string, unknown>> {\n /** All items (reactive) */\n items: ReadonlySignal<T[]>;\n /** Alias for items (reactive) */\n data: ReadonlySignal<T[]>;\n /** Get a single item by key */\n get(key: string): T | undefined;\n /** Add or update an item */\n put(item: T): void;\n /** Add a new item */\n add(item: T): void;\n /** Update an existing item */\n update(key: string, partial: Partial<T>): void;\n /** Remove an item by key */\n remove(key: string): void;\n /** Clear all items */\n clear(): void;\n /** Number of unsynced changes */\n pending: ReadonlySignal<number>;\n /** Whether sync is in progress */\n syncing: ReadonlySignal<boolean>;\n /** Whether online */\n online: ReadonlySignal<boolean>;\n /** Force a sync now */\n sync(): Promise<void>;\n /** Dispose the store (stop sync, close DB) */\n dispose(): void;\n}\n\n/** An operation in the change queue */\ninterface PendingOp<T> {\n type: 'put' | 'delete';\n key: string;\n value?: T;\n timestamp: number;\n}\n\n// =========================================================================\n// IndexedDB wrapper\n// =========================================================================\n\nasync function openDB(name: string, version = 1): Promise<IDBDatabase> {\n if (typeof indexedDB === 'undefined') {\n return Promise.reject(new Error('IndexedDB not available'));\n }\n return new Promise((resolve, reject) => {\n try {\n const request = indexedDB.open(name, version);\n request.onupgradeneeded = () => {\n const db = request.result;\n if (!db.objectStoreNames.contains('items')) {\n db.createObjectStore('items');\n }\n if (!db.objectStoreNames.contains('pending')) {\n db.createObjectStore('pending', { autoIncrement: true });\n }\n };\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error ?? new Error('IndexedDB open failed'));\n } catch (err) {\n reject(err);\n }\n });\n}\n\nasync function idbGetAll<T>(db: IDBDatabase, store: string): Promise<T[]> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(store, 'readonly');\n const req = tx.objectStore(store).getAll();\n req.onsuccess = () => resolve(req.result);\n req.onerror = () => reject(req.error);\n });\n}\n\nasync function idbPut(db: IDBDatabase, store: string, key: string, value: unknown): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(store, 'readwrite');\n tx.objectStore(store).put(value, key);\n tx.oncomplete = () => resolve();\n tx.onerror = () => reject(tx.error);\n });\n}\n\nasync function idbDelete(db: IDBDatabase, store: string, key: string): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(store, 'readwrite');\n tx.objectStore(store).delete(key);\n tx.oncomplete = () => resolve();\n tx.onerror = () => reject(tx.error);\n });\n}\n\nasync function idbClear(db: IDBDatabase, store: string): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(store, 'readwrite');\n tx.objectStore(store).clear();\n tx.oncomplete = () => resolve();\n tx.onerror = () => reject(tx.error);\n });\n}\n\n// =========================================================================\n// createOfflineStore\n// =========================================================================\n\n/**\n * Create an offline-first store backed by IndexedDB.\n */\nexport function createOfflineStore<T extends Record<string, unknown>>(\n name: string,\n options: OfflineStoreOptions<T> = {},\n): OfflineStore<T> {\n const keyField = options.keyField ?? 'id';\n const items = signal<T[]>([]);\n const pendingOps = signal<PendingOp<T>[]>([]);\n const syncing = signal(false);\n const online = signal(typeof navigator !== 'undefined' && navigator.onLine != null ? !!navigator.onLine : true);\n const pending = computed(() => pendingOps().length);\n\n let db: IDBDatabase | null = null;\n let syncTimer: ReturnType<typeof setInterval> | null = null;\n let disposed = false;\n\n // Online/offline tracking\n if (typeof window !== 'undefined') {\n window.addEventListener('online', () => {\n online.set(true);\n syncNow();\n });\n window.addEventListener('offline', () => online.set(false));\n }\n\n // Initialize — load from IndexedDB\n async function init(): Promise<void> {\n if (typeof indexedDB === 'undefined') return;\n try {\n db = await openDB(`akash-offline-${name}`, options.version ?? 1);\n const stored = await idbGetAll<T>(db, 'items');\n items.set(stored);\n const ops = await idbGetAll<PendingOp<T>>(db, 'pending');\n pendingOps.set(ops);\n } catch (err) { console.error('[AkashJS] Offline store init failed:', err); }\n }\n\n const initPromise = init();\n\n // Start periodic sync\n if (options.sync) {\n const interval = options.sync.interval ?? 30000;\n syncTimer = setInterval(() => {\n if (online() && pending() > 0 && !syncing()) {\n syncNow();\n }\n }, interval);\n }\n\n function getKey(item: T): string {\n return String((item as any)[keyField]);\n }\n\n function put(item: T): void {\n const key = getKey(item);\n items.update((list) => {\n const idx = list.findIndex((i) => getKey(i) === key);\n if (idx !== -1) {\n const next = [...list];\n next[idx] = item;\n return next;\n }\n return [...list, item];\n });\n\n queueOp({ type: 'put', key, value: item, timestamp: Date.now() });\n if (db) idbPut(db, 'items', key, item);\n }\n\n function remove(key: string): void {\n items.update((list) => list.filter((i) => getKey(i) !== key));\n queueOp({ type: 'delete', key, timestamp: Date.now() });\n if (db) idbDelete(db, 'items', key);\n }\n\n function queueOp(op: PendingOp<T>): void {\n pendingOps.update((ops) => [...ops, op]);\n if (db) {\n const tx = db.transaction('pending', 'readwrite');\n tx.objectStore('pending').add(op);\n }\n }\n\n async function syncNow(): Promise<void> {\n await initPromise;\n if (!options.sync || syncing() || disposed) return;\n const ops = pendingOps();\n if (ops.length === 0) return;\n\n syncing.set(true);\n const fetchFn = options.sync.fetch ?? globalThis.fetch.bind(globalThis);\n\n try {\n const response = await fetchFn(options.sync.url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...options.sync.headers,\n },\n body: JSON.stringify({ ops }),\n });\n\n if (response.ok) {\n pendingOps.set([]);\n if (db) await idbClear(db, 'pending');\n\n // Fetch latest server state\n const serverResponse = await fetchFn(options.sync.url, {\n headers: options.sync.headers,\n });\n if (serverResponse.ok) {\n const serverItems = await serverResponse.json() as T[];\n items.set(serverItems);\n // Update IndexedDB\n if (db) {\n await idbClear(db, 'items');\n for (const item of serverItems) {\n await idbPut(db, 'items', getKey(item), item);\n }\n }\n }\n }\n } catch (err) { console.warn('[AkashJS] Offline sync failed:', err); }\n finally {\n syncing.set(false);\n }\n }\n\n return {\n items: () => items(),\n data: () => items(),\n get(key: string) { return items().find((i) => getKey(i) === key); },\n put,\n add: put,\n update(key: string, partial: Partial<T>) {\n const existing = items().find((i) => getKey(i) === key);\n if (existing) put({ ...existing, ...partial });\n },\n remove,\n clear() {\n items.set([]);\n pendingOps.set([]);\n if (db) {\n idbClear(db, 'items');\n idbClear(db, 'pending');\n }\n },\n pending,\n syncing: () => syncing(),\n online: () => online(),\n sync: syncNow,\n dispose() {\n disposed = true;\n if (syncTimer) clearInterval(syncTimer);\n db?.close();\n },\n };\n}\n"]}
@@ -0,0 +1,2 @@
1
+ 'use strict';var chunkEEILP4OL_cjs=require('./chunk-EEILP4OL.cjs');var b=class{entry;constructor(s,o){this.entry={value:s,timestamp:Date.now(),peerId:o};}get value(){return this.entry.value}get timestamp(){return this.entry.timestamp}set(s,o){let r=Date.now();return o===this.entry.peerId||r>this.entry.timestamp||r===this.entry.timestamp&&o>this.entry.peerId?(this.entry={value:s,timestamp:Math.max(r,this.entry.timestamp+1),peerId:o},true):false}merge(s){return s.timestamp>this.entry.timestamp||s.timestamp===this.entry.timestamp&&(!s.peerId||!this.entry.peerId||s.peerId>=this.entry.peerId)?(this.entry=s,true):false}toEntry(){return {...this.entry}}};function L(c){let s=null,o=[],r=[];return {send(n){s?.send(JSON.stringify({...n,type:"op",room:c.room}));},onReceive(n){o.push(n);let t=false;return ()=>{if(t)return;t=true;let i=o.indexOf(n);i!==-1&&o.splice(i,1);}},onPresence(n){return r.push(n),()=>{let t=r.indexOf(n);t!==-1&&r.splice(t,1);}},sendPresence(n){s?.send(JSON.stringify({type:"presence",room:c.room,data:n}));},connect(){s=new WebSocket(c.url,c.protocols),s.onmessage=n=>{try{let t=JSON.parse(n.data);if(t.type==="op")for(let i of o)i(t);else if(t.type==="presence")for(let i of r)i(t.peerId,t.data);}catch{}},s.onopen=()=>{s?.send(JSON.stringify({type:"join",room:c.room}));};},disconnect(){s?.close(),s=null;}}}function W(){let c=[],s=[];return {send(o){for(let r of c)r(o);},onReceive(o){c.push(o);let r=false;return ()=>{if(r)return;r=true;let n=c.indexOf(o);n!==-1&&c.splice(n,1);}},sendPresence(o,r){for(let n of s)n(r??"unknown",o);},onPresence(o){s.push(o);let r=false;return ()=>{if(r)return;r=true;let n=s.indexOf(o);n!==-1&&s.splice(n,1);}},connect(){},disconnect(){}}}var D=0;function _(c,s,o){let r,n;typeof c=="string"?(r=s,n=o??{}):(r=c,n=s??{});let t=n.peerId??`peer-${++D}-${Date.now()}`,i=n.transport??W(),l=new Map,u={};for(let[e,a]of Object.entries(r))l.set(e,new b(a,t)),u[e]=chunkEEILP4OL_cjs.l(a);let y={};for(let e of Object.keys(r)){let a=u[e],d=l.get(e),p=(()=>a());p.set=k=>{d.set(k,t),a.set(k),i.send({type:"set",key:e,value:k,timestamp:d.timestamp,peerId:t});},p.update=k=>{p.set(k(a()));},p.peek=()=>a.peek(),y[e]=p;}let f=chunkEEILP4OL_cjs.l([]),g=chunkEEILP4OL_cjs.l(false),v=chunkEEILP4OL_cjs.l(n.presence??{}),x=chunkEEILP4OL_cjs.l(new Map),T=chunkEEILP4OL_cjs.l([]);function O(e,a){if(e in u){u[e].set(a);let d=l.get(e);d&&d.set(a,t),i.send({type:"set",key:e,value:a,timestamp:Date.now(),peerId:t});}T.update(d=>d.filter(p=>p.key!==e));}let R=i.onReceive(e=>{if(e.type==="set"&&l.has(e.key)){let a=l.get(e.key),d=a.value,p=a.timestamp;if(n.onConflict&&p>0&&Math.abs(e.timestamp-p)<1e3&&e.peerId!==t){let w={key:e.key,localValue:d,remoteValue:e.value,localTimestamp:p,remoteTimestamp:e.timestamp,remotePeerId:e.peerId},S;try{S=n.onConflict(w);}catch(h){console.warn("[AkashJS Sync] onConflict handler threw, falling back to LWW:",h),a.merge({value:e.value,timestamp:e.timestamp,peerId:e.peerId})&&u[e.key].set(e.value),S="__fallback__";}S==="__fallback__"||(S!==void 0?(a.set(S,t),u[e.key].set(S),i.send({type:"set",key:e.key,value:S,timestamp:Date.now(),peerId:t})):T.update(h=>[...h,w]));}else a.merge({value:e.value,timestamp:e.timestamp,peerId:e.peerId})&&u[e.key].set(e.value);}}),P=v.set;v.set=e=>{P.call(v,e),i.sendPresence?.(e,t);};let C=i.onPresence?.((e,a)=>{e!==t&&x.update(d=>{let p=new Map(d);return a===null?p.delete(e):p.set(e,a),p});});return {state:y,peers:()=>f(),presence:v,peerPresence:()=>x(),conflicts:()=>T(),resolveConflict:O,peerId:t,connected:()=>g(),connect(){i.connect(),g.set(true);let e=v();Object.keys(e).length>0&&i.sendPresence?.(e,t);},disconnect(){i.sendPresence?.(null,t),i.disconnect(),g.set(false);},dispose(){i.sendPresence?.(null,t),R(),C?.(),i.disconnect();}}}function j(c,s){let o=s?.throttle??50,r=s?.target??(typeof document<"u"?document:null),n=chunkEEILP4OL_cjs.l(0),t=chunkEEILP4OL_cjs.l(0),i=0,l=null;function u(y){n.set(y.clientX),t.set(y.clientY);let f=Date.now();f-i>=o?(i=f,c.presence.set({...c.presence.peek?.()??{},cursor:{x:y.clientX,y:y.clientY}})):l||(l=setTimeout(()=>{l=null,i=Date.now(),c.presence.set({...c.presence.peek?.()??{},cursor:{x:n.peek(),y:t.peek()}});},o-(f-i)));}return r?.addEventListener?.("mousemove",u),{x:()=>n(),y:()=>t(),dispose(){r?.removeEventListener?.("mousemove",u),l&&clearTimeout(l);}}}function A(c,s){let o=s?.timeout??2e3,r=chunkEEILP4OL_cjs.l(false),n=null;function t(){r.set(true),c.presence.set({...c.presence.peek?.()??{},typing:true}),n&&clearTimeout(n),n=setTimeout(i,o);}function i(){r.set(false),c.presence.set({...c.presence.peek?.()??{},typing:false}),n&&(clearTimeout(n),n=null);}let l=chunkEEILP4OL_cjs.m(()=>{let u=c.peerPresence(),y=[];if(u instanceof Map)for(let[f,g]of u)g&&typeof g=="object"&&g.typing&&y.push(f);return y});return {isTyping:()=>r(),othersTyping:l,start:t,stop:i}}exports.a=b;exports.b=L;exports.c=W;exports.d=_;exports.e=j;exports.f=A;//# sourceMappingURL=chunk-35DJOBEO.cjs.map
2
+ //# sourceMappingURL=chunk-35DJOBEO.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/sync.ts"],"names":["LWWRegister","initialValue","peerId","value","ts","remote","createWebSocketTransport","options","ws","opHandlers","presenceHandlers","op","handler","removed","i","data","e","msg","h","createLocalTransport","senderPeerId","peerIdCounter","createSync","roomIdOrState","stateOrOptions","maybeOptions","initialState","transport","registers","stateSignals","key","signal","state","original","register","proxy","fn","peers","connected","presence","peerPresenceMap","conflictsSignal","resolveConflict","list","c","unsubOps","localValue","localTimestamp","conflict","resolved","err","originalPresenceSet","unsubPresence","remotePeerId","map","next","p","useCursor","doc","throttleMs","target","x","y","lastSend","pending","onMove","now","useTypingIndicator","timeout","isTyping","timer","start","stop","othersTyping","computed","typing","id"],"mappings":"mEAqCO,IAAMA,CAAAA,CAAN,KAAqB,CAClB,KAAA,CAER,YAAYC,CAAAA,CAAiBC,CAAAA,CAAgB,CAC3C,IAAA,CAAK,MAAQ,CAAE,KAAA,CAAOD,CAAAA,CAAc,SAAA,CAAW,KAAK,GAAA,EAAI,CAAG,MAAA,CAAAC,CAAO,EACpE,CAEA,IAAI,KAAA,EAAW,CACb,OAAO,IAAA,CAAK,KAAA,CAAM,KACpB,CAEA,IAAI,SAAA,EAAoB,CACtB,OAAO,KAAK,KAAA,CAAM,SACpB,CAEA,GAAA,CAAIC,CAAAA,CAAUD,CAAAA,CAAyB,CACrC,IAAME,EAAK,IAAA,CAAK,GAAA,EAAI,CAGpB,OACEF,IAAW,IAAA,CAAK,KAAA,CAAM,MAAA,EACtBE,CAAAA,CAAK,KAAK,KAAA,CAAM,SAAA,EACfA,CAAAA,GAAO,IAAA,CAAK,MAAM,SAAA,EAAaF,CAAAA,CAAS,IAAA,CAAK,KAAA,CAAM,QAEpD,IAAA,CAAK,KAAA,CAAQ,CAAE,KAAA,CAAAC,EAAO,SAAA,CAAW,IAAA,CAAK,GAAA,CAAIC,CAAAA,CAAI,KAAK,KAAA,CAAM,SAAA,CAAY,CAAC,CAAA,CAAG,MAAA,CAAAF,CAAO,CAAA,CACzE,IAAA,EAEF,KACT,CAEA,KAAA,CAAMG,CAAAA,CAA8B,CAClC,OACEA,CAAAA,CAAO,SAAA,CAAY,IAAA,CAAK,KAAA,CAAM,WAC7BA,CAAAA,CAAO,SAAA,GAAc,IAAA,CAAK,KAAA,CAAM,YAC/B,CAACA,CAAAA,CAAO,MAAA,EAA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ACzBiB,CAAAA,CAAmB,EAAC,CAC1B,GAAItC,CAAAA,YAAiB,GAAA,CACnB,IAAA,GAAW,CAACuC,CAAAA,CAAI7D,CAAI,CAAA,GAAKsB,CAAAA,CACnBtB,GAAQ,OAAOA,CAAAA,EAAS,QAAA,EAAaA,CAAAA,CAAa,QACpD4D,CAAAA,CAAO,IAAA,CAAKC,CAAE,CAAA,CAIpB,OAAOD,CACT,CAAC,CAAA,CAED,OAAO,CACL,QAAA,CAAU,IAAMN,CAAAA,EAAS,CACzB,aAAAI,CAAAA,CACA,KAAA,CAAAF,CAAAA,CACA,IAAA,CAAAC,CACF,CACF","file":"chunk-35DJOBEO.cjs","sourcesContent":["/**\n * Collaborative signals with CRDT.\n *\n * Make any signal multiplayer with one line. Multiple users can\n * edit the same state simultaneously with automatic conflict\n * resolution via Last-Writer-Wins Register and Operation-based CRDTs.\n *\n * ```ts\n * const doc = createSync('doc-123', {\n * title: '',\n * blocks: [],\n * cursor: { x: 0, y: 0 },\n * });\n *\n * doc.state.title.set('Hello'); // syncs to all peers\n * doc.peers(); // list of connected users\n * doc.presence.set({ cursor: { x: 10, y: 20 } });\n * ```\n */\n\nimport { signal, computed } from './signals.js';\nimport type { Signal, ReadonlySignal } from './signals.js';\n\n// =========================================================================\n// CRDT — Last-Writer-Wins Register\n// =========================================================================\n\nexport interface LWWEntry<T> {\n value: T;\n timestamp: number;\n peerId: string;\n}\n\n/**\n * Last-Writer-Wins Register — simplest CRDT for single values.\n * The write with the highest timestamp wins on conflict.\n */\nexport class LWWRegister<T> {\n private entry: LWWEntry<T>;\n\n constructor(initialValue: T, peerId: string) {\n this.entry = { value: initialValue, timestamp: Date.now(), peerId };\n }\n\n get value(): T {\n return this.entry.value;\n }\n\n get timestamp(): number {\n return this.entry.timestamp;\n }\n\n set(value: T, peerId: string): boolean {\n const ts = Date.now();\n // Local writes always succeed (same peer always advances its own state).\n // Cross-peer conflicts resolve by highest timestamp, then peerId tiebreak.\n if (\n peerId === this.entry.peerId ||\n ts > this.entry.timestamp ||\n (ts === this.entry.timestamp && peerId > this.entry.peerId)\n ) {\n this.entry = { value, timestamp: Math.max(ts, this.entry.timestamp + 1), peerId };\n return true;\n }\n return false;\n }\n\n merge(remote: LWWEntry<T>): boolean {\n if (\n remote.timestamp > this.entry.timestamp ||\n (remote.timestamp === this.entry.timestamp && (\n !remote.peerId || !this.entry.peerId || remote.peerId >= this.entry.peerId\n ))\n ) {\n this.entry = remote;\n return true;\n }\n return false;\n }\n\n toEntry(): LWWEntry<T> {\n return { ...this.entry };\n }\n}\n\n// =========================================================================\n// Operation log for list CRDTs\n// =========================================================================\n\nexport type SyncOp =\n | { type: 'set'; key: string; value: unknown; timestamp: number; peerId: string }\n | { type: 'insert'; key: string; index: number; value: unknown; timestamp: number; peerId: string }\n | { type: 'delete'; key: string; index: number; timestamp: number; peerId: string };\n\n// =========================================================================\n// Sync transport interface\n// =========================================================================\n\nexport interface SyncTransport {\n /** Send an operation to peers */\n send(op: SyncOp): void;\n /** Listen for operations from peers */\n onReceive(handler: (op: SyncOp) => void): () => void;\n /** Listen for peer presence updates */\n onPresence?(handler: (peerId: string, data: unknown) => void): () => void;\n /** Send presence data (peerId passed by createSync internals) */\n sendPresence?(data: unknown, peerId?: string): void;\n /** Connect to the sync channel */\n connect(): void;\n /** Disconnect */\n disconnect(): void;\n}\n\n// =========================================================================\n// WebSocket transport\n// =========================================================================\n\nexport interface WebSocketTransportOptions {\n url: string;\n room: string;\n protocols?: string | string[];\n}\n\nexport function createWebSocketTransport(options: WebSocketTransportOptions): SyncTransport {\n let ws: WebSocket | null = null;\n const opHandlers: Array<(op: SyncOp) => void> = [];\n const presenceHandlers: Array<(peerId: string, data: unknown) => void> = [];\n\n return {\n send(op: SyncOp) {\n ws?.send(JSON.stringify({ ...op, type: 'op', room: options.room }));\n },\n onReceive(handler) {\n opHandlers.push(handler);\n let removed = false;\n return () => {\n if (removed) return;\n removed = true;\n const i = opHandlers.indexOf(handler);\n if (i !== -1) opHandlers.splice(i, 1);\n };\n },\n onPresence(handler) {\n presenceHandlers.push(handler);\n return () => {\n const i = presenceHandlers.indexOf(handler);\n if (i !== -1) presenceHandlers.splice(i, 1);\n };\n },\n sendPresence(data) {\n ws?.send(JSON.stringify({ type: 'presence', room: options.room, data }));\n },\n connect() {\n ws = new WebSocket(options.url, options.protocols);\n ws.onmessage = (e) => {\n try {\n const msg = JSON.parse(e.data);\n if (msg.type === 'op') {\n for (const h of opHandlers) h(msg);\n } else if (msg.type === 'presence') {\n for (const h of presenceHandlers) h(msg.peerId, msg.data);\n }\n } catch { /* ignore parse errors */ }\n };\n ws.onopen = () => {\n ws?.send(JSON.stringify({ type: 'join', room: options.room }));\n };\n },\n disconnect() {\n ws?.close();\n ws = null;\n },\n };\n}\n\n// =========================================================================\n// In-memory transport (for testing / single-tab)\n// =========================================================================\n\nexport function createLocalTransport(): SyncTransport {\n const opHandlers: Array<(op: SyncOp) => void> = [];\n const presenceHandlers: Array<(peerId: string, data: unknown) => void> = [];\n return {\n send(op) {\n for (const h of opHandlers) h(op);\n },\n onReceive(handler) {\n opHandlers.push(handler);\n let removed = false;\n return () => {\n if (removed) return;\n removed = true;\n const i = opHandlers.indexOf(handler);\n if (i !== -1) opHandlers.splice(i, 1);\n };\n },\n sendPresence(data, senderPeerId?: string) {\n for (const h of presenceHandlers) h(senderPeerId ?? 'unknown', data);\n },\n onPresence(handler) {\n presenceHandlers.push(handler);\n let removed = false;\n return () => {\n if (removed) return;\n removed = true;\n const i = presenceHandlers.indexOf(handler);\n if (i !== -1) presenceHandlers.splice(i, 1);\n };\n },\n connect() {},\n disconnect() {},\n };\n}\n\n// =========================================================================\n// createSync — the main API\n// =========================================================================\n\nexport interface SyncConflict {\n key: string;\n localValue: unknown;\n remoteValue: unknown;\n localTimestamp: number;\n remoteTimestamp: number;\n remotePeerId: string;\n}\n\nexport interface SyncOptions {\n /** Transport for sending/receiving operations */\n transport?: SyncTransport;\n /** Unique peer ID (default: random) */\n peerId?: string;\n /** Initial presence data — broadcast on connect */\n presence?: Record<string, unknown>;\n /** Custom conflict resolver. Return the winning value, or undefined to queue for manual resolution. */\n onConflict?: (conflict: SyncConflict) => unknown | undefined;\n}\n\nexport interface SyncDoc<T extends Record<string, unknown>> {\n /** Reactive synced state — each key is a Signal */\n state: { [K in keyof T]: Signal<T[K]> };\n /** Connected peers (reactive) */\n peers: ReadonlySignal<PeerInfo[]>;\n /** Local presence data */\n presence: Signal<Record<string, unknown>>;\n /** Peer presence map (reactive) */\n peerPresence: ReadonlySignal<Map<string, unknown>>;\n /** Unresolved conflicts (reactive) */\n conflicts: ReadonlySignal<SyncConflict[]>;\n /** Resolve a conflict by choosing a value for a key */\n resolveConflict: (key: string, value: unknown) => void;\n /** This peer's ID */\n peerId: string;\n /** Whether connected */\n connected: ReadonlySignal<boolean>;\n /** Connect to the sync channel */\n connect(): void;\n /** Disconnect */\n disconnect(): void;\n /** Dispose the sync doc */\n dispose(): void;\n}\n\nexport interface PeerInfo {\n id: string;\n joinedAt: number;\n}\n\nlet peerIdCounter = 0;\n\n/**\n * Create a collaborative synced document.\n *\n * ```ts\n * const doc = createSync('room-1', { title: '', count: 0 }, {\n * transport: createWebSocketTransport({ url: 'wss://sync.example.com', room: 'room-1' }),\n * });\n *\n * doc.state.title.set('Hello'); // auto-syncs to all peers\n * doc.peers(); // connected users\n * ```\n */\nexport function createSync<T extends Record<string, unknown>>(\n initialState: T,\n options?: SyncOptions,\n): SyncDoc<T>;\nexport function createSync<T extends Record<string, unknown>>(\n roomIdOrState: string | T,\n stateOrOptions?: T | SyncOptions,\n maybeOptions?: SyncOptions,\n): SyncDoc<T> {\n // Support both old (roomId, state, options) and new (state, options) signatures\n let initialState: T;\n let options: SyncOptions;\n if (typeof roomIdOrState === 'string') {\n initialState = stateOrOptions as T;\n options = maybeOptions ?? {};\n } else {\n initialState = roomIdOrState;\n options = (stateOrOptions as SyncOptions) ?? {};\n }\n const peerId = options.peerId ?? `peer-${++peerIdCounter}-${Date.now()}`;\n const transport = options.transport ?? createLocalTransport();\n\n // Create CRDT registers and signals for each state key\n const registers = new Map<string, LWWRegister<unknown>>();\n const stateSignals: Record<string, Signal<unknown>> = {};\n\n for (const [key, value] of Object.entries(initialState)) {\n registers.set(key, new LWWRegister(value, peerId));\n stateSignals[key] = signal(value);\n }\n\n // Intercept signal.set to broadcast operations\n const state = {} as { [K in keyof T]: Signal<T[K]> };\n for (const key of Object.keys(initialState)) {\n const original = stateSignals[key];\n const register = registers.get(key)!;\n\n const proxy: Signal<any> = (() => original()) as any;\n proxy.set = (value: any) => {\n register.set(value, peerId);\n original.set(value);\n transport.send({\n type: 'set',\n key,\n value,\n timestamp: register.timestamp,\n peerId,\n });\n };\n proxy.update = (fn: (prev: any) => any) => {\n proxy.set(fn(original()));\n };\n proxy.peek = () => original.peek();\n\n (state as any)[key] = proxy;\n }\n\n // Peers\n const peers = signal<PeerInfo[]>([]);\n const connected = signal(false);\n const presence = signal<Record<string, unknown>>(options.presence ?? {});\n const peerPresenceMap = signal(new Map<string, unknown>());\n\n // Conflict tracking\n const conflictsSignal = signal<SyncConflict[]>([]);\n\n function resolveConflict(key: string, value: unknown): void {\n // Apply the resolved value\n if (key in stateSignals) {\n stateSignals[key].set(value);\n const register = registers.get(key);\n if (register) register.set(value, peerId);\n // Broadcast the resolution\n transport.send({ type: 'set', key, value, timestamp: Date.now(), peerId });\n }\n // Remove from conflicts list\n conflictsSignal.update(list => list.filter(c => c.key !== key));\n }\n\n // Listen for remote operations\n const unsubOps = transport.onReceive((op) => {\n if (op.type === 'set' && registers.has(op.key)) {\n const register = registers.get(op.key)!;\n const localValue = register.value;\n const localTimestamp = register.timestamp;\n\n // Check for conflict: remote write to a key we also recently wrote\n const isConflict = options.onConflict && localTimestamp > 0 &&\n Math.abs(op.timestamp - localTimestamp) < 1000 && // within 1s window\n op.peerId !== peerId;\n\n if (isConflict) {\n const conflict: SyncConflict = {\n key: op.key,\n localValue,\n remoteValue: op.value,\n localTimestamp,\n remoteTimestamp: op.timestamp,\n remotePeerId: op.peerId,\n };\n let resolved: unknown;\n try {\n resolved = options.onConflict!(conflict);\n } catch (err) {\n console.warn('[AkashJS Sync] onConflict handler threw, falling back to LWW:', err);\n // Fallback to standard LWW merge\n const merged = register.merge({ value: op.value, timestamp: op.timestamp, peerId: op.peerId });\n if (merged) stateSignals[op.key].set(op.value);\n resolved = '__fallback__';\n }\n if (resolved === '__fallback__') {\n // Already handled by LWW fallback above\n } else if (resolved !== undefined) {\n register.set(resolved, peerId);\n stateSignals[op.key].set(resolved);\n transport.send({ type: 'set', key: op.key, value: resolved, timestamp: Date.now(), peerId });\n } else {\n conflictsSignal.update(list => [...list, conflict]);\n }\n } else {\n // No conflict handler or no conflict — standard LWW merge\n const merged = register.merge({\n value: op.value,\n timestamp: op.timestamp,\n peerId: op.peerId,\n });\n if (merged) {\n stateSignals[op.key].set(op.value);\n }\n }\n }\n });\n\n // Intercept presence.set to broadcast with peerId\n const originalPresenceSet = presence.set;\n presence.set = (data: Record<string, unknown>) => {\n originalPresenceSet.call(presence, data);\n transport.sendPresence?.(data, peerId);\n };\n\n // Listen for presence — filter out self\n const unsubPresence = transport.onPresence?.((remotePeerId, data) => {\n if (remotePeerId === peerId) return; // ignore own presence\n peerPresenceMap.update((map) => {\n const next = new Map(map);\n if (data === null) {\n next.delete(remotePeerId); // peer disconnected\n } else {\n next.set(remotePeerId, data);\n }\n return next;\n });\n });\n\n return {\n state,\n peers: () => peers(),\n presence,\n peerPresence: () => peerPresenceMap(),\n conflicts: () => conflictsSignal(),\n resolveConflict,\n peerId,\n connected: () => connected(),\n connect() {\n transport.connect();\n connected.set(true);\n // Broadcast initial presence on connect\n const p = presence();\n if (Object.keys(p).length > 0) {\n transport.sendPresence?.(p, peerId);\n }\n },\n disconnect() {\n // Broadcast null presence to signal departure\n transport.sendPresence?.(null, peerId);\n transport.disconnect();\n connected.set(false);\n },\n dispose() {\n transport.sendPresence?.(null, peerId);\n unsubOps();\n unsubPresence?.();\n transport.disconnect();\n },\n };\n}\n\n// =========================================================================\n// Presence composables\n// =========================================================================\n\n/**\n * Track cursor position and broadcast via sync presence.\n * Throttles updates to avoid flooding the transport.\n *\n * ```ts\n * const cursor = useCursor(doc, { throttle: 50 });\n * // Automatically tracks mousemove and broadcasts { cursor: { x, y } }\n * // Other peers: doc.peerPresence().get(peerId).cursor\n * ```\n */\nexport function useCursor(\n doc: SyncDoc<any>,\n options?: { throttle?: number; target?: HTMLElement },\n): { x: ReadonlySignal<number>; y: ReadonlySignal<number>; dispose: () => void } {\n const throttleMs = options?.throttle ?? 50;\n const target = options?.target ?? (typeof document !== 'undefined' ? document : null);\n const x = signal(0);\n const y = signal(0);\n let lastSend = 0;\n let pending: ReturnType<typeof setTimeout> | null = null;\n\n function onMove(e: MouseEvent): void {\n x.set(e.clientX);\n y.set(e.clientY);\n\n const now = Date.now();\n if (now - lastSend >= throttleMs) {\n lastSend = now;\n doc.presence.set({ ...doc.presence.peek?.() ?? {}, cursor: { x: e.clientX, y: e.clientY } });\n } else if (!pending) {\n pending = setTimeout(() => {\n pending = null;\n lastSend = Date.now();\n doc.presence.set({ ...doc.presence.peek?.() ?? {}, cursor: { x: x.peek(), y: y.peek() } });\n }, throttleMs - (now - lastSend));\n }\n }\n\n target?.addEventListener?.('mousemove', onMove as EventListener);\n\n return {\n x: () => x(),\n y: () => y(),\n dispose() {\n target?.removeEventListener?.('mousemove', onMove as EventListener);\n if (pending) clearTimeout(pending);\n },\n };\n}\n\n/**\n * Typing indicator — broadcasts typing state with auto-timeout.\n *\n * ```ts\n * const typing = useTypingIndicator(doc, { timeout: 2000 });\n * typing.start(); // broadcasts { typing: true }\n * // Auto-stops after 2s of inactivity\n * typing.stop(); // manual stop\n *\n * // Other peers typing:\n * typing.othersTyping(); // string[] of peer IDs currently typing\n * ```\n */\nexport function useTypingIndicator(\n doc: SyncDoc<any>,\n options?: { timeout?: number },\n): {\n isTyping: ReadonlySignal<boolean>;\n othersTyping: ReadonlySignal<string[]>;\n start: () => void;\n stop: () => void;\n} {\n const timeout = options?.timeout ?? 2000;\n const isTyping = signal(false);\n let timer: ReturnType<typeof setTimeout> | null = null;\n\n function start(): void {\n isTyping.set(true);\n doc.presence.set({ ...doc.presence.peek?.() ?? {}, typing: true });\n if (timer) clearTimeout(timer);\n timer = setTimeout(stop, timeout);\n }\n\n function stop(): void {\n isTyping.set(false);\n doc.presence.set({ ...doc.presence.peek?.() ?? {}, typing: false });\n if (timer) { clearTimeout(timer); timer = null; }\n }\n\n const othersTyping = computed(() => {\n const peers = doc.peerPresence();\n const typing: string[] = [];\n if (peers instanceof Map) {\n for (const [id, data] of peers) {\n if (data && typeof data === 'object' && (data as any).typing) {\n typing.push(id);\n }\n }\n }\n return typing;\n });\n\n return {\n isTyping: () => isTyping(),\n othersTyping,\n start,\n stop,\n };\n}\n"]}
@@ -1,4 +1,4 @@
1
- var f=new Set,l=new Set,b=false,d=0,g=false,x=3,P=100;function v(){if(b)return;b=true;let e=new Map,n=false,t=0;for(;(f.size>0||l.size>0)&&t<P;){if(t++,f.size>0){let s=[...f].sort(C);f.clear();for(let r of s){let o=(e.get(r)??0)+1;if(e.set(r,o),o>x){n=true;continue}r.run();}}if(l.size>0){let s=[...l].sort(C);l.clear();for(let r of s){let o=(e.get(r)??0)+1;if(e.set(r,o),o>x){n=true;continue}r.run();}}if(n&&f.size===0&&l.size===0)break}(n||t>=P)&&(f.clear(),l.clear(),console.error("[AkashJS] Circular dependency detected between effects. Two or more effects are writing to each other's dependencies. The cycle has been broken after "+t+" iterations.")),b=false,g&&(g=false,(f.size>0||l.size>0)&&v());}function N(e){e.isRender?f.add(e):l.add(e),d===0&&!b?v():b&&(g=true);}function _(e){d++;try{e();}finally{d--,d===0&&v();}}function R(){d++;}function k(){d--,d===0&&v();}function C(e,n){return (e.depth??0)-(n.depth??0)}function M(){v();}var y=false,h=[],S=0;function B(){y=true,h=[],S=performance.now();}function K(){y=false;let e=performance.now();return {entries:[...h],totalDuration:e-S,startTime:S,endTime:e}}function T(){return y}function p(e,n,t){y&&h.push({type:e,name:n,duration:t,timestamp:performance.now()});}function z(e){let n=e.entries.filter(i=>i.type==="render"),t=e.entries.filter(i=>i.type==="effect"),s=e.entries.filter(i=>i.type==="signal-update"),r=e.entries.filter(i=>i.type==="computed"),o=i=>i.length>0?i.reduce((u,m)=>u+m.duration,0)/i.length:0,c=i=>i.length>0?i.reduce((u,m)=>m.duration>u.duration?m:u):null;return {totalRenders:n.length,totalEffects:t.length,totalSignalUpdates:s.length,totalComputedEvals:r.length,avgRenderTime:o(n),avgEffectTime:o(t),slowestRender:c(n),slowestEffect:c(t)}}function O(e,n){let t=performance.now(),s=n(),r=performance.now()-t;return p("render",e,r),{result:s,duration:r}}async function j(e,n){let t=performance.now(),s=await n(),r=performance.now()-t;return p("render",e,r),{result:s,duration:r}}function I(e){let n=0,t=0;return {start(){n=performance.now();},stop(){t=performance.now()-n,p("render",e,t);},get duration(){return t}}}function X(e){let n=z(e),t=`
1
+ var f=new Set,l=new Set,b=false,d=0,g=false,x=3,P=100;function v(){if(b)return;b=true;let e=new Map,n=false,t=0;for(;(f.size>0||l.size>0)&&t<P;){if(t++,f.size>0){let s=[...f].sort(C);f.clear();for(let r of s){let o=(e.get(r)??0)+1;if(e.set(r,o),o>x){n=true;continue}r.run();}}if(l.size>0){let s=[...l].sort(C);l.clear();for(let r of s){let o=(e.get(r)??0)+1;if(e.set(r,o),o>x){n=true;continue}r.run();}}if(n&&f.size===0&&l.size===0)break}(n||t>=P)&&(f.clear(),l.clear(),console.error("[AkashJS] Circular dependency detected between effects. Two or more effects are writing to each other's dependencies. The cycle has been broken after "+t+" iterations.")),b=false,g&&(g=false,(f.size>0||l.size>0)&&v());}function N(e){e.isRender?f.add(e):l.add(e),d===0&&!b?v():b&&(g=true);}function _(e){d++;try{return e()}finally{d--,d===0&&v();}}function R(){d++;}function k(){d--,d===0&&v();}function C(e,n){return (e.depth??0)-(n.depth??0)}function M(){v();}var T=false,h=[],S=0;function B(){T=true,h=[],S=performance.now();}function K(){T=false;let e=performance.now();return {entries:[...h],totalDuration:e-S,startTime:S,endTime:e}}function y(){return T}function p(e,n,t){T&&h.push({type:e,name:n,duration:t,timestamp:performance.now()});}function z(e){let n=e.entries.filter(i=>i.type==="render"),t=e.entries.filter(i=>i.type==="effect"),s=e.entries.filter(i=>i.type==="signal-update"),r=e.entries.filter(i=>i.type==="computed"),o=i=>i.length>0?i.reduce((u,m)=>u+m.duration,0)/i.length:0,c=i=>i.length>0?i.reduce((u,m)=>m.duration>u.duration?m:u):null;return {totalRenders:n.length,totalEffects:t.length,totalSignalUpdates:s.length,totalComputedEvals:r.length,avgRenderTime:o(n),avgEffectTime:o(t),slowestRender:c(n),slowestEffect:c(t)}}function O(e,n){let t=performance.now(),s=n(),r=performance.now()-t;return p("render",e,r),{result:s,duration:r}}async function j(e,n){let t=performance.now(),s=await n(),r=performance.now()-t;return p("render",e,r),{result:s,duration:r}}function I(e){let n=0,t=0;return {start(){n=performance.now();},stop(){t=performance.now()-n,p("render",e,t);},get duration(){return t}}}function X(e){let n=z(e),t=`
2
2
  Performance Profile
3
3
  `;return t+=" "+"\u2500".repeat(50)+`
4
4
  `,t+=` Duration: ${e.totalDuration.toFixed(1)} ms
@@ -9,5 +9,5 @@ var f=new Set,l=new Set,b=false,d=0,g=false,x=3,P=100;function v(){if(b)return;b
9
9
  `,n.slowestRender&&(t+=` Slowest render: ${n.slowestRender.name} (${n.slowestRender.duration.toFixed(2)} ms)
10
10
  `),n.slowestEffect&&(t+=` Slowest effect: ${n.slowestEffect.name} (${n.slowestEffect.duration.toFixed(2)} ms)
11
11
  `),t+=" "+"\u2500".repeat(50)+`
12
- `,t}var F=typeof process>"u"||process.env?.NODE_ENV!=="production";var a=null;function G(e,n){let t={value:e,subscribers:new Set,equals:n?.equals??Object.is},s=()=>(J(t),t.value);return s.set=r=>{F&&a&&a._tag==="computed"&&console.warn("[AkashJS] Writing to a signal inside computed() is not allowed. The write will be lost on the next evaluation."),!t.equals(t.value,r)&&(t.value=r,T()&&p("signal-update","signal.set",0),_(()=>{E(t);}));},s.update=r=>{s.set(r(t.value));},s.peek=()=>t.value,s}function Q(e,n){let t={_tag:"computed",fn:e,value:void 0,state:1,subscribers:new Set,sources:new Set,equals:n?.equals??Object.is};return ()=>(a&&(t.subscribers.add(a),"sources"in a&&a.sources.add(t)),t.state!==0&&q(t),t.value)}var w=new Set;function q(e,n=false){if(w.has(e))throw new Error("[AkashJS] Circular dependency detected between computed values.");w.add(e),R();for(let r of e.sources)r.subscribers.delete(e);e.sources.clear();let t=a;a=e;let s=T()?performance.now():0;try{let r=e.fn(),o=e.value===void 0,c=o||!e.equals(e.value,r);if(e.value=r,e.state=0,c&&!o)if(n){for(let i of [...e.subscribers])if(i._tag==="computed"){i.state=1;for(let u of [...i.subscribers])u._tag==="computed"&&(u.state=1);}}else E(e);}finally{s&&p("computed","computed",performance.now()-s),w.delete(e),a=t,k();}}function Y(e,n){let t={_tag:"effect",fn:e,cleanup:null,sources:new Set,disposed:false,isRender:n?.render??false,run(){D(t);}};return D(t),()=>{t.disposed=true,A(t);for(let s of t.sources)s.subscribers.delete(t);t.sources.clear();}}function D(e){if(e.disposed)return;if(e.sources.size>0){let r=false;for(let o of e.sources)if("_tag"in o&&o._tag==="computed"){o.state===1&&q(o,true);let c=e._lastSeen;if(c?.has(o)){let i=c.get(o);o.equals(i,o.value)||(r=true);}else r=true;}else r=true;if(!r)return}A(e);let n=new Set(e.sources);for(let r of e.sources)r.subscribers.delete(e);e.sources.clear();let t=a;a=e;let s=T()?performance.now():0;try{let r=e.fn();typeof r=="function"&&(e.cleanup=r);}catch(r){for(let o of n)o.subscribers.add(e),e.sources.add(o);console.error("[AkashJS] Error in effect (will retry on next signal change):",r);}finally{s&&p("effect","effect",performance.now()-s),a=t;let r=new Map;for(let o of e.sources)"_tag"in o&&o._tag==="computed"&&r.set(o,o.value);r.size>0&&(e._lastSeen=r);}}function A(e){if(e.cleanup){try{e.cleanup();}catch(n){console.error("[AkashJS] Error in effect cleanup (ignored):",n);}e.cleanup=null;}}function U(e){let n=a;a=null;try{return e()}finally{a=n;}}function Z(e,n,t){let s=Array.isArray(e),r=s?e:[e],o,c=true;return ()=>{let i=r.map(m=>m());if(c&&(c=false,o=i.slice(),t?.defer!==false))return;let u=o;return o=i.slice(),U(()=>n(s?i:i[0],u?s?u:u[0]:void 0))}}function J(e){a&&(e.subscribers.add(a),"sources"in a&&a.sources.add(e));}function E(e){let n=[...e.subscribers];for(let t of n)t._tag==="computed"?(t.state=1,E(t)):t._tag==="effect"&&N(t);}export{_ as a,M as b,B as c,K as d,T as e,p as f,z as g,O as h,j as i,I as j,X as k,G as l,Q as m,Y as n,U as o,Z as p};//# sourceMappingURL=chunk-POLTPHUA.js.map
13
- //# sourceMappingURL=chunk-POLTPHUA.js.map
12
+ `,t}var F=typeof process>"u"||process.env?.NODE_ENV!=="production";var a=null;function G(e,n){let t={value:e,subscribers:new Set,equals:n?.equals??Object.is},s=()=>(J(t),t.value);return s.set=r=>{F&&a&&a._tag==="computed"&&console.warn("[AkashJS] Writing to a signal inside computed() is not allowed. The write will be lost on the next evaluation."),!t.equals(t.value,r)&&(t.value=r,y()&&p("signal-update","signal.set",0),_(()=>{E(t);}));},s.update=r=>{s.set(r(t.value));},s.peek=()=>t.value,s}function Q(e,n){let t={_tag:"computed",fn:e,value:void 0,state:1,subscribers:new Set,sources:new Set,equals:n?.equals??Object.is};return ()=>(a&&(t.subscribers.add(a),"sources"in a&&a.sources.add(t)),t.state!==0&&q(t),t.value)}var w=new Set;function q(e,n=false){if(w.has(e))throw new Error("[AkashJS] Circular dependency detected between computed values.");w.add(e),R();for(let r of e.sources)r.subscribers.delete(e);e.sources.clear();let t=a;a=e;let s=y()?performance.now():0;try{let r=e.fn(),o=e.value===void 0,c=o||!e.equals(e.value,r);if(e.value=r,e.state=0,c&&!o)if(n){for(let i of [...e.subscribers])if(i._tag==="computed"){i.state=1;for(let u of [...i.subscribers])u._tag==="computed"&&(u.state=1);}}else E(e);}finally{s&&p("computed","computed",performance.now()-s),w.delete(e),a=t,k();}}function Y(e,n){let t={_tag:"effect",fn:e,cleanup:null,sources:new Set,disposed:false,isRender:n?.render??false,run(){D(t);}};return D(t),()=>{t.disposed=true,A(t);for(let s of t.sources)s.subscribers.delete(t);t.sources.clear();}}function D(e){if(e.disposed)return;if(e.sources.size>0){let r=false;for(let o of e.sources)if("_tag"in o&&o._tag==="computed"){o.state===1&&q(o,true);let c=e._lastSeen;if(c?.has(o)){let i=c.get(o);o.equals(i,o.value)||(r=true);}else r=true;}else r=true;if(!r)return}A(e);let n=new Set(e.sources);for(let r of e.sources)r.subscribers.delete(e);e.sources.clear();let t=a;a=e;let s=y()?performance.now():0;try{let r=e.fn();typeof r=="function"&&(e.cleanup=r);}catch(r){for(let o of n)o.subscribers.add(e),e.sources.add(o);console.error("[AkashJS] Error in effect (will retry on next signal change):",r);}finally{s&&p("effect","effect",performance.now()-s),a=t;let r=new Map;for(let o of e.sources)"_tag"in o&&o._tag==="computed"&&r.set(o,o.value);r.size>0&&(e._lastSeen=r);}}function A(e){if(e.cleanup){try{e.cleanup();}catch(n){console.error("[AkashJS] Error in effect cleanup (ignored):",n);}e.cleanup=null;}}function U(e){let n=a;a=null;try{return e()}finally{a=n;}}function Z(e,n,t){let s=Array.isArray(e),r=s?e:[e],o,c=true;return ()=>{let i=r.map(m=>m());if(c&&(c=false,o=i.slice(),t?.defer!==false))return;let u=o;return o=i.slice(),U(()=>n(s?i:i[0],u?s?u:u[0]:void 0))}}function J(e){a&&(e.subscribers.add(a),"sources"in a&&a.sources.add(e));}function E(e){let n=[...e.subscribers];for(let t of n)t._tag==="computed"?(t.state=1,E(t)):t._tag==="effect"&&N(t);}export{_ as a,M as b,B as c,K as d,y as e,p as f,z as g,O as h,j as i,I as j,X as k,G as l,Q as m,Y as n,U as o,Z as p};//# sourceMappingURL=chunk-3GRR4VW2.js.map
13
+ //# sourceMappingURL=chunk-3GRR4VW2.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/scheduler.ts","../src/perf.ts","../src/signals.ts"],"names":["pendingRender","pendingUser","flushing","batchDepth","reflushNeeded","MAX_EFFECT_RUNS","MAX_FLUSH_ITERATIONS","flush","runCounts","circularDetected","iterations","queue","byDepth","fx","count","scheduleEffect","batch","fn","enterBatch","exitBatch","a","b","flushSync","profiling","entries","profileStart","startProfiling","stopProfiling","endTime","isProfiling","recordPerfEntry","type","name","duration","getProfileSummary","profile","renders","e","effects","signalUpdates","computeds","avg","arr","s","slowest","measureSync","start","result","measureAsync","createTimer","startTime","dur","formatProfile","summary","out","__DEV__","currentSubscriber","signal","initialValue","options","node","read","trackSubscriber","value","notifySubscribers","computed","recompute","computingSet","skipEffectNotify","source","prevSubscriber","_t0","newValue","isFirst","changed","sub","sub2","effect","runEffect","cleanupEffect","anyChanged","cachedValues","lastSeen","prevSources","err","untrack","prev","on","deps","isArray","depArray","prevValues","values","d","subs"],"mappings":"aAcA,IAAMA,CAAAA,CAAgB,IAAI,GAAA,CACpBC,CAAAA,CAAc,IAAI,GAAA,CACpBC,CAAAA,CAAW,KAAA,CACXC,CAAAA,CAAa,CAAA,CAEbC,EAAgB,KAAA,CAEdC,CAAAA,CAAkB,CAAA,CAClBC,CAAAA,CAAuB,IAE7B,SAASC,CAAAA,EAAc,CACrB,GAAIL,CAAAA,CAAU,OACdA,CAAAA,CAAW,IACM,CAGjB,IAAMM,EAAY,IAAI,GAAA,CAClBC,CAAAA,CAAmB,KAAA,CAInBC,CAAAA,CAAa,CAAA,CACjB,KAAA,CAAQV,CAAAA,CAAc,IAAA,CAAO,CAAA,EAAKC,CAAAA,CAAY,IAAA,CAAO,CAAA,GAAMS,CAAAA,CAAaJ,CAAAA,EAAsB,CAI5F,GAHAI,CAAAA,EAAAA,CAGIV,CAAAA,CAAc,IAAA,CAAO,CAAA,CAAG,CAC1B,IAAMW,CAAAA,CAAQ,CAAC,GAAGX,CAAa,CAAA,CAAE,IAAA,CAAKY,CAAO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CAAAA,EAAKA,GAAG,CAAA,CAIpC,GAAIpB,CAAAA,GACFA,EAAU,KAAA,CACVkB,CAAAA,CAAaC,CAAAA,CAAO,KAAA,GAChBjC,CAAAA,EAAS,KAAA,GAAU,KAAA,CAAA,CAAO,OAGhC,IAAM2B,CAAAA,CAAOK,CAAAA,CACb,OAAAA,CAAAA,CAAaC,CAAAA,CAAO,OAAM,CAGnBP,CAAAA,CAAQ,IAAMpE,CAAAA,CACnBwE,EAAUG,CAAAA,CAASA,CAAAA,CAAO,CAAC,CAAA,CAC3BN,CAAAA,CAAQG,EAAUH,CAAAA,CAAOA,CAAAA,CAAK,CAAC,CAAA,CAAK,MACtC,CAAC,CACH,CACF,CAIA,SAASxB,EACPF,CAAAA,CACM,CACFJ,CAAAA,GACFI,CAAAA,CAAK,YAAY,GAAA,CAAIJ,CAAiB,EAElC,SAAA,GAAaA,CAAAA,EACfA,EAAkB,OAAA,CAAQ,GAAA,CAAII,CAAI,CAAA,EAGxC,CAEA,SAASI,CAAAA,CACPJ,EACM,CAKN,IAAMkC,EAAO,CAAC,GAAGlC,CAAAA,CAAK,WAAW,EACjC,IAAA,IAAWe,CAAAA,IAAOmB,EACZnB,CAAAA,CAAI,IAAA,GAAS,YAEfA,CAAAA,CAAI,KAAA,CAAQ,CAAA,CAIZX,CAAAA,CAAkBW,CAAG,CAAA,EACZA,CAAAA,CAAI,OAAS,QAAA,EACtB5D,CAAAA,CAAe4D,CAAG,EAGxB","file":"chunk-3AL2DVPZ.cjs","sourcesContent":["/**\n * Microtask-based effect scheduler with priority levels and deduplication.\n *\n * Render effects (DOM bindings) run before user effects to ensure\n * DOM is consistent before user-side effects execute.\n */\n\nexport interface ScheduledEffect {\n run(): void;\n isRender: boolean;\n /** Depth in the dependency graph (0 = root). Lower depth runs first. */\n depth?: number;\n}\n\nconst pendingRender = new Set<ScheduledEffect>();\nconst pendingUser = new Set<ScheduledEffect>();\nlet flushing = false;\nlet batchDepth = 0;\nlet flushScheduled = false;\nlet reflushNeeded = false;\n\nconst MAX_EFFECT_RUNS = 3;\nconst MAX_FLUSH_ITERATIONS = 100;\n\nfunction flush(): void {\n if (flushing) return;\n flushing = true;\n flushScheduled = false;\n\n // Fresh per-effect run counter for THIS flush — reset every time\n const runCounts = new Map<ScheduledEffect, number>();\n let circularDetected = false;\n\n // Process render effects first, then user effects.\n // Effects may enqueue more effects during flush, so loop until empty.\n let iterations = 0;\n while ((pendingRender.size > 0 || pendingUser.size > 0) && iterations < MAX_FLUSH_ITERATIONS) {\n iterations++;\n\n // Drain render effects (sorted by depth — parents before children)\n if (pendingRender.size > 0) {\n const queue = [...pendingRender].sort(byDepth);\n pendingRender.clear();\n for (const fx of queue) {\n const count = (runCounts.get(fx) ?? 0) + 1;\n runCounts.set(fx, count);\n if (count > MAX_EFFECT_RUNS) {\n circularDetected = true;\n continue; // skip this effect — it's in a cycle\n }\n fx.run();\n }\n }\n\n // Drain user effects (sorted by depth)\n if (pendingUser.size > 0) {\n const queue = [...pendingUser].sort(byDepth);\n pendingUser.clear();\n for (const fx of queue) {\n const count = (runCounts.get(fx) ?? 0) + 1;\n runCounts.set(fx, count);\n if (count > MAX_EFFECT_RUNS) {\n circularDetected = true;\n continue; // skip this effect — it's in a cycle\n }\n fx.run();\n }\n }\n\n // If all remaining effects were skipped due to cycle detection, break\n if (circularDetected && pendingRender.size === 0 && pendingUser.size === 0) {\n break;\n }\n }\n\n if (circularDetected || iterations >= MAX_FLUSH_ITERATIONS) {\n pendingRender.clear();\n pendingUser.clear();\n console.error(\n '[AkashJS] Circular dependency detected between effects. ' +\n 'Two or more effects are writing to each other\\'s dependencies. ' +\n 'The cycle has been broken after ' + iterations + ' iterations.'\n );\n }\n\n flushing = false;\n\n // If effects were scheduled during the tail end of this flush (after the\n // while loop's condition was last checked), re-flush them now.\n if (reflushNeeded) {\n reflushNeeded = false;\n if (pendingRender.size > 0 || pendingUser.size > 0) {\n flush();\n }\n }\n}\n\nexport function scheduleEffect(fx: ScheduledEffect): void {\n if (fx.isRender) {\n pendingRender.add(fx);\n } else {\n pendingUser.add(fx);\n }\n\n if (batchDepth === 0 && !flushing) {\n flush();\n } else if (flushing) {\n // Effects were added during a flush. The while loop may still pick them\n // up, but if it has already checked the queues and found them empty,\n // we need a re-flush after the current one completes.\n reflushNeeded = true;\n }\n}\n\n/**\n * Batch multiple signal writes — subscribers are notified only once\n * at the end of the batch.\n */\nexport function batch(fn: () => void): void {\n batchDepth++;\n try {\n fn();\n } finally {\n batchDepth--;\n if (batchDepth === 0) {\n flush();\n }\n }\n}\n\n/** Enter/exit batch depth — used by computed recompute to prevent mid-evaluation flush */\nexport function enterBatch(): void { batchDepth++; }\nexport function exitBatch(): void {\n batchDepth--;\n if (batchDepth === 0) flush();\n}\n\n/** Sort effects by depth (lower depth first = parents before children) */\nfunction byDepth(a: ScheduledEffect, b: ScheduledEffect): number {\n return (a.depth ?? 0) - (b.depth ?? 0);\n}\n\n/** Synchronously flush all pending effects. Useful for testing. */\nexport function flushSync(): void {\n flush();\n}\n","/**\n * Performance profiling utilities.\n *\n * Measure component render times, signal propagation, and\n * effect execution for performance debugging.\n *\n * ```ts\n * import { startProfiling, stopProfiling, getProfile } from '@akashjs/runtime';\n *\n * startProfiling();\n * // ... do stuff ...\n * stopProfiling();\n * console.table(getProfile().entries);\n * ```\n */\n\n// --- Types ---\n\nexport interface PerfEntry {\n type: 'render' | 'effect' | 'signal-update' | 'computed';\n name: string;\n duration: number;\n timestamp: number;\n}\n\nexport interface PerfProfile {\n entries: PerfEntry[];\n totalDuration: number;\n startTime: number;\n endTime: number;\n}\n\nexport interface PerfSummary {\n totalRenders: number;\n totalEffects: number;\n totalSignalUpdates: number;\n totalComputedEvals: number;\n avgRenderTime: number;\n avgEffectTime: number;\n slowestRender: PerfEntry | null;\n slowestEffect: PerfEntry | null;\n}\n\n// --- Profiling state ---\n\nlet profiling = false;\nlet entries: PerfEntry[] = [];\nlet profileStart = 0;\n\n// --- Public API ---\n\n/**\n * Start profiling. Clears previous data.\n */\nexport function startProfiling(): void {\n profiling = true;\n entries = [];\n profileStart = performance.now();\n}\n\n/**\n * Stop profiling.\n */\nexport function stopProfiling(): PerfProfile {\n profiling = false;\n const endTime = performance.now();\n\n return {\n entries: [...entries],\n totalDuration: endTime - profileStart,\n startTime: profileStart,\n endTime,\n };\n}\n\n/**\n * Check if profiling is active.\n */\nexport function isProfiling(): boolean {\n return profiling;\n}\n\n/**\n * Record a performance entry (called internally by the framework).\n */\nexport function recordPerfEntry(\n type: PerfEntry['type'],\n name: string,\n duration: number,\n): void {\n if (!profiling) return;\n entries.push({\n type,\n name,\n duration,\n timestamp: performance.now(),\n });\n}\n\n/**\n * Get a summary of the profiling data.\n */\nexport function getProfileSummary(profile: PerfProfile): PerfSummary {\n const renders = profile.entries.filter((e) => e.type === 'render');\n const effects = profile.entries.filter((e) => e.type === 'effect');\n const signalUpdates = profile.entries.filter((e) => e.type === 'signal-update');\n const computeds = profile.entries.filter((e) => e.type === 'computed');\n\n const avg = (arr: PerfEntry[]) =>\n arr.length > 0 ? arr.reduce((s, e) => s + e.duration, 0) / arr.length : 0;\n\n const slowest = (arr: PerfEntry[]) =>\n arr.length > 0 ? arr.reduce((s, e) => (e.duration > s.duration ? e : s)) : null;\n\n return {\n totalRenders: renders.length,\n totalEffects: effects.length,\n totalSignalUpdates: signalUpdates.length,\n totalComputedEvals: computeds.length,\n avgRenderTime: avg(renders),\n avgEffectTime: avg(effects),\n slowestRender: slowest(renders),\n slowestEffect: slowest(effects),\n };\n}\n\n// --- Measurement helpers ---\n\n/**\n * Measure the time to execute a function.\n *\n * ```ts\n * const { result, duration } = measureSync('my-operation', () => expensiveWork());\n * ```\n */\nexport function measureSync<T>(name: string, fn: () => T): { result: T; duration: number } {\n const start = performance.now();\n const result = fn();\n const duration = performance.now() - start;\n recordPerfEntry('render', name, duration);\n return { result, duration };\n}\n\n/**\n * Measure the time to execute an async function.\n */\nexport async function measureAsync<T>(\n name: string,\n fn: () => Promise<T>,\n): Promise<{ result: T; duration: number }> {\n const start = performance.now();\n const result = await fn();\n const duration = performance.now() - start;\n recordPerfEntry('render', name, duration);\n return { result, duration };\n}\n\n/**\n * Create a named timer for manual start/stop measurement.\n *\n * ```ts\n * const timer = createTimer('data-fetch');\n * timer.start();\n * await fetch(...);\n * timer.stop(); // records the entry\n * timer.duration; // ms\n * ```\n */\nexport function createTimer(name: string) {\n let startTime = 0;\n let dur = 0;\n\n return {\n start() { startTime = performance.now(); },\n stop() {\n dur = performance.now() - startTime;\n recordPerfEntry('render', name, dur);\n },\n get duration() { return dur; },\n };\n}\n\n/**\n * Format a profile for console output.\n */\nexport function formatProfile(profile: PerfProfile): string {\n const summary = getProfileSummary(profile);\n let out = '\\n Performance Profile\\n';\n out += ' ' + '─'.repeat(50) + '\\n';\n out += ` Duration: ${profile.totalDuration.toFixed(1)} ms\\n`;\n out += ` Renders: ${summary.totalRenders} (avg ${summary.avgRenderTime.toFixed(2)} ms)\\n`;\n out += ` Effects: ${summary.totalEffects} (avg ${summary.avgEffectTime.toFixed(2)} ms)\\n`;\n out += ` Signal updates: ${summary.totalSignalUpdates}\\n`;\n out += ` Computed evals: ${summary.totalComputedEvals}\\n`;\n\n if (summary.slowestRender) {\n out += ` Slowest render: ${summary.slowestRender.name} (${summary.slowestRender.duration.toFixed(2)} ms)\\n`;\n }\n if (summary.slowestEffect) {\n out += ` Slowest effect: ${summary.slowestEffect.name} (${summary.slowestEffect.duration.toFixed(2)} ms)\\n`;\n }\n\n out += ' ' + '─'.repeat(50) + '\\n';\n return out;\n}\n","/**\n * Fine-grained reactivity system.\n *\n * Inspired by SolidJS/Preact Signals. Provides signal(), computed(),\n * effect(), and untrack() primitives with automatic dependency tracking\n * and glitch-free diamond dependency resolution.\n */\n\nimport { scheduleEffect, batch, enterBatch, exitBatch, type ScheduledEffect } from './scheduler.js';\nimport { recordPerfEntry, isProfiling } from './perf.js';\n\nconst __DEV__ = typeof process === 'undefined' || process.env?.NODE_ENV !== 'production';\n\n/** Depth counter to defer effect scheduling until all notifications complete (glitch-free) */\nlet notifyDepth = 0;\n\n// --- Tracking scope ---\n\ntype Subscriber = EffectNode | ComputedNode<any>;\n\nlet currentSubscriber: Subscriber | null = null;\n\n// --- Signal ---\n\nexport interface Signal<T> {\n /** Read the current value (tracks dependency if inside a reactive scope) */\n (): T;\n /** Set a new value */\n set(value: T): void;\n /** Update the value using the previous value */\n update(fn: (prev: T) => T): void;\n /** Read without tracking (no dependency registered) */\n peek(): T;\n}\n\nexport type ReadonlySignal<T> = () => T;\n\ninterface SignalNode<T> {\n value: T;\n subscribers: Set<Subscriber>;\n equals: (a: T, b: T) => boolean;\n}\n\nexport function signal<T>(\n initialValue: T,\n options?: { equals?: (a: T, b: T) => boolean },\n): Signal<T> {\n const node: SignalNode<T> = {\n value: initialValue,\n subscribers: new Set(),\n equals: options?.equals ?? Object.is,\n };\n\n const read = (): T => {\n trackSubscriber(node);\n return node.value;\n };\n\n read.set = (value: T): void => {\n if (__DEV__ && currentSubscriber && currentSubscriber._tag === 'computed') {\n console.warn('[AkashJS] Writing to a signal inside computed() is not allowed. The write will be lost on the next evaluation.');\n }\n if (node.equals(node.value, value)) return;\n node.value = value;\n if (isProfiling()) recordPerfEntry('signal-update', 'signal.set', 0);\n batch(() => { notifySubscribers(node); });\n };\n\n read.update = (fn: (prev: T) => T): void => {\n read.set(fn(node.value));\n };\n\n read.peek = (): T => node.value;\n\n return read;\n}\n\n// --- Computed ---\n\nconst enum ComputedState {\n Clean = 0,\n Dirty = 1,\n}\n\ninterface ComputedNode<T> {\n _tag: 'computed';\n fn: () => T;\n value: T | undefined;\n state: ComputedState;\n subscribers: Set<Subscriber>;\n sources: Set<SignalNode<any> | ComputedNode<any>>;\n equals: (a: T, b: T) => boolean;\n}\n\nexport function computed<T>(\n fn: () => T,\n options?: { equals?: (a: T, b: T) => boolean },\n): ReadonlySignal<T> {\n const node: ComputedNode<T> = {\n _tag: 'computed',\n fn,\n value: undefined,\n state: ComputedState.Dirty,\n subscribers: new Set(),\n sources: new Set(),\n equals: options?.equals ?? Object.is,\n };\n\n const read = (): T => {\n // Track this computed as a dependency of the current subscriber\n if (currentSubscriber) {\n node.subscribers.add(currentSubscriber);\n if ('sources' in currentSubscriber) {\n currentSubscriber.sources.add(node);\n }\n }\n\n if (node.state !== ComputedState.Clean) {\n recompute(node);\n }\n\n return node.value as T;\n };\n\n return read;\n}\n\nconst computingSet = new Set<ComputedNode<unknown>>();\n\n/**\n * @param skipEffectNotify - When true, don't schedule effects in notifySubscribers.\n * Used when recompute is called from runEffect's dirty check — the effect is\n * already being processed, re-scheduling it would trigger the circular run limit.\n */\nfunction recompute<T>(node: ComputedNode<T>, skipEffectNotify = false): void {\n // Detect circular computed dependencies — only if THIS node is already being computed\n if (computingSet.has(node as ComputedNode<unknown>)) {\n throw new Error('[AkashJS] Circular dependency detected between computed values.');\n }\n computingSet.add(node as ComputedNode<unknown>);\n\n // Prevent effects from flushing while computed is evaluating.\n // Without this, a computed that reads other dirty computeds can trigger\n // notifySubscribers → scheduleEffect → flush → re-enter recompute on\n // a node still in computingSet (false circular detection).\n enterBatch();\n\n // Clean up old source subscriptions\n for (const source of node.sources) {\n source.subscribers.delete(node);\n }\n node.sources.clear();\n\n const prevSubscriber = currentSubscriber;\n currentSubscriber = node;\n\n const _t0 = isProfiling() ? performance.now() : 0;\n try {\n const newValue = node.fn();\n const isFirst = node.value === undefined;\n const changed = isFirst || !node.equals(node.value as T, newValue);\n node.value = newValue;\n node.state = ComputedState.Clean;\n\n // Only propagate if value actually changed (skip first computation —\n // the effect that triggered the read is already running).\n if (changed && !isFirst) {\n if (skipEffectNotify) {\n // Only mark downstream computeds as dirty — don't schedule effects.\n // Effects are already handled by the runEffect that triggered this recompute.\n for (const sub of [...node.subscribers]) {\n if (sub._tag === 'computed') {\n sub.state = ComputedState.Dirty;\n // Recursively propagate to further computeds\n for (const sub2 of [...sub.subscribers]) {\n if (sub2._tag === 'computed') sub2.state = ComputedState.Dirty;\n }\n }\n }\n } else {\n notifySubscribers(node);\n }\n }\n } finally {\n if (_t0) recordPerfEntry('computed', 'computed', performance.now() - _t0);\n computingSet.delete(node as ComputedNode<unknown>);\n currentSubscriber = prevSubscriber;\n exitBatch();\n }\n}\n\n// --- Effect ---\n\ninterface EffectNode extends ScheduledEffect {\n _tag: 'effect';\n fn: () => void | (() => void);\n cleanup: (() => void) | null;\n sources: Set<SignalNode<any> | ComputedNode<any>>;\n disposed: boolean;\n isRender: boolean;\n}\n\nexport function effect(\n fn: () => void | (() => void),\n options?: { render?: boolean },\n): () => void {\n const node: EffectNode = {\n _tag: 'effect',\n fn,\n cleanup: null,\n sources: new Set(),\n disposed: false,\n isRender: options?.render ?? false,\n run() {\n runEffect(node);\n },\n };\n\n // Run immediately to establish dependencies\n runEffect(node);\n\n // Return dispose function\n return () => {\n node.disposed = true;\n cleanupEffect(node);\n for (const source of node.sources) {\n source.subscribers.delete(node);\n }\n node.sources.clear();\n };\n}\n\nfunction runEffect(node: EffectNode): void {\n if (node.disposed) return;\n\n // Before re-running, check if any dirty computed source actually changed.\n // Compare against per-effect cached values (not the shared source.value which\n // may have been updated by another effect's recompute already).\n if (node.sources.size > 0) {\n let anyChanged = false;\n for (const source of node.sources) {\n if ('_tag' in source && source._tag === 'computed') {\n if (source.state === ComputedState.Dirty) {\n recompute(source, true);\n }\n // Compare against this effect's last-seen value (not the shared source.value\n // which another effect's recompute may have already updated)\n const cachedValues = (node as any)._lastSeen as Map<unknown, unknown> | undefined;\n if (cachedValues?.has(source)) {\n const lastSeen = cachedValues.get(source);\n if (!source.equals(lastSeen as never, source.value as never)) {\n anyChanged = true;\n }\n } else {\n // No cached value — first dirty check after creation. Always run.\n anyChanged = true;\n }\n } else {\n // Plain signal source — if we got scheduled, something changed\n anyChanged = true;\n }\n }\n if (!anyChanged) return;\n }\n\n // Clean up previous run\n cleanupEffect(node);\n\n // Save old sources in case the effect throws — we need to re-subscribe\n const prevSources = new Set(node.sources);\n\n // Clean up old source subscriptions\n for (const source of node.sources) {\n source.subscribers.delete(node);\n }\n node.sources.clear();\n\n const prevSubscriber = currentSubscriber;\n currentSubscriber = node;\n const _t0 = isProfiling() ? performance.now() : 0;\n\n try {\n const result = node.fn();\n if (typeof result === 'function') {\n node.cleanup = result;\n }\n } catch (err) {\n // Re-subscribe to previous sources so the effect can recover on next change\n for (const source of prevSources) {\n source.subscribers.add(node);\n node.sources.add(source);\n }\n console.error('[AkashJS] Error in effect (will retry on next signal change):', err);\n } finally {\n if (_t0) recordPerfEntry('effect', 'effect', performance.now() - _t0);\n currentSubscriber = prevSubscriber;\n\n // Cache computed values so the next dirty check compares against THIS effect's\n // last-seen values, not the shared source.value (which another effect may update)\n const lastSeen = new Map();\n for (const source of node.sources) {\n if ('_tag' in source && source._tag === 'computed') {\n lastSeen.set(source, source.value);\n }\n }\n if (lastSeen.size > 0) (node as any)._lastSeen = lastSeen;\n }\n}\n\nfunction cleanupEffect(node: EffectNode): void {\n if (node.cleanup) {\n try {\n node.cleanup();\n } catch (err) {\n console.error('[AkashJS] Error in effect cleanup (ignored):', err);\n }\n node.cleanup = null;\n }\n}\n\n// --- Untrack ---\n\n/** Execute a function without tracking any signal reads */\nexport function untrack<T>(fn: () => T): T {\n const prev = currentSubscriber;\n currentSubscriber = null;\n try {\n return fn();\n } finally {\n currentSubscriber = prev;\n }\n}\n\n// --- on() helper ---\n\n/**\n * Create an effect callback that only tracks specific signals.\n * All other signal reads inside the callback are untracked.\n *\n * ```ts\n * effect(on(url, (currentUrl, prevUrl) => {\n * fetch(currentUrl, options()); // options() not tracked\n * }));\n *\n * effect(on([url, page], ([u, p], prev) => {\n * fetch(`${u}?page=${p}`);\n * }));\n * ```\n */\nexport function on<T>(\n dep: () => T,\n fn: (value: T, prev: T | undefined) => void | (() => void),\n options?: { defer?: boolean },\n): () => void | (() => void);\nexport function on<T extends readonly (() => unknown)[]>(\n deps: [...T],\n fn: (values: { [K in keyof T]: ReturnType<T[K]> }, prev: { [K in keyof T]: ReturnType<T[K]> } | undefined) => void | (() => void),\n options?: { defer?: boolean },\n): () => void | (() => void);\nexport function on(\n deps: (() => any) | (() => any)[],\n fn: (values: any, prev: any) => void | (() => void),\n options?: { defer?: boolean },\n): () => void | (() => void) {\n const isArray = Array.isArray(deps);\n const depArray = isArray ? deps : [deps];\n let prevValues: unknown[] | undefined;\n let isFirst = true;\n\n return () => {\n // Track only the specified deps\n const values = depArray.map(d => d());\n\n // Skip the initial effect run — only fire on actual changes\n // (pass { defer: false } to opt out and run immediately)\n if (isFirst) {\n isFirst = false;\n prevValues = values.slice();\n if (options?.defer !== false) return;\n }\n\n const prev = prevValues;\n prevValues = values.slice();\n\n // Run fn without tracking so reads inside don't create subscriptions\n return untrack(() => fn(\n isArray ? values : values[0],\n prev ? (isArray ? prev : prev[0]) : undefined,\n ));\n };\n}\n\n// --- Internal helpers ---\n\nfunction trackSubscriber(\n node: SignalNode<any> | ComputedNode<any>,\n): void {\n if (currentSubscriber) {\n node.subscribers.add(currentSubscriber);\n\n if ('sources' in currentSubscriber) {\n currentSubscriber.sources.add(node);\n }\n }\n}\n\nfunction notifySubscribers(\n node: SignalNode<any> | ComputedNode<any>,\n): void {\n // Snapshot subscribers before iterating. Effects that run synchronously\n // during flush() will delete and re-add themselves to the Set, and JS\n // Set iterators visit newly added entries — causing an infinite loop\n // without the snapshot.\n const subs = [...node.subscribers];\n for (const sub of subs) {\n if (sub._tag === 'computed') {\n // Mark dirty. The computed will re-evaluate lazily when read.\n sub.state = ComputedState.Dirty;\n // Propagate through the computed chain to reach effects.\n // The effects will re-read the computed, triggering recompute,\n // and only update DOM if the value actually changed.\n notifySubscribers(sub);\n } else if (sub._tag === 'effect') {\n scheduleEffect(sub);\n }\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/scheduler.ts","../src/perf.ts","../src/signals.ts"],"names":["pendingRender","pendingUser","flushing","batchDepth","reflushNeeded","MAX_EFFECT_RUNS","MAX_FLUSH_ITERATIONS","flush","runCounts","circularDetected","iterations","queue","byDepth","fx","count","scheduleEffect","batch","fn","enterBatch","exitBatch","a","b","flushSync","profiling","entries","profileStart","startProfiling","stopProfiling","endTime","isProfiling","recordPerfEntry","type","name","duration","getProfileSummary","profile","renders","e","effects","signalUpdates","computeds","avg","arr","s","slowest","measureSync","start","result","measureAsync","createTimer","startTime","dur","formatProfile","summary","out","__DEV__","currentSubscriber","signal","initialValue","options","node","read","trackSubscriber","value","notifySubscribers","computed","recompute","computingSet","skipEffectNotify","source","prevSubscriber","_t0","newValue","isFirst","changed","sub","sub2","effect","runEffect","cleanupEffect","anyChanged","cachedValues","lastSeen","prevSources","err","untrack","prev","on","deps","isArray","depArray","prevValues","values","d","subs"],"mappings":"AAcA,IAAMA,CAAAA,CAAgB,IAAI,GAAA,CACpBC,CAAAA,CAAc,IAAI,GAAA,CACpBC,CAAAA,CAAW,KAAA,CACXC,CAAAA,CAAa,CAAA,CAEbC,EAAgB,KAAA,CAEdC,CAAAA,CAAkB,CAAA,CAClBC,CAAAA,CAAuB,IAE7B,SAASC,CAAAA,EAAc,CACrB,GAAIL,CAAAA,CAAU,OACdA,CAAAA,CAAW,IACM,CAGjB,IAAMM,EAAY,IAAI,GAAA,CAClBC,CAAAA,CAAmB,KAAA,CAInBC,CAAAA,CAAa,CAAA,CACjB,KAAA,CAAQV,CAAAA,CAAc,IAAA,CAAO,CAAA,EAAKC,CAAAA,CAAY,IAAA,CAAO,CAAA,GAAMS,CAAAA,CAAaJ,CAAAA,EAAsB,CAI5F,GAHAI,CAAAA,EAAAA,CAGIV,CAAAA,CAAc,IAAA,CAAO,CAAA,CAAG,CAC1B,IAAMW,CAAAA,CAAQ,CAAC,GAAGX,CAAa,CAAA,CAAE,IAAA,CAAKY,CAAO,CAAA,CAC7CZ,CAAAA,CAAc,KAAA,EAAM,CACpB,QAAWa,CAAAA,IAAMF,CAAAA,CAAO,CACtB,IAAMG,CAAAA,CAAAA,CAASN,CAAAA,CAAU,GAAA,CAAIK,CAAE,CAAA,EAAK,CAAA,EAAK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AA,CAAOK,CAAAA,CACb,OAAAA,CAAAA,CAAaC,CAAAA,CAAO,OAAM,CAGnBP,CAAAA,CAAQ,IAAMpE,CAAAA,CACnBwE,EAAUG,CAAAA,CAASA,CAAAA,CAAO,CAAC,CAAA,CAC3BN,CAAAA,CAAQG,EAAUH,CAAAA,CAAOA,CAAAA,CAAK,CAAC,CAAA,CAAK,MACtC,CAAC,CACH,CACF,CAIA,SAASxB,EACPF,CAAAA,CACM,CACFJ,CAAAA,GACFI,CAAAA,CAAK,YAAY,GAAA,CAAIJ,CAAiB,EAElC,SAAA,GAAaA,CAAAA,EACfA,EAAkB,OAAA,CAAQ,GAAA,CAAII,CAAI,CAAA,EAGxC,CAEA,SAASI,CAAAA,CACPJ,EACM,CAKN,IAAMkC,EAAO,CAAC,GAAGlC,CAAAA,CAAK,WAAW,EACjC,IAAA,IAAWe,CAAAA,IAAOmB,EACZnB,CAAAA,CAAI,IAAA,GAAS,YAEfA,CAAAA,CAAI,KAAA,CAAQ,CAAA,CAIZX,CAAAA,CAAkBW,CAAG,CAAA,EACZA,CAAAA,CAAI,OAAS,QAAA,EACtB5D,CAAAA,CAAe4D,CAAG,EAGxB","file":"chunk-3GRR4VW2.js","sourcesContent":["/**\n * Microtask-based effect scheduler with priority levels and deduplication.\n *\n * Render effects (DOM bindings) run before user effects to ensure\n * DOM is consistent before user-side effects execute.\n */\n\nexport interface ScheduledEffect {\n run(): void;\n isRender: boolean;\n /** Depth in the dependency graph (0 = root). Lower depth runs first. */\n depth?: number;\n}\n\nconst pendingRender = new Set<ScheduledEffect>();\nconst pendingUser = new Set<ScheduledEffect>();\nlet flushing = false;\nlet batchDepth = 0;\nlet flushScheduled = false;\nlet reflushNeeded = false;\n\nconst MAX_EFFECT_RUNS = 3;\nconst MAX_FLUSH_ITERATIONS = 100;\n\nfunction flush(): void {\n if (flushing) return;\n flushing = true;\n flushScheduled = false;\n\n // Fresh per-effect run counter for THIS flush — reset every time\n const runCounts = new Map<ScheduledEffect, number>();\n let circularDetected = false;\n\n // Process render effects first, then user effects.\n // Effects may enqueue more effects during flush, so loop until empty.\n let iterations = 0;\n while ((pendingRender.size > 0 || pendingUser.size > 0) && iterations < MAX_FLUSH_ITERATIONS) {\n iterations++;\n\n // Drain render effects (sorted by depth — parents before children)\n if (pendingRender.size > 0) {\n const queue = [...pendingRender].sort(byDepth);\n pendingRender.clear();\n for (const fx of queue) {\n const count = (runCounts.get(fx) ?? 0) + 1;\n runCounts.set(fx, count);\n if (count > MAX_EFFECT_RUNS) {\n circularDetected = true;\n continue; // skip this effect — it's in a cycle\n }\n fx.run();\n }\n }\n\n // Drain user effects (sorted by depth)\n if (pendingUser.size > 0) {\n const queue = [...pendingUser].sort(byDepth);\n pendingUser.clear();\n for (const fx of queue) {\n const count = (runCounts.get(fx) ?? 0) + 1;\n runCounts.set(fx, count);\n if (count > MAX_EFFECT_RUNS) {\n circularDetected = true;\n continue; // skip this effect — it's in a cycle\n }\n fx.run();\n }\n }\n\n // If all remaining effects were skipped due to cycle detection, break\n if (circularDetected && pendingRender.size === 0 && pendingUser.size === 0) {\n break;\n }\n }\n\n if (circularDetected || iterations >= MAX_FLUSH_ITERATIONS) {\n pendingRender.clear();\n pendingUser.clear();\n console.error(\n '[AkashJS] Circular dependency detected between effects. ' +\n 'Two or more effects are writing to each other\\'s dependencies. ' +\n 'The cycle has been broken after ' + iterations + ' iterations.'\n );\n }\n\n flushing = false;\n\n // If effects were scheduled during the tail end of this flush (after the\n // while loop's condition was last checked), re-flush them now.\n if (reflushNeeded) {\n reflushNeeded = false;\n if (pendingRender.size > 0 || pendingUser.size > 0) {\n flush();\n }\n }\n}\n\nexport function scheduleEffect(fx: ScheduledEffect): void {\n if (fx.isRender) {\n pendingRender.add(fx);\n } else {\n pendingUser.add(fx);\n }\n\n if (batchDepth === 0 && !flushing) {\n flush();\n } else if (flushing) {\n // Effects were added during a flush. The while loop may still pick them\n // up, but if it has already checked the queues and found them empty,\n // we need a re-flush after the current one completes.\n reflushNeeded = true;\n }\n}\n\n/**\n * Batch multiple signal writes — subscribers are notified only once\n * at the end of the batch.\n */\nexport function batch<T>(fn: () => T): T {\n batchDepth++;\n try {\n return fn();\n } finally {\n batchDepth--;\n if (batchDepth === 0) {\n flush();\n }\n }\n}\n\n/** Enter/exit batch depth — used by computed recompute to prevent mid-evaluation flush */\nexport function enterBatch(): void { batchDepth++; }\nexport function exitBatch(): void {\n batchDepth--;\n if (batchDepth === 0) flush();\n}\n\n/** Sort effects by depth (lower depth first = parents before children) */\nfunction byDepth(a: ScheduledEffect, b: ScheduledEffect): number {\n return (a.depth ?? 0) - (b.depth ?? 0);\n}\n\n/** Synchronously flush all pending effects. Useful for testing. */\nexport function flushSync(): void {\n flush();\n}\n","/**\n * Performance profiling utilities.\n *\n * Measure component render times, signal propagation, and\n * effect execution for performance debugging.\n *\n * ```ts\n * import { startProfiling, stopProfiling, getProfile } from '@akashjs/runtime';\n *\n * startProfiling();\n * // ... do stuff ...\n * stopProfiling();\n * console.table(getProfile().entries);\n * ```\n */\n\n// --- Types ---\n\nexport interface PerfEntry {\n type: 'render' | 'effect' | 'signal-update' | 'computed';\n name: string;\n duration: number;\n timestamp: number;\n}\n\nexport interface PerfProfile {\n entries: PerfEntry[];\n totalDuration: number;\n startTime: number;\n endTime: number;\n}\n\nexport interface PerfSummary {\n totalRenders: number;\n totalEffects: number;\n totalSignalUpdates: number;\n totalComputedEvals: number;\n avgRenderTime: number;\n avgEffectTime: number;\n slowestRender: PerfEntry | null;\n slowestEffect: PerfEntry | null;\n}\n\n// --- Profiling state ---\n\nlet profiling = false;\nlet entries: PerfEntry[] = [];\nlet profileStart = 0;\n\n// --- Public API ---\n\n/**\n * Start profiling. Clears previous data.\n */\nexport function startProfiling(): void {\n profiling = true;\n entries = [];\n profileStart = performance.now();\n}\n\n/**\n * Stop profiling.\n */\nexport function stopProfiling(): PerfProfile {\n profiling = false;\n const endTime = performance.now();\n\n return {\n entries: [...entries],\n totalDuration: endTime - profileStart,\n startTime: profileStart,\n endTime,\n };\n}\n\n/**\n * Check if profiling is active.\n */\nexport function isProfiling(): boolean {\n return profiling;\n}\n\n/**\n * Record a performance entry (called internally by the framework).\n */\nexport function recordPerfEntry(\n type: PerfEntry['type'],\n name: string,\n duration: number,\n): void {\n if (!profiling) return;\n entries.push({\n type,\n name,\n duration,\n timestamp: performance.now(),\n });\n}\n\n/**\n * Get a summary of the profiling data.\n */\nexport function getProfileSummary(profile: PerfProfile): PerfSummary {\n const renders = profile.entries.filter((e) => e.type === 'render');\n const effects = profile.entries.filter((e) => e.type === 'effect');\n const signalUpdates = profile.entries.filter((e) => e.type === 'signal-update');\n const computeds = profile.entries.filter((e) => e.type === 'computed');\n\n const avg = (arr: PerfEntry[]) =>\n arr.length > 0 ? arr.reduce((s, e) => s + e.duration, 0) / arr.length : 0;\n\n const slowest = (arr: PerfEntry[]) =>\n arr.length > 0 ? arr.reduce((s, e) => (e.duration > s.duration ? e : s)) : null;\n\n return {\n totalRenders: renders.length,\n totalEffects: effects.length,\n totalSignalUpdates: signalUpdates.length,\n totalComputedEvals: computeds.length,\n avgRenderTime: avg(renders),\n avgEffectTime: avg(effects),\n slowestRender: slowest(renders),\n slowestEffect: slowest(effects),\n };\n}\n\n// --- Measurement helpers ---\n\n/**\n * Measure the time to execute a function.\n *\n * ```ts\n * const { result, duration } = measureSync('my-operation', () => expensiveWork());\n * ```\n */\nexport function measureSync<T>(name: string, fn: () => T): { result: T; duration: number } {\n const start = performance.now();\n const result = fn();\n const duration = performance.now() - start;\n recordPerfEntry('render', name, duration);\n return { result, duration };\n}\n\n/**\n * Measure the time to execute an async function.\n */\nexport async function measureAsync<T>(\n name: string,\n fn: () => Promise<T>,\n): Promise<{ result: T; duration: number }> {\n const start = performance.now();\n const result = await fn();\n const duration = performance.now() - start;\n recordPerfEntry('render', name, duration);\n return { result, duration };\n}\n\n/**\n * Create a named timer for manual start/stop measurement.\n *\n * ```ts\n * const timer = createTimer('data-fetch');\n * timer.start();\n * await fetch(...);\n * timer.stop(); // records the entry\n * timer.duration; // ms\n * ```\n */\nexport function createTimer(name: string) {\n let startTime = 0;\n let dur = 0;\n\n return {\n start() { startTime = performance.now(); },\n stop() {\n dur = performance.now() - startTime;\n recordPerfEntry('render', name, dur);\n },\n get duration() { return dur; },\n };\n}\n\n/**\n * Format a profile for console output.\n */\nexport function formatProfile(profile: PerfProfile): string {\n const summary = getProfileSummary(profile);\n let out = '\\n Performance Profile\\n';\n out += ' ' + '─'.repeat(50) + '\\n';\n out += ` Duration: ${profile.totalDuration.toFixed(1)} ms\\n`;\n out += ` Renders: ${summary.totalRenders} (avg ${summary.avgRenderTime.toFixed(2)} ms)\\n`;\n out += ` Effects: ${summary.totalEffects} (avg ${summary.avgEffectTime.toFixed(2)} ms)\\n`;\n out += ` Signal updates: ${summary.totalSignalUpdates}\\n`;\n out += ` Computed evals: ${summary.totalComputedEvals}\\n`;\n\n if (summary.slowestRender) {\n out += ` Slowest render: ${summary.slowestRender.name} (${summary.slowestRender.duration.toFixed(2)} ms)\\n`;\n }\n if (summary.slowestEffect) {\n out += ` Slowest effect: ${summary.slowestEffect.name} (${summary.slowestEffect.duration.toFixed(2)} ms)\\n`;\n }\n\n out += ' ' + '─'.repeat(50) + '\\n';\n return out;\n}\n","/**\n * Fine-grained reactivity system.\n *\n * Inspired by SolidJS/Preact Signals. Provides signal(), computed(),\n * effect(), and untrack() primitives with automatic dependency tracking\n * and glitch-free diamond dependency resolution.\n */\n\nimport { scheduleEffect, batch, enterBatch, exitBatch, type ScheduledEffect } from './scheduler.js';\nimport { recordPerfEntry, isProfiling } from './perf.js';\n\nconst __DEV__ = typeof process === 'undefined' || process.env?.NODE_ENV !== 'production';\n\n/** Depth counter to defer effect scheduling until all notifications complete (glitch-free) */\nlet notifyDepth = 0;\n\n// --- Tracking scope ---\n\ntype Subscriber = EffectNode | ComputedNode<any>;\n\nlet currentSubscriber: Subscriber | null = null;\n\n// --- Signal ---\n\nexport interface Signal<T> {\n /** Read the current value (tracks dependency if inside a reactive scope) */\n (): T;\n /** Set a new value */\n set(value: T): void;\n /** Update the value using the previous value */\n update(fn: (prev: T) => T): void;\n /** Read without tracking (no dependency registered) */\n peek(): T;\n}\n\nexport type ReadonlySignal<T> = () => T;\n\ninterface SignalNode<T> {\n value: T;\n subscribers: Set<Subscriber>;\n equals: (a: T, b: T) => boolean;\n}\n\nexport function signal<T>(\n initialValue: T,\n options?: { equals?: (a: T, b: T) => boolean },\n): Signal<T> {\n const node: SignalNode<T> = {\n value: initialValue,\n subscribers: new Set(),\n equals: options?.equals ?? Object.is,\n };\n\n const read = (): T => {\n trackSubscriber(node);\n return node.value;\n };\n\n read.set = (value: T): void => {\n if (__DEV__ && currentSubscriber && currentSubscriber._tag === 'computed') {\n console.warn('[AkashJS] Writing to a signal inside computed() is not allowed. The write will be lost on the next evaluation.');\n }\n if (node.equals(node.value, value)) return;\n node.value = value;\n if (isProfiling()) recordPerfEntry('signal-update', 'signal.set', 0);\n batch(() => { notifySubscribers(node); });\n };\n\n read.update = (fn: (prev: T) => T): void => {\n read.set(fn(node.value));\n };\n\n read.peek = (): T => node.value;\n\n return read;\n}\n\n// --- Computed ---\n\nconst enum ComputedState {\n Clean = 0,\n Dirty = 1,\n}\n\ninterface ComputedNode<T> {\n _tag: 'computed';\n fn: () => T;\n value: T | undefined;\n state: ComputedState;\n subscribers: Set<Subscriber>;\n sources: Set<SignalNode<any> | ComputedNode<any>>;\n equals: (a: T, b: T) => boolean;\n}\n\nexport function computed<T>(\n fn: () => T,\n options?: { equals?: (a: T, b: T) => boolean },\n): ReadonlySignal<T> {\n const node: ComputedNode<T> = {\n _tag: 'computed',\n fn,\n value: undefined,\n state: ComputedState.Dirty,\n subscribers: new Set(),\n sources: new Set(),\n equals: options?.equals ?? Object.is,\n };\n\n const read = (): T => {\n // Track this computed as a dependency of the current subscriber\n if (currentSubscriber) {\n node.subscribers.add(currentSubscriber);\n if ('sources' in currentSubscriber) {\n currentSubscriber.sources.add(node);\n }\n }\n\n if (node.state !== ComputedState.Clean) {\n recompute(node);\n }\n\n return node.value as T;\n };\n\n return read;\n}\n\nconst computingSet = new Set<ComputedNode<unknown>>();\n\n/**\n * @param skipEffectNotify - When true, don't schedule effects in notifySubscribers.\n * Used when recompute is called from runEffect's dirty check — the effect is\n * already being processed, re-scheduling it would trigger the circular run limit.\n */\nfunction recompute<T>(node: ComputedNode<T>, skipEffectNotify = false): void {\n // Detect circular computed dependencies — only if THIS node is already being computed\n if (computingSet.has(node as ComputedNode<unknown>)) {\n throw new Error('[AkashJS] Circular dependency detected between computed values.');\n }\n computingSet.add(node as ComputedNode<unknown>);\n\n // Prevent effects from flushing while computed is evaluating.\n // Without this, a computed that reads other dirty computeds can trigger\n // notifySubscribers → scheduleEffect → flush → re-enter recompute on\n // a node still in computingSet (false circular detection).\n enterBatch();\n\n // Clean up old source subscriptions\n for (const source of node.sources) {\n source.subscribers.delete(node);\n }\n node.sources.clear();\n\n const prevSubscriber = currentSubscriber;\n currentSubscriber = node;\n\n const _t0 = isProfiling() ? performance.now() : 0;\n try {\n const newValue = node.fn();\n const isFirst = node.value === undefined;\n const changed = isFirst || !node.equals(node.value as T, newValue);\n node.value = newValue;\n node.state = ComputedState.Clean;\n\n // Only propagate if value actually changed (skip first computation —\n // the effect that triggered the read is already running).\n if (changed && !isFirst) {\n if (skipEffectNotify) {\n // Only mark downstream computeds as dirty — don't schedule effects.\n // Effects are already handled by the runEffect that triggered this recompute.\n for (const sub of [...node.subscribers]) {\n if (sub._tag === 'computed') {\n sub.state = ComputedState.Dirty;\n // Recursively propagate to further computeds\n for (const sub2 of [...sub.subscribers]) {\n if (sub2._tag === 'computed') sub2.state = ComputedState.Dirty;\n }\n }\n }\n } else {\n notifySubscribers(node);\n }\n }\n } finally {\n if (_t0) recordPerfEntry('computed', 'computed', performance.now() - _t0);\n computingSet.delete(node as ComputedNode<unknown>);\n currentSubscriber = prevSubscriber;\n exitBatch();\n }\n}\n\n// --- Effect ---\n\ninterface EffectNode extends ScheduledEffect {\n _tag: 'effect';\n fn: () => void | (() => void);\n cleanup: (() => void) | null;\n sources: Set<SignalNode<any> | ComputedNode<any>>;\n disposed: boolean;\n isRender: boolean;\n}\n\nexport function effect(\n fn: () => void | (() => void),\n options?: { render?: boolean },\n): () => void {\n const node: EffectNode = {\n _tag: 'effect',\n fn,\n cleanup: null,\n sources: new Set(),\n disposed: false,\n isRender: options?.render ?? false,\n run() {\n runEffect(node);\n },\n };\n\n // Run immediately to establish dependencies\n runEffect(node);\n\n // Return dispose function\n return () => {\n node.disposed = true;\n cleanupEffect(node);\n for (const source of node.sources) {\n source.subscribers.delete(node);\n }\n node.sources.clear();\n };\n}\n\nfunction runEffect(node: EffectNode): void {\n if (node.disposed) return;\n\n // Before re-running, check if any dirty computed source actually changed.\n // Compare against per-effect cached values (not the shared source.value which\n // may have been updated by another effect's recompute already).\n if (node.sources.size > 0) {\n let anyChanged = false;\n for (const source of node.sources) {\n if ('_tag' in source && source._tag === 'computed') {\n if (source.state === ComputedState.Dirty) {\n recompute(source, true);\n }\n // Compare against this effect's last-seen value (not the shared source.value\n // which another effect's recompute may have already updated)\n const cachedValues = (node as any)._lastSeen as Map<unknown, unknown> | undefined;\n if (cachedValues?.has(source)) {\n const lastSeen = cachedValues.get(source);\n if (!source.equals(lastSeen as never, source.value as never)) {\n anyChanged = true;\n }\n } else {\n // No cached value — first dirty check after creation. Always run.\n anyChanged = true;\n }\n } else {\n // Plain signal source — if we got scheduled, something changed\n anyChanged = true;\n }\n }\n if (!anyChanged) return;\n }\n\n // Clean up previous run\n cleanupEffect(node);\n\n // Save old sources in case the effect throws — we need to re-subscribe\n const prevSources = new Set(node.sources);\n\n // Clean up old source subscriptions\n for (const source of node.sources) {\n source.subscribers.delete(node);\n }\n node.sources.clear();\n\n const prevSubscriber = currentSubscriber;\n currentSubscriber = node;\n const _t0 = isProfiling() ? performance.now() : 0;\n\n try {\n const result = node.fn();\n if (typeof result === 'function') {\n node.cleanup = result;\n }\n } catch (err) {\n // Re-subscribe to previous sources so the effect can recover on next change\n for (const source of prevSources) {\n source.subscribers.add(node);\n node.sources.add(source);\n }\n console.error('[AkashJS] Error in effect (will retry on next signal change):', err);\n } finally {\n if (_t0) recordPerfEntry('effect', 'effect', performance.now() - _t0);\n currentSubscriber = prevSubscriber;\n\n // Cache computed values so the next dirty check compares against THIS effect's\n // last-seen values, not the shared source.value (which another effect may update)\n const lastSeen = new Map();\n for (const source of node.sources) {\n if ('_tag' in source && source._tag === 'computed') {\n lastSeen.set(source, source.value);\n }\n }\n if (lastSeen.size > 0) (node as any)._lastSeen = lastSeen;\n }\n}\n\nfunction cleanupEffect(node: EffectNode): void {\n if (node.cleanup) {\n try {\n node.cleanup();\n } catch (err) {\n console.error('[AkashJS] Error in effect cleanup (ignored):', err);\n }\n node.cleanup = null;\n }\n}\n\n// --- Untrack ---\n\n/** Execute a function without tracking any signal reads */\nexport function untrack<T>(fn: () => T): T {\n const prev = currentSubscriber;\n currentSubscriber = null;\n try {\n return fn();\n } finally {\n currentSubscriber = prev;\n }\n}\n\n// --- on() helper ---\n\n/**\n * Create an effect callback that only tracks specific signals.\n * All other signal reads inside the callback are untracked.\n *\n * ```ts\n * effect(on(url, (currentUrl, prevUrl) => {\n * fetch(currentUrl, options()); // options() not tracked\n * }));\n *\n * effect(on([url, page], ([u, p], prev) => {\n * fetch(`${u}?page=${p}`);\n * }));\n * ```\n */\nexport function on<T>(\n dep: () => T,\n fn: (value: T, prev: T | undefined) => void | (() => void),\n options?: { defer?: boolean },\n): () => void | (() => void);\nexport function on<T extends readonly (() => unknown)[]>(\n deps: [...T],\n fn: (values: { [K in keyof T]: ReturnType<T[K]> }, prev: { [K in keyof T]: ReturnType<T[K]> } | undefined) => void | (() => void),\n options?: { defer?: boolean },\n): () => void | (() => void);\nexport function on(\n deps: (() => any) | (() => any)[],\n fn: (values: any, prev: any) => void | (() => void),\n options?: { defer?: boolean },\n): () => void | (() => void) {\n const isArray = Array.isArray(deps);\n const depArray = isArray ? deps : [deps];\n let prevValues: unknown[] | undefined;\n let isFirst = true;\n\n return () => {\n // Track only the specified deps\n const values = depArray.map(d => d());\n\n // Skip the initial effect run — only fire on actual changes\n // (pass { defer: false } to opt out and run immediately)\n if (isFirst) {\n isFirst = false;\n prevValues = values.slice();\n if (options?.defer !== false) return;\n }\n\n const prev = prevValues;\n prevValues = values.slice();\n\n // Run fn without tracking so reads inside don't create subscriptions\n return untrack(() => fn(\n isArray ? values : values[0],\n prev ? (isArray ? prev : prev[0]) : undefined,\n ));\n };\n}\n\n// --- Internal helpers ---\n\nfunction trackSubscriber(\n node: SignalNode<any> | ComputedNode<any>,\n): void {\n if (currentSubscriber) {\n node.subscribers.add(currentSubscriber);\n\n if ('sources' in currentSubscriber) {\n currentSubscriber.sources.add(node);\n }\n }\n}\n\nfunction notifySubscribers(\n node: SignalNode<any> | ComputedNode<any>,\n): void {\n // Snapshot subscribers before iterating. Effects that run synchronously\n // during flush() will delete and re-add themselves to the Set, and JS\n // Set iterators visit newly added entries — causing an infinite loop\n // without the snapshot.\n const subs = [...node.subscribers];\n for (const sub of subs) {\n if (sub._tag === 'computed') {\n // Mark dirty. The computed will re-evaluate lazily when read.\n sub.state = ComputedState.Dirty;\n // Propagate through the computed chain to reach effects.\n // The effects will re-read the computed, triggering recompute,\n // and only update DOM if the value actually changed.\n notifySubscribers(sub);\n } else if (sub._tag === 'effect') {\n scheduleEffect(sub);\n }\n }\n}\n"]}
@@ -1,2 +1,2 @@
1
- import {l,m}from'./chunk-POLTPHUA.js';function M(t){let n=l(t.initial),a=l(t.context??{}),d=l([t.initial]),g=m(()=>t.states[n()]?.type==="final"),y=m(()=>{let e=t.states[n()];return e?.on?Object.keys(e.on):[]}),S=t.states[t.initial];S?.entry&&S.entry({data:a(),event:""});function E(e,i){if(g())return;let s=n(),o=t.states[s];if(!o?.on)return;let x=o.on[e];if(!x)return;let r,l,f;if(typeof x=="string")r=x;else {let T=x;r=T.target,l=T.guard,f=T.action;}let c={data:a(),event:e,payload:i};if(l&&!l(c))return;o.exit&&o.exit(c),f&&f(c,i),a.set(c.data),n.set(r),d.update(T=>[...T,r]);let v=t.states[r];v?.entry&&v.entry({data:a(),event:e});}function h(e){let i=t.states[n()];if(!i?.on)return false;let s=i.on[e];if(!s)return false;if(typeof s!="string"){let o=s;if(o.guard)return o.guard({data:a(),event:e})}return true}return {state:()=>n(),context:()=>a(),send:E,matches:e=>n()===e,done:g,reset(){n.set(t.initial),a.set(t.context??{}),d.set([t.initial]);},history:()=>d(),can:h,nextEvents:y}}export{M as a};//# sourceMappingURL=chunk-Z5LQV5ND.js.map
2
- //# sourceMappingURL=chunk-Z5LQV5ND.js.map
1
+ import {l,m}from'./chunk-3GRR4VW2.js';function M(t){let n=l(t.initial),a=l(t.context??{}),d=l([t.initial]),g=m(()=>t.states[n()]?.type==="final"),y=m(()=>{let e=t.states[n()];return e?.on?Object.keys(e.on):[]}),S=t.states[t.initial];S?.entry&&S.entry({data:a(),event:""});function E(e,i){if(g())return;let s=n(),o=t.states[s];if(!o?.on)return;let x=o.on[e];if(!x)return;let r,l,f;if(typeof x=="string")r=x;else {let T=x;r=T.target,l=T.guard,f=T.action;}let c={data:a(),event:e,payload:i};if(l&&!l(c))return;o.exit&&o.exit(c),f&&f(c,i),a.set(c.data),n.set(r),d.update(T=>[...T,r]);let v=t.states[r];v?.entry&&v.entry({data:a(),event:e});}function h(e){let i=t.states[n()];if(!i?.on)return false;let s=i.on[e];if(!s)return false;if(typeof s!="string"){let o=s;if(o.guard)return o.guard({data:a(),event:e})}return true}return {state:()=>n(),context:()=>a(),send:E,matches:e=>n()===e,done:g,reset(){n.set(t.initial),a.set(t.context??{}),d.set([t.initial]);},history:()=>d(),can:h,nextEvents:y}}export{M as a};//# sourceMappingURL=chunk-5A7KDBDU.js.map
2
+ //# sourceMappingURL=chunk-5A7KDBDU.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/machine.ts"],"names":["createMachine","config","currentState","signal","contextData","stateHistory","done","computed","nextEvents","stateCfg","initialStateCfg","send","event","payload","state","transition","target","guard","action","t","ctx","h","targetCfg","can"],"mappings":"sCAkGO,SAASA,CAAAA,CAIdC,CAAAA,CAAoF,CACpF,IAAMC,EAAeC,CAAAA,CAAeF,CAAAA,CAAO,OAAO,CAAA,CAC5CG,EAAcD,CAAAA,CAAkBF,CAAAA,CAAO,OAAA,EAAW,EAAe,CAAA,CACjEI,CAAAA,CAAeF,CAAAA,CAAiB,CAACF,CAAAA,CAAO,OAAO,CAAC,CAAA,CAEhDK,EAAOC,CAAAA,CAAS,IACHN,CAAAA,CAAO,MAAA,CAAOC,GAAc,CAAA,EAC5B,IAAA,GAAS,OAC3B,EAEKM,CAAAA,CAAaD,CAAAA,CAAS,IAAgB,CAC1C,IAAME,CAAAA,CAAWR,CAAAA,CAAO,MAAA,CAAOC,GAAc,CAAA,CAC7C,OAAKO,CAAAA,EAAU,GACR,MAAA,CAAO,IAAA,CAAKA,CAAAA,CAAS,EAAE,EADJ,EAE5B,CAAC,CAAA,CAGKC,CAAAA,CAAkBT,CAAAA,CAAO,MAAA,CAAOA,CAAAA,CAAO,OAAO,CAAA,CAChDS,CAAAA,EAAiB,KAAA,EACnBA,CAAAA,CAAgB,MAAM,CAAE,IAAA,CAAMN,CAAAA,EAAY,CAAe,MAAO,EAAG,CAAC,CAAA,CAGtE,SAASO,CAAAA,CAAKC,CAAAA,CAAeC,CAAAA,CAAyB,CACpD,GAAIP,CAAAA,EAAK,CAAG,OAEZ,IAAMQ,EAAQZ,CAAAA,EAAa,CACrBO,CAAAA,CAAWR,CAAAA,CAAO,OAAOa,CAAK,CAAA,CACpC,GAAI,CAACL,CAAAA,EAAU,EAAA,CAAI,OAEnB,IAAMM,EAAaN,CAAAA,CAAS,EAAA,CAAGG,CAAK,CAAA,CACpC,GAAI,CAACG,CAAAA,CAAY,OAEjB,IAAIC,EACAC,CAAAA,CACAC,CAAAA,CAEJ,GAAI,OAAOH,CAAAA,EAAe,QAAA,CACxBC,CAAAA,C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AAMf,GAAY,CAAe,KAAA,CAAAQ,CAAM,CAAC,CAE7D,CACA,OAAO,KACT,CAEA,OAAO,CACL,KAAA,CAAO,IAAMV,GAAa,CAC1B,OAAA,CAAS,IAAME,CAAAA,GACf,IAAA,CAAAO,CAAAA,CACA,OAAA,CAAUG,CAAAA,EAAkBZ,CAAAA,EAAa,GAAMY,CAAAA,CAC/C,IAAA,CAAAR,EACA,KAAA,EAAQ,CACNJ,CAAAA,CAAa,GAAA,CAAID,EAAO,OAAO,CAAA,CAC/BG,CAAAA,CAAY,GAAA,CAAKH,EAAO,OAAA,EAAW,EAAe,CAAA,CAClDI,EAAa,GAAA,CAAI,CAACJ,CAAAA,CAAO,OAAO,CAAC,EACnC,CAAA,CACA,OAAA,CAAS,IAAMI,GAAa,CAC5B,GAAA,CAAAkB,CAAAA,CACA,UAAA,CAAAf,CACF,CACF","file":"chunk-Z5LQV5ND.js","sourcesContent":["/**\n * State machines for complex UI flows.\n *\n * Finite state machines with typed states, events, guards,\n * actions, and reactive current state signal.\n *\n * ```ts\n * const checkout = createMachine({\n * initial: 'cart',\n * states: {\n * cart: { on: { CHECKOUT: 'shipping' } },\n * shipping: { on: { NEXT: 'payment', BACK: 'cart' } },\n * payment: { on: { PAY: 'processing', BACK: 'shipping' } },\n * processing: { on: { SUCCESS: 'complete', FAIL: 'payment' } },\n * complete: { type: 'final' },\n * },\n * });\n *\n * checkout.state(); // 'cart'\n * checkout.send('CHECKOUT');\n * checkout.state(); // 'shipping'\n * checkout.matches('shipping'); // true\n * ```\n */\n\nimport { signal, computed } from './signals.js';\nimport type { ReadonlySignal } from './signals.js';\n\n// =========================================================================\n// Types\n// =========================================================================\n\nexport interface MachineConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Initial state */\n initial: TState;\n /** Initial context data */\n context?: TContext;\n /** State definitions */\n states: Record<TState, StateConfig<TState, TEvent, TContext>>;\n}\n\nexport interface StateConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Transitions: event → target state */\n on?: Partial<Record<TEvent, TState | TransitionConfig<TState, TEvent, TContext>>>;\n /** Entry action — runs when entering this state */\n entry?: (ctx: MachineContext<TContext>) => void;\n /** Exit action — runs when leaving this state */\n exit?: (ctx: MachineContext<TContext>) => void;\n /** Final state — machine stops accepting events */\n type?: 'final';\n}\n\nexport interface TransitionConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Target state */\n target: TState;\n /** Guard — transition only if this returns true */\n guard?: (ctx: MachineContext<TContext>) => boolean;\n /** Action — runs during the transition */\n action?: (ctx: MachineContext<TContext>) => void;\n}\n\nexport interface MachineContext<TContext> {\n /** The context data (mutable) */\n data: TContext;\n /** The current event that triggered the transition */\n event: string;\n /** Optional payload passed with the event via send(event, payload) */\n payload?: unknown;\n}\n\nexport interface Machine<TState extends string, TEvent extends string, TContext = unknown> {\n /** Current state (reactive signal) */\n state: ReadonlySignal<TState>;\n /** Context data (reactive signal) */\n context: ReadonlySignal<TContext>;\n /** Send an event to the machine */\n send(event: TEvent, payload?: Record<string, unknown>): void;\n /** Check if currently in a specific state */\n matches(state: TState): boolean;\n /** Whether the machine is in a final state */\n done: ReadonlySignal<boolean>;\n /** Reset to initial state */\n reset(): void;\n /** State history */\n history: ReadonlySignal<TState[]>;\n /** Can a specific event be sent in the current state? */\n can(event: TEvent): boolean;\n /** Get all possible events from current state */\n nextEvents: ReadonlySignal<TEvent[]>;\n}\n\n// =========================================================================\n// createMachine\n// =========================================================================\n\n/**\n * Create a finite state machine.\n */\nexport function createMachine<\n TState extends string,\n TEvent extends string,\n TContext = unknown,\n>(config: MachineConfig<TState, TEvent, TContext>): Machine<TState, TEvent, TContext> {\n const currentState = signal<TState>(config.initial);\n const contextData = signal<TContext>((config.context ?? {}) as TContext);\n const stateHistory = signal<TState[]>([config.initial]);\n\n const done = computed(() => {\n const stateCfg = config.states[currentState()];\n return stateCfg?.type === 'final';\n });\n\n const nextEvents = computed((): TEvent[] => {\n const stateCfg = config.states[currentState()];\n if (!stateCfg?.on) return [];\n return Object.keys(stateCfg.on) as TEvent[];\n });\n\n // Run entry action for initial state\n const initialStateCfg = config.states[config.initial];\n if (initialStateCfg?.entry) {\n initialStateCfg.entry({ data: contextData() as TContext, event: '' });\n }\n\n function send(event: TEvent, payload?: unknown): void {\n if (done()) return; // Final state — no more transitions\n\n const state = currentState();\n const stateCfg = config.states[state];\n if (!stateCfg?.on) return;\n\n const transition = stateCfg.on[event];\n if (!transition) return;\n\n let target: TState;\n let guard: ((ctx: MachineContext<TContext>) => boolean) | undefined;\n let action: ((ctx: MachineContext<TContext>) => void) | undefined;\n\n if (typeof transition === 'string') {\n target = transition as TState;\n } else {\n const t = transition as TransitionConfig<TState, TEvent, TContext>;\n target = t.target;\n guard = t.guard;\n action = t.action;\n }\n\n const ctx: MachineContext<TContext> = {\n data: contextData() as TContext,\n event,\n payload,\n };\n\n // Check guard\n if (guard && !guard(ctx)) return;\n\n // Exit action\n if (stateCfg.exit) {\n stateCfg.exit(ctx);\n }\n\n // Transition action — pass payload as second argument for convenience\n if (action) {\n (action as Function)(ctx, payload);\n }\n\n // Update context if modified\n contextData.set(ctx.data);\n\n // Enter new state\n currentState.set(target);\n stateHistory.update((h) => [...h, target]);\n\n // Entry action for new state\n const targetCfg = config.states[target];\n if (targetCfg?.entry) {\n targetCfg.entry({ data: contextData() as TContext, event });\n }\n }\n\n function can(event: TEvent): boolean {\n const stateCfg = config.states[currentState()];\n if (!stateCfg?.on) return false;\n const transition = stateCfg.on[event];\n if (!transition) return false;\n\n if (typeof transition !== 'string') {\n const t = transition as TransitionConfig<TState, TEvent, TContext>;\n if (t.guard) {\n return t.guard({ data: contextData() as TContext, event });\n }\n }\n return true;\n }\n\n return {\n state: () => currentState(),\n context: () => contextData(),\n send,\n matches: (state: TState) => currentState() === state,\n done,\n reset() {\n currentState.set(config.initial);\n contextData.set((config.context ?? {}) as TContext);\n stateHistory.set([config.initial]);\n },\n history: () => stateHistory(),\n can,\n nextEvents,\n };\n}\n"]}
1
+ {"version":3,"sources":["../src/machine.ts"],"names":["createMachine","config","currentState","signal","contextData","stateHistory","done","computed","nextEvents","stateCfg","initialStateCfg","send","event","payload","state","transition","target","guard","action","t","ctx","h","targetCfg","can"],"mappings":"sCAkGO,SAASA,CAAAA,CAIdC,CAAAA,CAAoF,CACpF,IAAMC,EAAeC,CAAAA,CAAeF,CAAAA,CAAO,OAAO,CAAA,CAC5CG,EAAcD,CAAAA,CAAkBF,CAAAA,CAAO,OAAA,EAAW,EAAe,CAAA,CACjEI,CAAAA,CAAeF,CAAAA,CAAiB,CAACF,CAAAA,CAAO,OAAO,CAAC,CAAA,CAEhDK,EAAOC,CAAAA,CAAS,IACHN,CAAAA,CAAO,MAAA,CAAOC,GAAc,CAAA,EAC5B,IAAA,GAAS,OAC3B,EAEKM,CAAAA,CAAaD,CAAAA,CAAS,IAAgB,CAC1C,IAAME,CAAAA,CAAWR,CAAAA,CAAO,MAAA,CAAOC,GAAc,CAAA,CAC7C,OAAKO,CAAAA,EAAU,GACR,MAAA,CAAO,IAAA,CAAKA,CAAAA,CAAS,EAAE,EADJ,EAE5B,CAAC,CAAA,CAGKC,CAAAA,CAAkBT,CAAAA,CAAO,MAAA,CAAOA,CAAAA,CAAO,OAAO,CAAA,CAChDS,CAAAA,EAAiB,KAAA,EACnBA,CAAAA,CAAgB,MAAM,CAAE,IAAA,CAAMN,CAAAA,EAAY,CAAe,MAAO,EAAG,CAAC,CAAA,CAGtE,SAASO,CAAAA,CAAKC,CAAAA,CAAeC,CAAAA,CAAyB,CACpD,GAAIP,CAAAA,EAAK,CAAG,OAEZ,IAAMQ,EAAQZ,CAAAA,EAAa,CACrBO,CAAAA,CAAWR,CAAAA,CAAO,OAAOa,CAAK,CAAA,CACpC,GAAI,CAACL,CAAAA,EAAU,EAAA,CAAI,OAEnB,IAAMM,EAAaN,CAAAA,CAAS,EAAA,CAAGG,CAAK,CAAA,CACpC,GAAI,CAACG,CAAAA,CAAY,OAEjB,IAAIC,EACAC,CAAAA,CACAC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CAAE,KAAA,CACJ,OAAOA,CAAAA,CAAE,KAAA,CAAM,CAAE,IAAA,CAAMf,GAAY,CAAe,KAAA,CAAAQ,CAAM,CAAC,CAE7D,CACA,OAAO,KACT,CAEA,OAAO,CACL,KAAA,CAAO,IAAMV,GAAa,CAC1B,OAAA,CAAS,IAAME,CAAAA,GACf,IAAA,CAAAO,CAAAA,CACA,OAAA,CAAUG,CAAAA,EAAkBZ,CAAAA,EAAa,GAAMY,CAAAA,CAC/C,IAAA,CAAAR,EACA,KAAA,EAAQ,CACNJ,CAAAA,CAAa,GAAA,CAAID,EAAO,OAAO,CAAA,CAC/BG,CAAAA,CAAY,GAAA,CAAKH,EAAO,OAAA,EAAW,EAAe,CAAA,CAClDI,EAAa,GAAA,CAAI,CAACJ,CAAAA,CAAO,OAAO,CAAC,EACnC,CAAA,CACA,OAAA,CAAS,IAAMI,GAAa,CAC5B,GAAA,CAAAkB,CAAAA,CACA,UAAA,CAAAf,CACF,CACF","file":"chunk-5A7KDBDU.js","sourcesContent":["/**\n * State machines for complex UI flows.\n *\n * Finite state machines with typed states, events, guards,\n * actions, and reactive current state signal.\n *\n * ```ts\n * const checkout = createMachine({\n * initial: 'cart',\n * states: {\n * cart: { on: { CHECKOUT: 'shipping' } },\n * shipping: { on: { NEXT: 'payment', BACK: 'cart' } },\n * payment: { on: { PAY: 'processing', BACK: 'shipping' } },\n * processing: { on: { SUCCESS: 'complete', FAIL: 'payment' } },\n * complete: { type: 'final' },\n * },\n * });\n *\n * checkout.state(); // 'cart'\n * checkout.send('CHECKOUT');\n * checkout.state(); // 'shipping'\n * checkout.matches('shipping'); // true\n * ```\n */\n\nimport { signal, computed } from './signals.js';\nimport type { ReadonlySignal } from './signals.js';\n\n// =========================================================================\n// Types\n// =========================================================================\n\nexport interface MachineConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Initial state */\n initial: TState;\n /** Initial context data */\n context?: TContext;\n /** State definitions */\n states: Record<TState, StateConfig<TState, TEvent, TContext>>;\n}\n\nexport interface StateConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Transitions: event → target state */\n on?: Partial<Record<TEvent, TState | TransitionConfig<TState, TEvent, TContext>>>;\n /** Entry action — runs when entering this state */\n entry?: (ctx: MachineContext<TContext>) => void;\n /** Exit action — runs when leaving this state */\n exit?: (ctx: MachineContext<TContext>) => void;\n /** Final state — machine stops accepting events */\n type?: 'final';\n}\n\nexport interface TransitionConfig<TState extends string, TEvent extends string, TContext = unknown> {\n /** Target state */\n target: TState;\n /** Guard — transition only if this returns true */\n guard?: (ctx: MachineContext<TContext>) => boolean;\n /** Action — runs during the transition */\n action?: (ctx: MachineContext<TContext>) => void;\n}\n\nexport interface MachineContext<TContext> {\n /** The context data (mutable) */\n data: TContext;\n /** The current event that triggered the transition */\n event: string;\n /** Optional payload passed with the event via send(event, payload) */\n payload?: unknown;\n}\n\nexport interface Machine<TState extends string, TEvent extends string, TContext = unknown> {\n /** Current state (reactive signal) */\n state: ReadonlySignal<TState>;\n /** Context data (reactive signal) */\n context: ReadonlySignal<TContext>;\n /** Send an event to the machine */\n send(event: TEvent, payload?: Record<string, unknown>): void;\n /** Check if currently in a specific state */\n matches(state: TState): boolean;\n /** Whether the machine is in a final state */\n done: ReadonlySignal<boolean>;\n /** Reset to initial state */\n reset(): void;\n /** State history */\n history: ReadonlySignal<TState[]>;\n /** Can a specific event be sent in the current state? */\n can(event: TEvent): boolean;\n /** Get all possible events from current state */\n nextEvents: ReadonlySignal<TEvent[]>;\n}\n\n// =========================================================================\n// createMachine\n// =========================================================================\n\n/**\n * Create a finite state machine.\n */\nexport function createMachine<\n TState extends string,\n TEvent extends string,\n TContext = unknown,\n>(config: MachineConfig<TState, TEvent, TContext>): Machine<TState, TEvent, TContext> {\n const currentState = signal<TState>(config.initial);\n const contextData = signal<TContext>((config.context ?? {}) as TContext);\n const stateHistory = signal<TState[]>([config.initial]);\n\n const done = computed(() => {\n const stateCfg = config.states[currentState()];\n return stateCfg?.type === 'final';\n });\n\n const nextEvents = computed((): TEvent[] => {\n const stateCfg = config.states[currentState()];\n if (!stateCfg?.on) return [];\n return Object.keys(stateCfg.on) as TEvent[];\n });\n\n // Run entry action for initial state\n const initialStateCfg = config.states[config.initial];\n if (initialStateCfg?.entry) {\n initialStateCfg.entry({ data: contextData() as TContext, event: '' });\n }\n\n function send(event: TEvent, payload?: unknown): void {\n if (done()) return; // Final state — no more transitions\n\n const state = currentState();\n const stateCfg = config.states[state];\n if (!stateCfg?.on) return;\n\n const transition = stateCfg.on[event];\n if (!transition) return;\n\n let target: TState;\n let guard: ((ctx: MachineContext<TContext>) => boolean) | undefined;\n let action: ((ctx: MachineContext<TContext>) => void) | undefined;\n\n if (typeof transition === 'string') {\n target = transition as TState;\n } else {\n const t = transition as TransitionConfig<TState, TEvent, TContext>;\n target = t.target;\n guard = t.guard;\n action = t.action;\n }\n\n const ctx: MachineContext<TContext> = {\n data: contextData() as TContext,\n event,\n payload,\n };\n\n // Check guard\n if (guard && !guard(ctx)) return;\n\n // Exit action\n if (stateCfg.exit) {\n stateCfg.exit(ctx);\n }\n\n // Transition action — pass payload as second argument for convenience\n if (action) {\n (action as Function)(ctx, payload);\n }\n\n // Update context if modified\n contextData.set(ctx.data);\n\n // Enter new state\n currentState.set(target);\n stateHistory.update((h) => [...h, target]);\n\n // Entry action for new state\n const targetCfg = config.states[target];\n if (targetCfg?.entry) {\n targetCfg.entry({ data: contextData() as TContext, event });\n }\n }\n\n function can(event: TEvent): boolean {\n const stateCfg = config.states[currentState()];\n if (!stateCfg?.on) return false;\n const transition = stateCfg.on[event];\n if (!transition) return false;\n\n if (typeof transition !== 'string') {\n const t = transition as TransitionConfig<TState, TEvent, TContext>;\n if (t.guard) {\n return t.guard({ data: contextData() as TContext, event });\n }\n }\n return true;\n }\n\n return {\n state: () => currentState(),\n context: () => contextData(),\n send,\n matches: (state: TState) => currentState() === state,\n done,\n reset() {\n currentState.set(config.initial);\n contextData.set((config.context ?? {}) as TContext);\n stateHistory.set([config.initial]);\n },\n history: () => stateHistory(),\n can,\n nextEvents,\n };\n}\n"]}
@@ -1,2 +1,2 @@
1
- import {l,m}from'./chunk-POLTPHUA.js';async function j(a,t=1){return typeof indexedDB>"u"?Promise.reject(new Error("IndexedDB not available")):new Promise((c,r)=>{try{let n=indexedDB.open(a,t);n.onupgradeneeded=()=>{let i=n.result;i.objectStoreNames.contains("items")||i.createObjectStore("items"),i.objectStoreNames.contains("pending")||i.createObjectStore("pending",{autoIncrement:!0});},n.onsuccess=()=>c(n.result),n.onerror=()=>r(n.error??new Error("IndexedDB open failed"));}catch(n){r(n);}})}async function x(a,t){return new Promise((c,r)=>{let i=a.transaction(t,"readonly").objectStore(t).getAll();i.onsuccess=()=>c(i.result),i.onerror=()=>r(i.error);})}async function D(a,t,c,r){return new Promise((n,i)=>{let l=a.transaction(t,"readwrite");l.objectStore(t).put(r,c),l.oncomplete=()=>n(),l.onerror=()=>i(l.error);})}async function B(a,t,c){return new Promise((r,n)=>{let i=a.transaction(t,"readwrite");i.objectStore(t).delete(c),i.oncomplete=()=>r(),i.onerror=()=>n(i.error);})}async function p(a,t){return new Promise((c,r)=>{let n=a.transaction(t,"readwrite");n.objectStore(t).clear(),n.oncomplete=()=>c(),n.onerror=()=>r(n.error);})}function A(a,t={}){let c=t.keyField??"id",r=l([]),n=l([]),i=l(false),l$1=l(typeof navigator<"u"&&navigator.onLine!=null?!!navigator.onLine:true),b=m(()=>n().length),o=null,m$1=null,T=false;typeof window<"u"&&(window.addEventListener("online",()=>{l$1.set(true),v();}),window.addEventListener("offline",()=>l$1.set(false)));async function O(){if(!(typeof indexedDB>"u"))try{o=await j(`akash-offline-${a}`,t.version??1);let e=await x(o,"items");r.set(e);let s=await x(o,"pending");n.set(s);}catch(e){console.error("[AkashJS] Offline store init failed:",e);}}let k=O();if(t.sync){let e=t.sync.interval??3e4;m$1=setInterval(()=>{l$1()&&b()>0&&!i()&&v();},e);}function g(e){return String(e[c])}function w(e){let s=g(e);r.update(d=>{let f=d.findIndex(u=>g(u)===s);if(f!==-1){let u=[...d];return u[f]=e,u}return [...d,e]}),S({type:"put",key:s,value:e,timestamp:Date.now()}),o&&D(o,"items",s,e);}function I(e){r.update(s=>s.filter(d=>g(d)!==e)),S({type:"delete",key:e,timestamp:Date.now()}),o&&B(o,"items",e);}function S(e){n.update(s=>[...s,e]),o&&o.transaction("pending","readwrite").objectStore("pending").add(e);}async function v(){if(await k,!t.sync||i()||T)return;let e=n();if(e.length===0)return;i.set(true);let s=t.sync.fetch??globalThis.fetch.bind(globalThis);try{if((await s(t.sync.url,{method:"POST",headers:{"Content-Type":"application/json",...t.sync.headers},body:JSON.stringify({ops:e})})).ok){n.set([]),o&&await p(o,"pending");let f=await s(t.sync.url,{headers:t.sync.headers});if(f.ok){let u=await f.json();if(r.set(u),o){await p(o,"items");for(let P of u)await D(o,"items",g(P),P);}}}}catch(d){console.warn("[AkashJS] Offline sync failed:",d);}finally{i.set(false);}}return {items:()=>r(),get(e){return r().find(s=>g(s)===e)},put:w,add:w,update(e,s){let d=r().find(f=>g(f)===e);d&&w({...d,...s});},remove:I,clear(){r.set([]),n.set([]),o&&(p(o,"items"),p(o,"pending"));},pending:b,syncing:()=>i(),online:()=>l$1(),sync:v,dispose(){T=true,m$1&&clearInterval(m$1),o?.close();}}}export{A as a};//# sourceMappingURL=chunk-D6QQYZIC.js.map
2
- //# sourceMappingURL=chunk-D6QQYZIC.js.map
1
+ import {l,m}from'./chunk-3GRR4VW2.js';async function j(a,t=1){return typeof indexedDB>"u"?Promise.reject(new Error("IndexedDB not available")):new Promise((d,r)=>{try{let n=indexedDB.open(a,t);n.onupgradeneeded=()=>{let i=n.result;i.objectStoreNames.contains("items")||i.createObjectStore("items"),i.objectStoreNames.contains("pending")||i.createObjectStore("pending",{autoIncrement:!0});},n.onsuccess=()=>d(n.result),n.onerror=()=>r(n.error??new Error("IndexedDB open failed"));}catch(n){r(n);}})}async function x(a,t){return new Promise((d,r)=>{let i=a.transaction(t,"readonly").objectStore(t).getAll();i.onsuccess=()=>d(i.result),i.onerror=()=>r(i.error);})}async function D(a,t,d,r){return new Promise((n,i)=>{let l=a.transaction(t,"readwrite");l.objectStore(t).put(r,d),l.oncomplete=()=>n(),l.onerror=()=>i(l.error);})}async function B(a,t,d){return new Promise((r,n)=>{let i=a.transaction(t,"readwrite");i.objectStore(t).delete(d),i.oncomplete=()=>r(),i.onerror=()=>n(i.error);})}async function p(a,t){return new Promise((d,r)=>{let n=a.transaction(t,"readwrite");n.objectStore(t).clear(),n.oncomplete=()=>d(),n.onerror=()=>r(n.error);})}function A(a,t={}){let d=t.keyField??"id",r=l([]),n=l([]),i=l(false),l$1=l(typeof navigator<"u"&&navigator.onLine!=null?!!navigator.onLine:true),b=m(()=>n().length),o=null,m$1=null,T=false;typeof window<"u"&&(window.addEventListener("online",()=>{l$1.set(true),v();}),window.addEventListener("offline",()=>l$1.set(false)));async function O(){if(!(typeof indexedDB>"u"))try{o=await j(`akash-offline-${a}`,t.version??1);let e=await x(o,"items");r.set(e);let s=await x(o,"pending");n.set(s);}catch(e){console.error("[AkashJS] Offline store init failed:",e);}}let k=O();if(t.sync){let e=t.sync.interval??3e4;m$1=setInterval(()=>{l$1()&&b()>0&&!i()&&v();},e);}function g(e){return String(e[d])}function w(e){let s=g(e);r.update(c=>{let f=c.findIndex(u=>g(u)===s);if(f!==-1){let u=[...c];return u[f]=e,u}return [...c,e]}),S({type:"put",key:s,value:e,timestamp:Date.now()}),o&&D(o,"items",s,e);}function I(e){r.update(s=>s.filter(c=>g(c)!==e)),S({type:"delete",key:e,timestamp:Date.now()}),o&&B(o,"items",e);}function S(e){n.update(s=>[...s,e]),o&&o.transaction("pending","readwrite").objectStore("pending").add(e);}async function v(){if(await k,!t.sync||i()||T)return;let e=n();if(e.length===0)return;i.set(true);let s=t.sync.fetch??globalThis.fetch.bind(globalThis);try{if((await s(t.sync.url,{method:"POST",headers:{"Content-Type":"application/json",...t.sync.headers},body:JSON.stringify({ops:e})})).ok){n.set([]),o&&await p(o,"pending");let f=await s(t.sync.url,{headers:t.sync.headers});if(f.ok){let u=await f.json();if(r.set(u),o){await p(o,"items");for(let P of u)await D(o,"items",g(P),P);}}}}catch(c){console.warn("[AkashJS] Offline sync failed:",c);}finally{i.set(false);}}return {items:()=>r(),data:()=>r(),get(e){return r().find(s=>g(s)===e)},put:w,add:w,update(e,s){let c=r().find(f=>g(f)===e);c&&w({...c,...s});},remove:I,clear(){r.set([]),n.set([]),o&&(p(o,"items"),p(o,"pending"));},pending:b,syncing:()=>i(),online:()=>l$1(),sync:v,dispose(){T=true,m$1&&clearInterval(m$1),o?.close();}}}export{A as a};//# sourceMappingURL=chunk-5NHDEY2C.js.map
2
+ //# sourceMappingURL=chunk-5NHDEY2C.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/offline.ts"],"names":["openDB","name","version","resolve","reject","request","db","err","idbGetAll","store","req","idbPut","key","value","tx","idbDelete","idbClear","createOfflineStore","options","keyField","items","signal","pendingOps","syncing","online","pending","computed","syncTimer","disposed","syncNow","init","stored","ops","initPromise","interval","getKey","item","put","list","idx","i","next","queueOp","remove","op","fetchFn","serverResponse","serverItems","partial","existing"],"mappings":"sCAyFA,eAAeA,CAAAA,CAAOC,EAAcC,CAAAA,CAAU,CAAA,CAAyB,CACrE,OAAI,OAAO,UAAc,GAAA,CAChB,OAAA,CAAQ,OAAO,IAAI,KAAA,CAAM,yBAAyB,CAAC,CAAA,CAErD,IAAI,OAAA,CAAQ,CAACC,CAAAA,CAASC,CAAAA,GAAW,CACtC,GAAI,CACF,IAAMC,CAAAA,CAAU,UAAU,IAAA,CAAKJ,CAAAA,CAAMC,CAAO,CAAA,CAC5CG,CAAAA,CAAQ,gBAAkB,IAAM,CAC9B,IAAMC,CAAAA,CAAKD,CAAAA,CAAQ,OACdC,CAAAA,CAAG,gBAAA,CAAiB,SAAS,OAAO,CAAA,EACvCA,CAAAA,CAAG,iBAAA,CAAkB,OAAO,CAAA,CAEzBA,EAAG,g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gB,EAAQ,GAAA,CAAI,KAAK,EACnB,CACF,CAEA,OAAO,CACL,KAAA,CAAO,IAAMH,GAAM,CACnB,IAAA,CAAM,IAAMA,CAAAA,EAAM,CAClB,IAAIR,CAAAA,CAAa,CAAE,OAAOQ,CAAAA,EAAM,CAAE,KAAMoB,CAAAA,EAAML,CAAAA,CAAOK,CAAC,CAAA,GAAM5B,CAAG,CAAG,CAAA,CAClE,GAAA,CAAAyB,CAAAA,CACA,IAAKA,CAAAA,CACL,MAAA,CAAOzB,EAAaoC,CAAAA,CAAqB,CACvC,IAAMC,CAAAA,CAAW7B,CAAAA,EAAM,CAAE,IAAA,CAAMoB,CAAAA,EAAML,CAAAA,CAAOK,CAAC,CAAA,GAAM5B,CAAG,EAClDqC,CAAAA,EAAUZ,CAAAA,CAAI,CAAE,GAAGY,CAAAA,CAAU,GAAGD,CAAQ,CAAC,EAC/C,EACA,MAAA,CAAAL,CAAAA,CACA,OAAQ,CACNvB,CAAAA,CAAM,IAAI,EAAE,EACZE,CAAAA,CAAW,GAAA,CAAI,EAAE,CAAA,CACbhB,IACFU,CAAAA,CAASV,CAAAA,CAAI,OAAO,CAAA,CACpBU,CAAAA,CAASV,CAAAA,CAAI,SAAS,CAAA,EAE1B,CAAA,CACA,QAAAmB,CAAAA,CACA,OAAA,CAAS,IAAMF,CAAAA,EAAQ,CACvB,OAAQ,IAAMC,GAAAA,EAAO,CACrB,IAAA,CAAMK,CAAAA,CACN,OAAA,EAAU,CACRD,CAAAA,CAAW,IAAA,CACPD,KAAW,aAAA,CAAcA,GAAS,EACtCrB,CAAAA,EAAI,KAAA,GACN,CACF,CACF","file":"chunk-5NHDEY2C.js","sourcesContent":["/**\n * Offline-first store with IndexedDB persistence and background sync.\n *\n * Data persists across page reloads. Changes made offline queue up\n * and sync automatically when connection returns. Conflict resolution\n * via configurable strategies (last-write-wins, merge, custom).\n *\n * ```ts\n * const todos = createOfflineStore('todos', {\n * sync: { url: '/api/todos', strategy: 'last-write-wins' },\n * });\n *\n * todos.add({ id: '1', text: 'Buy milk', done: false });\n * todos.items(); // reactive list\n * todos.syncing(); // boolean\n * todos.pending(); // number of unsynced changes\n * ```\n */\n\nimport { signal, computed } from './signals.js';\nimport type { Signal, ReadonlySignal } from './signals.js';\n\n// =========================================================================\n// Types\n// =========================================================================\n\nexport type ConflictStrategy = 'last-write-wins' | 'client-wins' | 'server-wins' | 'manual';\n\nexport interface OfflineStoreOptions<T> {\n /** Sync configuration */\n sync?: {\n /** Server URL for sync */\n url: string;\n /** Conflict resolution strategy (default: 'last-write-wins') */\n strategy?: ConflictStrategy;\n /** Sync interval in ms (default: 30000) */\n interval?: number;\n /** Custom fetch */\n fetch?: typeof globalThis.fetch;\n /** Auth headers */\n headers?: Record<string, string>;\n };\n /** Key field for items (default: 'id') */\n keyField?: string;\n /** Version for schema migrations */\n version?: number;\n}\n\nexport interface OfflineStore<T extends Record<string, unknown>> {\n /** All items (reactive) */\n items: ReadonlySignal<T[]>;\n /** Alias for items (reactive) */\n data: ReadonlySignal<T[]>;\n /** Get a single item by key */\n get(key: string): T | undefined;\n /** Add or update an item */\n put(item: T): void;\n /** Add a new item */\n add(item: T): void;\n /** Update an existing item */\n update(key: string, partial: Partial<T>): void;\n /** Remove an item by key */\n remove(key: string): void;\n /** Clear all items */\n clear(): void;\n /** Number of unsynced changes */\n pending: ReadonlySignal<number>;\n /** Whether sync is in progress */\n syncing: ReadonlySignal<boolean>;\n /** Whether online */\n online: ReadonlySignal<boolean>;\n /** Force a sync now */\n sync(): Promise<void>;\n /** Dispose the store (stop sync, close DB) */\n dispose(): void;\n}\n\n/** An operation in the change queue */\ninterface PendingOp<T> {\n type: 'put' | 'delete';\n key: string;\n value?: T;\n timestamp: number;\n}\n\n// =========================================================================\n// IndexedDB wrapper\n// =========================================================================\n\nasync function openDB(name: string, version = 1): Promise<IDBDatabase> {\n if (typeof indexedDB === 'undefined') {\n return Promise.reject(new Error('IndexedDB not available'));\n }\n return new Promise((resolve, reject) => {\n try {\n const request = indexedDB.open(name, version);\n request.onupgradeneeded = () => {\n const db = request.result;\n if (!db.objectStoreNames.contains('items')) {\n db.createObjectStore('items');\n }\n if (!db.objectStoreNames.contains('pending')) {\n db.createObjectStore('pending', { autoIncrement: true });\n }\n };\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error ?? new Error('IndexedDB open failed'));\n } catch (err) {\n reject(err);\n }\n });\n}\n\nasync function idbGetAll<T>(db: IDBDatabase, store: string): Promise<T[]> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(store, 'readonly');\n const req = tx.objectStore(store).getAll();\n req.onsuccess = () => resolve(req.result);\n req.onerror = () => reject(req.error);\n });\n}\n\nasync function idbPut(db: IDBDatabase, store: string, key: string, value: unknown): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(store, 'readwrite');\n tx.objectStore(store).put(value, key);\n tx.oncomplete = () => resolve();\n tx.onerror = () => reject(tx.error);\n });\n}\n\nasync function idbDelete(db: IDBDatabase, store: string, key: string): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(store, 'readwrite');\n tx.objectStore(store).delete(key);\n tx.oncomplete = () => resolve();\n tx.onerror = () => reject(tx.error);\n });\n}\n\nasync function idbClear(db: IDBDatabase, store: string): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(store, 'readwrite');\n tx.objectStore(store).clear();\n tx.oncomplete = () => resolve();\n tx.onerror = () => reject(tx.error);\n });\n}\n\n// =========================================================================\n// createOfflineStore\n// =========================================================================\n\n/**\n * Create an offline-first store backed by IndexedDB.\n */\nexport function createOfflineStore<T extends Record<string, unknown>>(\n name: string,\n options: OfflineStoreOptions<T> = {},\n): OfflineStore<T> {\n const keyField = options.keyField ?? 'id';\n const items = signal<T[]>([]);\n const pendingOps = signal<PendingOp<T>[]>([]);\n const syncing = signal(false);\n const online = signal(typeof navigator !== 'undefined' && navigator.onLine != null ? !!navigator.onLine : true);\n const pending = computed(() => pendingOps().length);\n\n let db: IDBDatabase | null = null;\n let syncTimer: ReturnType<typeof setInterval> | null = null;\n let disposed = false;\n\n // Online/offline tracking\n if (typeof window !== 'undefined') {\n window.addEventListener('online', () => {\n online.set(true);\n syncNow();\n });\n window.addEventListener('offline', () => online.set(false));\n }\n\n // Initialize — load from IndexedDB\n async function init(): Promise<void> {\n if (typeof indexedDB === 'undefined') return;\n try {\n db = await openDB(`akash-offline-${name}`, options.version ?? 1);\n const stored = await idbGetAll<T>(db, 'items');\n items.set(stored);\n const ops = await idbGetAll<PendingOp<T>>(db, 'pending');\n pendingOps.set(ops);\n } catch (err) { console.error('[AkashJS] Offline store init failed:', err); }\n }\n\n const initPromise = init();\n\n // Start periodic sync\n if (options.sync) {\n const interval = options.sync.interval ?? 30000;\n syncTimer = setInterval(() => {\n if (online() && pending() > 0 && !syncing()) {\n syncNow();\n }\n }, interval);\n }\n\n function getKey(item: T): string {\n return String((item as any)[keyField]);\n }\n\n function put(item: T): void {\n const key = getKey(item);\n items.update((list) => {\n const idx = list.findIndex((i) => getKey(i) === key);\n if (idx !== -1) {\n const next = [...list];\n next[idx] = item;\n return next;\n }\n return [...list, item];\n });\n\n queueOp({ type: 'put', key, value: item, timestamp: Date.now() });\n if (db) idbPut(db, 'items', key, item);\n }\n\n function remove(key: string): void {\n items.update((list) => list.filter((i) => getKey(i) !== key));\n queueOp({ type: 'delete', key, timestamp: Date.now() });\n if (db) idbDelete(db, 'items', key);\n }\n\n function queueOp(op: PendingOp<T>): void {\n pendingOps.update((ops) => [...ops, op]);\n if (db) {\n const tx = db.transaction('pending', 'readwrite');\n tx.objectStore('pending').add(op);\n }\n }\n\n async function syncNow(): Promise<void> {\n await initPromise;\n if (!options.sync || syncing() || disposed) return;\n const ops = pendingOps();\n if (ops.length === 0) return;\n\n syncing.set(true);\n const fetchFn = options.sync.fetch ?? globalThis.fetch.bind(globalThis);\n\n try {\n const response = await fetchFn(options.sync.url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...options.sync.headers,\n },\n body: JSON.stringify({ ops }),\n });\n\n if (response.ok) {\n pendingOps.set([]);\n if (db) await idbClear(db, 'pending');\n\n // Fetch latest server state\n const serverResponse = await fetchFn(options.sync.url, {\n headers: options.sync.headers,\n });\n if (serverResponse.ok) {\n const serverItems = await serverResponse.json() as T[];\n items.set(serverItems);\n // Update IndexedDB\n if (db) {\n await idbClear(db, 'items');\n for (const item of serverItems) {\n await idbPut(db, 'items', getKey(item), item);\n }\n }\n }\n }\n } catch (err) { console.warn('[AkashJS] Offline sync failed:', err); }\n finally {\n syncing.set(false);\n }\n }\n\n return {\n items: () => items(),\n data: () => items(),\n get(key: string) { return items().find((i) => getKey(i) === key); },\n put,\n add: put,\n update(key: string, partial: Partial<T>) {\n const existing = items().find((i) => getKey(i) === key);\n if (existing) put({ ...existing, ...partial });\n },\n remove,\n clear() {\n items.set([]);\n pendingOps.set([]);\n if (db) {\n idbClear(db, 'items');\n idbClear(db, 'pending');\n }\n },\n pending,\n syncing: () => syncing(),\n online: () => online(),\n sync: syncNow,\n dispose() {\n disposed = true;\n if (syncTimer) clearInterval(syncTimer);\n db?.close();\n },\n };\n}\n"]}