@salesforce/lds-worker-api 1.245.0 → 1.247.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.
@@ -925,30 +925,76 @@ const MessagingDurableSegmentName = 'MESSAGING';
925
925
  const DEFAULT_MAX_ENTRIES_PER_OPERATION = 500;
926
926
  /**
927
927
  * Keep tracking of ongoing eviction id to a flag to trigger eviction cancellation.
928
- * false in the begining, set to false when cancelEviction is called.
928
+ * false in the begining, set to true when cancelEviction is called.
929
929
  */
930
- const inProgressEvictions = new Map();
930
+ const evictionCancellationFlag = new Map();
931
931
  /**
932
932
  * Purging records specified by an array of record id from durable store
933
933
  * @param recordIds record id array
934
934
  * @onProgressUpdate call back to report evict progress
935
935
  *
936
- * @returns a function to cancel the eviction
936
+ * @returns evictId, call cancelEviction with it to abort the eviction.
937
937
  *
938
938
  * The record will not be purged if the record has draft.
939
939
  */
940
940
  function evictCacheRecordsByIds(recordIds, onProgressUpdate) {
941
941
  const evictionId = uuidv4();
942
- inProgressEvictions.set(evictionId, false);
943
- const onEvicted = (progress) => {
942
+ evictionCancellationFlag.set(evictionId, false);
943
+ const evictAChunk = () => {
944
+ evictChunksOfRecord(recordIds).then(onEvicted);
945
+ };
946
+ const onEvicted = getOnEvictedCallback(onProgressUpdate, evictionId, evictAChunk);
947
+ evictAChunk();
948
+ return evictionId;
949
+ }
950
+ /**
951
+ * Purging the db entries which passed expired time by specified days
952
+ * from durable store.
953
+ * @param expiredByDays
954
+ * @param onProgressUpdate
955
+ *
956
+ * @returns an evictId, call cancelEviction with it to abort the eviction.
957
+ *
958
+ * Important: records with draft, objectInfo entries will not be purged even expired by the specified days.
959
+ */
960
+ function evictExpiredCacheEntries(expiredByDays, onProgressUpdate) {
961
+ const evictionId = uuidv4();
962
+ evictionCancellationFlag.set(evictionId, false);
963
+ const overdueExpirationTimeStamp = Date.now() - expiredByDays * 24 * 3600 * 1000;
964
+ const evictAChunk = () => {
965
+ evictChunkOfOverdueEntries(overdueExpirationTimeStamp).then(onEvicted);
966
+ };
967
+ const onEvicted = getOnEvictedCallback(onProgressUpdate, evictionId, evictAChunk);
968
+ evictAChunk();
969
+ return evictionId;
970
+ }
971
+ /**
972
+ * Cancel the eviction by specified id.
973
+ * Signal to cancel the eviction if the eviction is going on.
974
+ * @param evictionId
975
+ */
976
+ function cancelEviction(evictionId) {
977
+ if (evictionCancellationFlag.has(evictionId)) {
978
+ evictionCancellationFlag.set(evictionId, true);
979
+ }
980
+ }
981
+ /**
982
+ * Get a callback function when a chunk of records or entries are removed.
983
+ * @param onProgressUpdate
984
+ * @param evictionId
985
+ * @param evictAChunk
986
+ * @returns a callback to call when a chunk of records or entries are removed.
987
+ */
988
+ function getOnEvictedCallback(onProgressUpdate, evictionId, evictAChunk) {
989
+ return (progress) => {
944
990
  onProgressUpdate(progress);
945
991
  const { status } = progress;
946
992
  if (status === EvictStatus.Succeeded || status === EvictStatus.Error) {
947
- inProgressEvictions.delete(evictionId);
993
+ evictionCancellationFlag.delete(evictionId);
948
994
  }
949
995
  if (status === EvictStatus.InProgress) {
950
- if (inProgressEvictions.get(evictionId)) {
951
- inProgressEvictions.delete(evictionId);
996
+ if (evictionCancellationFlag.get(evictionId)) {
997
+ evictionCancellationFlag.delete(evictionId);
952
998
  onProgressUpdate({ status: EvictStatus.Cancelled });
953
999
  }
954
1000
  else {
@@ -956,18 +1002,6 @@ function evictCacheRecordsByIds(recordIds, onProgressUpdate) {
956
1002
  }
957
1003
  }
958
1004
  };
959
- const evictAChunk = () => {
960
- evictChunksOfRecord(recordIds).then(onEvicted);
961
- };
962
- evictAChunk();
963
- return evictionId;
964
- }
965
- /**
966
- * Cancel the eviction by specified id.
967
- * @param evictionId
968
- */
969
- function cancelEviction(evictionId) {
970
- inProgressEvictions.set(evictionId, true);
971
1005
  }
972
1006
  /**
973
1007
  * pull a chunk of record id, evict status back in a promise.
@@ -993,8 +1027,10 @@ function evictChunksOfRecord(ids) {
993
1027
  .then((result) => {
994
1028
  const evictedEntries = result.rows.map((row) => row[0]);
995
1029
  const skippedEntries = params.filter((entryKey) => evictedEntries.indexOf(entryKey) === -1);
996
- // broadcast entries evicted
997
- nimbusSqliteStore.setEntries({ notifyStoreUpdateAvailable: { data: evictedEntries } }, MessagingDurableSegmentName);
1030
+ // broadcast entries evicted if some entries are removed
1031
+ if (evictedEntries.length > 0) {
1032
+ nimbusSqliteStore.setEntries({ notifyStoreUpdateAvailable: { data: evictedEntries } }, MessagingDurableSegmentName);
1033
+ }
998
1034
  resolve({
999
1035
  status: EvictStatus.InProgress,
1000
1036
  evictedEntries,
@@ -1016,6 +1052,63 @@ function evictChunksOfRecord(ids) {
1016
1052
  }
1017
1053
  });
1018
1054
  }
1055
+ /**
1056
+ * Evict chunk of overdue entries from db.
1057
+ * @param overdueExpirationTimestamp
1058
+ * @returns
1059
+ */
1060
+ function evictChunkOfOverdueEntries(overdueExpirationTimestamp) {
1061
+ return new Promise((resolve) => {
1062
+ // evict the chunk
1063
+ const { nimbusSqliteStore } = getRuntime();
1064
+ try {
1065
+ nimbusSqliteStore
1066
+ .query(`DELETE FROM lds_data
1067
+ WHERE key IN (
1068
+ SELECT key
1069
+ FROM lds_data
1070
+ WHERE key NOT LIKE 'UiApi::ObjectInfoRepresentation:%'
1071
+ AND
1072
+ (
1073
+ key NOT LIKE 'UiApi::RecordRepresentation:%'
1074
+ OR
1075
+ key LIKE 'UiApi::RecordRepresentation:%' AND
1076
+ JSON_EXTRACT(data, '$.drafts') IS NULL AND
1077
+ JSON_EXTRACT(metadata, '$.expirationTimestamp') < ${overdueExpirationTimestamp}
1078
+ )
1079
+ LIMIT ${DEFAULT_MAX_ENTRIES_PER_OPERATION}
1080
+ )
1081
+ RETURNING key`, [])
1082
+ .then((result) => {
1083
+ const evictedEntries = result.rows.map((row) => row[0]);
1084
+ if (evictedEntries.length > 0) {
1085
+ // broadcast entries evicted
1086
+ nimbusSqliteStore.setEntries({ notifyStoreUpdateAvailable: { data: evictedEntries } }, MessagingDurableSegmentName);
1087
+ resolve({
1088
+ status: EvictStatus.InProgress,
1089
+ evictedEntries,
1090
+ skippedEntries: [],
1091
+ });
1092
+ }
1093
+ else {
1094
+ resolve({ status: EvictStatus.Succeeded });
1095
+ }
1096
+ })
1097
+ .catch((reason) => {
1098
+ resolve({
1099
+ status: EvictStatus.Error,
1100
+ message: JSON.stringify(reason),
1101
+ });
1102
+ });
1103
+ }
1104
+ catch (reason) {
1105
+ resolve({
1106
+ status: EvictStatus.Error,
1107
+ message: JSON.stringify(reason),
1108
+ });
1109
+ }
1110
+ });
1111
+ }
1019
1112
 
1020
1113
  if (process.env.NODE_ENV !== 'production') {
1021
1114
  // eslint-disable-next-line no-undef
@@ -1026,5 +1119,5 @@ if (process.env.NODE_ENV !== 'production') {
1026
1119
  });
1027
1120
  }
1028
1121
 
1029
- export { cancelEviction, createPrimingSession, draftManager, draftQueue, evictCacheRecordsByIds, executeAdapter, executeMutatingAdapter, getImperativeAdapterNames, invokeAdapter, invokeAdapterWithDraftToReplace, invokeAdapterWithMetadata, nimbusDraftQueue, setMetadataTTL, setUiApiRecordTTL, subscribeToAdapter };
1030
- // version: 1.245.0-0ea124370
1122
+ export { cancelEviction, createPrimingSession, draftManager, draftQueue, evictCacheRecordsByIds, evictExpiredCacheEntries, executeAdapter, executeMutatingAdapter, getImperativeAdapterNames, invokeAdapter, invokeAdapterWithDraftToReplace, invokeAdapterWithMetadata, nimbusDraftQueue, setMetadataTTL, setUiApiRecordTTL, subscribeToAdapter };
1123
+ // version: 1.247.0-4fe38c091
@@ -26,13 +26,25 @@ export type ProgressUpdateCallback = (progess: EvictProgress) => void;
26
26
  * @param recordIds record id array
27
27
  * @onProgressUpdate call back to report evict progress
28
28
  *
29
- * @returns a function to cancel the eviction
29
+ * @returns evictId, call cancelEviction with it to abort the eviction.
30
30
  *
31
31
  * The record will not be purged if the record has draft.
32
32
  */
33
33
  export declare function evictCacheRecordsByIds(recordIds: string[], onProgressUpdate: ProgressUpdateCallback): string;
34
+ /**
35
+ * Purging the db entries which passed expired time by specified days
36
+ * from durable store.
37
+ * @param expiredByDays
38
+ * @param onProgressUpdate
39
+ *
40
+ * @returns an evictId, call cancelEviction with it to abort the eviction.
41
+ *
42
+ * Important: records with draft, objectInfo entries will not be purged even expired by the specified days.
43
+ */
44
+ export declare function evictExpiredCacheEntries(expiredByDays: number, onProgressUpdate: ProgressUpdateCallback): string;
34
45
  /**
35
46
  * Cancel the eviction by specified id.
47
+ * Signal to cancel the eviction if the eviction is going on.
36
48
  * @param evictionId
37
49
  */
38
50
  export declare function cancelEviction(evictionId: string): void;
@@ -5,5 +5,5 @@ import { draftQueue, draftManager } from './draftQueueImplementation';
5
5
  import { setUiApiRecordTTL, setMetadataTTL } from './ttl';
6
6
  import { registerReportObserver } from '@salesforce/lds-runtime-mobile';
7
7
  import { createPrimingSession } from './primingApi';
8
- import { evictCacheRecordsByIds, cancelEviction } from './cachePurging';
9
- export { subscribeToAdapter, invokeAdapter, invokeAdapterWithMetadata, invokeAdapterWithDraftToReplace, executeAdapter, executeMutatingAdapter, nimbusDraftQueue, draftQueue, draftManager, setUiApiRecordTTL, setMetadataTTL, registerReportObserver, getImperativeAdapterNames, createPrimingSession, evictCacheRecordsByIds, cancelEviction, };
8
+ import { evictCacheRecordsByIds, evictExpiredCacheEntries, cancelEviction } from './cachePurging';
9
+ export { subscribeToAdapter, invokeAdapter, invokeAdapterWithMetadata, invokeAdapterWithDraftToReplace, executeAdapter, executeMutatingAdapter, nimbusDraftQueue, draftQueue, draftManager, setUiApiRecordTTL, setMetadataTTL, registerReportObserver, getImperativeAdapterNames, createPrimingSession, evictCacheRecordsByIds, evictExpiredCacheEntries, cancelEviction, };