hyperstack-react 0.4.0 → 0.4.1

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.esm.js CHANGED
@@ -7383,6 +7383,198 @@ class MemoryAdapter {
7383
7383
  }
7384
7384
  }
7385
7385
 
7386
+ function getNestedValue$1(obj, path) {
7387
+ let current = obj;
7388
+ for (const segment of path) {
7389
+ if (current === null || current === undefined)
7390
+ return undefined;
7391
+ if (typeof current !== 'object')
7392
+ return undefined;
7393
+ current = current[segment];
7394
+ }
7395
+ return current;
7396
+ }
7397
+ function compareSortValues$1(a, b) {
7398
+ if (a === b)
7399
+ return 0;
7400
+ if (a === undefined || a === null)
7401
+ return -1;
7402
+ if (b === undefined || b === null)
7403
+ return 1;
7404
+ if (typeof a === 'number' && typeof b === 'number') {
7405
+ return a - b;
7406
+ }
7407
+ if (typeof a === 'string' && typeof b === 'string') {
7408
+ return a.localeCompare(b);
7409
+ }
7410
+ if (typeof a === 'boolean' && typeof b === 'boolean') {
7411
+ return (a ? 1 : 0) - (b ? 1 : 0);
7412
+ }
7413
+ return String(a).localeCompare(String(b));
7414
+ }
7415
+ class SortedStorageDecorator {
7416
+ constructor(inner) {
7417
+ this.sortConfigs = new Map();
7418
+ this.sortedKeysMap = new Map();
7419
+ this.inner = inner;
7420
+ }
7421
+ get(viewPath, key) {
7422
+ return this.inner.get(viewPath, key);
7423
+ }
7424
+ getAll(viewPath) {
7425
+ const sortedKeys = this.sortedKeysMap.get(viewPath);
7426
+ if (sortedKeys && sortedKeys.length > 0) {
7427
+ return sortedKeys
7428
+ .map(k => this.inner.get(viewPath, k))
7429
+ .filter((v) => v !== null);
7430
+ }
7431
+ return this.inner.getAll(viewPath);
7432
+ }
7433
+ getAllSync(viewPath) {
7434
+ const sortedKeys = this.sortedKeysMap.get(viewPath);
7435
+ if (sortedKeys && sortedKeys.length > 0) {
7436
+ return sortedKeys
7437
+ .map(k => this.inner.getSync(viewPath, k))
7438
+ .filter((v) => v !== null && v !== undefined);
7439
+ }
7440
+ return this.inner.getAllSync(viewPath);
7441
+ }
7442
+ getSync(viewPath, key) {
7443
+ return this.inner.getSync(viewPath, key);
7444
+ }
7445
+ has(viewPath, key) {
7446
+ return this.inner.has(viewPath, key);
7447
+ }
7448
+ keys(viewPath) {
7449
+ const sortedKeys = this.sortedKeysMap.get(viewPath);
7450
+ if (sortedKeys)
7451
+ return [...sortedKeys];
7452
+ return this.inner.keys(viewPath);
7453
+ }
7454
+ size(viewPath) {
7455
+ return this.inner.size(viewPath);
7456
+ }
7457
+ set(viewPath, key, data) {
7458
+ this.inner.set(viewPath, key, data);
7459
+ const sortConfig = this.sortConfigs.get(viewPath);
7460
+ if (sortConfig) {
7461
+ this.updateSortedPosition(viewPath, key, data, sortConfig);
7462
+ }
7463
+ }
7464
+ delete(viewPath, key) {
7465
+ const sortedKeys = this.sortedKeysMap.get(viewPath);
7466
+ if (sortedKeys) {
7467
+ const idx = sortedKeys.indexOf(key);
7468
+ if (idx !== -1) {
7469
+ sortedKeys.splice(idx, 1);
7470
+ }
7471
+ }
7472
+ this.inner.delete(viewPath, key);
7473
+ }
7474
+ clear(viewPath) {
7475
+ if (viewPath) {
7476
+ this.sortedKeysMap.delete(viewPath);
7477
+ this.sortConfigs.delete(viewPath);
7478
+ }
7479
+ else {
7480
+ this.sortedKeysMap.clear();
7481
+ this.sortConfigs.clear();
7482
+ }
7483
+ this.inner.clear(viewPath);
7484
+ }
7485
+ evictOldest(viewPath) {
7486
+ const sortedKeys = this.sortedKeysMap.get(viewPath);
7487
+ if (sortedKeys && sortedKeys.length > 0) {
7488
+ const oldest = sortedKeys.pop();
7489
+ this.inner.delete(viewPath, oldest);
7490
+ return oldest;
7491
+ }
7492
+ return this.inner.evictOldest?.(viewPath);
7493
+ }
7494
+ setViewConfig(viewPath, config) {
7495
+ if (config.sort && !this.sortConfigs.has(viewPath)) {
7496
+ this.sortConfigs.set(viewPath, config.sort);
7497
+ this.rebuildSortedKeys(viewPath, config.sort);
7498
+ }
7499
+ this.inner.setViewConfig?.(viewPath, config);
7500
+ }
7501
+ getViewConfig(viewPath) {
7502
+ const sortConfig = this.sortConfigs.get(viewPath);
7503
+ if (sortConfig)
7504
+ return { sort: sortConfig };
7505
+ return this.inner.getViewConfig?.(viewPath);
7506
+ }
7507
+ onUpdate(callback) {
7508
+ return this.inner.onUpdate(callback);
7509
+ }
7510
+ onRichUpdate(callback) {
7511
+ return this.inner.onRichUpdate(callback);
7512
+ }
7513
+ notifyUpdate(viewPath, key, update) {
7514
+ this.inner.notifyUpdate(viewPath, key, update);
7515
+ }
7516
+ notifyRichUpdate(viewPath, key, update) {
7517
+ this.inner.notifyRichUpdate(viewPath, key, update);
7518
+ }
7519
+ updateSortedPosition(viewPath, key, data, sortConfig) {
7520
+ let sortedKeys = this.sortedKeysMap.get(viewPath);
7521
+ if (!sortedKeys) {
7522
+ sortedKeys = [];
7523
+ this.sortedKeysMap.set(viewPath, sortedKeys);
7524
+ }
7525
+ const existingIdx = sortedKeys.indexOf(key);
7526
+ if (existingIdx !== -1) {
7527
+ sortedKeys.splice(existingIdx, 1);
7528
+ }
7529
+ const insertIdx = this.binarySearchInsertPosition(viewPath, sortedKeys, sortConfig, key, data);
7530
+ sortedKeys.splice(insertIdx, 0, key);
7531
+ }
7532
+ binarySearchInsertPosition(viewPath, sortedKeys, sortConfig, newKey, newValue) {
7533
+ const newSortValue = getNestedValue$1(newValue, sortConfig.field);
7534
+ const isDesc = sortConfig.order === 'desc';
7535
+ let low = 0;
7536
+ let high = sortedKeys.length;
7537
+ while (low < high) {
7538
+ const mid = Math.floor((low + high) / 2);
7539
+ const midKey = sortedKeys[mid];
7540
+ const midEntity = this.inner.get(viewPath, midKey);
7541
+ const midValue = getNestedValue$1(midEntity, sortConfig.field);
7542
+ let cmp = compareSortValues$1(newSortValue, midValue);
7543
+ if (isDesc)
7544
+ cmp = -cmp;
7545
+ if (cmp === 0) {
7546
+ cmp = newKey.localeCompare(midKey);
7547
+ }
7548
+ if (cmp < 0) {
7549
+ high = mid;
7550
+ }
7551
+ else {
7552
+ low = mid + 1;
7553
+ }
7554
+ }
7555
+ return low;
7556
+ }
7557
+ rebuildSortedKeys(viewPath, sortConfig) {
7558
+ const allKeys = this.inner.keys(viewPath);
7559
+ if (allKeys.length === 0)
7560
+ return;
7561
+ const isDesc = sortConfig.order === 'desc';
7562
+ const entries = allKeys.map(k => [k, this.inner.get(viewPath, k)]);
7563
+ entries.sort((a, b) => {
7564
+ const aValue = getNestedValue$1(a[1], sortConfig.field);
7565
+ const bValue = getNestedValue$1(b[1], sortConfig.field);
7566
+ let cmp = compareSortValues$1(aValue, bValue);
7567
+ if (isDesc)
7568
+ cmp = -cmp;
7569
+ if (cmp === 0) {
7570
+ cmp = a[0].localeCompare(b[0]);
7571
+ }
7572
+ return cmp;
7573
+ });
7574
+ this.sortedKeysMap.set(viewPath, entries.map(([k]) => k));
7575
+ }
7576
+ }
7577
+
7386
7578
  class SubscriptionRegistry {
7387
7579
  constructor(connection) {
7388
7580
  this.subscriptions = new Map();
@@ -8175,9 +8367,10 @@ function createInstructionExecutor(wallet) {
8175
8367
  class HyperStack {
8176
8368
  constructor(url, options) {
8177
8369
  this.stack = options.stack;
8178
- this.storage = options.storage ?? new MemoryAdapter();
8370
+ this.storage = new SortedStorageDecorator(options.storage ?? new MemoryAdapter());
8179
8371
  this.processor = new FrameProcessor(this.storage, {
8180
8372
  maxEntriesPerView: options.maxEntriesPerView,
8373
+ flushIntervalMs: options.flushIntervalMs,
8181
8374
  });
8182
8375
  this.connection = new ConnectionManager({
8183
8376
  websocketUrl: url,
@@ -8211,6 +8404,7 @@ class HyperStack {
8211
8404
  stack,
8212
8405
  storage: options?.storage,
8213
8406
  maxEntriesPerView: options?.maxEntriesPerView,
8407
+ flushIntervalMs: options?.flushIntervalMs,
8214
8408
  autoReconnect: options?.autoReconnect,
8215
8409
  reconnectIntervals: options?.reconnectIntervals,
8216
8410
  maxReconnectAttempts: options?.maxReconnectAttempts,
@@ -8266,6 +8460,174 @@ class HyperStack {
8266
8460
  }
8267
8461
  }
8268
8462
 
8463
+ const DEFAULT_FLUSH_INTERVAL_MS = 16;
8464
+
8465
+ class ZustandAdapter {
8466
+ constructor(_config = {}) {
8467
+ this.updateCallbacks = new Set();
8468
+ this.richUpdateCallbacks = new Set();
8469
+ this.accessOrder = new Map();
8470
+ this.store = create((set) => ({
8471
+ entities: new Map(),
8472
+ viewConfigs: new Map(),
8473
+ connectionState: 'disconnected',
8474
+ lastError: undefined,
8475
+ _set: (viewPath, key, data) => {
8476
+ set((state) => {
8477
+ const newEntities = new Map(state.entities);
8478
+ const viewMap = new Map(newEntities.get(viewPath) ?? new Map());
8479
+ viewMap.set(key, data);
8480
+ newEntities.set(viewPath, viewMap);
8481
+ return { entities: newEntities };
8482
+ });
8483
+ },
8484
+ _delete: (viewPath, key) => {
8485
+ set((state) => {
8486
+ const newEntities = new Map(state.entities);
8487
+ const viewMap = newEntities.get(viewPath);
8488
+ if (viewMap) {
8489
+ const newViewMap = new Map(viewMap);
8490
+ newViewMap.delete(key);
8491
+ newEntities.set(viewPath, newViewMap);
8492
+ }
8493
+ return { entities: newEntities };
8494
+ });
8495
+ },
8496
+ _clear: (viewPath) => {
8497
+ set((state) => {
8498
+ if (viewPath) {
8499
+ const newEntities = new Map(state.entities);
8500
+ newEntities.delete(viewPath);
8501
+ return { entities: newEntities };
8502
+ }
8503
+ return { entities: new Map() };
8504
+ });
8505
+ },
8506
+ _setConnectionState: (connectionState, lastError) => {
8507
+ set({ connectionState, lastError });
8508
+ },
8509
+ _setViewConfig: (viewPath, config) => {
8510
+ set((state) => {
8511
+ const newConfigs = new Map(state.viewConfigs);
8512
+ newConfigs.set(viewPath, config);
8513
+ return { viewConfigs: newConfigs };
8514
+ });
8515
+ },
8516
+ }));
8517
+ }
8518
+ get(viewPath, key) {
8519
+ const viewMap = this.store.getState().entities.get(viewPath);
8520
+ if (!viewMap)
8521
+ return null;
8522
+ const value = viewMap.get(key);
8523
+ return value !== undefined ? value : null;
8524
+ }
8525
+ getAll(viewPath) {
8526
+ const viewMap = this.store.getState().entities.get(viewPath);
8527
+ if (!viewMap)
8528
+ return [];
8529
+ return Array.from(viewMap.values());
8530
+ }
8531
+ getAllSync(viewPath) {
8532
+ const viewMap = this.store.getState().entities.get(viewPath);
8533
+ if (!viewMap)
8534
+ return undefined;
8535
+ return Array.from(viewMap.values());
8536
+ }
8537
+ getSync(viewPath, key) {
8538
+ const viewMap = this.store.getState().entities.get(viewPath);
8539
+ if (!viewMap)
8540
+ return undefined;
8541
+ const value = viewMap.get(key);
8542
+ return value !== undefined ? value : null;
8543
+ }
8544
+ has(viewPath, key) {
8545
+ return this.store.getState().entities.get(viewPath)?.has(key) ?? false;
8546
+ }
8547
+ keys(viewPath) {
8548
+ const viewMap = this.store.getState().entities.get(viewPath);
8549
+ if (!viewMap)
8550
+ return [];
8551
+ return Array.from(viewMap.keys());
8552
+ }
8553
+ size(viewPath) {
8554
+ return this.store.getState().entities.get(viewPath)?.size ?? 0;
8555
+ }
8556
+ set(viewPath, key, data) {
8557
+ let order = this.accessOrder.get(viewPath);
8558
+ if (!order) {
8559
+ order = [];
8560
+ this.accessOrder.set(viewPath, order);
8561
+ }
8562
+ const existingIdx = order.indexOf(key);
8563
+ if (existingIdx !== -1) {
8564
+ order.splice(existingIdx, 1);
8565
+ }
8566
+ order.push(key);
8567
+ this.store.getState()._set(viewPath, key, data);
8568
+ }
8569
+ delete(viewPath, key) {
8570
+ const order = this.accessOrder.get(viewPath);
8571
+ if (order) {
8572
+ const idx = order.indexOf(key);
8573
+ if (idx !== -1) {
8574
+ order.splice(idx, 1);
8575
+ }
8576
+ }
8577
+ this.store.getState()._delete(viewPath, key);
8578
+ }
8579
+ clear(viewPath) {
8580
+ if (viewPath) {
8581
+ this.accessOrder.delete(viewPath);
8582
+ }
8583
+ else {
8584
+ this.accessOrder.clear();
8585
+ }
8586
+ this.store.getState()._clear(viewPath);
8587
+ }
8588
+ evictOldest(viewPath) {
8589
+ const order = this.accessOrder.get(viewPath);
8590
+ if (!order || order.length === 0)
8591
+ return undefined;
8592
+ const oldest = order.shift();
8593
+ if (oldest !== undefined) {
8594
+ this.store.getState()._delete(viewPath, oldest);
8595
+ }
8596
+ return oldest;
8597
+ }
8598
+ onUpdate(callback) {
8599
+ this.updateCallbacks.add(callback);
8600
+ return () => this.updateCallbacks.delete(callback);
8601
+ }
8602
+ onRichUpdate(callback) {
8603
+ this.richUpdateCallbacks.add(callback);
8604
+ return () => this.richUpdateCallbacks.delete(callback);
8605
+ }
8606
+ notifyUpdate(viewPath, key, update) {
8607
+ for (const callback of this.updateCallbacks) {
8608
+ callback(viewPath, key, update);
8609
+ }
8610
+ }
8611
+ notifyRichUpdate(viewPath, key, update) {
8612
+ for (const callback of this.richUpdateCallbacks) {
8613
+ callback(viewPath, key, update);
8614
+ }
8615
+ }
8616
+ setConnectionState(state, error) {
8617
+ this.store.getState()._setConnectionState(state, error);
8618
+ }
8619
+ setViewConfig(viewPath, config) {
8620
+ const state = this.store.getState();
8621
+ const existingConfig = state.viewConfigs.get(viewPath);
8622
+ if (existingConfig)
8623
+ return;
8624
+ state._setViewConfig(viewPath, config);
8625
+ }
8626
+ getViewConfig(viewPath) {
8627
+ return this.store.getState().viewConfigs.get(viewPath);
8628
+ }
8629
+ }
8630
+
8269
8631
  const HyperstackContext = createContext(null);
8270
8632
  function HyperstackProvider({ children, fallback = null, ...config }) {
8271
8633
  const clientsRef = useRef(new Map());
@@ -8280,13 +8642,20 @@ function HyperstackProvider({ children, fallback = null, ...config }) {
8280
8642
  if (connecting) {
8281
8643
  return connecting;
8282
8644
  }
8645
+ const adapter = new ZustandAdapter();
8283
8646
  const connectionPromise = HyperStack.connect(stack, {
8284
8647
  url: urlOverride,
8648
+ storage: adapter,
8285
8649
  autoReconnect: config.autoConnect,
8286
8650
  reconnectIntervals: config.reconnectIntervals,
8287
8651
  maxReconnectAttempts: config.maxReconnectAttempts,
8288
8652
  maxEntriesPerView: config.maxEntriesPerView,
8653
+ flushIntervalMs: config.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS,
8289
8654
  }).then((client) => {
8655
+ client.onConnectionStateChange((state, error) => {
8656
+ adapter.setConnectionState(state, error);
8657
+ });
8658
+ adapter.setConnectionState(client.connectionState);
8290
8659
  clientsRef.current.set(cacheKey, {
8291
8660
  client,
8292
8661
  disconnect: () => client.disconnect()
@@ -8722,307 +9091,12 @@ function useHyperstack(stack, options) {
8722
9091
  return {
8723
9092
  views: views,
8724
9093
  instructions: instructions,
8725
- zustandStore: client?.store,
9094
+ zustandStore: client?.store?.store,
8726
9095
  client: client,
8727
9096
  isLoading,
8728
9097
  error
8729
9098
  };
8730
9099
  }
8731
9100
 
8732
- function getNestedValue(obj, path) {
8733
- let current = obj;
8734
- for (const segment of path) {
8735
- if (current === null || current === undefined)
8736
- return undefined;
8737
- if (typeof current !== 'object')
8738
- return undefined;
8739
- current = current[segment];
8740
- }
8741
- return current;
8742
- }
8743
- function compareSortValues(a, b) {
8744
- if (a === b)
8745
- return 0;
8746
- if (a === undefined || a === null)
8747
- return -1;
8748
- if (b === undefined || b === null)
8749
- return 1;
8750
- if (typeof a === 'number' && typeof b === 'number') {
8751
- return a - b;
8752
- }
8753
- if (typeof a === 'string' && typeof b === 'string') {
8754
- return a.localeCompare(b);
8755
- }
8756
- if (typeof a === 'boolean' && typeof b === 'boolean') {
8757
- return (a ? 1 : 0) - (b ? 1 : 0);
8758
- }
8759
- return String(a).localeCompare(String(b));
8760
- }
8761
- function binarySearchInsertPosition(sortedKeys, entities, sortConfig, newKey, newValue) {
8762
- const newSortValue = getNestedValue(newValue, sortConfig.field);
8763
- const isDesc = sortConfig.order === 'desc';
8764
- let low = 0;
8765
- let high = sortedKeys.length;
8766
- while (low < high) {
8767
- const mid = Math.floor((low + high) / 2);
8768
- const midKey = sortedKeys[mid];
8769
- const midEntity = entities.get(midKey);
8770
- const midValue = getNestedValue(midEntity, sortConfig.field);
8771
- let cmp = compareSortValues(newSortValue, midValue);
8772
- if (isDesc)
8773
- cmp = -cmp;
8774
- if (cmp === 0) {
8775
- cmp = newKey.localeCompare(midKey);
8776
- }
8777
- if (cmp < 0) {
8778
- high = mid;
8779
- }
8780
- else {
8781
- low = mid + 1;
8782
- }
8783
- }
8784
- return low;
8785
- }
8786
- class ZustandAdapter {
8787
- constructor(_config = {}) {
8788
- this.updateCallbacks = new Set();
8789
- this.richUpdateCallbacks = new Set();
8790
- this.accessOrder = new Map();
8791
- this.store = create((set) => ({
8792
- entities: new Map(),
8793
- sortedKeys: new Map(),
8794
- viewConfigs: new Map(),
8795
- connectionState: 'disconnected',
8796
- lastError: undefined,
8797
- _set: (viewPath, key, data) => {
8798
- set((state) => {
8799
- const newEntities = new Map(state.entities);
8800
- const viewMap = new Map(newEntities.get(viewPath) ?? new Map());
8801
- viewMap.set(key, data);
8802
- newEntities.set(viewPath, viewMap);
8803
- return { entities: newEntities };
8804
- });
8805
- },
8806
- _delete: (viewPath, key) => {
8807
- set((state) => {
8808
- const newEntities = new Map(state.entities);
8809
- const viewMap = newEntities.get(viewPath);
8810
- if (viewMap) {
8811
- const newViewMap = new Map(viewMap);
8812
- newViewMap.delete(key);
8813
- newEntities.set(viewPath, newViewMap);
8814
- }
8815
- return { entities: newEntities };
8816
- });
8817
- },
8818
- _clear: (viewPath) => {
8819
- set((state) => {
8820
- if (viewPath) {
8821
- const newEntities = new Map(state.entities);
8822
- newEntities.delete(viewPath);
8823
- const newSortedKeys = new Map(state.sortedKeys);
8824
- newSortedKeys.delete(viewPath);
8825
- return { entities: newEntities, sortedKeys: newSortedKeys };
8826
- }
8827
- return { entities: new Map(), sortedKeys: new Map() };
8828
- });
8829
- },
8830
- _setConnectionState: (connectionState, lastError) => {
8831
- set({ connectionState, lastError });
8832
- },
8833
- _setViewConfig: (viewPath, config) => {
8834
- set((state) => {
8835
- const newConfigs = new Map(state.viewConfigs);
8836
- newConfigs.set(viewPath, config);
8837
- return { viewConfigs: newConfigs };
8838
- });
8839
- },
8840
- _updateSortedKeys: (viewPath, newSortedKeys) => {
8841
- set((state) => {
8842
- const newSortedKeysMap = new Map(state.sortedKeys);
8843
- newSortedKeysMap.set(viewPath, newSortedKeys);
8844
- return { sortedKeys: newSortedKeysMap };
8845
- });
8846
- },
8847
- }));
8848
- }
8849
- get(viewPath, key) {
8850
- const entities = this.store.getState().entities;
8851
- const viewMap = entities.get(viewPath);
8852
- if (!viewMap)
8853
- return null;
8854
- const value = viewMap.get(key);
8855
- return value !== undefined ? value : null;
8856
- }
8857
- getAll(viewPath) {
8858
- const state = this.store.getState();
8859
- const viewMap = state.entities.get(viewPath);
8860
- if (!viewMap)
8861
- return [];
8862
- const sortedKeys = state.sortedKeys.get(viewPath);
8863
- if (sortedKeys && sortedKeys.length > 0) {
8864
- return sortedKeys.map(k => viewMap.get(k)).filter(v => v !== undefined);
8865
- }
8866
- return Array.from(viewMap.values());
8867
- }
8868
- getAllSync(viewPath) {
8869
- const state = this.store.getState();
8870
- const viewMap = state.entities.get(viewPath);
8871
- if (!viewMap)
8872
- return undefined;
8873
- const sortedKeys = state.sortedKeys.get(viewPath);
8874
- if (sortedKeys && sortedKeys.length > 0) {
8875
- return sortedKeys.map(k => viewMap.get(k)).filter(v => v !== undefined);
8876
- }
8877
- return Array.from(viewMap.values());
8878
- }
8879
- getSync(viewPath, key) {
8880
- const entities = this.store.getState().entities;
8881
- const viewMap = entities.get(viewPath);
8882
- if (!viewMap)
8883
- return undefined;
8884
- const value = viewMap.get(key);
8885
- return value !== undefined ? value : null;
8886
- }
8887
- has(viewPath, key) {
8888
- return this.store.getState().entities.get(viewPath)?.has(key) ?? false;
8889
- }
8890
- keys(viewPath) {
8891
- const viewMap = this.store.getState().entities.get(viewPath);
8892
- if (!viewMap)
8893
- return [];
8894
- return Array.from(viewMap.keys());
8895
- }
8896
- size(viewPath) {
8897
- return this.store.getState().entities.get(viewPath)?.size ?? 0;
8898
- }
8899
- set(viewPath, key, data) {
8900
- const state = this.store.getState();
8901
- const viewConfig = state.viewConfigs.get(viewPath);
8902
- if (viewConfig?.sort) {
8903
- const viewMap = state.entities.get(viewPath) ?? new Map();
8904
- const currentSortedKeys = [...(state.sortedKeys.get(viewPath) ?? [])];
8905
- const existingIdx = currentSortedKeys.indexOf(key);
8906
- if (existingIdx !== -1) {
8907
- currentSortedKeys.splice(existingIdx, 1);
8908
- }
8909
- const tempMap = new Map(viewMap);
8910
- tempMap.set(key, data);
8911
- const insertIdx = binarySearchInsertPosition(currentSortedKeys, tempMap, viewConfig.sort, key, data);
8912
- currentSortedKeys.splice(insertIdx, 0, key);
8913
- state._set(viewPath, key, data);
8914
- state._updateSortedKeys(viewPath, currentSortedKeys);
8915
- }
8916
- else {
8917
- let order = this.accessOrder.get(viewPath);
8918
- if (!order) {
8919
- order = [];
8920
- this.accessOrder.set(viewPath, order);
8921
- }
8922
- const existingIdx = order.indexOf(key);
8923
- if (existingIdx !== -1) {
8924
- order.splice(existingIdx, 1);
8925
- }
8926
- order.push(key);
8927
- state._set(viewPath, key, data);
8928
- }
8929
- }
8930
- delete(viewPath, key) {
8931
- const state = this.store.getState();
8932
- const viewConfig = state.viewConfigs.get(viewPath);
8933
- if (viewConfig?.sort) {
8934
- const currentSortedKeys = state.sortedKeys.get(viewPath);
8935
- if (currentSortedKeys) {
8936
- const newSortedKeys = currentSortedKeys.filter(k => k !== key);
8937
- state._updateSortedKeys(viewPath, newSortedKeys);
8938
- }
8939
- }
8940
- else {
8941
- const order = this.accessOrder.get(viewPath);
8942
- if (order) {
8943
- const idx = order.indexOf(key);
8944
- if (idx !== -1) {
8945
- order.splice(idx, 1);
8946
- }
8947
- }
8948
- }
8949
- state._delete(viewPath, key);
8950
- }
8951
- clear(viewPath) {
8952
- if (viewPath) {
8953
- this.accessOrder.delete(viewPath);
8954
- }
8955
- else {
8956
- this.accessOrder.clear();
8957
- }
8958
- this.store.getState()._clear(viewPath);
8959
- }
8960
- evictOldest(viewPath) {
8961
- const order = this.accessOrder.get(viewPath);
8962
- if (!order || order.length === 0)
8963
- return undefined;
8964
- const oldest = order.shift();
8965
- if (oldest !== undefined) {
8966
- this.store.getState()._delete(viewPath, oldest);
8967
- }
8968
- return oldest;
8969
- }
8970
- onUpdate(callback) {
8971
- this.updateCallbacks.add(callback);
8972
- return () => this.updateCallbacks.delete(callback);
8973
- }
8974
- onRichUpdate(callback) {
8975
- this.richUpdateCallbacks.add(callback);
8976
- return () => this.richUpdateCallbacks.delete(callback);
8977
- }
8978
- notifyUpdate(viewPath, key, update) {
8979
- for (const callback of this.updateCallbacks) {
8980
- callback(viewPath, key, update);
8981
- }
8982
- }
8983
- notifyRichUpdate(viewPath, key, update) {
8984
- for (const callback of this.richUpdateCallbacks) {
8985
- callback(viewPath, key, update);
8986
- }
8987
- }
8988
- setConnectionState(state, error) {
8989
- this.store.getState()._setConnectionState(state, error);
8990
- }
8991
- setViewConfig(viewPath, config) {
8992
- const state = this.store.getState();
8993
- const existingConfig = state.viewConfigs.get(viewPath);
8994
- if (existingConfig?.sort)
8995
- return;
8996
- state._setViewConfig(viewPath, config);
8997
- if (config.sort) {
8998
- this.rebuildSortedKeys(viewPath, config.sort);
8999
- }
9000
- }
9001
- getViewConfig(viewPath) {
9002
- return this.store.getState().viewConfigs.get(viewPath);
9003
- }
9004
- rebuildSortedKeys(viewPath, sortConfig) {
9005
- const state = this.store.getState();
9006
- const viewMap = state.entities.get(viewPath);
9007
- if (!viewMap || viewMap.size === 0)
9008
- return;
9009
- const isDesc = sortConfig.order === 'desc';
9010
- const entries = Array.from(viewMap.entries());
9011
- entries.sort((a, b) => {
9012
- const aValue = getNestedValue(a[1], sortConfig.field);
9013
- const bValue = getNestedValue(b[1], sortConfig.field);
9014
- let cmp = compareSortValues(aValue, bValue);
9015
- if (isDesc)
9016
- cmp = -cmp;
9017
- if (cmp === 0) {
9018
- cmp = a[0].localeCompare(b[0]);
9019
- }
9020
- return cmp;
9021
- });
9022
- const sortedKeys = entries.map(([k]) => k);
9023
- state._updateSortedKeys(viewPath, sortedKeys);
9024
- }
9025
- }
9026
-
9027
9101
  export { ConnectionManager, DEFAULT_CONFIG, DEFAULT_MAX_ENTRIES_PER_VIEW, FrameProcessor, HyperStack, HyperStackError, HyperstackProvider, MemoryAdapter, SubscriptionRegistry, ZustandAdapter, createInstructionExecutor, createPublicKeySeed, createSeed, derivePda, executeInstruction, formatProgramError, isSnapshotFrame, isValidFrame, parseFrame, parseFrameFromBlob, parseInstructionError, resolveAccounts, serializeInstructionData, useConnectionState, useEntity, useHyperstack, useHyperstackContext, useInstructionMutation, useView, validateAccountResolution, waitForConfirmation };
9028
9102
  //# sourceMappingURL=index.esm.js.map