hyperstack-typescript 0.2.4 → 0.3.0

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
@@ -51,6 +51,7 @@ interface HyperStackOptions<TStack extends StackDefinition> {
51
51
  reconnectIntervals?: number[];
52
52
  maxReconnectAttempts?: number;
53
53
  }
54
+ declare const DEFAULT_MAX_ENTRIES_PER_VIEW = 10000;
54
55
  interface HyperStackConfig {
55
56
  websocketUrl?: string;
56
57
  reconnectIntervals?: number[];
@@ -113,11 +114,11 @@ interface SnapshotFrame<T = unknown> {
113
114
  }
114
115
  type Frame<T = unknown> = EntityFrame<T> | SnapshotFrame<T>;
115
116
  declare function isSnapshotFrame<T>(frame: Frame<T>): frame is SnapshotFrame<T>;
116
- declare function parseFrame(data: ArrayBuffer | string): EntityFrame;
117
- declare function parseFrameFromBlob(blob: Blob): Promise<EntityFrame>;
117
+ declare function parseFrame(data: ArrayBuffer | string): Frame;
118
+ declare function parseFrameFromBlob(blob: Blob): Promise<Frame>;
118
119
  declare function isValidFrame(frame: unknown): frame is Frame;
119
120
 
120
- type FrameHandler = <T>(frame: EntityFrame<T>) => void;
121
+ type FrameHandler = <T>(frame: Frame<T>) => void;
121
122
  declare class ConnectionManager {
122
123
  private ws;
123
124
  private websocketUrl;
@@ -151,36 +152,31 @@ declare class ConnectionManager {
151
152
  private stopPingInterval;
152
153
  }
153
154
 
154
- interface EntityStoreConfig {
155
+ type UpdateCallback<T = unknown> = (viewPath: string, key: string, update: Update<T>) => void;
156
+ type RichUpdateCallback<T = unknown> = (viewPath: string, key: string, update: RichUpdate<T>) => void;
157
+ interface StorageAdapterConfig {
155
158
  maxEntriesPerView?: number | null;
156
159
  }
157
- type EntityUpdateCallback = (viewPath: string, key: string, update: Update<unknown>) => void;
158
- type RichUpdateCallback = (viewPath: string, key: string, update: RichUpdate<unknown>) => void;
159
- declare class EntityStore {
160
- private views;
161
- private updateCallbacks;
162
- private richUpdateCallbacks;
163
- private maxEntriesPerView;
164
- constructor(config?: EntityStoreConfig);
165
- private enforceMaxEntries;
166
- handleFrame<T>(frame: Frame<T>): void;
167
- private handleSnapshotFrame;
168
- private handleEntityFrame;
169
- getAll<T>(viewPath: string): T[];
160
+ /**
161
+ * Storage adapter interface for HyperStack entity storage.
162
+ * Implement this to integrate with Zustand, Pinia, Svelte stores, Redux, IndexedDB, etc.
163
+ */
164
+ interface StorageAdapter {
170
165
  get<T>(viewPath: string, key: string): T | null;
166
+ getAll<T>(viewPath: string): T[];
171
167
  getAllSync<T>(viewPath: string): T[] | undefined;
172
168
  getSync<T>(viewPath: string, key: string): T | null | undefined;
169
+ has(viewPath: string, key: string): boolean;
173
170
  keys(viewPath: string): string[];
174
171
  size(viewPath: string): number;
175
- clear(): void;
176
- clearView(viewPath: string): void;
177
- onUpdate(callback: EntityUpdateCallback): UnsubscribeFn;
178
- onRichUpdate(callback: RichUpdateCallback): UnsubscribeFn;
179
- subscribe<T>(viewPath: string, callback: SubscribeCallback<T>): UnsubscribeFn;
180
- subscribeToKey<T>(viewPath: string, key: string, callback: SubscribeCallback<T>): UnsubscribeFn;
181
- private notifyUpdate;
182
- private notifyRichUpdate;
183
- private notifyRichDelete;
172
+ set<T>(viewPath: string, key: string, data: T): void;
173
+ delete(viewPath: string, key: string): void;
174
+ clear(viewPath?: string): void;
175
+ evictOldest?(viewPath: string): string | undefined;
176
+ onUpdate(callback: UpdateCallback): () => void;
177
+ onRichUpdate(callback: RichUpdateCallback): () => void;
178
+ notifyUpdate<T>(viewPath: string, key: string, update: Update<T>): void;
179
+ notifyRichUpdate<T>(viewPath: string, key: string, update: RichUpdate<T>): void;
184
180
  }
185
181
 
186
182
  declare class SubscriptionRegistry {
@@ -195,34 +191,76 @@ declare class SubscriptionRegistry {
195
191
  private makeSubKey;
196
192
  }
197
193
 
194
+ interface HyperStackOptionsWithStorage<TStack extends StackDefinition> extends HyperStackOptions<TStack> {
195
+ storage?: StorageAdapter;
196
+ maxEntriesPerView?: number | null;
197
+ }
198
198
  declare class HyperStack<TStack extends StackDefinition> {
199
199
  private readonly connection;
200
- private readonly store;
200
+ private readonly storage;
201
+ private readonly processor;
201
202
  private readonly subscriptionRegistry;
202
203
  private readonly _views;
203
204
  private readonly stack;
204
205
  private constructor();
205
- static connect<T extends StackDefinition>(url: string, options: HyperStackOptions<T>): Promise<HyperStack<T>>;
206
+ static connect<T extends StackDefinition>(url: string, options: HyperStackOptionsWithStorage<T>): Promise<HyperStack<T>>;
206
207
  get views(): TypedViews<TStack['views']>;
207
208
  get connectionState(): ConnectionState;
208
209
  get stackName(): string;
210
+ get store(): StorageAdapter;
209
211
  onConnectionStateChange(callback: ConnectionStateCallback): UnsubscribeFn;
210
- onFrame(callback: (frame: EntityFrame) => void): UnsubscribeFn;
212
+ onFrame(callback: (frame: Frame) => void): UnsubscribeFn;
211
213
  connect(): Promise<void>;
212
214
  disconnect(): void;
213
215
  isConnected(): boolean;
214
216
  clearStore(): void;
215
- getStore(): EntityStore;
217
+ getStore(): StorageAdapter;
216
218
  getConnection(): ConnectionManager;
217
219
  getSubscriptionRegistry(): SubscriptionRegistry;
218
220
  }
219
221
 
220
- declare function createUpdateStream<T>(store: EntityStore, subscriptionRegistry: SubscriptionRegistry, subscription: Subscription, keyFilter?: string): AsyncIterable<Update<T>>;
221
- declare function createRichUpdateStream<T>(store: EntityStore, subscriptionRegistry: SubscriptionRegistry, subscription: Subscription, keyFilter?: string): AsyncIterable<RichUpdate<T>>;
222
+ interface FrameProcessorConfig {
223
+ maxEntriesPerView?: number | null;
224
+ }
225
+ declare class FrameProcessor {
226
+ private storage;
227
+ private maxEntriesPerView;
228
+ constructor(storage: StorageAdapter, config?: FrameProcessorConfig);
229
+ handleFrame<T>(frame: Frame<T>): void;
230
+ private handleSnapshotFrame;
231
+ private handleEntityFrame;
232
+ private emitRichUpdate;
233
+ private enforceMaxEntries;
234
+ }
235
+
236
+ declare class MemoryAdapter implements StorageAdapter {
237
+ private views;
238
+ private updateCallbacks;
239
+ private richUpdateCallbacks;
240
+ constructor(_config?: StorageAdapterConfig);
241
+ get<T>(viewPath: string, key: string): T | null;
242
+ getAll<T>(viewPath: string): T[];
243
+ getAllSync<T>(viewPath: string): T[] | undefined;
244
+ getSync<T>(viewPath: string, key: string): T | null | undefined;
245
+ has(viewPath: string, key: string): boolean;
246
+ keys(viewPath: string): string[];
247
+ size(viewPath: string): number;
248
+ set<T>(viewPath: string, key: string, data: T): void;
249
+ delete(viewPath: string, key: string): void;
250
+ clear(viewPath?: string): void;
251
+ evictOldest(viewPath: string): string | undefined;
252
+ onUpdate(callback: UpdateCallback): () => void;
253
+ onRichUpdate(callback: RichUpdateCallback): () => void;
254
+ notifyUpdate<T>(viewPath: string, key: string, update: Update<T>): void;
255
+ notifyRichUpdate<T>(viewPath: string, key: string, update: RichUpdate<T>): void;
256
+ }
257
+
258
+ declare function createUpdateStream<T>(storage: StorageAdapter, subscriptionRegistry: SubscriptionRegistry, subscription: Subscription, keyFilter?: string): AsyncIterable<Update<T>>;
259
+ declare function createRichUpdateStream<T>(storage: StorageAdapter, subscriptionRegistry: SubscriptionRegistry, subscription: Subscription, keyFilter?: string): AsyncIterable<RichUpdate<T>>;
222
260
 
223
- declare function createTypedStateView<T>(viewDef: ViewDef<T, 'state'>, store: EntityStore, subscriptionRegistry: SubscriptionRegistry): TypedStateView<T>;
224
- declare function createTypedListView<T>(viewDef: ViewDef<T, 'list'>, store: EntityStore, subscriptionRegistry: SubscriptionRegistry): TypedListView<T>;
225
- declare function createTypedViews<TStack extends StackDefinition>(stack: TStack, store: EntityStore, subscriptionRegistry: SubscriptionRegistry): TypedViews<TStack['views']>;
261
+ declare function createTypedStateView<T>(viewDef: ViewDef<T, 'state'>, storage: StorageAdapter, subscriptionRegistry: SubscriptionRegistry): TypedStateView<T>;
262
+ declare function createTypedListView<T>(viewDef: ViewDef<T, 'list'>, storage: StorageAdapter, subscriptionRegistry: SubscriptionRegistry): TypedListView<T>;
263
+ declare function createTypedViews<TStack extends StackDefinition>(stack: TStack, storage: StorageAdapter, subscriptionRegistry: SubscriptionRegistry): TypedViews<TStack['views']>;
226
264
 
227
- export { ConnectionManager, DEFAULT_CONFIG, EntityStore, HyperStack, HyperStackError, SubscriptionRegistry, createRichUpdateStream, createTypedListView, createTypedStateView, createTypedViews, createUpdateStream, isSnapshotFrame, isValidFrame, parseFrame, parseFrameFromBlob };
228
- export type { ConnectionState, ConnectionStateCallback, EntityFrame, Frame, FrameMode, FrameOp, HyperStackConfig, HyperStackOptions, RichUpdate, SnapshotEntity, SnapshotFrame, StackDefinition, SubscribeCallback, Subscription, TypedListView, TypedStateView, TypedViewGroup, TypedViews, UnsubscribeFn, Update, ViewDef, ViewGroup };
265
+ export { ConnectionManager, DEFAULT_CONFIG, DEFAULT_MAX_ENTRIES_PER_VIEW, FrameProcessor, HyperStack, HyperStackError, MemoryAdapter, SubscriptionRegistry, createRichUpdateStream, createTypedListView, createTypedStateView, createTypedViews, createUpdateStream, isSnapshotFrame, isValidFrame, parseFrame, parseFrameFromBlob };
266
+ 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 };