@lunora/db 1.0.0-alpha.30 → 1.0.0-alpha.31

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.
@@ -1,5 +1,5 @@
1
- export { type C as CheckpointRegistry, type L as LunoraCollectionConfig, type a as LunoraCollectionOptions, c as createCheckpointRegistry, l as lunoraCollectionOptions } from "../packem_shared/collection-options.d-C03Rjjxn.mjs";
2
- export { type C as CollectionDef, type I as InsertBinding, type L as LunoraDb, d as defineCollections } from "../packem_shared/define-collections.d-Y9AaX0yt.mjs";
1
+ export { type C as CheckpointRegistry, type L as LunoraCollectionConfig, type a as LunoraCollectionOptions, c as createCheckpointRegistry, l as lunoraCollectionOptions } from "../packem_shared/collection-options.d-B_2IXvdU.mjs";
2
+ export { type C as CollectionDef, type I as InsertBinding, type L as LunoraDb, d as defineCollections } from "../packem_shared/define-collections.d-C6J1Y4Q_.mjs";
3
3
  import '@lunora/client';
4
4
  import '@tanstack/db';
5
5
  import '@tanstack/offline-transactions';
@@ -1,5 +1,5 @@
1
- export { type C as CheckpointRegistry, type L as LunoraCollectionConfig, type a as LunoraCollectionOptions, c as createCheckpointRegistry, l as lunoraCollectionOptions } from "../packem_shared/collection-options.d-C03Rjjxn.js";
2
- export { type C as CollectionDef, type I as InsertBinding, type L as LunoraDb, d as defineCollections } from "../packem_shared/define-collections.d-Ds-5C0M9.js";
1
+ export { type C as CheckpointRegistry, type L as LunoraCollectionConfig, type a as LunoraCollectionOptions, c as createCheckpointRegistry, l as lunoraCollectionOptions } from "../packem_shared/collection-options.d-B_2IXvdU.js";
2
+ export { type C as CollectionDef, type I as InsertBinding, type L as LunoraDb, d as defineCollections } from "../packem_shared/define-collections.d-DAFPHFxr.js";
3
3
  import '@lunora/client';
4
4
  import '@tanstack/db';
5
5
  import '@tanstack/offline-transactions';
@@ -1 +1 @@
1
- import{createCheckpointRegistry as t,lunoraCollectionOptions as r}from"../packem_shared/CHECKPOINT_FALLBACK_MS-CtPxAU9J.mjs";import{defineCollections as n}from"../packem_shared/defineCollections-CnFdW1Ff.mjs";export{t as createCheckpointRegistry,n as defineCollections,r as lunoraCollectionOptions};
1
+ import{createCheckpointRegistry as t,lunoraCollectionOptions as r}from"../packem_shared/CHECKPOINT_FALLBACK_MS-vBJLOBjX.mjs";import{defineCollections as n}from"../packem_shared/defineCollections-BCgkk3A6.mjs";export{t as createCheckpointRegistry,n as defineCollections,r as lunoraCollectionOptions};
package/dist/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Collection } from '@tanstack/db';
2
- import { R as Row } from "./packem_shared/collection-options.d-C03Rjjxn.mjs";
3
- export { b as CHECKPOINT_FALLBACK_MS, type d as CheckpointFallbackEvent, type C as CheckpointRegistry, type e as CheckpointRegistryOptions, type f as CheckpointRegistryStats, type g as CheckpointWatermark, type E as ExecutorOutboxSinkOptions, type L as LunoraCollectionConfig, type a as LunoraCollectionOptions, O as OUTBOX_MUTATION_FN_NAME, type h as OutboxExecutor, type i as OutboxMutationMetadata, type S as SyncWriter, c as createCheckpointRegistry, j as createExecutorOutboxSink, k as createOptimisticOnlineDetector, m as getShardCheckpoints, l as lunoraCollectionOptions, n as makeDiffEmit, r as releaseShardCheckpoints, o as runOutboxMutation, s as shardCheckpointStats, t as toMap } from "./packem_shared/collection-options.d-C03Rjjxn.mjs";
4
- export { type C as CollectionDef, type D as DefineCollectionsOptions, type I as InsertBinding, type L as LunoraDb, type W as WriteRejectedEvent, d as defineCollections } from "./packem_shared/define-collections.d-Y9AaX0yt.mjs";
5
- export { type B as BindMutatorsContext, type a as BoundMutators, type C as ClientMutatorContext, type b as ClientMutatorDef, D as DIRECT_TRANSACTION_METADATA_KEY, type M as MutatorReference, c as bindMutators, d as defineMutator } from "./packem_shared/define-mutators.d-tPb72T4I.mjs";
2
+ import { R as Row } from "./packem_shared/collection-options.d-B_2IXvdU.mjs";
3
+ export { b as CHECKPOINT_FALLBACK_MS, type d as CheckpointFallbackEvent, type C as CheckpointRegistry, type e as CheckpointRegistryOptions, type f as CheckpointRegistryStats, type g as CheckpointWatermark, type E as ExecutorOutboxSinkOptions, type L as LunoraCollectionConfig, type a as LunoraCollectionOptions, O as OUTBOX_MUTATION_FN_NAME, type h as OutboxExecutor, type i as OutboxMutationMetadata, type S as SyncWriter, c as createCheckpointRegistry, j as createExecutorOutboxSink, k as createOptimisticOnlineDetector, m as getShardCheckpoints, l as lunoraCollectionOptions, n as makeDiffEmit, r as releaseShardCheckpoints, o as runOutboxMutation, s as shardCheckpointStats, t as toMap } from "./packem_shared/collection-options.d-B_2IXvdU.mjs";
4
+ export { type C as CollectionDef, type D as DefineCollectionsOptions, type I as InsertBinding, type L as LunoraDb, type W as WriteRejectedEvent, d as defineCollections } from "./packem_shared/define-collections.d-C6J1Y4Q_.mjs";
5
+ export { type B as BindMutatorsContext, type a as BoundMutators, type C as ClientMutatorContext, type b as ClientMutatorDef, D as DIRECT_TRANSACTION_METADATA_KEY, type M as MutatorReference, c as bindMutators, d as defineMutator } from "./packem_shared/define-mutators.d--WcIPEJa.mjs";
6
6
  import '@lunora/client';
7
7
  import '@tanstack/offline-transactions';
8
8
  /** One row to insert. `_id` may be pre-minted client-side so the optimistic row keys match the persisted one. */
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Collection } from '@tanstack/db';
2
- import { R as Row } from "./packem_shared/collection-options.d-C03Rjjxn.js";
3
- export { b as CHECKPOINT_FALLBACK_MS, type d as CheckpointFallbackEvent, type C as CheckpointRegistry, type e as CheckpointRegistryOptions, type f as CheckpointRegistryStats, type g as CheckpointWatermark, type E as ExecutorOutboxSinkOptions, type L as LunoraCollectionConfig, type a as LunoraCollectionOptions, O as OUTBOX_MUTATION_FN_NAME, type h as OutboxExecutor, type i as OutboxMutationMetadata, type S as SyncWriter, c as createCheckpointRegistry, j as createExecutorOutboxSink, k as createOptimisticOnlineDetector, m as getShardCheckpoints, l as lunoraCollectionOptions, n as makeDiffEmit, r as releaseShardCheckpoints, o as runOutboxMutation, s as shardCheckpointStats, t as toMap } from "./packem_shared/collection-options.d-C03Rjjxn.js";
4
- export { type C as CollectionDef, type D as DefineCollectionsOptions, type I as InsertBinding, type L as LunoraDb, type W as WriteRejectedEvent, d as defineCollections } from "./packem_shared/define-collections.d-Ds-5C0M9.js";
5
- export { type B as BindMutatorsContext, type a as BoundMutators, type C as ClientMutatorContext, type b as ClientMutatorDef, D as DIRECT_TRANSACTION_METADATA_KEY, type M as MutatorReference, c as bindMutators, d as defineMutator } from "./packem_shared/define-mutators.d-DqkWcDks.js";
2
+ import { R as Row } from "./packem_shared/collection-options.d-B_2IXvdU.js";
3
+ export { b as CHECKPOINT_FALLBACK_MS, type d as CheckpointFallbackEvent, type C as CheckpointRegistry, type e as CheckpointRegistryOptions, type f as CheckpointRegistryStats, type g as CheckpointWatermark, type E as ExecutorOutboxSinkOptions, type L as LunoraCollectionConfig, type a as LunoraCollectionOptions, O as OUTBOX_MUTATION_FN_NAME, type h as OutboxExecutor, type i as OutboxMutationMetadata, type S as SyncWriter, c as createCheckpointRegistry, j as createExecutorOutboxSink, k as createOptimisticOnlineDetector, m as getShardCheckpoints, l as lunoraCollectionOptions, n as makeDiffEmit, r as releaseShardCheckpoints, o as runOutboxMutation, s as shardCheckpointStats, t as toMap } from "./packem_shared/collection-options.d-B_2IXvdU.js";
4
+ export { type C as CollectionDef, type D as DefineCollectionsOptions, type I as InsertBinding, type L as LunoraDb, type W as WriteRejectedEvent, d as defineCollections } from "./packem_shared/define-collections.d-DAFPHFxr.js";
5
+ export { type B as BindMutatorsContext, type a as BoundMutators, type C as ClientMutatorContext, type b as ClientMutatorDef, D as DIRECT_TRANSACTION_METADATA_KEY, type M as MutatorReference, c as bindMutators, d as defineMutator } from "./packem_shared/define-mutators.d-C_E1q1_B.js";
6
6
  import '@lunora/client';
7
7
  import '@tanstack/offline-transactions';
8
8
  /** One row to insert. `_id` may be pre-minted client-side so the optimistic row keys match the persisted one. */
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{applyPlanToCollections as r,applyPlanToDb as i}from"./packem_shared/applyPlanToCollections-C29s6OoX.mjs";import{CHECKPOINT_FALLBACK_MS as a,createCheckpointRegistry as p,getShardCheckpoints as c,lunoraCollectionOptions as l,releaseShardCheckpoints as s,shardCheckpointStats as C}from"./packem_shared/CHECKPOINT_FALLBACK_MS-CtPxAU9J.mjs";import{defineCollections as T}from"./packem_shared/defineCollections-CnFdW1Ff.mjs";import{DIRECT_TRANSACTION_METADATA_KEY as x,bindMutators as A,defineMutator as m}from"./packem_shared/DIRECT_TRANSACTION_METADATA_KEY-Cvm6vs5S.mjs";import{OUTBOX_MUTATION_FN_NAME as E,createExecutorOutboxSink as M,createOptimisticOnlineDetector as _,makeDiffEmit as h,runOutboxMutation as N,toMap as S}from"./packem_shared/OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const t="0.0.0";export{a as CHECKPOINT_FALLBACK_MS,x as DIRECT_TRANSACTION_METADATA_KEY,E as OUTBOX_MUTATION_FN_NAME,t as VERSION,r as applyPlanToCollections,i as applyPlanToDb,A as bindMutators,p as createCheckpointRegistry,M as createExecutorOutboxSink,_ as createOptimisticOnlineDetector,T as defineCollections,m as defineMutator,c as getShardCheckpoints,l as lunoraCollectionOptions,h as makeDiffEmit,s as releaseShardCheckpoints,N as runOutboxMutation,C as shardCheckpointStats,S as toMap};
1
+ import{applyPlanToCollections as r,applyPlanToDb as i}from"./packem_shared/applyPlanToCollections-C29s6OoX.mjs";import{CHECKPOINT_FALLBACK_MS as a,createCheckpointRegistry as p,getShardCheckpoints as c,lunoraCollectionOptions as l,releaseShardCheckpoints as s,shardCheckpointStats as C}from"./packem_shared/CHECKPOINT_FALLBACK_MS-vBJLOBjX.mjs";import{defineCollections as T}from"./packem_shared/defineCollections-BCgkk3A6.mjs";import{DIRECT_TRANSACTION_METADATA_KEY as x,bindMutators as A,defineMutator as m}from"./packem_shared/DIRECT_TRANSACTION_METADATA_KEY-BxF71ve7.mjs";import{OUTBOX_MUTATION_FN_NAME as E,createExecutorOutboxSink as M,createOptimisticOnlineDetector as _,makeDiffEmit as h,runOutboxMutation as N,toMap as S}from"./packem_shared/OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const t="0.0.0";export{a as CHECKPOINT_FALLBACK_MS,x as DIRECT_TRANSACTION_METADATA_KEY,E as OUTBOX_MUTATION_FN_NAME,t as VERSION,r as applyPlanToCollections,i as applyPlanToDb,A as bindMutators,p as createCheckpointRegistry,M as createExecutorOutboxSink,_ as createOptimisticOnlineDetector,T as defineCollections,m as defineMutator,c as getShardCheckpoints,l as lunoraCollectionOptions,h as makeDiffEmit,s as releaseShardCheckpoints,N as runOutboxMutation,C as shardCheckpointStats,S as toMap};
@@ -1,5 +1,5 @@
1
- export { type B as BindMutatorsContext, type a as BoundMutators, type C as ClientMutatorContext, type b as ClientMutatorDef, c as bindMutators, d as defineMutator } from "../packem_shared/define-mutators.d-tPb72T4I.mjs";
1
+ export { type B as BindMutatorsContext, type a as BoundMutators, type C as ClientMutatorContext, type b as ClientMutatorDef, c as bindMutators, d as defineMutator } from "../packem_shared/define-mutators.d--WcIPEJa.mjs";
2
2
  import '@lunora/client';
3
3
  import '@tanstack/db';
4
- import "../packem_shared/collection-options.d-C03Rjjxn.mjs";
4
+ import "../packem_shared/collection-options.d-B_2IXvdU.mjs";
5
5
  import '@tanstack/offline-transactions';
@@ -1,5 +1,5 @@
1
- export { type B as BindMutatorsContext, type a as BoundMutators, type C as ClientMutatorContext, type b as ClientMutatorDef, c as bindMutators, d as defineMutator } from "../packem_shared/define-mutators.d-DqkWcDks.js";
1
+ export { type B as BindMutatorsContext, type a as BoundMutators, type C as ClientMutatorContext, type b as ClientMutatorDef, c as bindMutators, d as defineMutator } from "../packem_shared/define-mutators.d-C_E1q1_B.js";
2
2
  import '@lunora/client';
3
3
  import '@tanstack/db';
4
- import "../packem_shared/collection-options.d-C03Rjjxn.js";
4
+ import "../packem_shared/collection-options.d-B_2IXvdU.js";
5
5
  import '@tanstack/offline-transactions';
@@ -1 +1 @@
1
- import{bindMutators as r,defineMutator as e}from"../packem_shared/DIRECT_TRANSACTION_METADATA_KEY-Cvm6vs5S.mjs";export{r as bindMutators,e as defineMutator};
1
+ import{bindMutators as r,defineMutator as e}from"../packem_shared/DIRECT_TRANSACTION_METADATA_KEY-BxF71ve7.mjs";export{r as bindMutators,e as defineMutator};
@@ -0,0 +1 @@
1
+ import{LunoraError as y}from"@lunora/errors";import{BTreeIndex as w}from"@tanstack/db";import{toMap as b,makeDiffEmit as C}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const v=()=>{let e=Number.NEGATIVE_INFINITY;const s=[];return{advance:o=>{if(!(o<=e)){e=o;for(let a=s.length-1;a>=0;a-=1){const r=s[a];r&&r.threshold<=e&&(r.resolve(),s.splice(a,1))}}},await:o=>o<=e?Promise.resolve():new Promise(a=>{s.push({resolve:a,threshold:o})}),passed:o=>o<=e,waiting:()=>s.length}},M=3e3;let I=!1;const N=e=>{I||(I=!0,console.warn(`[@lunora/db] released an optimistic overlay via the ${String(e.waitedMs)}ms checkpoint fallback: the server confirmed ${e.kind} ${String(e.watermark)} but no sync frame ever echoed it. A dropped shape poke or \`settled\` frame is the usual cause — inspect the subscription rather than raising \`fallbackMs\`. (Reported once per process.)`))},T=(e={})=>{const s=e.fallbackMs??M,o=e.onFallback??N,a=v(),r=v();let c=0;const i=new Set,l=(n,t)=>{for(const d of i)d.kind===n&&d.threshold<=t&&(clearTimeout(d.handle),i.delete(d))},h=(n,t)=>{if(s<=0)return;const d=n==="checkpoint"?a:r;if(d.passed(t))return;for(const u of i)if(u.kind===n&&u.threshold>=t)return;const p={handle:setTimeout(()=>{if(i.delete(p),!d.passed(t)){c+=1,d.advance(t);try{o({kind:n,waitedMs:s,watermark:t})}catch{}}},s),kind:n,threshold:t};i.add(p)};return{acknowledge:({checkpoint:n,mutationId:t})=>{n!==void 0&&h("checkpoint",n),t!==void 0&&h("mutationId",t)},awaitCheckpoint:n=>a.await(n),awaitMutationId:n=>r.await(n),dispose:()=>{for(const n of i)clearTimeout(n.handle);i.clear()},resolve:({checkpoint:n,mutationId:t})=>{n!==void 0&&(a.advance(n),l("checkpoint",n)),t!==void 0&&(r.advance(t),l("mutationId",t))},stats:()=>({fallbacks:c,pendingCheckpointWaiters:a.waiting(),pendingMutationWaiters:r.waiting()})}},f=new WeakMap,S=(e,s,o)=>{let a=f.get(e);a||(a=new Map,f.set(e,a));const r=s??"",c=a.get(r);if(c)return c;const i=T(o);return a.set(r,i),i},g=new WeakSet,E=e=>{g.add(e)},R=e=>g.has(e),x=e=>{const s=f.get(e);if(s){for(const o of s.values())o.resolve({checkpoint:Number.POSITIVE_INFINITY,mutationId:Number.POSITIVE_INFINITY}),o.dispose();f.delete(e)}},F=e=>{const s=f.get(e),o={};if(s)for(const[a,r]of s)o[a]=r.stats();return o},O=e=>{if(e.list===void 0==(e.shape===void 0))throw new y("INTERNAL","lunoraCollectionOptions: pass exactly one of `list` or `shape`");const s=e.getKey??(t=>t._id),o=e.checkpoints??S(e.client,e.shape?.shardKey??e.shardKey);E(o);const a=new Map;let r,c,i,l;const h=(t,d)=>{const p=k=>{r?.(b(k,s)),d?.(),e.shape===void 0&&o.resolve({mutationId:e.client.confirmedMutationWatermark(e.shardKey)})},u=k=>i?.(k),m=k=>{o.resolve(k)};return e.shape!==void 0?e.client.subscribeShape({args:t,name:e.shape.name},p,{onCheckpoint:m,onError:u,shardKey:e.shape.shardKey}):e.client.subscribe(e.list,t,p,{onCheckpoint:m,onError:u,shardKey:e.shardKey})},n={autoIndex:"eager",defaultIndexType:w,getKey:s,id:e.id??e.list?.__lunoraRef??`shape:${e.shape?.name??""}`,...e.load==="eager"?{startSync:!0}:{},sync:{sync:t=>(r=C(a,t),i=d=>{t.markReady(),e.onError?.(d)},e.scopeBy===void 0?c=h(e.shape?.args??{},()=>{t.markReady()}):(t.markReady(),l!==void 0&&(c=h(l,void 0))),()=>{r=void 0,i=void 0,c?.(),c=void 0,a.clear()})}};return{checkpoints:o,config:n,scope:t=>{e.scopeBy!==void 0&&(l=t,c?.(),c=void 0,r?.(new Map),t!==void 0&&r!==void 0&&(c=h(t,void 0)))}}};export{M as CHECKPOINT_FALLBACK_MS,T as createCheckpointRegistry,S as getShardCheckpoints,R as hasCheckpointsAttached,O as lunoraCollectionOptions,E as markCheckpointsAttached,x as releaseShardCheckpoints,F as shardCheckpointStats};
@@ -1 +1 @@
1
- import{LunoraError as l}from"@lunora/errors";import{createTransaction as y}from"@tanstack/db";import{getShardCheckpoints as R,hasCheckpointsAttached as M}from"./CHECKPOINT_FALLBACK_MS-CtPxAU9J.mjs";import{runOutboxMutation as _}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const w="__tanstack_db_direct",A="defineMutator: `serverRef` must be a generated mutator reference (api.mutators.*) or a 'namespace:fn' string",g=e=>{const t=typeof e=="string"?e:e?.__lunoraRef;if(typeof t!="string"||t.length===0)throw new l("INTERNAL",A);return t},E=e=>({__lunoraClientMutator:!0,apply:e.apply,serverRef:g(e.serverRef)}),I=(e,t,m)=>{let c=0;const p=()=>(c=Math.max(c,e.confirmedMutationWatermark(t.shardKey))+1,c);let u=Promise.resolve();const d=(r,o)=>{const a=u.then(async()=>{for(let s=0;;s+=1){const n=p(),{applied:i}=await e.callMutator(r,o,{clientSeq:n,shardKey:t.shardKey});if(i)return n;if(s>=32)throw new l("INTERNAL",`lunora: custom mutator "${r}" could not claim a fresh client sequence after ${String(32)} attempts`)}});return u=a.then(()=>{},()=>{}),a},h=()=>{if(t.checkpoints===!1)return;if(t.checkpoints)return t.checkpoints;const r=R(e,t.shardKey);return M(r)?r:void 0},f={};for(const[r,o]of Object.entries(m))f[r]=a=>{const s=y({autoCommit:!0,metadata:{[w]:!0,serverRef:o.serverRef},mutationFn:async()=>{let n=0;await _(async()=>{n=await d(o.serverRef,a)});const i=h();i&&(i.acknowledge({mutationId:n}),await i.awaitMutationId(n))}});return s.mutate(()=>{o.apply({collections:t.collections},a)}),s};return f};export{w as DIRECT_TRANSACTION_METADATA_KEY,I as bindMutators,E as defineMutator};
1
+ import{LunoraError as l}from"@lunora/errors";import{createTransaction as y}from"@tanstack/db";import{getShardCheckpoints as R,hasCheckpointsAttached as M}from"./CHECKPOINT_FALLBACK_MS-vBJLOBjX.mjs";import{runOutboxMutation as _}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const w="__tanstack_db_direct",A="defineMutator: `serverRef` must be a generated mutator reference (api.mutators.*) or a 'namespace:fn' string",g=e=>{const t=typeof e=="string"?e:e?.__lunoraRef;if(typeof t!="string"||t.length===0)throw new l("INTERNAL",A);return t},E=e=>({__lunoraClientMutator:!0,apply:e.apply,serverRef:g(e.serverRef)}),I=(e,t,m)=>{let c=0;const p=()=>(c=Math.max(c,e.confirmedMutationWatermark(t.shardKey))+1,c);let u=Promise.resolve();const d=(r,o)=>{const a=u.then(async()=>{for(let s=0;;s+=1){const n=p(),{applied:i}=await e.callMutator(r,o,{clientSeq:n,shardKey:t.shardKey});if(i)return n;if(s>=32)throw new l("INTERNAL",`lunora: custom mutator "${r}" could not claim a fresh client sequence after ${String(32)} attempts`)}});return u=a.then(()=>{},()=>{}),a},h=()=>{if(t.checkpoints===!1)return;if(t.checkpoints)return t.checkpoints;const r=R(e,t.shardKey);return M(r)?r:void 0},f={};for(const[r,o]of Object.entries(m))f[r]=a=>{const s=y({autoCommit:!0,metadata:{[w]:!0,serverRef:o.serverRef},mutationFn:async()=>{let n=0;await _(async()=>{n=await d(o.serverRef,a)});const i=h();i&&(i.acknowledge({mutationId:n}),await i.awaitMutationId(n))}});return s.mutate(()=>{o.apply({collections:t.collections},a)}),s};return f};export{w as DIRECT_TRANSACTION_METADATA_KEY,I as bindMutators,E as defineMutator};
@@ -206,6 +206,16 @@ interface CheckpointRegistry {
206
206
  awaitCheckpoint: (cursor: number) => Promise<void>;
207
207
  /** Resolve once the server has echoed a `lastMutationId >= id` for this client. */
208
208
  awaitMutationId: (id: number) => Promise<void>;
209
+ /**
210
+ * Tear the registry down: `clearTimeout` every armed fallback timer and empty
211
+ * the armed set. Idempotent. A discarded registry (Vite HMR dispose, sign-out)
212
+ * can otherwise hold up to `fallbackMs` of pending `setTimeout`s alive through
213
+ * their closures — keeping a Node/SSR event loop from draining. Distinct from
214
+ * {@link resolve}: `resolve` settles parked *waiters* (and disarms the timers it
215
+ * subsumes as a side effect); `dispose` guarantees no armed timer survives,
216
+ * independent of any watermark.
217
+ */
218
+ dispose: () => void;
209
219
  /** Advance the gates from a sync frame's watermark; later callers past the mark settle immediately. */
210
220
  resolve: (watermark: CheckpointWatermark) => void;
211
221
  /** Diagnostics counters — notably how often the fallback had to fire. */
@@ -206,6 +206,16 @@ interface CheckpointRegistry {
206
206
  awaitCheckpoint: (cursor: number) => Promise<void>;
207
207
  /** Resolve once the server has echoed a `lastMutationId >= id` for this client. */
208
208
  awaitMutationId: (id: number) => Promise<void>;
209
+ /**
210
+ * Tear the registry down: `clearTimeout` every armed fallback timer and empty
211
+ * the armed set. Idempotent. A discarded registry (Vite HMR dispose, sign-out)
212
+ * can otherwise hold up to `fallbackMs` of pending `setTimeout`s alive through
213
+ * their closures — keeping a Node/SSR event loop from draining. Distinct from
214
+ * {@link resolve}: `resolve` settles parked *waiters* (and disarms the timers it
215
+ * subsumes as a side effect); `dispose` guarantees no armed timer survives,
216
+ * independent of any watermark.
217
+ */
218
+ dispose: () => void;
209
219
  /** Advance the gates from a sync frame's watermark; later callers past the mark settle immediately. */
210
220
  resolve: (watermark: CheckpointWatermark) => void;
211
221
  /** Diagnostics counters — notably how often the fallback had to fire. */
@@ -1,7 +1,7 @@
1
1
  import { FunctionReference, SubscriptionError, LunoraClient } from '@lunora/client';
2
2
  import { Transaction, Collection } from '@tanstack/db';
3
3
  import { OfflineExecutor, StorageDiagnostic } from '@tanstack/offline-transactions';
4
- import { R as Row } from "./collection-options.d-C03Rjjxn.mjs";
4
+ import { R as Row } from "./collection-options.d-B_2IXvdU.mjs";
5
5
  /** Element type of an array (the row type a `list` query returns). */
6
6
  type Element<T> = T extends ReadonlyArray<infer E> ? E : never;
7
7
  /** `true` for the `any` type, `false` otherwise. */
@@ -1,7 +1,7 @@
1
1
  import { FunctionReference, SubscriptionError, LunoraClient } from '@lunora/client';
2
2
  import { Transaction, Collection } from '@tanstack/db';
3
3
  import { OfflineExecutor, StorageDiagnostic } from '@tanstack/offline-transactions';
4
- import { R as Row } from "./collection-options.d-C03Rjjxn.js";
4
+ import { R as Row } from "./collection-options.d-B_2IXvdU.js";
5
5
  /** Element type of an array (the row type a `list` query returns). */
6
6
  type Element<T> = T extends ReadonlyArray<infer E> ? E : never;
7
7
  /** `true` for the `any` type, `false` otherwise. */
@@ -1,6 +1,6 @@
1
1
  import { LunoraClient } from '@lunora/client';
2
2
  import { Collection, Transaction } from '@tanstack/db';
3
- import { C as CheckpointRegistry, R as Row } from "./collection-options.d-C03Rjjxn.mjs";
3
+ import { C as CheckpointRegistry, R as Row } from "./collection-options.d-B_2IXvdU.mjs";
4
4
  /**
5
5
  * TanStack DB's "direct transaction" marker.
6
6
  *
@@ -1,6 +1,6 @@
1
1
  import { LunoraClient } from '@lunora/client';
2
2
  import { Collection, Transaction } from '@tanstack/db';
3
- import { C as CheckpointRegistry, R as Row } from "./collection-options.d-C03Rjjxn.js";
3
+ import { C as CheckpointRegistry, R as Row } from "./collection-options.d-B_2IXvdU.js";
4
4
  /**
5
5
  * TanStack DB's "direct transaction" marker.
6
6
  *
@@ -1 +1 @@
1
- import{createCollection as I,safeRandomUUID as R}from"@tanstack/db";import{startOfflineExecutor as U,NonRetriableError as w}from"@tanstack/offline-transactions";import{lunoraCollectionOptions as _}from"./CHECKPOINT_FALLBACK_MS-CtPxAU9J.mjs";import{createOutboxCarrier as b,createOptimisticOnlineDetector as E,OUTBOX_MUTATION_FN_NAME as C,registerOutboxCarrier as M,runOutboxMutation as N}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const S=(a,K,e={})=>{const c={},p={},l={},y=Object.entries(K);for(const[t,o]of y){const n=o.insert,{config:d,scope:m}=_({client:a,getKey:o.getKey,id:t,list:o.list,...o.load===void 0?{}:{load:o.load},onError:o.onError,scopeBy:o.scopeBy,shardKey:o.shardKey});c[t]=I(d),o.scopeBy!==void 0&&(p[t]=m),n&&(l[t]=async({idempotencyKey:r,transaction:i})=>{for(const[f,x]of i.mutations.entries()){const O=x.modified,F=`${r}:${String(f)}`;try{await N(()=>a.mutation(n.mutation,n.toArgs(O),{mutationId:F}))}catch(u){if(u instanceof w&&e.onWriteRejected)try{e.onWriteRejected({code:u.code,collection:t,error:u,row:O})}catch{}throw u}}})}l[C]=async({transaction:t})=>{const o=t.metadata;if(o){if(o.identity!==a.currentIdentity())throw new w("outbox write dropped: identity changed since it was queued");await N(()=>a.mutation({__lunoraRef:o.functionPath},o.args,{mutationId:o.idempotencyKey,shardKey:o.shardKey}))}};const g=b(),s=U({collections:{...c,[C]:g},mutationFns:l,onlineDetector:E(),...e.onLeadershipChange?{onLeadershipChange:e.onLeadershipChange}:{},...e.onStorageFailure?{onStorageFailure:e.onStorageFailure}:{},onUnknownMutationFn:(t,o)=>{try{e.onWriteRejected?.({code:"UNKNOWN_MUTATION_FN",collection:t,error:new Error(`offline write dropped: mutation "${t}" no longer exists (removed or renamed in a deploy?)`),row:o.mutations[0]?.modified})}catch{}}});M(s,g);const h={};for(const[t,o]of y){const n=o.insert,d=c[t];if(!n||!d)continue;const m=s.createOfflineAction({mutationFnName:t,onMutate:({id:r,input:i})=>{d.insert(n.optimistic(i,r))}});h[t]=r=>{const i=R(),f=m({id:i,input:r});return{id:i,transaction:f}}}return{actions:h,collections:c,executor:s,pendingCount:()=>s.getPendingCount(),scope:p}};export{S as defineCollections};
1
+ import{createCollection as I,safeRandomUUID as R}from"@tanstack/db";import{startOfflineExecutor as U,NonRetriableError as w}from"@tanstack/offline-transactions";import{lunoraCollectionOptions as _}from"./CHECKPOINT_FALLBACK_MS-vBJLOBjX.mjs";import{createOutboxCarrier as b,createOptimisticOnlineDetector as E,OUTBOX_MUTATION_FN_NAME as C,registerOutboxCarrier as M,runOutboxMutation as N}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const S=(a,K,e={})=>{const c={},p={},l={},y=Object.entries(K);for(const[t,o]of y){const n=o.insert,{config:d,scope:m}=_({client:a,getKey:o.getKey,id:t,list:o.list,...o.load===void 0?{}:{load:o.load},onError:o.onError,scopeBy:o.scopeBy,shardKey:o.shardKey});c[t]=I(d),o.scopeBy!==void 0&&(p[t]=m),n&&(l[t]=async({idempotencyKey:r,transaction:i})=>{for(const[f,x]of i.mutations.entries()){const O=x.modified,F=`${r}:${String(f)}`;try{await N(()=>a.mutation(n.mutation,n.toArgs(O),{mutationId:F}))}catch(u){if(u instanceof w&&e.onWriteRejected)try{e.onWriteRejected({code:u.code,collection:t,error:u,row:O})}catch{}throw u}}})}l[C]=async({transaction:t})=>{const o=t.metadata;if(o){if(o.identity!==a.currentIdentity())throw new w("outbox write dropped: identity changed since it was queued");await N(()=>a.mutation({__lunoraRef:o.functionPath},o.args,{mutationId:o.idempotencyKey,shardKey:o.shardKey}))}};const g=b(),s=U({collections:{...c,[C]:g},mutationFns:l,onlineDetector:E(),...e.onLeadershipChange?{onLeadershipChange:e.onLeadershipChange}:{},...e.onStorageFailure?{onStorageFailure:e.onStorageFailure}:{},onUnknownMutationFn:(t,o)=>{try{e.onWriteRejected?.({code:"UNKNOWN_MUTATION_FN",collection:t,error:new Error(`offline write dropped: mutation "${t}" no longer exists (removed or renamed in a deploy?)`),row:o.mutations[0]?.modified})}catch{}}});M(s,g);const h={};for(const[t,o]of y){const n=o.insert,d=c[t];if(!n||!d)continue;const m=s.createOfflineAction({mutationFnName:t,onMutate:({id:r,input:i})=>{d.insert(n.optimistic(i,r))}});h[t]=r=>{const i=R(),f=m({id:i,input:r});return{id:i,transaction:f}}}return{actions:h,collections:c,executor:s,pendingCount:()=>s.getPendingCount(),scope:p}};export{S as defineCollections};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/db",
3
- "version": "1.0.0-alpha.30",
3
+ "version": "1.0.0-alpha.31",
4
4
  "description": "TanStack DB binding: typed, live-synced collections and a durable offline outbox over the Lunora client",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -54,7 +54,7 @@
54
54
  "access": "public"
55
55
  },
56
56
  "dependencies": {
57
- "@lunora/client": "1.0.0-alpha.30",
57
+ "@lunora/client": "1.0.0-alpha.31",
58
58
  "@lunora/errors": "1.0.0-alpha.8"
59
59
  },
60
60
  "peerDependencies": {
@@ -1 +0,0 @@
1
- import{LunoraError as y}from"@lunora/errors";import{BTreeIndex as w}from"@tanstack/db";import{toMap as b,makeDiffEmit as C}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const v=()=>{let e=Number.NEGATIVE_INFINITY;const a=[];return{advance:n=>{if(!(n<=e)){e=n;for(let o=a.length-1;o>=0;o-=1){const r=a[o];r&&r.threshold<=e&&(r.resolve(),a.splice(o,1))}}},await:n=>n<=e?Promise.resolve():new Promise(o=>{a.push({resolve:o,threshold:n})}),passed:n=>n<=e,waiting:()=>a.length}},M=3e3;let I=!1;const N=e=>{I||(I=!0,console.warn(`[@lunora/db] released an optimistic overlay via the ${String(e.waitedMs)}ms checkpoint fallback: the server confirmed ${e.kind} ${String(e.watermark)} but no sync frame ever echoed it. A dropped shape poke or \`settled\` frame is the usual cause — inspect the subscription rather than raising \`fallbackMs\`. (Reported once per process.)`))},S=(e={})=>{const a=e.fallbackMs??M,n=e.onFallback??N,o=v(),r=v();let s=0;const c=new Set,h=(i,t)=>{for(const d of c)d.kind===i&&d.threshold<=t&&(clearTimeout(d.handle),c.delete(d))},l=(i,t)=>{if(a<=0)return;const d=i==="checkpoint"?o:r;if(d.passed(t))return;for(const u of c)if(u.kind===i&&u.threshold>=t)return;const p={handle:setTimeout(()=>{if(c.delete(p),!d.passed(t)){s+=1,d.advance(t);try{n({kind:i,waitedMs:a,watermark:t})}catch{}}},a),kind:i,threshold:t};c.add(p)};return{acknowledge:({checkpoint:i,mutationId:t})=>{i!==void 0&&l("checkpoint",i),t!==void 0&&l("mutationId",t)},awaitCheckpoint:i=>o.await(i),awaitMutationId:i=>r.await(i),resolve:({checkpoint:i,mutationId:t})=>{i!==void 0&&(o.advance(i),h("checkpoint",i)),t!==void 0&&(r.advance(t),h("mutationId",t))},stats:()=>({fallbacks:s,pendingCheckpointWaiters:o.waiting(),pendingMutationWaiters:r.waiting()})}},m=new WeakMap,T=(e,a,n)=>{let o=m.get(e);o||(o=new Map,m.set(e,o));const r=a??"",s=o.get(r);if(s)return s;const c=S(n);return o.set(r,c),c},g=new WeakSet,E=e=>{g.add(e)},R=e=>g.has(e),x=e=>{const a=m.get(e);if(a){for(const n of a.values())n.resolve({checkpoint:Number.POSITIVE_INFINITY,mutationId:Number.POSITIVE_INFINITY});m.delete(e)}},F=e=>{const a=m.get(e),n={};if(a)for(const[o,r]of a)n[o]=r.stats();return n},O=e=>{if(e.list===void 0==(e.shape===void 0))throw new y("INTERNAL","lunoraCollectionOptions: pass exactly one of `list` or `shape`");const a=e.getKey??(t=>t._id),n=e.checkpoints??T(e.client,e.shape?.shardKey??e.shardKey);E(n);const o=new Map;let r,s,c,h;const l=(t,d)=>{const p=k=>{r?.(b(k,a)),d?.(),e.shape===void 0&&n.resolve({mutationId:e.client.confirmedMutationWatermark(e.shardKey)})},u=k=>c?.(k),f=k=>{n.resolve(k)};return e.shape!==void 0?e.client.subscribeShape({args:t,name:e.shape.name},p,{onCheckpoint:f,onError:u,shardKey:e.shape.shardKey}):e.client.subscribe(e.list,t,p,{onCheckpoint:f,onError:u,shardKey:e.shardKey})},i={autoIndex:"eager",defaultIndexType:w,getKey:a,id:e.id??e.list?.__lunoraRef??`shape:${e.shape?.name??""}`,...e.load==="eager"?{startSync:!0}:{},sync:{sync:t=>(r=C(o,t),c=d=>{t.markReady(),e.onError?.(d)},e.scopeBy===void 0?s=l(e.shape?.args??{},()=>{t.markReady()}):(t.markReady(),h!==void 0&&(s=l(h,void 0))),()=>{r=void 0,c=void 0,s?.(),s=void 0,o.clear()})}};return{checkpoints:n,config:i,scope:t=>{e.scopeBy!==void 0&&(h=t,s?.(),s=void 0,r?.(new Map),t!==void 0&&r!==void 0&&(s=l(t,void 0)))}}};export{M as CHECKPOINT_FALLBACK_MS,S as createCheckpointRegistry,T as getShardCheckpoints,R as hasCheckpointsAttached,O as lunoraCollectionOptions,E as markCheckpointsAttached,x as releaseShardCheckpoints,F as shardCheckpointStats};