@oimdb/core 1.4.1 → 1.5.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.cjs CHANGED
@@ -239,58 +239,75 @@ var EOIMIndexEventType = /* @__PURE__ */ ((EOIMIndexEventType2) => {
239
239
  var OIMIndexStoreMapDrivenSetBased = class extends OIMIndexStoreSetBased {
240
240
  constructor() {
241
241
  super(...arguments);
242
- this.pks = /* @__PURE__ */ new Map();
242
+ this.slots = /* @__PURE__ */ new Map();
243
243
  }
244
- setOneByKey(key, pks) {
245
- this.pks.set(key, pks);
244
+ setOneByKey(key, slots) {
245
+ this.slots.set(key, slots);
246
246
  }
247
247
  removeOneByKey(key) {
248
- this.pks.delete(key);
248
+ this.slots.delete(key);
249
249
  }
250
250
  removeManyByKeys(keys) {
251
251
  for (const key of keys) {
252
- this.pks.delete(key);
252
+ this.slots.delete(key);
253
253
  }
254
254
  }
255
255
  getOneByKey(key) {
256
- return this.pks.get(key);
256
+ return this.slots.get(key);
257
257
  }
258
258
  getManyByKeys(keys) {
259
259
  const result = /* @__PURE__ */ new Map();
260
260
  for (const key of keys) {
261
- const pks = this.getOneByKey(key);
262
- if (pks !== void 0) {
263
- result.set(key, pks);
261
+ const slots = this.getOneByKey(key);
262
+ if (slots !== void 0) {
263
+ result.set(key, slots);
264
264
  }
265
265
  }
266
266
  return result;
267
267
  }
268
268
  getAllKeys() {
269
- return Array.from(this.pks.keys());
269
+ return Array.from(this.slots.keys());
270
270
  }
271
271
  getAll() {
272
- return new Map(this.pks);
272
+ return new Map(this.slots);
273
273
  }
274
274
  countAll() {
275
- return this.pks.size;
275
+ return this.slots.size;
276
276
  }
277
277
  clear() {
278
- this.pks.clear();
278
+ this.slots.clear();
279
279
  }
280
280
  };
281
281
 
282
282
  // src/abstract/OIMIndexSetBased.ts
283
283
  var OIMIndexSetBased = class {
284
284
  constructor(options = {}) {
285
+ this.fallbackSlots = /* @__PURE__ */ new Map();
285
286
  this.emitter = new OIMEventEmitter();
286
287
  this.comparePks = options.comparePks;
287
288
  this.store = options.store ?? new OIMIndexStoreMapDrivenSetBased();
289
+ this.resolveSlot = options.resolveSlot;
288
290
  }
289
291
  /**
290
292
  * Get primary keys for multiple index keys
291
293
  */
292
294
  getPksByKeys(keys) {
293
- return this.store.getManyByKeys(keys);
295
+ const result = /* @__PURE__ */ new Map();
296
+ const slotsByKey = this.store.getManyByKeys(keys);
297
+ for (const [key, slots] of slotsByKey) {
298
+ result.set(key, this.slotsToPks(slots));
299
+ }
300
+ return result;
301
+ }
302
+ setSlotResolver(resolveSlot) {
303
+ this.resolveSlot = resolveSlot;
304
+ const allSlots = this.store.getAll();
305
+ for (const [key, slots] of allSlots) {
306
+ this.store.setOneByKey(
307
+ key,
308
+ this.resolveSlots(this.slotsToPks(slots))
309
+ );
310
+ }
294
311
  }
295
312
  /**
296
313
  * @deprecated Use getPksByKey instead
@@ -303,8 +320,15 @@ var OIMIndexSetBased = class {
303
320
  * Get primary keys for a specific index key
304
321
  */
305
322
  getPksByKey(key) {
306
- const pksSet = this.store.getOneByKey(key);
307
- return pksSet ? pksSet : /* @__PURE__ */ new Set();
323
+ const slotsSet = this.store.getOneByKey(key);
324
+ return slotsSet ? this.slotsToPks(slotsSet) : /* @__PURE__ */ new Set();
325
+ }
326
+ getSlotsByKey(key) {
327
+ const slotsSet = this.store.getOneByKey(key);
328
+ return slotsSet ? slotsSet : /* @__PURE__ */ new Set();
329
+ }
330
+ getSlotsByKeys(keys) {
331
+ return this.store.getManyByKeys(keys);
308
332
  }
309
333
  /**
310
334
  * Check if an index key exists
@@ -322,8 +346,8 @@ var OIMIndexSetBased = class {
322
346
  * Get the number of primary keys for a specific index key
323
347
  */
324
348
  getKeySize(key) {
325
- const pksSet = this.store.getOneByKey(key);
326
- return pksSet ? pksSet.size : 0;
349
+ const slotsSet = this.store.getOneByKey(key);
350
+ return slotsSet ? slotsSet.size : 0;
327
351
  }
328
352
  /**
329
353
  * Get total number of index keys
@@ -345,10 +369,10 @@ var OIMIndexSetBased = class {
345
369
  let maxBucketSize = 0;
346
370
  let minBucketSize = Infinity;
347
371
  const allPks = this.store.getAll();
348
- for (const pksSet of allPks.values()) {
349
- totalPks += pksSet.size;
350
- maxBucketSize = Math.max(maxBucketSize, pksSet.size);
351
- minBucketSize = Math.min(minBucketSize, pksSet.size);
372
+ for (const slotsSet of allPks.values()) {
373
+ totalPks += slotsSet.size;
374
+ maxBucketSize = Math.max(maxBucketSize, slotsSet.size);
375
+ minBucketSize = Math.min(minBucketSize, slotsSet.size);
352
376
  }
353
377
  return {
354
378
  totalKeys: allPks.size,
@@ -370,21 +394,46 @@ var OIMIndexSetBased = class {
370
394
  * If comparator is provided and returns true (no changes), skip the update.
371
395
  */
372
396
  setPksWithComparison(key, newPks) {
397
+ return this.setSlotsWithComparison(key, this.resolveSlots(newPks));
398
+ }
399
+ setSlotsWithComparison(key, newSlots) {
373
400
  if (this.comparePks) {
374
- const existingPksSet = this.store.getOneByKey(key);
375
- if (existingPksSet && existingPksSet.size === newPks.size) {
376
- const existingPksArray = existingPksSet.size > 0 ? [...existingPksSet] : [];
377
- const newPksArray = newPks.size > 0 ? [...newPks] : [];
401
+ const existingSlotsSet = this.store.getOneByKey(key);
402
+ if (existingSlotsSet && existingSlotsSet.size === newSlots.size) {
403
+ const existingPksArray = Array.from(
404
+ this.slotsToPks(existingSlotsSet)
405
+ );
406
+ const newPksArray = Array.from(this.slotsToPks(newSlots));
378
407
  if (this.comparePks(existingPksArray, newPksArray)) {
379
408
  return false;
380
409
  }
381
- } else if (!existingPksSet && newPks.size === 0) {
410
+ } else if (!existingSlotsSet && newSlots.size === 0) {
382
411
  return false;
383
412
  }
384
413
  }
385
- this.store.setOneByKey(key, newPks);
414
+ this.store.setOneByKey(key, newSlots);
386
415
  return true;
387
416
  }
417
+ resolveSlots(pks) {
418
+ const slots = /* @__PURE__ */ new Set();
419
+ for (const pk of pks) slots.add(this.getOrCreateSlot(pk));
420
+ return slots;
421
+ }
422
+ getOrCreateSlot(pk) {
423
+ const resolved = this.resolveSlot?.(pk);
424
+ if (resolved) return resolved;
425
+ let fallback = this.fallbackSlots.get(pk);
426
+ if (!fallback) {
427
+ fallback = { pk, item: void 0 };
428
+ this.fallbackSlots.set(pk, fallback);
429
+ }
430
+ return fallback;
431
+ }
432
+ slotsToPks(slots) {
433
+ const pks = /* @__PURE__ */ new Set();
434
+ for (const slot of slots) pks.add(slot.pk);
435
+ return pks;
436
+ }
388
437
  /**
389
438
  * Emit update event with changed keys
390
439
  */
@@ -403,65 +452,89 @@ var OIMIndexSetBased = class {
403
452
  var OIMIndexStoreMapDrivenArrayBased = class extends OIMIndexStoreArrayBased {
404
453
  constructor() {
405
454
  super(...arguments);
406
- this.pks = /* @__PURE__ */ new Map();
455
+ this.slots = /* @__PURE__ */ new Map();
407
456
  }
408
- setOneByKey(key, pks) {
409
- this.pks.set(key, pks);
457
+ setOneByKey(key, slots) {
458
+ this.slots.set(key, slots);
410
459
  }
411
460
  removeOneByKey(key) {
412
- this.pks.delete(key);
461
+ this.slots.delete(key);
413
462
  }
414
463
  removeManyByKeys(keys) {
415
464
  for (const key of keys) {
416
- this.pks.delete(key);
465
+ this.slots.delete(key);
417
466
  }
418
467
  }
419
468
  getOneByKey(key) {
420
- return this.pks.get(key);
469
+ return this.slots.get(key);
421
470
  }
422
471
  getManyByKeys(keys) {
423
472
  const result = /* @__PURE__ */ new Map();
424
473
  for (const key of keys) {
425
- const pks = this.getOneByKey(key);
426
- if (pks !== void 0) {
427
- result.set(key, pks);
474
+ const slots = this.getOneByKey(key);
475
+ if (slots !== void 0) {
476
+ result.set(key, slots);
428
477
  }
429
478
  }
430
479
  return result;
431
480
  }
432
481
  getAllKeys() {
433
- return Array.from(this.pks.keys());
482
+ return Array.from(this.slots.keys());
434
483
  }
435
484
  getAll() {
436
- return new Map(this.pks);
485
+ return new Map(this.slots);
437
486
  }
438
487
  countAll() {
439
- return this.pks.size;
488
+ return this.slots.size;
440
489
  }
441
490
  clear() {
442
- this.pks.clear();
491
+ this.slots.clear();
443
492
  }
444
493
  };
445
494
 
446
495
  // src/abstract/OIMIndexArrayBased.ts
447
496
  var OIMIndexArrayBased = class {
448
497
  constructor(options = {}) {
498
+ this.fallbackSlots = /* @__PURE__ */ new Map();
449
499
  this.emitter = new OIMEventEmitter();
450
500
  this.comparePks = options.comparePks;
451
501
  this.store = options.store ?? new OIMIndexStoreMapDrivenArrayBased();
502
+ this.resolveSlot = options.resolveSlot;
452
503
  }
453
504
  /**
454
505
  * Get primary keys for multiple index keys
455
506
  */
456
507
  getPksByKeys(keys) {
457
- return this.store.getManyByKeys(keys);
508
+ const result = /* @__PURE__ */ new Map();
509
+ const slotsByKey = this.store.getManyByKeys(keys);
510
+ for (const [key, slots] of slotsByKey) {
511
+ result.set(key, this.slotsToPks(slots));
512
+ }
513
+ return result;
514
+ }
515
+ setSlotResolver(resolveSlot) {
516
+ this.resolveSlot = resolveSlot;
517
+ const allSlots = this.store.getAll();
518
+ for (const [key, slots] of allSlots) {
519
+ this.store.setOneByKey(
520
+ key,
521
+ slots.map((slot) => this.getOrCreateSlot(slot.pk))
522
+ );
523
+ }
458
524
  }
459
525
  /**
460
526
  * Get primary keys for a specific index key
461
527
  */
462
528
  getPksByKey(key) {
463
- const pksArray = this.store.getOneByKey(key);
464
- return pksArray ? pksArray : [];
529
+ const slotsArray = this.store.getOneByKey(key);
530
+ return slotsArray ? this.slotsToPks(slotsArray) : [];
531
+ }
532
+ getSlotsByKey(key) {
533
+ const slotsArray = this.store.getOneByKey(key);
534
+ return slotsArray ? slotsArray : [];
535
+ }
536
+ getSlotsByKeys(keys) {
537
+ return this.store.getManyByKeys(keys);
465
538
  }
466
539
  /**
467
540
  * Check if an index key exists
@@ -479,8 +552,8 @@ var OIMIndexArrayBased = class {
479
552
  * Get the number of primary keys for a specific index key
480
553
  */
481
554
  getKeySize(key) {
482
- const pksArray = this.store.getOneByKey(key);
483
- return pksArray ? pksArray.length : 0;
555
+ const slotsArray = this.store.getOneByKey(key);
556
+ return slotsArray ? slotsArray.length : 0;
484
557
  }
485
558
  /**
486
559
  * Get total number of index keys
@@ -502,10 +575,10 @@ var OIMIndexArrayBased = class {
502
575
  let maxBucketSize = 0;
503
576
  let minBucketSize = Infinity;
504
577
  const allPks = this.store.getAll();
505
- for (const pksArray of allPks.values()) {
506
- totalPks += pksArray.length;
507
- maxBucketSize = Math.max(maxBucketSize, pksArray.length);
508
- minBucketSize = Math.min(minBucketSize, pksArray.length);
578
+ for (const slotsArray of allPks.values()) {
579
+ totalPks += slotsArray.length;
580
+ maxBucketSize = Math.max(maxBucketSize, slotsArray.length);
581
+ minBucketSize = Math.min(minBucketSize, slotsArray.length);
509
582
  }
510
583
  return {
511
584
  totalKeys: allPks.size,
@@ -527,19 +600,49 @@ var OIMIndexArrayBased = class {
527
600
  * If comparator is provided and returns true (no changes), skip the update.
528
601
  */
529
602
  setPksWithComparison(key, newPks) {
603
+ return this.setSlotsWithComparison(key, this.resolveSlots(newPks));
604
+ }
605
+ setSlotsWithComparison(key, newSlots) {
530
606
  if (this.comparePks) {
531
- const existingPksArray = this.store.getOneByKey(key);
532
- if (existingPksArray && existingPksArray.length === newPks.length) {
533
- if (this.comparePks(existingPksArray, newPks)) {
607
+ const existingSlotsArray = this.store.getOneByKey(key);
608
+ if (existingSlotsArray && existingSlotsArray.length === newSlots.length) {
609
+ if (this.comparePks(
610
+ this.slotsToPks(existingSlotsArray),
611
+ this.slotsToPks(newSlots)
612
+ )) {
534
613
  return false;
535
614
  }
536
- } else if (!existingPksArray && newPks.length === 0) {
615
+ } else if (!existingSlotsArray && newSlots.length === 0) {
537
616
  return false;
538
617
  }
539
618
  }
540
- this.store.setOneByKey(key, newPks);
619
+ this.store.setOneByKey(key, newSlots);
541
620
  return true;
542
621
  }
622
+ resolveSlots(pks) {
623
+ const slots = [];
624
+ slots.length = pks.length;
625
+ for (let i = 0; i < pks.length; i++) {
626
+ slots[i] = this.getOrCreateSlot(pks[i]);
627
+ }
628
+ return slots;
629
+ }
630
+ getOrCreateSlot(pk) {
631
+ const resolved = this.resolveSlot?.(pk);
632
+ if (resolved) return resolved;
633
+ let fallback = this.fallbackSlots.get(pk);
634
+ if (!fallback) {
635
+ fallback = { pk, item: void 0 };
636
+ this.fallbackSlots.set(pk, fallback);
637
+ }
638
+ return fallback;
639
+ }
640
+ slotsToPks(slots) {
641
+ const pks = [];
642
+ pks.length = slots.length;
643
+ for (let i = 0; i < slots.length; i++) pks[i] = slots[i].pk;
644
+ return pks;
645
+ }
543
646
  /**
544
647
  * Emit update event with changed keys
545
648
  */
@@ -820,6 +923,12 @@ var OIMReactiveIndexSetBased = class {
820
923
  getPksByKeys(keys) {
821
924
  return this.index.getPksByKeys(keys);
822
925
  }
926
+ getSlotsByKey(key) {
927
+ return this.index.getSlotsByKey(key);
928
+ }
929
+ getSlotsByKeys(keys) {
930
+ return this.index.getSlotsByKeys(keys);
931
+ }
823
932
  hasKey(key) {
824
933
  return this.index.hasKey(key);
825
934
  }
@@ -877,6 +986,12 @@ var OIMReactiveIndexArrayBased = class {
877
986
  getPksByKeys(keys) {
878
987
  return this.index.getPksByKeys(keys);
879
988
  }
989
+ getSlotsByKey(key) {
990
+ return this.index.getSlotsByKey(key);
991
+ }
992
+ getSlotsByKeys(keys) {
993
+ return this.index.getSlotsByKeys(keys);
994
+ }
880
995
  hasKey(key) {
881
996
  return this.index.hasKey(key);
882
997
  }
@@ -937,26 +1052,52 @@ var OIMPkSelectorFactory = class {
937
1052
  var OIMCollectionStoreMapDriven = class extends OIMCollectionStore {
938
1053
  constructor() {
939
1054
  super(...arguments);
940
- this.entities = /* @__PURE__ */ new Map();
1055
+ this.slots = /* @__PURE__ */ new Map();
941
1056
  }
942
1057
  setOneByPk(pk, entity) {
943
- this.entities.set(pk, entity);
1058
+ const slot = this.slots.get(pk);
1059
+ if (slot) {
1060
+ slot.item = entity;
1061
+ } else {
1062
+ this.slots.set(pk, { pk, item: entity });
1063
+ }
944
1064
  }
945
1065
  setManyByPks(pks, entities) {
946
1066
  for (let i = 0; i < pks.length; i++) {
947
- this.entities.set(pks[i], entities[i]);
1067
+ this.setOneByPk(pks[i], entities[i]);
1068
+ }
1069
+ }
1070
+ getSlotByPk(pk) {
1071
+ return this.slots.get(pk);
1072
+ }
1073
+ getSlotsByPks(pks) {
1074
+ const result = [];
1075
+ result.length = pks.length;
1076
+ let writeIndex = 0;
1077
+ for (let i = 0; i < pks.length; i++) {
1078
+ const slot = this.slots.get(pks[i]);
1079
+ if (slot !== void 0) {
1080
+ result[writeIndex++] = slot;
1081
+ }
948
1082
  }
1083
+ result.length = writeIndex;
1084
+ return result;
1085
+ }
1086
+ getAllSlots() {
1087
+ return Array.from(this.slots.values());
949
1088
  }
950
1089
  removeOneByPk(pk) {
951
- this.entities.delete(pk);
1090
+ const slot = this.slots.get(pk);
1091
+ if (slot) slot.item = void 0;
1092
+ this.slots.delete(pk);
952
1093
  }
953
1094
  removeManyByPks(pks) {
954
1095
  for (const pk of pks) {
955
- this.entities.delete(pk);
1096
+ this.removeOneByPk(pk);
956
1097
  }
957
1098
  }
958
1099
  getOneByPk(pk) {
959
- return this.entities.get(pk);
1100
+ return this.slots.get(pk)?.item;
960
1101
  }
961
1102
  getManyByPks(pks) {
962
1103
  const result = [];
@@ -972,16 +1113,23 @@ var OIMCollectionStoreMapDriven = class extends OIMCollectionStore {
972
1113
  return result;
973
1114
  }
974
1115
  getAll() {
975
- return Array.from(this.entities.values());
1116
+ const result = [];
1117
+ for (const slot of this.slots.values()) {
1118
+ if (slot.item !== void 0) result.push(slot.item);
1119
+ }
1120
+ return result;
976
1121
  }
977
1122
  countAll() {
978
- return this.entities.size;
1123
+ return this.slots.size;
979
1124
  }
980
1125
  clear() {
981
- this.entities.clear();
1126
+ for (const slot of this.slots.values()) {
1127
+ slot.item = void 0;
1128
+ }
1129
+ this.slots.clear();
982
1130
  }
983
1131
  getAllPks() {
984
- return Array.from(this.entities.keys());
1132
+ return Array.from(this.slots.keys());
985
1133
  }
986
1134
  destroy() {
987
1135
  this.clear();
@@ -1017,6 +1165,15 @@ var OIMCollection = class {
1017
1165
  getManyByPks(pks) {
1018
1166
  return this.store.getManyByPks(pks);
1019
1167
  }
1168
+ getSlotByPk(pk) {
1169
+ return this.store.getSlotByPk(pk);
1170
+ }
1171
+ getSlotsByPks(pks) {
1172
+ return this.store.getSlotsByPks(pks);
1173
+ }
1174
+ getAllSlots() {
1175
+ return this.store.getAllSlots();
1176
+ }
1020
1177
  upsertOneByPk(pk, entity) {
1021
1178
  this.upsertOneWithoutNotificationsByPk(pk, entity);
1022
1179
  this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
@@ -1142,11 +1299,13 @@ var OIMReactiveCollection = class extends OIMCollection {
1142
1299
  }
1143
1300
  upsertOneByPk(pk, entity) {
1144
1301
  this.upsertOneWithoutNotificationsByPk(pk, entity);
1302
+ this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
1145
1303
  this.updateEmitter.markUpdatedKey(pk);
1146
1304
  this.trackAnyUpdatePk(pk);
1147
1305
  }
1148
1306
  upsertOne(entity) {
1149
1307
  const pk = this.upsertOneWithoutNotifications(entity);
1308
+ this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
1150
1309
  this.updateEmitter.markUpdatedKey(pk);
1151
1310
  this.trackAnyUpdatePk(pk);
1152
1311
  }
@@ -1154,33 +1313,39 @@ var OIMReactiveCollection = class extends OIMCollection {
1154
1313
  const pks = entities.map(
1155
1314
  (entity) => this.upsertOneWithoutNotifications(entity)
1156
1315
  );
1316
+ this.emitter.emit(0 /* UPDATE */, { pks });
1157
1317
  this.updateEmitter.markUpdatedKeys(pks);
1158
1318
  this.trackAnyUpdatePks(pks);
1159
1319
  }
1160
1320
  removeOne(entity) {
1161
1321
  const pk = this.selectPk(entity);
1162
1322
  this.store.removeOneByPk(pk);
1323
+ this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
1163
1324
  this.updateEmitter.markUpdatedKey(pk);
1164
1325
  this.trackAnyUpdatePk(pk);
1165
1326
  }
1166
1327
  removeMany(entities) {
1167
1328
  const pks = entities.map(this.selectPk);
1168
1329
  this.store.removeManyByPks(pks);
1330
+ this.emitter.emit(0 /* UPDATE */, { pks });
1169
1331
  this.updateEmitter.markUpdatedKeys(pks);
1170
1332
  this.trackAnyUpdatePks(pks);
1171
1333
  }
1172
1334
  removeOneByPk(pk) {
1173
1335
  this.store.removeOneByPk(pk);
1336
+ this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
1174
1337
  this.updateEmitter.markUpdatedKey(pk);
1175
1338
  this.trackAnyUpdatePk(pk);
1176
1339
  }
1177
1340
  removeManyByPks(pks) {
1178
1341
  this.store.removeManyByPks(pks);
1342
+ this.emitter.emit(0 /* UPDATE */, { pks });
1179
1343
  this.updateEmitter.markUpdatedKeys(pks);
1180
1344
  this.trackAnyUpdatePks(pks);
1181
1345
  }
1182
1346
  clear() {
1183
1347
  this.store.clear();
1348
+ this.emitter.emit(0 /* UPDATE */, { pks: [] });
1184
1349
  this.updateEmitter.markAllUpdated();
1185
1350
  this.trackAnyUpdateClear();
1186
1351
  }
@@ -1364,25 +1529,33 @@ var OIMIndexManualSetBased = class extends OIMIndexSetBased {
1364
1529
  this.emitUpdateOne(key);
1365
1530
  }
1366
1531
  }
1532
+ setSlots(key, slots) {
1533
+ const hasChanges = this.setSlotsWithComparison(key, new Set(slots));
1534
+ if (hasChanges) {
1535
+ this.emitUpdateOne(key);
1536
+ }
1537
+ }
1367
1538
  /**
1368
1539
  * Add primary keys to a specific index key
1369
1540
  */
1370
1541
  addPks(key, pks) {
1371
1542
  if (pks.length === 0) return;
1372
- let pksSet = this.store.getOneByKey(key);
1373
- if (!pksSet) {
1374
- pksSet = /* @__PURE__ */ new Set();
1375
- this.store.setOneByKey(key, pksSet);
1543
+ let slotsSet = this.store.getOneByKey(key);
1544
+ if (!slotsSet) {
1545
+ slotsSet = /* @__PURE__ */ new Set();
1546
+ this.store.setOneByKey(key, slotsSet);
1376
1547
  }
1548
+ const pksSet = this.slotsToPks(slotsSet);
1377
1549
  let hasChanges = false;
1378
1550
  for (const pk of pks) {
1379
1551
  if (!pksSet.has(pk)) {
1380
1552
  pksSet.add(pk);
1553
+ slotsSet.add(this.getOrCreateSlot(pk));
1381
1554
  hasChanges = true;
1382
1555
  }
1383
1556
  }
1384
1557
  if (hasChanges) {
1385
- this.store.setOneByKey(key, pksSet);
1558
+ this.store.setOneByKey(key, slotsSet);
1386
1559
  this.emitUpdateOne(key);
1387
1560
  }
1388
1561
  }
@@ -1391,19 +1564,20 @@ var OIMIndexManualSetBased = class extends OIMIndexSetBased {
1391
1564
  */
1392
1565
  removePks(key, pks) {
1393
1566
  if (pks.length === 0) return;
1394
- const pksSet = this.store.getOneByKey(key);
1395
- if (!pksSet) return;
1567
+ const slotsSet = this.store.getOneByKey(key);
1568
+ if (!slotsSet) return;
1396
1569
  let hasChanges = false;
1397
- for (const pk of pks) {
1398
- if (pksSet.delete(pk)) {
1570
+ const pksToRemove = new Set(pks);
1571
+ for (const slot of Array.from(slotsSet)) {
1572
+ if (pksToRemove.has(slot.pk) && slotsSet.delete(slot)) {
1399
1573
  hasChanges = true;
1400
1574
  }
1401
1575
  }
1402
- if (pksSet.size === 0) {
1576
+ if (slotsSet.size === 0) {
1403
1577
  this.store.removeOneByKey(key);
1404
1578
  hasChanges = true;
1405
1579
  } else if (hasChanges) {
1406
- this.store.setOneByKey(key, pksSet);
1580
+ this.store.setOneByKey(key, slotsSet);
1407
1581
  }
1408
1582
  if (hasChanges) {
1409
1583
  this.emitUpdateOne(key);
@@ -1453,6 +1627,9 @@ var OIMReactiveIndexManualSetBased = class extends OIMReactiveIndexSetBased {
1453
1627
  setPks(key, pks) {
1454
1628
  this.index.setPks(key, pks);
1455
1629
  }
1630
+ setSlots(key, slots) {
1631
+ this.index.setSlots(key, slots);
1632
+ }
1456
1633
  addPks(key, pks) {
1457
1634
  this.index.addPks(key, pks);
1458
1635
  }
@@ -1479,26 +1656,34 @@ var OIMIndexManualArrayBased = class extends OIMIndexArrayBased {
1479
1656
  this.emitUpdateOne(key);
1480
1657
  }
1481
1658
  }
1659
+ setSlots(key, slots) {
1660
+ const hasChanges = this.setSlotsWithComparison(key, slots);
1661
+ if (hasChanges) {
1662
+ this.emitUpdateOne(key);
1663
+ }
1664
+ }
1482
1665
  /**
1483
1666
  * Add primary keys to a specific index key
1484
1667
  */
1485
1668
  addPks(key, pks) {
1486
1669
  if (pks.length === 0) return;
1487
- let pksArray = this.store.getOneByKey(key);
1488
- if (!pksArray) {
1489
- pksArray = [];
1490
- this.store.setOneByKey(key, pksArray);
1670
+ let slotsArray = this.store.getOneByKey(key);
1671
+ if (!slotsArray) {
1672
+ slotsArray = [];
1673
+ this.store.setOneByKey(key, slotsArray);
1491
1674
  }
1492
- const pksSet = new Set(pksArray);
1675
+ const pksSet = new Set(slotsArray.map((slot) => slot.pk));
1493
1676
  let hasChanges = false;
1677
+ const nextSlots = slotsArray.slice();
1494
1678
  for (const pk of pks) {
1495
1679
  if (!pksSet.has(pk)) {
1496
1680
  pksSet.add(pk);
1681
+ nextSlots.push(this.getOrCreateSlot(pk));
1497
1682
  hasChanges = true;
1498
1683
  }
1499
1684
  }
1500
1685
  if (hasChanges) {
1501
- this.store.setOneByKey(key, Array.from(pksSet));
1686
+ this.store.setOneByKey(key, nextSlots);
1502
1687
  this.emitUpdateOne(key);
1503
1688
  }
1504
1689
  }
@@ -1507,11 +1692,11 @@ var OIMIndexManualArrayBased = class extends OIMIndexArrayBased {
1507
1692
  */
1508
1693
  removePks(key, pks) {
1509
1694
  if (pks.length === 0) return;
1510
- const pksArray = this.store.getOneByKey(key);
1511
- if (!pksArray) return;
1695
+ const slotsArray = this.store.getOneByKey(key);
1696
+ if (!slotsArray) return;
1512
1697
  const pksSet = new Set(pks);
1513
- const filtered = pksArray.filter((pk) => !pksSet.has(pk));
1514
- const hasChanges = filtered.length !== pksArray.length;
1698
+ const filtered = slotsArray.filter((slot) => !pksSet.has(slot.pk));
1699
+ const hasChanges = filtered.length !== slotsArray.length;
1515
1700
  if (hasChanges) {
1516
1701
  if (filtered.length === 0) {
1517
1702
  this.store.removeOneByKey(key);
@@ -1565,6 +1750,9 @@ var OIMReactiveIndexManualArrayBased = class extends OIMReactiveIndexArrayBased
1565
1750
  setPks(key, pks) {
1566
1751
  this.index.setPks(key, pks);
1567
1752
  }
1753
+ setSlots(key, slots) {
1754
+ this.index.setSlots(key, slots);
1755
+ }
1568
1756
  addPks(key, pks) {
1569
1757
  this.index.addPks(key, pks);
1570
1758
  }
@@ -1581,6 +1769,10 @@ var OIMRICollection = class extends OIMReactiveCollection {
1581
1769
  constructor(queue, opts) {
1582
1770
  super(queue, opts.collectionOpts);
1583
1771
  this.indexes = opts.indexes || {};
1772
+ const indexes = Object.values(this.indexes);
1773
+ for (const index of indexes) {
1774
+ index.index.setSlotResolver((pk) => this.getSlotByPk(pk));
1775
+ }
1584
1776
  }
1585
1777
  };
1586
1778
 
@@ -2043,14 +2235,14 @@ var OIMIndexManualOrderedArrayBased = class extends OIMIndexArrayBased {
2043
2235
  push(key, itemKey) {
2044
2236
  const list = this.getOrCreateList(key);
2045
2237
  const index = list.length;
2046
- list.push(itemKey);
2238
+ list.push(this.getOrCreateSlot(itemKey));
2047
2239
  this.emitUpdate([key]);
2048
2240
  return index;
2049
2241
  }
2050
2242
  insertAt(key, index, itemKey) {
2051
2243
  const list = this.getOrCreateList(key);
2052
2244
  const safeIndex = Math.max(0, Math.min(index, list.length));
2053
- list.splice(safeIndex, 0, itemKey);
2245
+ list.splice(safeIndex, 0, this.getOrCreateSlot(itemKey));
2054
2246
  this.emitUpdate([key]);
2055
2247
  }
2056
2248
  removeAt(key, index) {
@@ -2062,19 +2254,19 @@ var OIMIndexManualOrderedArrayBased = class extends OIMIndexArrayBased {
2062
2254
  this.store.removeOneByKey(key);
2063
2255
  }
2064
2256
  this.emitUpdate([key]);
2065
- return removed;
2257
+ return removed?.pk;
2066
2258
  }
2067
2259
  move(key, fromIndex, toIndex) {
2068
2260
  const list = this.store.getOneByKey(key);
2069
2261
  if (!list) return void 0;
2070
2262
  if (fromIndex < 0 || fromIndex >= list.length) return void 0;
2071
2263
  if (toIndex < 0 || toIndex >= list.length) return void 0;
2072
- if (fromIndex === toIndex) return list[fromIndex];
2264
+ if (fromIndex === toIndex) return list[fromIndex]?.pk;
2073
2265
  const [itemKey] = list.splice(fromIndex, 1);
2074
2266
  if (itemKey === void 0) return void 0;
2075
2267
  list.splice(toIndex, 0, itemKey);
2076
2268
  this.emitUpdate([key]);
2077
- return itemKey;
2269
+ return itemKey.pk;
2078
2270
  }
2079
2271
  reset(key, itemKeys) {
2080
2272
  if (itemKeys.length === 0) {
@@ -2084,7 +2276,7 @@ var OIMIndexManualOrderedArrayBased = class extends OIMIndexArrayBased {
2084
2276
  }
2085
2277
  return;
2086
2278
  }
2087
- this.store.setOneByKey(key, Array.from(itemKeys));
2279
+ this.store.setOneByKey(key, this.resolveSlots(itemKeys));
2088
2280
  this.emitUpdate([key]);
2089
2281
  }
2090
2282
  getOrCreateList(key) {
@@ -2985,7 +3177,8 @@ var OIMSelectorSourceDependencyEntitiesByIndexKeySetBased = class {
2985
3177
  subscribe(onUpdate) {
2986
3178
  const resubscribeCollection = () => {
2987
3179
  const nextPks = Array.from(
2988
- this.reactiveIndex.getPksByKey(this.key)
3180
+ this.reactiveIndex.getSlotsByKey(this.key),
3181
+ (slot) => slot.pk
2989
3182
  );
2990
3183
  if (this.sameArray(this.currentPks, nextPks)) return;
2991
3184
  this.unsubscribeFromCollectionKeys?.();
@@ -3027,10 +3220,19 @@ var OIMEntitiesByIndexKeySetBasedSelector = class extends OIMSelector {
3027
3220
  this.collection = collection;
3028
3221
  this.reactiveIndex = reactiveIndex;
3029
3222
  this.key = key;
3223
+ this.reactiveIndex.index.setSlotResolver(
3224
+ (pk) => this.collection.getSlotByPk(pk)
3225
+ );
3030
3226
  }
3031
3227
  getValue() {
3032
- const pks = Array.from(this.reactiveIndex.getPksByKey(this.key));
3033
- return pks.map((pk) => this.collection.getOneByPk(pk));
3228
+ const slots = this.reactiveIndex.getSlotsByKey(this.key);
3229
+ const entities = [];
3230
+ entities.length = slots.size;
3231
+ let writeIndex = 0;
3232
+ for (const slot of slots) {
3233
+ entities[writeIndex++] = slot.item;
3234
+ }
3235
+ return entities;
3034
3236
  }
3035
3237
  areEqual(prev, next) {
3036
3238
  if (prev === next) return true;
@@ -3051,7 +3253,7 @@ var OIMSelectorSourceDependencyEntitiesByIndexKeyArrayBased = class {
3051
3253
  }
3052
3254
  subscribe(onUpdate) {
3053
3255
  const resubscribeCollection = () => {
3054
- const nextPks = this.reactiveIndex.getPksByKey(this.key);
3256
+ const nextPks = this.reactiveIndex.getSlotsByKey(this.key).map((slot) => slot.pk);
3055
3257
  if (this.sameArray(this.currentPks, nextPks)) return;
3056
3258
  this.unsubscribeFromCollectionKeys?.();
3057
3259
  this.unsubscribeFromCollectionKeys = void 0;
@@ -3092,10 +3294,18 @@ var OIMEntitiesByIndexKeyArrayBasedSelector = class extends OIMSelector {
3092
3294
  this.collection = collection;
3093
3295
  this.reactiveIndex = reactiveIndex;
3094
3296
  this.key = key;
3297
+ this.reactiveIndex.index.setSlotResolver(
3298
+ (pk) => this.collection.getSlotByPk(pk)
3299
+ );
3095
3300
  }
3096
3301
  getValue() {
3097
- const pks = this.reactiveIndex.getPksByKey(this.key);
3098
- return pks.map((pk) => this.collection.getOneByPk(pk));
3302
+ const slots = this.reactiveIndex.getSlotsByKey(this.key);
3303
+ const entities = [];
3304
+ entities.length = slots.length;
3305
+ for (let i = 0; i < slots.length; i++) {
3306
+ entities[i] = slots[i].item;
3307
+ }
3308
+ return entities;
3099
3309
  }
3100
3310
  areEqual(prev, next) {
3101
3311
  if (prev === next) return true;