@luvio/environments 0.144.8 → 0.145.1

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.
@@ -19,6 +19,8 @@ function isDeprecatedDurableStoreEntry(durableRecord) {
19
19
  }
20
20
  const DefaultDurableSegment = 'DEFAULT';
21
21
  const RedirectDurableSegment = 'REDIRECT_KEYS';
22
+ const MessagingDurableSegment = 'MESSAGING';
23
+ const MessageNotifyStoreUpdateAvailable = 'notifyStoreUpdateAvailable';
22
24
 
23
25
  const { keys, create, assign, freeze } = Object;
24
26
 
@@ -460,6 +462,7 @@ function makeDurable(environment, { durableStore, instrumentation }) {
460
462
  const defaultSegmentKeys = [];
461
463
  const adapterContextSegmentKeys = [];
462
464
  const redirectSegmentKeys = [];
465
+ const messagingSegmentKeys = [];
463
466
  let shouldBroadcast = false;
464
467
  for (let i = 0, len = changes.length; i < len; i++) {
465
468
  const change = changes[i];
@@ -474,6 +477,9 @@ function makeDurable(environment, { durableStore, instrumentation }) {
474
477
  else if (change.segment === RedirectDurableSegment) {
475
478
  redirectSegmentKeys.push(...change.ids);
476
479
  }
480
+ else if (change.segment === MessagingDurableSegment) {
481
+ messagingSegmentKeys.push(...change.ids);
482
+ }
477
483
  }
478
484
  if (redirectSegmentKeys.length > 0) {
479
485
  const redirectEntries = await durableStore.getEntries(redirectSegmentKeys, RedirectDurableSegment);
@@ -533,6 +539,20 @@ function makeDurable(environment, { durableStore, instrumentation }) {
533
539
  if (shouldBroadcast) {
534
540
  await environment.storeBroadcast(rebuildSnapshot, environment.snapshotAvailable);
535
541
  }
542
+ // if having notifyStoreUpdateAvailable msg, then pull the message body out from store
543
+ // and call environment.notifyStoreUpdateAvailable
544
+ if (messagingSegmentKeys.includes(MessageNotifyStoreUpdateAvailable)) {
545
+ const entries = await durableStore.getEntries([MessageNotifyStoreUpdateAvailable], MessagingDurableSegment);
546
+ if (entries !== undefined) {
547
+ const notifyEntry = entries[MessageNotifyStoreUpdateAvailable];
548
+ if (notifyEntry !== undefined) {
549
+ const keys = notifyEntry.data;
550
+ if (keys.length > 0) {
551
+ environment.notifyStoreUpdateAvailable(keys);
552
+ }
553
+ }
554
+ }
555
+ }
536
556
  });
537
557
  const dispose = function () {
538
558
  validateNotDisposed();
@@ -868,6 +888,39 @@ function makeDurable(environment, { durableStore, instrumentation }) {
868
888
  return entries;
869
889
  });
870
890
  };
891
+ // flag entries specified by keys in durable store as expired.
892
+ // send a notifyStoreUpdateAvailable message through durable store set entries to
893
+ // indirectly triggers all js environments to refresh snapshots if overlapping with keys.
894
+ const notifyStoreUpdateAvailable = async function (keys$1) {
895
+ validateNotDisposed();
896
+ const entryKeys = keys$1.map(serializeStructuredKey);
897
+ const entries = await durableStore.getEntries(entryKeys, DefaultDurableSegment);
898
+ if (entries === undefined || keys(entries).length === 0) {
899
+ return environment.notifyStoreUpdateAvailable(keys$1);
900
+ }
901
+ const now = Date.now();
902
+ let needWriteBack = false;
903
+ for (let i = 0; i < entryKeys.length; i++) {
904
+ const key = entryKeys[i];
905
+ const entry = entries[key];
906
+ if (entry !== undefined) {
907
+ const storeEntry = entry;
908
+ if (storeEntry.metadata !== undefined) {
909
+ storeEntry.metadata = {
910
+ ...storeEntry.metadata,
911
+ expirationTimestamp: now,
912
+ };
913
+ }
914
+ needWriteBack = true;
915
+ }
916
+ }
917
+ if (needWriteBack) {
918
+ await durableStore.setEntries(entries, DefaultDurableSegment);
919
+ }
920
+ // push a notifyStoreUpdateAvailable message with entryKeys as data into messaging segment
921
+ await durableStore.setEntries({ notifyStoreUpdateAvailable: { data: entryKeys } }, MessagingDurableSegment);
922
+ return Promise.resolve(undefined);
923
+ };
871
924
  // set the default cache policy of the base environment
872
925
  environment.setDefaultCachePolicy({
873
926
  type: 'stale-while-revalidate',
@@ -898,6 +951,7 @@ function makeDurable(environment, { durableStore, instrumentation }) {
898
951
  handleSuccessResponse: { value: handleSuccessResponse },
899
952
  handleErrorResponse: { value: handleErrorResponse },
900
953
  getNotifyChangeStoreEntries: { value: getNotifyChangeStoreEntries },
954
+ notifyStoreUpdateAvailable: { value: notifyStoreUpdateAvailable },
901
955
  });
902
956
  }
903
957
 
@@ -1,6 +1,6 @@
1
1
  import type { StoreMetadata } from '@luvio/engine';
2
2
  export declare const DURABLE_METADATA_VERSION = "0.111.0";
3
- interface DurableStoreMetadata extends StoreMetadata {
3
+ export interface DurableStoreMetadata extends StoreMetadata {
4
4
  metadataVersion: string;
5
5
  }
6
6
  /**
@@ -43,8 +43,11 @@ export interface DurableStoreEvictOperation extends BaseOperation {
43
43
  export type DurableStoreOperation<T> = DurableStoreSetOperation<T> | DurableStoreEvictOperation;
44
44
  export type DefaultDurableSegmentName = 'DEFAULT';
45
45
  export type RedirectDurableSegmentName = 'REDIRECT_KEYS';
46
+ export type MessagingDurableSegmentName = 'MESSAGING';
46
47
  export declare const DefaultDurableSegment: DefaultDurableSegmentName;
47
48
  export declare const RedirectDurableSegment: RedirectDurableSegmentName;
49
+ export declare const MessagingDurableSegment: MessagingDurableSegmentName;
50
+ export declare const MessageNotifyStoreUpdateAvailable = "notifyStoreUpdateAvailable";
48
51
  export interface DurableStoreChange {
49
52
  /** The entry IDs of the entries that have changed */
50
53
  ids: string[];
@@ -133,4 +136,3 @@ export interface DurableStore {
133
136
  */
134
137
  registerOnChangedListener(listener: OnDurableStoreChangedListener): () => Promise<void>;
135
138
  }
136
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luvio/environments",
3
- "version": "0.144.8",
3
+ "version": "0.145.1",
4
4
  "description": "Luvio Environments",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,7 +27,7 @@
27
27
  "watch": "yarn build --watch"
28
28
  },
29
29
  "dependencies": {
30
- "@luvio/engine": "^0.144.8"
30
+ "@luvio/engine": "^0.145.1"
31
31
  },
32
32
  "volta": {
33
33
  "extends": "../../../package.json"
@@ -36,9 +36,9 @@
36
36
  {
37
37
  "path": "./dist/environments.js",
38
38
  "maxSize": {
39
- "none": "40 kB",
40
- "min": "11.5 kB",
41
- "compressed": "8 kB"
39
+ "none": "42.2 kB",
40
+ "min": "12.3 kB",
41
+ "compressed": "8.1 kB"
42
42
  }
43
43
  }
44
44
  ],