hyperstack-typescript 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -44,6 +44,8 @@ interface Subscription {
44
44
  key?: string;
45
45
  partition?: string;
46
46
  filters?: Record<string, string>;
47
+ take?: number;
48
+ skip?: number;
47
49
  }
48
50
  interface HyperStackOptions<TStack extends StackDefinition> {
49
51
  stack: TStack;
@@ -93,7 +95,18 @@ type UnsubscribeFn = () => void;
93
95
  type ConnectionStateCallback = (state: ConnectionState, error?: string) => void;
94
96
 
95
97
  type FrameMode = 'state' | 'append' | 'list';
96
- type FrameOp = 'create' | 'upsert' | 'patch' | 'delete' | 'snapshot';
98
+ type FrameOp = 'create' | 'upsert' | 'patch' | 'delete' | 'snapshot' | 'subscribed';
99
+ type SortOrder = 'asc' | 'desc';
100
+ interface SortConfig {
101
+ field: string[];
102
+ order: SortOrder;
103
+ }
104
+ interface SubscribedFrame {
105
+ op: 'subscribed';
106
+ view: string;
107
+ mode: FrameMode;
108
+ sort?: SortConfig;
109
+ }
97
110
  interface EntityFrame<T = unknown> {
98
111
  mode: FrameMode;
99
112
  entity: string;
@@ -114,8 +127,10 @@ interface SnapshotFrame<T = unknown> {
114
127
  /** Indicates if this is the final snapshot batch. When false, more batches will follow. */
115
128
  complete?: boolean;
116
129
  }
117
- type Frame<T = unknown> = EntityFrame<T> | SnapshotFrame<T>;
130
+ type Frame<T = unknown> = EntityFrame<T> | SnapshotFrame<T> | SubscribedFrame;
118
131
  declare function isSnapshotFrame<T>(frame: Frame<T>): frame is SnapshotFrame<T>;
132
+ declare function isSubscribedFrame(frame: Frame): frame is SubscribedFrame;
133
+ declare function isEntityFrame<T>(frame: Frame<T>): frame is EntityFrame<T>;
119
134
  declare function parseFrame(data: ArrayBuffer | string): Frame;
120
135
  declare function parseFrameFromBlob(blob: Blob): Promise<Frame>;
121
136
  declare function isValidFrame(frame: unknown): frame is Frame;
@@ -155,10 +170,13 @@ declare class ConnectionManager {
155
170
  }
156
171
 
157
172
  type UpdateCallback<T = unknown> = (viewPath: string, key: string, update: Update<T>) => void;
158
- type RichUpdateCallback<T = unknown> = (viewPath: string, key: string, update: RichUpdate<T>) => void;
173
+ type RichUpdateCallback$1<T = unknown> = (viewPath: string, key: string, update: RichUpdate<T>) => void;
159
174
  interface StorageAdapterConfig {
160
175
  maxEntriesPerView?: number | null;
161
176
  }
177
+ interface ViewSortConfig {
178
+ sort?: SortConfig;
179
+ }
162
180
  /**
163
181
  * Storage adapter interface for HyperStack entity storage.
164
182
  * Implement this to integrate with Zustand, Pinia, Svelte stores, Redux, IndexedDB, etc.
@@ -175,8 +193,10 @@ interface StorageAdapter {
175
193
  delete(viewPath: string, key: string): void;
176
194
  clear(viewPath?: string): void;
177
195
  evictOldest?(viewPath: string): string | undefined;
196
+ setViewConfig?(viewPath: string, config: ViewSortConfig): void;
197
+ getViewConfig?(viewPath: string): ViewSortConfig | undefined;
178
198
  onUpdate(callback: UpdateCallback): () => void;
179
- onRichUpdate(callback: RichUpdateCallback): () => void;
199
+ onRichUpdate(callback: RichUpdateCallback$1): () => void;
180
200
  notifyUpdate<T>(viewPath: string, key: string, update: Update<T>): void;
181
201
  notifyRichUpdate<T>(viewPath: string, key: string, update: RichUpdate<T>): void;
182
202
  }
@@ -223,18 +243,86 @@ declare class HyperStack<TStack extends StackDefinition> {
223
243
 
224
244
  interface FrameProcessorConfig {
225
245
  maxEntriesPerView?: number | null;
246
+ /**
247
+ * Interval in milliseconds to buffer frames before flushing to storage.
248
+ * Set to 0 for immediate processing (no buffering).
249
+ * Default: 0 (immediate)
250
+ *
251
+ * For React applications, 16ms (one frame at 60fps) is recommended to
252
+ * reduce unnecessary re-renders during high-frequency updates.
253
+ */
254
+ flushIntervalMs?: number;
226
255
  }
227
256
  declare class FrameProcessor {
228
257
  private storage;
229
258
  private maxEntriesPerView;
259
+ private flushIntervalMs;
260
+ private pendingUpdates;
261
+ private flushTimer;
262
+ private isProcessing;
230
263
  constructor(storage: StorageAdapter, config?: FrameProcessorConfig);
231
264
  handleFrame<T>(frame: Frame<T>): void;
265
+ /**
266
+ * Immediately flush all pending updates.
267
+ * Useful for ensuring all updates are processed before reading state.
268
+ */
269
+ flush(): void;
270
+ /**
271
+ * Clean up any pending timers. Call when disposing the processor.
272
+ */
273
+ dispose(): void;
274
+ private scheduleFlush;
275
+ private flushPendingUpdates;
276
+ private processFrame;
277
+ private processFrameWithoutEnforce;
278
+ private handleSubscribedFrame;
232
279
  private handleSnapshotFrame;
280
+ private handleSnapshotFrameWithoutEnforce;
233
281
  private handleEntityFrame;
282
+ private handleEntityFrameWithoutEnforce;
234
283
  private emitRichUpdate;
235
284
  private enforceMaxEntries;
236
285
  }
237
286
 
287
+ interface EntityStoreConfig {
288
+ maxEntriesPerView?: number | null;
289
+ }
290
+ interface ViewConfig {
291
+ sort?: SortConfig;
292
+ }
293
+ type EntityUpdateCallback = (viewPath: string, key: string, update: Update<unknown>) => void;
294
+ type RichUpdateCallback = (viewPath: string, key: string, update: RichUpdate<unknown>) => void;
295
+ declare class EntityStore {
296
+ private views;
297
+ private viewConfigs;
298
+ private updateCallbacks;
299
+ private richUpdateCallbacks;
300
+ private maxEntriesPerView;
301
+ constructor(config?: EntityStoreConfig);
302
+ private enforceMaxEntries;
303
+ handleFrame<T>(frame: Frame<T>): void;
304
+ private handleSubscribedFrame;
305
+ private handleSnapshotFrame;
306
+ private handleEntityFrame;
307
+ getAll<T>(viewPath: string): T[];
308
+ get<T>(viewPath: string, key: string): T | null;
309
+ getAllSync<T>(viewPath: string): T[] | undefined;
310
+ getSync<T>(viewPath: string, key: string): T | null | undefined;
311
+ keys(viewPath: string): string[];
312
+ size(viewPath: string): number;
313
+ clear(): void;
314
+ clearView(viewPath: string): void;
315
+ getViewConfig(viewPath: string): ViewConfig | undefined;
316
+ setViewConfig(viewPath: string, config: ViewConfig): void;
317
+ onUpdate(callback: EntityUpdateCallback): UnsubscribeFn;
318
+ onRichUpdate(callback: RichUpdateCallback): UnsubscribeFn;
319
+ subscribe<T>(viewPath: string, callback: SubscribeCallback<T>): UnsubscribeFn;
320
+ subscribeToKey<T>(viewPath: string, key: string, callback: SubscribeCallback<T>): UnsubscribeFn;
321
+ private notifyUpdate;
322
+ private notifyRichUpdate;
323
+ private notifyRichDelete;
324
+ }
325
+
238
326
  declare class MemoryAdapter implements StorageAdapter {
239
327
  private views;
240
328
  private updateCallbacks;
@@ -252,7 +340,7 @@ declare class MemoryAdapter implements StorageAdapter {
252
340
  clear(viewPath?: string): void;
253
341
  evictOldest(viewPath: string): string | undefined;
254
342
  onUpdate(callback: UpdateCallback): () => void;
255
- onRichUpdate(callback: RichUpdateCallback): () => void;
343
+ onRichUpdate(callback: RichUpdateCallback$1): () => void;
256
344
  notifyUpdate<T>(viewPath: string, key: string, update: Update<T>): void;
257
345
  notifyRichUpdate<T>(viewPath: string, key: string, update: RichUpdate<T>): void;
258
346
  }
@@ -264,5 +352,5 @@ declare function createTypedStateView<T>(viewDef: ViewDef<T, 'state'>, storage:
264
352
  declare function createTypedListView<T>(viewDef: ViewDef<T, 'list'>, storage: StorageAdapter, subscriptionRegistry: SubscriptionRegistry): TypedListView<T>;
265
353
  declare function createTypedViews<TStack extends StackDefinition>(stack: TStack, storage: StorageAdapter, subscriptionRegistry: SubscriptionRegistry): TypedViews<TStack['views']>;
266
354
 
267
- export { ConnectionManager, DEFAULT_CONFIG, DEFAULT_MAX_ENTRIES_PER_VIEW, FrameProcessor, HyperStack, HyperStackError, MemoryAdapter, SubscriptionRegistry, createRichUpdateStream, createTypedListView, createTypedStateView, createTypedViews, createUpdateStream, isSnapshotFrame, isValidFrame, parseFrame, parseFrameFromBlob };
268
- export type { ConnectionState, ConnectionStateCallback, EntityFrame, Frame, FrameMode, FrameOp, FrameProcessorConfig, HyperStackConfig, HyperStackOptions, HyperStackOptionsWithStorage, RichUpdate, RichUpdateCallback, SnapshotEntity, SnapshotFrame, StackDefinition, StorageAdapter, StorageAdapterConfig, SubscribeCallback, Subscription, TypedListView, TypedStateView, TypedViewGroup, TypedViews, UnsubscribeFn, Update, UpdateCallback, ViewDef, ViewGroup };
355
+ export { ConnectionManager, DEFAULT_CONFIG, DEFAULT_MAX_ENTRIES_PER_VIEW, EntityStore, FrameProcessor, HyperStack, HyperStackError, MemoryAdapter, SubscriptionRegistry, createRichUpdateStream, createTypedListView, createTypedStateView, createTypedViews, createUpdateStream, isEntityFrame, isSnapshotFrame, isSubscribedFrame, isValidFrame, parseFrame, parseFrameFromBlob };
356
+ export type { ConnectionState, ConnectionStateCallback, EntityFrame, EntityStoreConfig, Frame, FrameMode, FrameOp, FrameProcessorConfig, HyperStackConfig, HyperStackOptions, HyperStackOptionsWithStorage, RichUpdate, RichUpdateCallback$1 as RichUpdateCallback, SnapshotEntity, SnapshotFrame, SortConfig, SortOrder, StackDefinition, StorageAdapter, StorageAdapterConfig, SubscribeCallback, SubscribedFrame, Subscription, TypedListView, TypedStateView, TypedViewGroup, TypedViews, UnsubscribeFn, Update, UpdateCallback, ViewConfig, ViewDef, ViewGroup, ViewSortConfig };