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