@forcecalendar/core 2.1.65 → 2.1.66

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.
@@ -29,6 +29,7 @@ export class EventStore {
29
29
  /** @type {Map<string, Set<string>>} Status -> Set of event IDs */
30
30
  byStatus: new Map()
31
31
  };
32
+ this.eventIndexRefs = new Map();
32
33
 
33
34
  // Timezone manager for conversions (use singleton to share cache)
34
35
  this.timezoneManager = TimezoneManager.getInstance();
@@ -334,9 +335,6 @@ export class EventStore {
334
335
  getEventsForDate(date, timezone = null) {
335
336
  timezone = timezone || this.defaultTimezone;
336
337
 
337
- // Use local date string for the query date (in the calendar's timezone)
338
- const dateStr = DateUtils.getLocalDateString(date);
339
-
340
338
  // Collect candidate event IDs from indices
341
339
  const candidateIds = new Set();
342
340
 
@@ -409,23 +407,40 @@ export class EventStore {
409
407
 
410
408
  // Collect all events from those dates
411
409
  const checkedIds = new Set();
412
- dates.forEach(date => {
413
- // Use getLocalDateString to match the index key format (YYYY-MM-DD)
414
- const dateStr = DateUtils.getLocalDateString(date);
415
- const eventIds = this.indices.byDate.get(dateStr) || new Set();
410
+ const addCandidateIds = eventIds => {
411
+ if (!eventIds) return;
416
412
 
417
413
  eventIds.forEach(id => {
418
414
  if (!checkedIds.has(id) && id !== excludeId) {
419
415
  checkedIds.add(id);
420
- const event = this.events.get(id);
421
-
422
- if (event && event.overlaps({ start, end })) {
423
- overlapping.push(event);
424
- }
425
416
  }
426
417
  });
418
+ };
419
+
420
+ dates.forEach(date => {
421
+ // Use getLocalDateString to match the index key format (YYYY-MM-DD)
422
+ const dateStr = DateUtils.getLocalDateString(date);
423
+ addCandidateIds(this.indices.byDate.get(dateStr));
427
424
  });
428
425
 
426
+ // Lazy-indexed long events may not have every day in byDate. Use month
427
+ // buckets as candidates and rely on precise overlap filtering below.
428
+ const currentMonth = new Date(startDate.getFullYear(), startDate.getMonth(), 1);
429
+ const endMonth = new Date(endDate.getFullYear(), endDate.getMonth(), 1);
430
+ while (currentMonth <= endMonth) {
431
+ const monthKey = `${currentMonth.getFullYear()}-${String(currentMonth.getMonth() + 1).padStart(2, '0')}`;
432
+ addCandidateIds(this.indices.byMonth.get(monthKey));
433
+ currentMonth.setMonth(currentMonth.getMonth() + 1);
434
+ }
435
+
436
+ for (const id of checkedIds) {
437
+ const event = this.events.get(id);
438
+
439
+ if (event && event.overlaps({ start, end })) {
440
+ overlapping.push(event);
441
+ }
442
+ }
443
+
429
444
  return overlapping.sort((a, b) => a.start - b.start);
430
445
  }
431
446
 
@@ -647,6 +662,7 @@ export class EventStore {
647
662
  this.indices.recurring.clear();
648
663
  this.indices.byCategory.clear();
649
664
  this.indices.byStatus.clear();
665
+ this.eventIndexRefs.clear();
650
666
 
651
667
  this._notifyChange({
652
668
  type: 'clear',
@@ -687,6 +703,8 @@ export class EventStore {
687
703
  * @private
688
704
  */
689
705
  _indexEvent(event) {
706
+ this._createIndexRefs(event.id);
707
+
690
708
  // Check if should use lazy indexing for large date ranges
691
709
  if (this.optimizer.shouldUseLazyIndexing(event)) {
692
710
  this._indexEventLazy(event);
@@ -706,26 +724,14 @@ export class EventStore {
706
724
 
707
725
  dates.forEach(date => {
708
726
  const dateStr = DateUtils.getLocalDateString(date);
709
-
710
- if (!this.indices.byDate.has(dateStr)) {
711
- this.indices.byDate.set(dateStr, new Set());
712
- }
713
- this.indices.byDate.get(dateStr).add(event.id);
727
+ this._addToKeyedIndex('byDate', dateStr, event.id);
714
728
  });
715
729
 
716
- // Index by month(s) using UTC
717
- const startMonth = `${startDate.getFullYear()}-${String(startDate.getMonth() + 1).padStart(2, '0')}`;
718
- const endMonth = `${endDate.getFullYear()}-${String(endDate.getMonth() + 1).padStart(2, '0')}`;
719
-
720
730
  // Add to all months the event spans
721
731
  const currentMonth = new Date(startDate.getFullYear(), startDate.getMonth(), 1);
722
732
  while (currentMonth <= endDate) {
723
733
  const monthKey = `${currentMonth.getFullYear()}-${String(currentMonth.getMonth() + 1).padStart(2, '0')}`;
724
-
725
- if (!this.indices.byMonth.has(monthKey)) {
726
- this.indices.byMonth.set(monthKey, new Set());
727
- }
728
- this.indices.byMonth.get(monthKey).add(event.id);
734
+ this._addToKeyedIndex('byMonth', monthKey, event.id);
729
735
 
730
736
  currentMonth.setMonth(currentMonth.getMonth() + 1);
731
737
  }
@@ -733,24 +739,19 @@ export class EventStore {
733
739
  // Index by categories
734
740
  if (event.categories && event.categories.length > 0) {
735
741
  event.categories.forEach(category => {
736
- if (!this.indices.byCategory.has(category)) {
737
- this.indices.byCategory.set(category, new Set());
738
- }
739
- this.indices.byCategory.get(category).add(event.id);
742
+ this._addToKeyedIndex('byCategory', category, event.id);
740
743
  });
741
744
  }
742
745
 
743
746
  // Index by status
744
747
  if (event.status) {
745
- if (!this.indices.byStatus.has(event.status)) {
746
- this.indices.byStatus.set(event.status, new Set());
747
- }
748
- this.indices.byStatus.get(event.status).add(event.id);
748
+ this._addToKeyedIndex('byStatus', event.status, event.id);
749
749
  }
750
750
 
751
751
  // Index recurring events
752
752
  if (event.recurring) {
753
753
  this.indices.recurring.add(event.id);
754
+ this.eventIndexRefs.get(event.id).recurring = true;
754
755
  }
755
756
  }
756
757
 
@@ -759,8 +760,7 @@ export class EventStore {
759
760
  * @private
760
761
  */
761
762
  _indexEventLazy(event) {
762
- // Create lazy index markers
763
- const markers = this.optimizer.createLazyIndexMarkers(event);
763
+ this.optimizer.createLazyIndexMarkers(event);
764
764
 
765
765
  // Index only the boundaries initially (in event's local timezone)
766
766
  const eventStartLocal = event.getStartInTimezone(event.timeZone);
@@ -779,10 +779,7 @@ export class EventStore {
779
779
 
780
780
  firstWeekDates.forEach(date => {
781
781
  const dateStr = DateUtils.getLocalDateString(date);
782
- if (!this.indices.byDate.has(dateStr)) {
783
- this.indices.byDate.set(dateStr, new Set());
784
- }
785
- this.indices.byDate.get(dateStr).add(event.id);
782
+ this._addToKeyedIndex('byDate', dateStr, event.id);
786
783
  });
787
784
 
788
785
  // Index last week if different from first
@@ -796,10 +793,7 @@ export class EventStore {
796
793
 
797
794
  lastWeekDates.forEach(date => {
798
795
  const dateStr = DateUtils.getLocalDateString(date);
799
- if (!this.indices.byDate.has(dateStr)) {
800
- this.indices.byDate.set(dateStr, new Set());
801
- }
802
- this.indices.byDate.get(dateStr).add(event.id);
796
+ this._addToKeyedIndex('byDate', dateStr, event.id);
803
797
  });
804
798
  }
805
799
 
@@ -807,33 +801,53 @@ export class EventStore {
807
801
  const currentMonth = new Date(startDate.getFullYear(), startDate.getMonth(), 1);
808
802
  while (currentMonth <= endDate) {
809
803
  const monthKey = `${currentMonth.getFullYear()}-${String(currentMonth.getMonth() + 1).padStart(2, '0')}`;
810
- if (!this.indices.byMonth.has(monthKey)) {
811
- this.indices.byMonth.set(monthKey, new Set());
812
- }
813
- this.indices.byMonth.get(monthKey).add(event.id);
804
+ this._addToKeyedIndex('byMonth', monthKey, event.id);
814
805
  currentMonth.setMonth(currentMonth.getMonth() + 1);
815
806
  }
816
807
 
817
808
  // Index other properties normally
818
809
  if (event.categories && event.categories.length > 0) {
819
810
  event.categories.forEach(category => {
820
- if (!this.indices.byCategory.has(category)) {
821
- this.indices.byCategory.set(category, new Set());
822
- }
823
- this.indices.byCategory.get(category).add(event.id);
811
+ this._addToKeyedIndex('byCategory', category, event.id);
824
812
  });
825
813
  }
826
814
 
827
815
  if (event.status) {
828
- if (!this.indices.byStatus.has(event.status)) {
829
- this.indices.byStatus.set(event.status, new Set());
830
- }
831
- this.indices.byStatus.get(event.status).add(event.id);
816
+ this._addToKeyedIndex('byStatus', event.status, event.id);
832
817
  }
833
818
 
834
819
  if (event.recurring) {
835
820
  this.indices.recurring.add(event.id);
821
+ this.eventIndexRefs.get(event.id).recurring = true;
822
+ }
823
+ }
824
+
825
+ /**
826
+ * Create reverse index references for an event.
827
+ * @private
828
+ */
829
+ _createIndexRefs(eventId) {
830
+ this.eventIndexRefs.set(eventId, {
831
+ byDate: new Set(),
832
+ byMonth: new Set(),
833
+ byCategory: new Set(),
834
+ byStatus: new Set(),
835
+ recurring: false
836
+ });
837
+ }
838
+
839
+ /**
840
+ * Add an event to a keyed index and record the reverse reference.
841
+ * @private
842
+ */
843
+ _addToKeyedIndex(indexName, key, eventId) {
844
+ const index = this.indices[indexName];
845
+ if (!index.has(key)) {
846
+ index.set(key, new Set());
836
847
  }
848
+
849
+ index.get(key).add(eventId);
850
+ this.eventIndexRefs.get(eventId)?.[indexName].add(key);
837
851
  }
838
852
 
839
853
  /**
@@ -841,6 +855,22 @@ export class EventStore {
841
855
  * @private
842
856
  */
843
857
  _unindexEvent(event) {
858
+ const refs = this.eventIndexRefs.get(event.id);
859
+
860
+ if (refs) {
861
+ this._removeFromReferencedIndex('byDate', refs.byDate, event.id);
862
+ this._removeFromReferencedIndex('byMonth', refs.byMonth, event.id);
863
+ this._removeFromReferencedIndex('byCategory', refs.byCategory, event.id);
864
+ this._removeFromReferencedIndex('byStatus', refs.byStatus, event.id);
865
+
866
+ if (refs.recurring) {
867
+ this.indices.recurring.delete(event.id);
868
+ }
869
+
870
+ this.eventIndexRefs.delete(event.id);
871
+ return;
872
+ }
873
+
844
874
  // Remove from date indices
845
875
  for (const [dateStr, eventIds] of this.indices.byDate) {
846
876
  eventIds.delete(event.id);
@@ -877,6 +907,24 @@ export class EventStore {
877
907
  this.indices.recurring.delete(event.id);
878
908
  }
879
909
 
910
+ /**
911
+ * Remove an event from only the keys it was indexed into.
912
+ * @private
913
+ */
914
+ _removeFromReferencedIndex(indexName, keys, eventId) {
915
+ const index = this.indices[indexName];
916
+
917
+ for (const key of keys) {
918
+ const eventIds = index.get(key);
919
+ if (!eventIds) continue;
920
+
921
+ eventIds.delete(eventId);
922
+ if (eventIds.size === 0) {
923
+ index.delete(key);
924
+ }
925
+ }
926
+ }
927
+
880
928
  /**
881
929
  * Notify listeners of changes
882
930
  * @private
@@ -938,6 +986,18 @@ export class EventStore {
938
986
  Array.from(this.indices.byStatus.entries()).map(([k, v]) => [k, new Set(v)])
939
987
  )
940
988
  },
989
+ eventIndexRefs: new Map(
990
+ Array.from(this.eventIndexRefs.entries()).map(([eventId, refs]) => [
991
+ eventId,
992
+ {
993
+ byDate: new Set(refs.byDate),
994
+ byMonth: new Set(refs.byMonth),
995
+ byCategory: new Set(refs.byCategory),
996
+ byStatus: new Set(refs.byStatus),
997
+ recurring: refs.recurring
998
+ }
999
+ ])
1000
+ ),
941
1001
  version: this.version
942
1002
  };
943
1003
  }
@@ -981,6 +1041,7 @@ export class EventStore {
981
1041
  if (this.batchBackup) {
982
1042
  this.events = this.batchBackup.events;
983
1043
  this.indices = this.batchBackup.indices;
1044
+ this.eventIndexRefs = this.batchBackup.eventIndexRefs;
984
1045
  this.version = this.batchBackup.version;
985
1046
  this.batchBackup = null;
986
1047
 
@@ -1154,7 +1215,6 @@ export class EventStore {
1154
1215
  cutoffDate.setMonth(cutoffDate.getMonth() - 6); // Default: 6 months ago
1155
1216
  }
1156
1217
 
1157
- const cutoffStr = cutoffDate.toDateString();
1158
1218
  let removed = 0;
1159
1219
 
1160
1220
  // Clean up date indices
@@ -1172,13 +1232,15 @@ export class EventStore {
1172
1232
  }
1173
1233
 
1174
1234
  if (!stillNeeded) {
1235
+ for (const eventId of eventIds) {
1236
+ this.eventIndexRefs.get(eventId)?.byDate.delete(dateStr);
1237
+ }
1175
1238
  this.indices.byDate.delete(dateStr);
1176
1239
  removed++;
1177
1240
  }
1178
1241
  }
1179
1242
  }
1180
1243
 
1181
- console.log(`Optimized indices: removed ${removed} old date entries`);
1182
1244
  return removed;
1183
1245
  }
1184
1246
 
package/core/index.js CHANGED
@@ -28,7 +28,7 @@ export { RRuleParser } from './events/RRuleParser.js';
28
28
  export { EnhancedCalendar } from './integration/EnhancedCalendar.js';
29
29
 
30
30
  // Version — keep in sync with package.json
31
- export const VERSION = '2.1.65';
31
+ export const VERSION = '2.1.66';
32
32
 
33
33
  // Default export
34
34
  export { Calendar as default } from './calendar/Calendar.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forcecalendar/core",
3
- "version": "2.1.65",
3
+ "version": "2.1.66",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "description": "A modern, lightweight, framework-agnostic calendar engine optimized for Salesforce",