@salesforce/lds-drafts 1.319.0 → 1.321.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/ldsDrafts.js CHANGED
@@ -100,59 +100,6 @@ const { keys, create, assign, values } = Object;
100
100
  const { stringify, parse } = JSON;
101
101
  const { isArray } = Array;
102
102
 
103
- const DraftIdMappingKeyPrefix240 = 'DraftIdMapping::';
104
- const DRAFT_ID_MAPPINGS_SEGMENT = 'DRAFT_ID_MAPPINGS';
105
- function isLegacyDraftIdMapping(key, data) {
106
- return key.startsWith(DraftIdMappingKeyPrefix240);
107
- }
108
- // TODO [W-11677776]: in 242 we changed the format to store keys instead of ids
109
- // this can be removed when we drop support for id storing
110
- function getRecordKeyForId(id) {
111
- return `UiApi::RecordRepresentation:${id}`;
112
- }
113
- /**
114
- *
115
- * @param mappingIds (optional) requested mapping ids, if undefined all will be retrieved
116
- */
117
- async function getDraftIdMappings(durableStore, mappingIds) {
118
- const mappings = [];
119
- let durableStoreOperation;
120
- if (mappingIds === undefined) {
121
- durableStoreOperation =
122
- durableStore.getAllEntries(DRAFT_ID_MAPPINGS_SEGMENT);
123
- }
124
- else {
125
- durableStoreOperation = durableStore.getEntries(mappingIds, DRAFT_ID_MAPPINGS_SEGMENT);
126
- }
127
- const entries = await durableStoreOperation;
128
- if (entries === undefined) {
129
- return mappings;
130
- }
131
- const keys$1 = keys(entries);
132
- for (const key of keys$1) {
133
- const entry = entries[key].data;
134
- if (isLegacyDraftIdMapping(key)) {
135
- mappings.push({
136
- draftKey: getRecordKeyForId(entry.draftId),
137
- canonicalKey: getRecordKeyForId(entry.canonicalId),
138
- });
139
- }
140
- else {
141
- mappings.push(entry);
142
- }
143
- }
144
- return mappings;
145
- }
146
- async function clearDraftIdSegment(durableStore) {
147
- const entries = await durableStore.getAllEntries(DRAFT_ID_MAPPINGS_SEGMENT);
148
- if (entries) {
149
- const keys$1 = keys(entries);
150
- if (keys$1.length > 0) {
151
- await durableStore.evictEntries(keys$1, DRAFT_ID_MAPPINGS_SEGMENT);
152
- }
153
- }
154
- }
155
-
156
103
  class DraftSynthesisError extends Error {
157
104
  constructor(message, errorType) {
158
105
  super(message);
@@ -349,17 +296,9 @@ function customActionHandler(executor, id, draftQueue) {
349
296
  handleActionRemoved: () => Promise.resolve(),
350
297
  handleActionCompleted: () => Promise.resolve(),
351
298
  handleActionEnqueued: () => Promise.resolve(),
352
- getDataForAction: () => Promise.resolve(undefined),
353
- getDraftMetadata: () => {
354
- throw Error('getDraftMetadata not supported for custom actions');
355
- },
356
- applyDraftsToIncomingData: () => {
357
- throw Error('applyDraftsToIncomingData not supported for custom actions');
358
- },
299
+ handleActionReplaced: () => Promise.resolve(),
359
300
  shouldDeleteActionByTagOnRemoval: () => false,
360
301
  updateMetadata: (existing, incoming) => incoming,
361
- canHandlePublish: () => false,
362
- canRepresentationContainDraftMetadata: () => false,
363
302
  mergeActions: () => {
364
303
  throw Error('mergeActions not supported for custom actions');
365
304
  },
@@ -834,6 +773,7 @@ class DurableDraftQueue {
834
773
  : handler.handleReplaceAction(target, source);
835
774
  // update the target
836
775
  await this.draftStore.writeAction(updatedTarget);
776
+ await handler.handleActionReplaced(updatedTarget, source);
837
777
  await this.notifyChangedListeners({
838
778
  type: DraftQueueEventType.ActionUpdated,
839
779
  action: updatedTarget,
@@ -1446,94 +1386,4 @@ function isResourceRequestAction(action) {
1446
1386
  return (dataAsAny !== undefined && dataAsAny.method !== undefined && dataAsAny.body !== undefined);
1447
1387
  }
1448
1388
 
1449
- function makeEnvironmentDraftAware(luvio, env, durableStore, handlers, draftQueue) {
1450
- const draftMetadata = {};
1451
- // in 246 luvio took charge of persisting redirect mappings, this needs to stick around
1452
- // for a couple of releases to support older environments
1453
- // setup existing store redirects when bootstrapping the environment
1454
- (async () => {
1455
- const mappings = await getDraftIdMappings(durableStore);
1456
- mappings.forEach((mapping) => {
1457
- const { draftKey, canonicalKey } = mapping;
1458
- env.storeRedirect(draftKey, canonicalKey);
1459
- });
1460
- await env.storeBroadcast(env.rebuildSnapshot, env.snapshotAvailable);
1461
- await clearDraftIdSegment(durableStore);
1462
- })();
1463
- const handleSuccessResponse = async function (ingestAndBroadcastFunc, getResponseCacheKeysFunc) {
1464
- const queue = await draftQueue.getQueueActions();
1465
- if (queue.length === 0) {
1466
- return env.handleSuccessResponse(ingestAndBroadcastFunc, getResponseCacheKeysFunc);
1467
- }
1468
- const cacheKeySet = getResponseCacheKeysFunc();
1469
- for (const possibleKey of cacheKeySet.keys()) {
1470
- const key = typeof possibleKey === 'string' ? possibleKey : '';
1471
- const cacheKey = cacheKeySet.get(key);
1472
- for (const handler of handlers) {
1473
- if (cacheKey !== undefined &&
1474
- handler.canRepresentationContainDraftMetadata(cacheKey.representationName)) {
1475
- const metadata = await handler.getDraftMetadata(key);
1476
- if (metadata !== undefined) {
1477
- // if this key is related to a draft then mark it mergeable so
1478
- // base environment revives it to staging store before ingestion
1479
- cacheKey.mergeable = true;
1480
- const existing = draftMetadata[key];
1481
- if (existing === undefined) {
1482
- draftMetadata[key] = { metadata, refCount: 1 };
1483
- }
1484
- else {
1485
- draftMetadata[key] = { metadata, refCount: existing.refCount + 1 };
1486
- }
1487
- }
1488
- break;
1489
- }
1490
- }
1491
- }
1492
- return env.handleSuccessResponse(ingestAndBroadcastFunc, () => cacheKeySet);
1493
- };
1494
- const storePublish = function (key, data) {
1495
- const draftMetadataForKey = draftMetadata[key];
1496
- let handled = false;
1497
- for (const handler of handlers) {
1498
- if (handler.canHandlePublish(key)) {
1499
- handled = true;
1500
- handler.applyDraftsToIncomingData(key, data, draftMetadataForKey && draftMetadataForKey.metadata, env.storePublish);
1501
- // deletes the draft metadata once it is published and has 0 ref.
1502
- if (draftMetadataForKey &&
1503
- draftMetadataForKey.metadata &&
1504
- draftMetadataForKey.refCount === 0) {
1505
- delete draftMetadata[key];
1506
- }
1507
- break;
1508
- }
1509
- }
1510
- decrementRefCount(key);
1511
- // no handler could handle it so publish
1512
- if (!handled) {
1513
- env.storePublish(key, data);
1514
- }
1515
- };
1516
- const decrementRefCount = function (key) {
1517
- const draftMetadataForKey = draftMetadata[key];
1518
- if (draftMetadataForKey !== undefined) {
1519
- // metadata is needed for a once-draft record to create ref link for spanning fields.
1520
- if (draftMetadataForKey.refCount === 1) {
1521
- draftMetadata[key].metadata.recordOperations = [];
1522
- }
1523
- draftMetadataForKey.refCount--;
1524
- }
1525
- };
1526
- // when a record is soft deleted it won't be published so won't decrement the ref count
1527
- // this is a backdoor to decrement the ref count
1528
- const softEvict = function (key) {
1529
- decrementRefCount(key);
1530
- };
1531
- // note the makeEnvironmentUiApiRecordDraftAware will eventually go away once the adapters become draft aware
1532
- return create(env, {
1533
- storePublish: { value: storePublish },
1534
- handleSuccessResponse: { value: handleSuccessResponse },
1535
- softEvict: { value: softEvict },
1536
- });
1537
- }
1538
-
1539
- export { CustomActionResultType, DRAFT_ERROR_CODE, DRAFT_ID_MAPPINGS_SEGMENT, DRAFT_SEGMENT, DraftActionOperationType, DraftActionStatus, DraftErrorFetchResponse, DraftFetchResponse, DraftManager, DraftQueueEventType, DraftQueueState, DraftSynthesisError, DurableDraftQueue, DurableDraftStore, ProcessActionResult, QueueOperationType, createBadRequestResponse, createDeletedResponse, createDraftSynthesisErrorResponse, createInternalErrorResponse, createNotFoundResponse, createOkResponse, generateUniqueDraftActionId, isDraftSynthesisError, makeEnvironmentDraftAware, transformErrorToDraftSynthesisError };
1389
+ export { CustomActionResultType, DRAFT_ERROR_CODE, DRAFT_SEGMENT, DraftActionOperationType, DraftActionStatus, DraftErrorFetchResponse, DraftFetchResponse, DraftManager, DraftQueueEventType, DraftQueueState, DraftSynthesisError, DurableDraftQueue, DurableDraftStore, ProcessActionResult, QueueOperationType, createBadRequestResponse, createDeletedResponse, createDraftSynthesisErrorResponse, createInternalErrorResponse, createNotFoundResponse, createOkResponse, generateUniqueDraftActionId, isDraftSynthesisError, transformErrorToDraftSynthesisError };
@@ -171,7 +171,7 @@ export interface DraftQueue {
171
171
  * @param id identifier to the handler
172
172
  * @param handler ActionHandler to process action
173
173
  */
174
- addHandler<Data, Type>(handler: ActionHandler<Data, unknown, Type>): Promise<void>;
174
+ addHandler<Data, Type>(handler: ActionHandler<Data, Type>): Promise<void>;
175
175
  /**
176
176
  * Creates a ActionHandler<CustomActionData> with a callback to let the DraftQueue know when it has been processed
177
177
  * @param id identifier of the handler
@@ -21,7 +21,7 @@ export declare class DurableDraftQueue implements DraftQueue {
21
21
  private handlers;
22
22
  private getHandler;
23
23
  constructor(draftStore: DraftStore);
24
- addHandler<Data>(handler: ActionHandler<Data, unknown, unknown>): Promise<void>;
24
+ addHandler<Data>(handler: ActionHandler<Data, unknown>): Promise<void>;
25
25
  removeHandler(id: string): Promise<void>;
26
26
  addCustomHandler(id: string, executor: CustomActionExecutor): Promise<void>;
27
27
  getQueueState(): DraftQueueState;
@@ -1,14 +1,9 @@
1
1
  import type { CompletedDraftAction, DraftAction, DraftActionMetadata, PendingDraftAction, ProcessActionResult, QueueOperation } from '../DraftQueue';
2
- import type { DraftKeyMapping } from '../DraftIdMapping';
3
2
  export interface ReplacingActions<Response, Data> {
4
3
  original: DraftAction<Response, Data>;
5
4
  actionToReplace: DraftAction<Response, Data>;
6
5
  replacingAction: DraftAction<Response, Data>;
7
6
  }
8
- export interface DraftIdAndKeyMapping extends DraftKeyMapping {
9
- draftId: string;
10
- canonicalId: string;
11
- }
12
7
  /**
13
8
  * A Handler that knows what the Data and Response type expected are in a DraftAction.
14
9
  * It knows how to process and set them up.
@@ -16,22 +11,12 @@ export interface DraftIdAndKeyMapping extends DraftKeyMapping {
16
11
  * The action handler is registered both with the DraftQueue and the Draft environment to handle
17
12
  * all draft application logic to a particular type
18
13
  */
19
- export interface ActionHandler<Data, DraftMetadata, Type> {
14
+ export interface ActionHandler<Data, Type> {
20
15
  /**
21
16
  * The id for this handler. Every action in the queue will have a handlerId property
22
17
  * associated with it which specifies which handler knows how to handle that draft action
23
18
  */
24
19
  handlerId: string;
25
- /**
26
- * Invoked by the draft environment while ingesting incoming data to determine if the handler knows how to apply drafts to the incoming key
27
- * @param key
28
- */
29
- canHandlePublish(key: string): boolean;
30
- /**
31
- * Invoked by the draft environment prior to ingesting to determine if the representation type can potentially have draft metadata
32
- * @param representationName
33
- */
34
- canRepresentationContainDraftMetadata(representationName: string): boolean;
35
20
  /**
36
21
  * Enqueues an action to the draft queue
37
22
  * @param data the data that will get added to the draft action
@@ -77,7 +62,7 @@ export interface ActionHandler<Data, DraftMetadata, Type> {
77
62
  * @param action The completed action
78
63
  * @param queueOperations The queue modification operations that were executes with the completed action
79
64
  */
80
- handleActionCompleted(action: CompletedDraftAction<Data, Type>, queueOperations: QueueOperation[], allHandlers: ActionHandler<unknown, unknown, unknown>[]): Promise<void>;
65
+ handleActionCompleted(action: CompletedDraftAction<Data, Type>, queueOperations: QueueOperation[], allHandlers: ActionHandler<unknown, unknown>[]): Promise<void>;
81
66
  /**
82
67
  * Replace the targetAction's data with the sourceAction's data and return
83
68
  * the targetAction. Also sets the targetAction's status to Pending.
@@ -92,32 +77,6 @@ export interface ActionHandler<Data, DraftMetadata, Type> {
92
77
  * in an error.
93
78
  */
94
79
  handleReplaceAction(targetAction: DraftAction<Data, Type>, sourceAction: DraftAction<Data, Type>): DraftAction<Data, Type>;
95
- /**
96
- * Invoked by the durable store and the draft environment to get the data that is associated with this draft action
97
- * The data should include all drafts applied to it (if any)
98
- * @param action The action to get data for
99
- * @param queue The draft queue
100
- *
101
- * @returns the data with the up to date draft changes applied. returns undefined if after replaying drafts there is no
102
- * data to return (i.e. a draft-created record had it's post action removed)
103
- */
104
- getDataForAction(action: DraftAction<Data, Type>): Promise<Type | undefined>;
105
- /**
106
- * Asynchronous function to fetch any necessary metadata required to apply drafts during ingest
107
- * Note that since ingest is synchronous any necessary metadata that requires asynchronicity must be fetch prior to ingest commencing
108
- * @param key
109
- * @returns the metadata or undefined if there's no drafts associated or no metadata for this key
110
- */
111
- getDraftMetadata(key: string): Promise<DraftMetadata | undefined>;
112
- /**
113
- * Invoked by the draft environment while ingesting normalized data that may contain drafts.
114
- * Note that this function is synchronous since ingestion is synchronous (see note in getDraftMetadata)
115
- * @param key The key being ingested
116
- * @param data The normalized data being ingested
117
- * @param draftMetadata The metadata that was previously fetched using getDraftMetadata
118
- * @param publishFn The environment's publish method to publish the data once drafts have been applied
119
- */
120
- applyDraftsToIncomingData(key: string, data: unknown, draftMetadata: DraftMetadata | undefined, publishFn: (key: string, data: any) => void): void;
121
80
  /**
122
81
  * Invoked by the draft queue when a request to remove an action is made. This tells the queue whether it should just
123
82
  * delete the requested action or any action with the same tag. This is helpful in examples where other related queue items no longer
@@ -145,4 +104,9 @@ export interface ActionHandler<Data, DraftMetadata, Type> {
145
104
  * NOTE: the resulting merged action will use the target's timestamp and id.
146
105
  */
147
106
  mergeActions(targetAction: DraftAction<Data, Type>, sourceAction: DraftAction<Data, Type>): DraftAction<Data, Type>;
107
+ /**
108
+ * Invoked when an action is replaced
109
+ * @param action
110
+ */
111
+ handleActionReplaced(target: DraftAction<Data, Type>, source: DraftAction<Data, Type>): Promise<void>;
148
112
  }
@@ -29,5 +29,5 @@ export type CustomActionCompletionResponse = (result: CustomActionResult) => voi
29
29
  export interface CustomActionData {
30
30
  [key: string]: string;
31
31
  }
32
- export declare function customActionHandler(executor: CustomActionExecutor, id: string, draftQueue: DraftQueue): ActionHandler<CustomActionData, unknown, unknown>;
32
+ export declare function customActionHandler(executor: CustomActionExecutor, id: string, draftQueue: DraftQueue): ActionHandler<CustomActionData, unknown>;
33
33
  export {};
@@ -1,15 +1,12 @@
1
1
  export { DraftQueue, DraftQueueState, DraftAction, ErrorDraftAction, PendingDraftAction, CompletedDraftAction, DraftActionStatus, ProcessActionResult, DraftQueueChangeListener, DraftActionMetadata, DraftQueueEventType, QueueOperation, UpdateQueueOperation, QueueOperationType, } from './DraftQueue';
2
- export { DRAFT_ID_MAPPINGS_SEGMENT, DraftKeyMapping } from './DraftIdMapping';
3
2
  export { DurableDraftQueue, DRAFT_SEGMENT } from './DurableDraftQueue';
4
3
  export { generateUniqueDraftActionId, uuidv4 } from './utils/id';
5
4
  export { DurableDraftStore } from './DurableDraftStore';
6
5
  export { DraftStore } from './DraftStore';
7
6
  export { DraftManager, DraftManagerState, DraftActionOperationType, DraftQueueItem, DraftQueueItemMetadata, } from './DraftManager';
8
- export { ActionHandler, ReplacingActions, DraftIdAndKeyMapping, } from './actionHandlers/ActionHandler';
7
+ export { ActionHandler, ReplacingActions } from './actionHandlers/ActionHandler';
9
8
  export type { CustomActionResult } from './actionHandlers/CustomActionHandler';
10
9
  export { CustomActionResultType, CustomActionExecutor } from './actionHandlers/CustomActionHandler';
11
- export { makeEnvironmentDraftAware } from './makeEnvironmentDraftAware';
12
- export type { DraftAwareEnvironment } from './makeEnvironmentDraftAware';
13
10
  export * from './DraftFetchResponse';
14
11
  export { DraftSynthesisErrorType, DraftSynthesisError, isDraftSynthesisError, } from './DraftSynthesisError';
15
12
  export { transformErrorToDraftSynthesisError } from './DraftFetchResponse';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/lds-drafts",
3
- "version": "1.319.0",
3
+ "version": "1.321.0",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "description": "LDS Drafts",
6
6
  "main": "dist/ldsDrafts.js",
@@ -24,12 +24,12 @@
24
24
  "release:corejar": "yarn build && ../core-build/scripts/core.js --adapter=lds-drafts"
25
25
  },
26
26
  "dependencies": {
27
- "@luvio/engine": "0.156.4",
28
- "@luvio/environments": "0.156.4",
29
- "@salesforce/lds-utils-adapters": "^1.319.0"
27
+ "@luvio/engine": "0.156.5",
28
+ "@luvio/environments": "0.156.5",
29
+ "@salesforce/lds-utils-adapters": "^1.321.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@salesforce/nimbus-plugin-lds": "^1.319.0"
32
+ "@salesforce/nimbus-plugin-lds": "^1.321.0"
33
33
  },
34
34
  "volta": {
35
35
  "extends": "../../package.json"
@@ -1,12 +0,0 @@
1
- import type { DurableStore } from '@luvio/environments';
2
- export interface DraftKeyMapping {
3
- draftKey: string;
4
- canonicalKey: string;
5
- }
6
- export declare const DRAFT_ID_MAPPINGS_SEGMENT = "DRAFT_ID_MAPPINGS";
7
- /**
8
- *
9
- * @param mappingIds (optional) requested mapping ids, if undefined all will be retrieved
10
- */
11
- export declare function getDraftIdMappings(durableStore: DurableStore, mappingIds?: string[]): Promise<DraftKeyMapping[]>;
12
- export declare function clearDraftIdSegment(durableStore: DurableStore): Promise<void>;
@@ -1,7 +0,0 @@
1
- import type { Luvio, ResourceRequest } from '@luvio/engine';
2
- import type { DurableEnvironment, DurableStore } from '@luvio/environments';
3
- import type { ActionHandler, DraftQueue } from './main';
4
- export interface DraftAwareEnvironment extends DurableEnvironment {
5
- softEvict: (key: string) => void;
6
- }
7
- export declare function makeEnvironmentDraftAware(luvio: Luvio, env: DurableEnvironment, durableStore: DurableStore, handlers: ActionHandler<ResourceRequest, any, any>[], draftQueue: DraftQueue): DraftAwareEnvironment;