@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.js CHANGED
@@ -150,58 +150,75 @@ var EOIMIndexEventType = /* @__PURE__ */ ((EOIMIndexEventType2) => {
150
150
  var OIMIndexStoreMapDrivenSetBased = class extends OIMIndexStoreSetBased {
151
151
  constructor() {
152
152
  super(...arguments);
153
- this.pks = /* @__PURE__ */ new Map();
153
+ this.slots = /* @__PURE__ */ new Map();
154
154
  }
155
- setOneByKey(key, pks) {
156
- this.pks.set(key, pks);
155
+ setOneByKey(key, slots) {
156
+ this.slots.set(key, slots);
157
157
  }
158
158
  removeOneByKey(key) {
159
- this.pks.delete(key);
159
+ this.slots.delete(key);
160
160
  }
161
161
  removeManyByKeys(keys) {
162
162
  for (const key of keys) {
163
- this.pks.delete(key);
163
+ this.slots.delete(key);
164
164
  }
165
165
  }
166
166
  getOneByKey(key) {
167
- return this.pks.get(key);
167
+ return this.slots.get(key);
168
168
  }
169
169
  getManyByKeys(keys) {
170
170
  const result = /* @__PURE__ */ new Map();
171
171
  for (const key of keys) {
172
- const pks = this.getOneByKey(key);
173
- if (pks !== void 0) {
174
- result.set(key, pks);
172
+ const slots = this.getOneByKey(key);
173
+ if (slots !== void 0) {
174
+ result.set(key, slots);
175
175
  }
176
176
  }
177
177
  return result;
178
178
  }
179
179
  getAllKeys() {
180
- return Array.from(this.pks.keys());
180
+ return Array.from(this.slots.keys());
181
181
  }
182
182
  getAll() {
183
- return new Map(this.pks);
183
+ return new Map(this.slots);
184
184
  }
185
185
  countAll() {
186
- return this.pks.size;
186
+ return this.slots.size;
187
187
  }
188
188
  clear() {
189
- this.pks.clear();
189
+ this.slots.clear();
190
190
  }
191
191
  };
192
192
 
193
193
  // src/abstract/OIMIndexSetBased.ts
194
194
  var OIMIndexSetBased = class {
195
195
  constructor(options = {}) {
196
+ this.fallbackSlots = /* @__PURE__ */ new Map();
196
197
  this.emitter = new OIMEventEmitter();
197
198
  this.comparePks = options.comparePks;
198
199
  this.store = options.store ?? new OIMIndexStoreMapDrivenSetBased();
200
+ this.resolveSlot = options.resolveSlot;
199
201
  }
200
202
  /**
201
203
  * Get primary keys for multiple index keys
202
204
  */
203
205
  getPksByKeys(keys) {
204
- return this.store.getManyByKeys(keys);
206
+ const result = /* @__PURE__ */ new Map();
207
+ const slotsByKey = this.store.getManyByKeys(keys);
208
+ for (const [key, slots] of slotsByKey) {
209
+ result.set(key, this.slotsToPks(slots));
210
+ }
211
+ return result;
212
+ }
213
+ setSlotResolver(resolveSlot) {
214
+ this.resolveSlot = resolveSlot;
215
+ const allSlots = this.store.getAll();
216
+ for (const [key, slots] of allSlots) {
217
+ this.store.setOneByKey(
218
+ key,
219
+ this.resolveSlots(this.slotsToPks(slots))
220
+ );
221
+ }
205
222
  }
206
223
  /**
207
224
  * @deprecated Use getPksByKey instead
@@ -214,8 +231,15 @@ var OIMIndexSetBased = class {
214
231
  * Get primary keys for a specific index key
215
232
  */
216
233
  getPksByKey(key) {
217
- const pksSet = this.store.getOneByKey(key);
218
- return pksSet ? pksSet : /* @__PURE__ */ new Set();
234
+ const slotsSet = this.store.getOneByKey(key);
235
+ return slotsSet ? this.slotsToPks(slotsSet) : /* @__PURE__ */ new Set();
236
+ }
237
+ getSlotsByKey(key) {
238
+ const slotsSet = this.store.getOneByKey(key);
239
+ return slotsSet ? slotsSet : /* @__PURE__ */ new Set();
240
+ }
241
+ getSlotsByKeys(keys) {
242
+ return this.store.getManyByKeys(keys);
219
243
  }
220
244
  /**
221
245
  * Check if an index key exists
@@ -233,8 +257,8 @@ var OIMIndexSetBased = class {
233
257
  * Get the number of primary keys for a specific index key
234
258
  */
235
259
  getKeySize(key) {
236
- const pksSet = this.store.getOneByKey(key);
237
- return pksSet ? pksSet.size : 0;
260
+ const slotsSet = this.store.getOneByKey(key);
261
+ return slotsSet ? slotsSet.size : 0;
238
262
  }
239
263
  /**
240
264
  * Get total number of index keys
@@ -256,10 +280,10 @@ var OIMIndexSetBased = class {
256
280
  let maxBucketSize = 0;
257
281
  let minBucketSize = Infinity;
258
282
  const allPks = this.store.getAll();
259
- for (const pksSet of allPks.values()) {
260
- totalPks += pksSet.size;
261
- maxBucketSize = Math.max(maxBucketSize, pksSet.size);
262
- minBucketSize = Math.min(minBucketSize, pksSet.size);
283
+ for (const slotsSet of allPks.values()) {
284
+ totalPks += slotsSet.size;
285
+ maxBucketSize = Math.max(maxBucketSize, slotsSet.size);
286
+ minBucketSize = Math.min(minBucketSize, slotsSet.size);
263
287
  }
264
288
  return {
265
289
  totalKeys: allPks.size,
@@ -281,21 +305,46 @@ var OIMIndexSetBased = class {
281
305
  * If comparator is provided and returns true (no changes), skip the update.
282
306
  */
283
307
  setPksWithComparison(key, newPks) {
308
+ return this.setSlotsWithComparison(key, this.resolveSlots(newPks));
309
+ }
310
+ setSlotsWithComparison(key, newSlots) {
284
311
  if (this.comparePks) {
285
- const existingPksSet = this.store.getOneByKey(key);
286
- if (existingPksSet && existingPksSet.size === newPks.size) {
287
- const existingPksArray = existingPksSet.size > 0 ? [...existingPksSet] : [];
288
- const newPksArray = newPks.size > 0 ? [...newPks] : [];
312
+ const existingSlotsSet = this.store.getOneByKey(key);
313
+ if (existingSlotsSet && existingSlotsSet.size === newSlots.size) {
314
+ const existingPksArray = Array.from(
315
+ this.slotsToPks(existingSlotsSet)
316
+ );
317
+ const newPksArray = Array.from(this.slotsToPks(newSlots));
289
318
  if (this.comparePks(existingPksArray, newPksArray)) {
290
319
  return false;
291
320
  }
292
- } else if (!existingPksSet && newPks.size === 0) {
321
+ } else if (!existingSlotsSet && newSlots.size === 0) {
293
322
  return false;
294
323
  }
295
324
  }
296
- this.store.setOneByKey(key, newPks);
325
+ this.store.setOneByKey(key, newSlots);
297
326
  return true;
298
327
  }
328
+ resolveSlots(pks) {
329
+ const slots = /* @__PURE__ */ new Set();
330
+ for (const pk of pks) slots.add(this.getOrCreateSlot(pk));
331
+ return slots;
332
+ }
333
+ getOrCreateSlot(pk) {
334
+ const resolved = this.resolveSlot?.(pk);
335
+ if (resolved) return resolved;
336
+ let fallback = this.fallbackSlots.get(pk);
337
+ if (!fallback) {
338
+ fallback = { pk, item: void 0 };
339
+ this.fallbackSlots.set(pk, fallback);
340
+ }
341
+ return fallback;
342
+ }
343
+ slotsToPks(slots) {
344
+ const pks = /* @__PURE__ */ new Set();
345
+ for (const slot of slots) pks.add(slot.pk);
346
+ return pks;
347
+ }
299
348
  /**
300
349
  * Emit update event with changed keys
301
350
  */
@@ -314,65 +363,89 @@ var OIMIndexSetBased = class {
314
363
  var OIMIndexStoreMapDrivenArrayBased = class extends OIMIndexStoreArrayBased {
315
364
  constructor() {
316
365
  super(...arguments);
317
- this.pks = /* @__PURE__ */ new Map();
366
+ this.slots = /* @__PURE__ */ new Map();
318
367
  }
319
- setOneByKey(key, pks) {
320
- this.pks.set(key, pks);
368
+ setOneByKey(key, slots) {
369
+ this.slots.set(key, slots);
321
370
  }
322
371
  removeOneByKey(key) {
323
- this.pks.delete(key);
372
+ this.slots.delete(key);
324
373
  }
325
374
  removeManyByKeys(keys) {
326
375
  for (const key of keys) {
327
- this.pks.delete(key);
376
+ this.slots.delete(key);
328
377
  }
329
378
  }
330
379
  getOneByKey(key) {
331
- return this.pks.get(key);
380
+ return this.slots.get(key);
332
381
  }
333
382
  getManyByKeys(keys) {
334
383
  const result = /* @__PURE__ */ new Map();
335
384
  for (const key of keys) {
336
- const pks = this.getOneByKey(key);
337
- if (pks !== void 0) {
338
- result.set(key, pks);
385
+ const slots = this.getOneByKey(key);
386
+ if (slots !== void 0) {
387
+ result.set(key, slots);
339
388
  }
340
389
  }
341
390
  return result;
342
391
  }
343
392
  getAllKeys() {
344
- return Array.from(this.pks.keys());
393
+ return Array.from(this.slots.keys());
345
394
  }
346
395
  getAll() {
347
- return new Map(this.pks);
396
+ return new Map(this.slots);
348
397
  }
349
398
  countAll() {
350
- return this.pks.size;
399
+ return this.slots.size;
351
400
  }
352
401
  clear() {
353
- this.pks.clear();
402
+ this.slots.clear();
354
403
  }
355
404
  };
356
405
 
357
406
  // src/abstract/OIMIndexArrayBased.ts
358
407
  var OIMIndexArrayBased = class {
359
408
  constructor(options = {}) {
409
+ this.fallbackSlots = /* @__PURE__ */ new Map();
360
410
  this.emitter = new OIMEventEmitter();
361
411
  this.comparePks = options.comparePks;
362
412
  this.store = options.store ?? new OIMIndexStoreMapDrivenArrayBased();
413
+ this.resolveSlot = options.resolveSlot;
363
414
  }
364
415
  /**
365
416
  * Get primary keys for multiple index keys
366
417
  */
367
418
  getPksByKeys(keys) {
368
- return this.store.getManyByKeys(keys);
419
+ const result = /* @__PURE__ */ new Map();
420
+ const slotsByKey = this.store.getManyByKeys(keys);
421
+ for (const [key, slots] of slotsByKey) {
422
+ result.set(key, this.slotsToPks(slots));
423
+ }
424
+ return result;
425
+ }
426
+ setSlotResolver(resolveSlot) {
427
+ this.resolveSlot = resolveSlot;
428
+ const allSlots = this.store.getAll();
429
+ for (const [key, slots] of allSlots) {
430
+ this.store.setOneByKey(
431
+ key,
432
+ slots.map((slot) => this.getOrCreateSlot(slot.pk))
433
+ );
434
+ }
369
435
  }
370
436
  /**
371
437
  * Get primary keys for a specific index key
372
438
  */
373
439
  getPksByKey(key) {
374
- const pksArray = this.store.getOneByKey(key);
375
- return pksArray ? pksArray : [];
440
+ const slotsArray = this.store.getOneByKey(key);
441
+ return slotsArray ? this.slotsToPks(slotsArray) : [];
442
+ }
443
+ getSlotsByKey(key) {
444
+ const slotsArray = this.store.getOneByKey(key);
445
+ return slotsArray ? slotsArray : [];
446
+ }
447
+ getSlotsByKeys(keys) {
448
+ return this.store.getManyByKeys(keys);
376
449
  }
377
450
  /**
378
451
  * Check if an index key exists
@@ -390,8 +463,8 @@ var OIMIndexArrayBased = class {
390
463
  * Get the number of primary keys for a specific index key
391
464
  */
392
465
  getKeySize(key) {
393
- const pksArray = this.store.getOneByKey(key);
394
- return pksArray ? pksArray.length : 0;
466
+ const slotsArray = this.store.getOneByKey(key);
467
+ return slotsArray ? slotsArray.length : 0;
395
468
  }
396
469
  /**
397
470
  * Get total number of index keys
@@ -413,10 +486,10 @@ var OIMIndexArrayBased = class {
413
486
  let maxBucketSize = 0;
414
487
  let minBucketSize = Infinity;
415
488
  const allPks = this.store.getAll();
416
- for (const pksArray of allPks.values()) {
417
- totalPks += pksArray.length;
418
- maxBucketSize = Math.max(maxBucketSize, pksArray.length);
419
- minBucketSize = Math.min(minBucketSize, pksArray.length);
489
+ for (const slotsArray of allPks.values()) {
490
+ totalPks += slotsArray.length;
491
+ maxBucketSize = Math.max(maxBucketSize, slotsArray.length);
492
+ minBucketSize = Math.min(minBucketSize, slotsArray.length);
420
493
  }
421
494
  return {
422
495
  totalKeys: allPks.size,
@@ -438,19 +511,49 @@ var OIMIndexArrayBased = class {
438
511
  * If comparator is provided and returns true (no changes), skip the update.
439
512
  */
440
513
  setPksWithComparison(key, newPks) {
514
+ return this.setSlotsWithComparison(key, this.resolveSlots(newPks));
515
+ }
516
+ setSlotsWithComparison(key, newSlots) {
441
517
  if (this.comparePks) {
442
- const existingPksArray = this.store.getOneByKey(key);
443
- if (existingPksArray && existingPksArray.length === newPks.length) {
444
- if (this.comparePks(existingPksArray, newPks)) {
518
+ const existingSlotsArray = this.store.getOneByKey(key);
519
+ if (existingSlotsArray && existingSlotsArray.length === newSlots.length) {
520
+ if (this.comparePks(
521
+ this.slotsToPks(existingSlotsArray),
522
+ this.slotsToPks(newSlots)
523
+ )) {
445
524
  return false;
446
525
  }
447
- } else if (!existingPksArray && newPks.length === 0) {
526
+ } else if (!existingSlotsArray && newSlots.length === 0) {
448
527
  return false;
449
528
  }
450
529
  }
451
- this.store.setOneByKey(key, newPks);
530
+ this.store.setOneByKey(key, newSlots);
452
531
  return true;
453
532
  }
533
+ resolveSlots(pks) {
534
+ const slots = [];
535
+ slots.length = pks.length;
536
+ for (let i = 0; i < pks.length; i++) {
537
+ slots[i] = this.getOrCreateSlot(pks[i]);
538
+ }
539
+ return slots;
540
+ }
541
+ getOrCreateSlot(pk) {
542
+ const resolved = this.resolveSlot?.(pk);
543
+ if (resolved) return resolved;
544
+ let fallback = this.fallbackSlots.get(pk);
545
+ if (!fallback) {
546
+ fallback = { pk, item: void 0 };
547
+ this.fallbackSlots.set(pk, fallback);
548
+ }
549
+ return fallback;
550
+ }
551
+ slotsToPks(slots) {
552
+ const pks = [];
553
+ pks.length = slots.length;
554
+ for (let i = 0; i < slots.length; i++) pks[i] = slots[i].pk;
555
+ return pks;
556
+ }
454
557
  /**
455
558
  * Emit update event with changed keys
456
559
  */
@@ -731,6 +834,12 @@ var OIMReactiveIndexSetBased = class {
731
834
  getPksByKeys(keys) {
732
835
  return this.index.getPksByKeys(keys);
733
836
  }
837
+ getSlotsByKey(key) {
838
+ return this.index.getSlotsByKey(key);
839
+ }
840
+ getSlotsByKeys(keys) {
841
+ return this.index.getSlotsByKeys(keys);
842
+ }
734
843
  hasKey(key) {
735
844
  return this.index.hasKey(key);
736
845
  }
@@ -788,6 +897,12 @@ var OIMReactiveIndexArrayBased = class {
788
897
  getPksByKeys(keys) {
789
898
  return this.index.getPksByKeys(keys);
790
899
  }
900
+ getSlotsByKey(key) {
901
+ return this.index.getSlotsByKey(key);
902
+ }
903
+ getSlotsByKeys(keys) {
904
+ return this.index.getSlotsByKeys(keys);
905
+ }
791
906
  hasKey(key) {
792
907
  return this.index.hasKey(key);
793
908
  }
@@ -848,26 +963,52 @@ var OIMPkSelectorFactory = class {
848
963
  var OIMCollectionStoreMapDriven = class extends OIMCollectionStore {
849
964
  constructor() {
850
965
  super(...arguments);
851
- this.entities = /* @__PURE__ */ new Map();
966
+ this.slots = /* @__PURE__ */ new Map();
852
967
  }
853
968
  setOneByPk(pk, entity) {
854
- this.entities.set(pk, entity);
969
+ const slot = this.slots.get(pk);
970
+ if (slot) {
971
+ slot.item = entity;
972
+ } else {
973
+ this.slots.set(pk, { pk, item: entity });
974
+ }
855
975
  }
856
976
  setManyByPks(pks, entities) {
857
977
  for (let i = 0; i < pks.length; i++) {
858
- this.entities.set(pks[i], entities[i]);
978
+ this.setOneByPk(pks[i], entities[i]);
979
+ }
980
+ }
981
+ getSlotByPk(pk) {
982
+ return this.slots.get(pk);
983
+ }
984
+ getSlotsByPks(pks) {
985
+ const result = [];
986
+ result.length = pks.length;
987
+ let writeIndex = 0;
988
+ for (let i = 0; i < pks.length; i++) {
989
+ const slot = this.slots.get(pks[i]);
990
+ if (slot !== void 0) {
991
+ result[writeIndex++] = slot;
992
+ }
859
993
  }
994
+ result.length = writeIndex;
995
+ return result;
996
+ }
997
+ getAllSlots() {
998
+ return Array.from(this.slots.values());
860
999
  }
861
1000
  removeOneByPk(pk) {
862
- this.entities.delete(pk);
1001
+ const slot = this.slots.get(pk);
1002
+ if (slot) slot.item = void 0;
1003
+ this.slots.delete(pk);
863
1004
  }
864
1005
  removeManyByPks(pks) {
865
1006
  for (const pk of pks) {
866
- this.entities.delete(pk);
1007
+ this.removeOneByPk(pk);
867
1008
  }
868
1009
  }
869
1010
  getOneByPk(pk) {
870
- return this.entities.get(pk);
1011
+ return this.slots.get(pk)?.item;
871
1012
  }
872
1013
  getManyByPks(pks) {
873
1014
  const result = [];
@@ -883,16 +1024,23 @@ var OIMCollectionStoreMapDriven = class extends OIMCollectionStore {
883
1024
  return result;
884
1025
  }
885
1026
  getAll() {
886
- return Array.from(this.entities.values());
1027
+ const result = [];
1028
+ for (const slot of this.slots.values()) {
1029
+ if (slot.item !== void 0) result.push(slot.item);
1030
+ }
1031
+ return result;
887
1032
  }
888
1033
  countAll() {
889
- return this.entities.size;
1034
+ return this.slots.size;
890
1035
  }
891
1036
  clear() {
892
- this.entities.clear();
1037
+ for (const slot of this.slots.values()) {
1038
+ slot.item = void 0;
1039
+ }
1040
+ this.slots.clear();
893
1041
  }
894
1042
  getAllPks() {
895
- return Array.from(this.entities.keys());
1043
+ return Array.from(this.slots.keys());
896
1044
  }
897
1045
  destroy() {
898
1046
  this.clear();
@@ -928,6 +1076,15 @@ var OIMCollection = class {
928
1076
  getManyByPks(pks) {
929
1077
  return this.store.getManyByPks(pks);
930
1078
  }
1079
+ getSlotByPk(pk) {
1080
+ return this.store.getSlotByPk(pk);
1081
+ }
1082
+ getSlotsByPks(pks) {
1083
+ return this.store.getSlotsByPks(pks);
1084
+ }
1085
+ getAllSlots() {
1086
+ return this.store.getAllSlots();
1087
+ }
931
1088
  upsertOneByPk(pk, entity) {
932
1089
  this.upsertOneWithoutNotificationsByPk(pk, entity);
933
1090
  this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
@@ -1053,11 +1210,13 @@ var OIMReactiveCollection = class extends OIMCollection {
1053
1210
  }
1054
1211
  upsertOneByPk(pk, entity) {
1055
1212
  this.upsertOneWithoutNotificationsByPk(pk, entity);
1213
+ this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
1056
1214
  this.updateEmitter.markUpdatedKey(pk);
1057
1215
  this.trackAnyUpdatePk(pk);
1058
1216
  }
1059
1217
  upsertOne(entity) {
1060
1218
  const pk = this.upsertOneWithoutNotifications(entity);
1219
+ this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
1061
1220
  this.updateEmitter.markUpdatedKey(pk);
1062
1221
  this.trackAnyUpdatePk(pk);
1063
1222
  }
@@ -1065,33 +1224,39 @@ var OIMReactiveCollection = class extends OIMCollection {
1065
1224
  const pks = entities.map(
1066
1225
  (entity) => this.upsertOneWithoutNotifications(entity)
1067
1226
  );
1227
+ this.emitter.emit(0 /* UPDATE */, { pks });
1068
1228
  this.updateEmitter.markUpdatedKeys(pks);
1069
1229
  this.trackAnyUpdatePks(pks);
1070
1230
  }
1071
1231
  removeOne(entity) {
1072
1232
  const pk = this.selectPk(entity);
1073
1233
  this.store.removeOneByPk(pk);
1234
+ this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
1074
1235
  this.updateEmitter.markUpdatedKey(pk);
1075
1236
  this.trackAnyUpdatePk(pk);
1076
1237
  }
1077
1238
  removeMany(entities) {
1078
1239
  const pks = entities.map(this.selectPk);
1079
1240
  this.store.removeManyByPks(pks);
1241
+ this.emitter.emit(0 /* UPDATE */, { pks });
1080
1242
  this.updateEmitter.markUpdatedKeys(pks);
1081
1243
  this.trackAnyUpdatePks(pks);
1082
1244
  }
1083
1245
  removeOneByPk(pk) {
1084
1246
  this.store.removeOneByPk(pk);
1247
+ this.emitter.emit(0 /* UPDATE */, { pks: [pk] });
1085
1248
  this.updateEmitter.markUpdatedKey(pk);
1086
1249
  this.trackAnyUpdatePk(pk);
1087
1250
  }
1088
1251
  removeManyByPks(pks) {
1089
1252
  this.store.removeManyByPks(pks);
1253
+ this.emitter.emit(0 /* UPDATE */, { pks });
1090
1254
  this.updateEmitter.markUpdatedKeys(pks);
1091
1255
  this.trackAnyUpdatePks(pks);
1092
1256
  }
1093
1257
  clear() {
1094
1258
  this.store.clear();
1259
+ this.emitter.emit(0 /* UPDATE */, { pks: [] });
1095
1260
  this.updateEmitter.markAllUpdated();
1096
1261
  this.trackAnyUpdateClear();
1097
1262
  }
@@ -1275,25 +1440,33 @@ var OIMIndexManualSetBased = class extends OIMIndexSetBased {
1275
1440
  this.emitUpdateOne(key);
1276
1441
  }
1277
1442
  }
1443
+ setSlots(key, slots) {
1444
+ const hasChanges = this.setSlotsWithComparison(key, new Set(slots));
1445
+ if (hasChanges) {
1446
+ this.emitUpdateOne(key);
1447
+ }
1448
+ }
1278
1449
  /**
1279
1450
  * Add primary keys to a specific index key
1280
1451
  */
1281
1452
  addPks(key, pks) {
1282
1453
  if (pks.length === 0) return;
1283
- let pksSet = this.store.getOneByKey(key);
1284
- if (!pksSet) {
1285
- pksSet = /* @__PURE__ */ new Set();
1286
- this.store.setOneByKey(key, pksSet);
1454
+ let slotsSet = this.store.getOneByKey(key);
1455
+ if (!slotsSet) {
1456
+ slotsSet = /* @__PURE__ */ new Set();
1457
+ this.store.setOneByKey(key, slotsSet);
1287
1458
  }
1459
+ const pksSet = this.slotsToPks(slotsSet);
1288
1460
  let hasChanges = false;
1289
1461
  for (const pk of pks) {
1290
1462
  if (!pksSet.has(pk)) {
1291
1463
  pksSet.add(pk);
1464
+ slotsSet.add(this.getOrCreateSlot(pk));
1292
1465
  hasChanges = true;
1293
1466
  }
1294
1467
  }
1295
1468
  if (hasChanges) {
1296
- this.store.setOneByKey(key, pksSet);
1469
+ this.store.setOneByKey(key, slotsSet);
1297
1470
  this.emitUpdateOne(key);
1298
1471
  }
1299
1472
  }
@@ -1302,19 +1475,20 @@ var OIMIndexManualSetBased = class extends OIMIndexSetBased {
1302
1475
  */
1303
1476
  removePks(key, pks) {
1304
1477
  if (pks.length === 0) return;
1305
- const pksSet = this.store.getOneByKey(key);
1306
- if (!pksSet) return;
1478
+ const slotsSet = this.store.getOneByKey(key);
1479
+ if (!slotsSet) return;
1307
1480
  let hasChanges = false;
1308
- for (const pk of pks) {
1309
- if (pksSet.delete(pk)) {
1481
+ const pksToRemove = new Set(pks);
1482
+ for (const slot of Array.from(slotsSet)) {
1483
+ if (pksToRemove.has(slot.pk) && slotsSet.delete(slot)) {
1310
1484
  hasChanges = true;
1311
1485
  }
1312
1486
  }
1313
- if (pksSet.size === 0) {
1487
+ if (slotsSet.size === 0) {
1314
1488
  this.store.removeOneByKey(key);
1315
1489
  hasChanges = true;
1316
1490
  } else if (hasChanges) {
1317
- this.store.setOneByKey(key, pksSet);
1491
+ this.store.setOneByKey(key, slotsSet);
1318
1492
  }
1319
1493
  if (hasChanges) {
1320
1494
  this.emitUpdateOne(key);
@@ -1364,6 +1538,9 @@ var OIMReactiveIndexManualSetBased = class extends OIMReactiveIndexSetBased {
1364
1538
  setPks(key, pks) {
1365
1539
  this.index.setPks(key, pks);
1366
1540
  }
1541
+ setSlots(key, slots) {
1542
+ this.index.setSlots(key, slots);
1543
+ }
1367
1544
  addPks(key, pks) {
1368
1545
  this.index.addPks(key, pks);
1369
1546
  }
@@ -1390,26 +1567,34 @@ var OIMIndexManualArrayBased = class extends OIMIndexArrayBased {
1390
1567
  this.emitUpdateOne(key);
1391
1568
  }
1392
1569
  }
1570
+ setSlots(key, slots) {
1571
+ const hasChanges = this.setSlotsWithComparison(key, slots);
1572
+ if (hasChanges) {
1573
+ this.emitUpdateOne(key);
1574
+ }
1575
+ }
1393
1576
  /**
1394
1577
  * Add primary keys to a specific index key
1395
1578
  */
1396
1579
  addPks(key, pks) {
1397
1580
  if (pks.length === 0) return;
1398
- let pksArray = this.store.getOneByKey(key);
1399
- if (!pksArray) {
1400
- pksArray = [];
1401
- this.store.setOneByKey(key, pksArray);
1581
+ let slotsArray = this.store.getOneByKey(key);
1582
+ if (!slotsArray) {
1583
+ slotsArray = [];
1584
+ this.store.setOneByKey(key, slotsArray);
1402
1585
  }
1403
- const pksSet = new Set(pksArray);
1586
+ const pksSet = new Set(slotsArray.map((slot) => slot.pk));
1404
1587
  let hasChanges = false;
1588
+ const nextSlots = slotsArray.slice();
1405
1589
  for (const pk of pks) {
1406
1590
  if (!pksSet.has(pk)) {
1407
1591
  pksSet.add(pk);
1592
+ nextSlots.push(this.getOrCreateSlot(pk));
1408
1593
  hasChanges = true;
1409
1594
  }
1410
1595
  }
1411
1596
  if (hasChanges) {
1412
- this.store.setOneByKey(key, Array.from(pksSet));
1597
+ this.store.setOneByKey(key, nextSlots);
1413
1598
  this.emitUpdateOne(key);
1414
1599
  }
1415
1600
  }
@@ -1418,11 +1603,11 @@ var OIMIndexManualArrayBased = class extends OIMIndexArrayBased {
1418
1603
  */
1419
1604
  removePks(key, pks) {
1420
1605
  if (pks.length === 0) return;
1421
- const pksArray = this.store.getOneByKey(key);
1422
- if (!pksArray) return;
1606
+ const slotsArray = this.store.getOneByKey(key);
1607
+ if (!slotsArray) return;
1423
1608
  const pksSet = new Set(pks);
1424
- const filtered = pksArray.filter((pk) => !pksSet.has(pk));
1425
- const hasChanges = filtered.length !== pksArray.length;
1609
+ const filtered = slotsArray.filter((slot) => !pksSet.has(slot.pk));
1610
+ const hasChanges = filtered.length !== slotsArray.length;
1426
1611
  if (hasChanges) {
1427
1612
  if (filtered.length === 0) {
1428
1613
  this.store.removeOneByKey(key);
@@ -1476,6 +1661,9 @@ var OIMReactiveIndexManualArrayBased = class extends OIMReactiveIndexArrayBased
1476
1661
  setPks(key, pks) {
1477
1662
  this.index.setPks(key, pks);
1478
1663
  }
1664
+ setSlots(key, slots) {
1665
+ this.index.setSlots(key, slots);
1666
+ }
1479
1667
  addPks(key, pks) {
1480
1668
  this.index.addPks(key, pks);
1481
1669
  }
@@ -1492,6 +1680,10 @@ var OIMRICollection = class extends OIMReactiveCollection {
1492
1680
  constructor(queue, opts) {
1493
1681
  super(queue, opts.collectionOpts);
1494
1682
  this.indexes = opts.indexes || {};
1683
+ const indexes = Object.values(this.indexes);
1684
+ for (const index of indexes) {
1685
+ index.index.setSlotResolver((pk) => this.getSlotByPk(pk));
1686
+ }
1495
1687
  }
1496
1688
  };
1497
1689
 
@@ -1954,14 +2146,14 @@ var OIMIndexManualOrderedArrayBased = class extends OIMIndexArrayBased {
1954
2146
  push(key, itemKey) {
1955
2147
  const list = this.getOrCreateList(key);
1956
2148
  const index = list.length;
1957
- list.push(itemKey);
2149
+ list.push(this.getOrCreateSlot(itemKey));
1958
2150
  this.emitUpdate([key]);
1959
2151
  return index;
1960
2152
  }
1961
2153
  insertAt(key, index, itemKey) {
1962
2154
  const list = this.getOrCreateList(key);
1963
2155
  const safeIndex = Math.max(0, Math.min(index, list.length));
1964
- list.splice(safeIndex, 0, itemKey);
2156
+ list.splice(safeIndex, 0, this.getOrCreateSlot(itemKey));
1965
2157
  this.emitUpdate([key]);
1966
2158
  }
1967
2159
  removeAt(key, index) {
@@ -1973,19 +2165,19 @@ var OIMIndexManualOrderedArrayBased = class extends OIMIndexArrayBased {
1973
2165
  this.store.removeOneByKey(key);
1974
2166
  }
1975
2167
  this.emitUpdate([key]);
1976
- return removed;
2168
+ return removed?.pk;
1977
2169
  }
1978
2170
  move(key, fromIndex, toIndex) {
1979
2171
  const list = this.store.getOneByKey(key);
1980
2172
  if (!list) return void 0;
1981
2173
  if (fromIndex < 0 || fromIndex >= list.length) return void 0;
1982
2174
  if (toIndex < 0 || toIndex >= list.length) return void 0;
1983
- if (fromIndex === toIndex) return list[fromIndex];
2175
+ if (fromIndex === toIndex) return list[fromIndex]?.pk;
1984
2176
  const [itemKey] = list.splice(fromIndex, 1);
1985
2177
  if (itemKey === void 0) return void 0;
1986
2178
  list.splice(toIndex, 0, itemKey);
1987
2179
  this.emitUpdate([key]);
1988
- return itemKey;
2180
+ return itemKey.pk;
1989
2181
  }
1990
2182
  reset(key, itemKeys) {
1991
2183
  if (itemKeys.length === 0) {
@@ -1995,7 +2187,7 @@ var OIMIndexManualOrderedArrayBased = class extends OIMIndexArrayBased {
1995
2187
  }
1996
2188
  return;
1997
2189
  }
1998
- this.store.setOneByKey(key, Array.from(itemKeys));
2190
+ this.store.setOneByKey(key, this.resolveSlots(itemKeys));
1999
2191
  this.emitUpdate([key]);
2000
2192
  }
2001
2193
  getOrCreateList(key) {
@@ -2896,7 +3088,8 @@ var OIMSelectorSourceDependencyEntitiesByIndexKeySetBased = class {
2896
3088
  subscribe(onUpdate) {
2897
3089
  const resubscribeCollection = () => {
2898
3090
  const nextPks = Array.from(
2899
- this.reactiveIndex.getPksByKey(this.key)
3091
+ this.reactiveIndex.getSlotsByKey(this.key),
3092
+ (slot) => slot.pk
2900
3093
  );
2901
3094
  if (this.sameArray(this.currentPks, nextPks)) return;
2902
3095
  this.unsubscribeFromCollectionKeys?.();
@@ -2938,10 +3131,19 @@ var OIMEntitiesByIndexKeySetBasedSelector = class extends OIMSelector {
2938
3131
  this.collection = collection;
2939
3132
  this.reactiveIndex = reactiveIndex;
2940
3133
  this.key = key;
3134
+ this.reactiveIndex.index.setSlotResolver(
3135
+ (pk) => this.collection.getSlotByPk(pk)
3136
+ );
2941
3137
  }
2942
3138
  getValue() {
2943
- const pks = Array.from(this.reactiveIndex.getPksByKey(this.key));
2944
- return pks.map((pk) => this.collection.getOneByPk(pk));
3139
+ const slots = this.reactiveIndex.getSlotsByKey(this.key);
3140
+ const entities = [];
3141
+ entities.length = slots.size;
3142
+ let writeIndex = 0;
3143
+ for (const slot of slots) {
3144
+ entities[writeIndex++] = slot.item;
3145
+ }
3146
+ return entities;
2945
3147
  }
2946
3148
  areEqual(prev, next) {
2947
3149
  if (prev === next) return true;
@@ -2962,7 +3164,7 @@ var OIMSelectorSourceDependencyEntitiesByIndexKeyArrayBased = class {
2962
3164
  }
2963
3165
  subscribe(onUpdate) {
2964
3166
  const resubscribeCollection = () => {
2965
- const nextPks = this.reactiveIndex.getPksByKey(this.key);
3167
+ const nextPks = this.reactiveIndex.getSlotsByKey(this.key).map((slot) => slot.pk);
2966
3168
  if (this.sameArray(this.currentPks, nextPks)) return;
2967
3169
  this.unsubscribeFromCollectionKeys?.();
2968
3170
  this.unsubscribeFromCollectionKeys = void 0;
@@ -3003,10 +3205,18 @@ var OIMEntitiesByIndexKeyArrayBasedSelector = class extends OIMSelector {
3003
3205
  this.collection = collection;
3004
3206
  this.reactiveIndex = reactiveIndex;
3005
3207
  this.key = key;
3208
+ this.reactiveIndex.index.setSlotResolver(
3209
+ (pk) => this.collection.getSlotByPk(pk)
3210
+ );
3006
3211
  }
3007
3212
  getValue() {
3008
- const pks = this.reactiveIndex.getPksByKey(this.key);
3009
- return pks.map((pk) => this.collection.getOneByPk(pk));
3213
+ const slots = this.reactiveIndex.getSlotsByKey(this.key);
3214
+ const entities = [];
3215
+ entities.length = slots.length;
3216
+ for (let i = 0; i < slots.length; i++) {
3217
+ entities[i] = slots[i].item;
3218
+ }
3219
+ return entities;
3010
3220
  }
3011
3221
  areEqual(prev, next) {
3012
3222
  if (prev === next) return true;