stream-chat 10.0.0-rc.21 → 10.0.0-rc.23
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/cjs/index.browser.js +2196 -3902
- package/dist/cjs/index.browser.js.map +3 -3
- package/dist/cjs/index.node.js +2197 -3902
- package/dist/cjs/index.node.js.map +3 -3
- package/dist/esm/index.mjs +2199 -3905
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/ChannelManager.d.ts +120 -233
- package/dist/types/CooldownTimer.d.ts +8 -23
- package/dist/types/EventHandlerPipeline.d.ts +17 -37
- package/dist/types/LiveLocationManager.d.ts +22 -50
- package/dist/types/api-client.d.ts +9 -14
- package/dist/types/channel.d.ts +253 -448
- package/dist/types/channel_state.d.ts +48 -95
- package/dist/types/client.d.ts +245 -370
- package/dist/types/client_state.d.ts +1 -3
- package/dist/types/configuration/ConfigController.d.ts +30 -124
- package/dist/types/configuration/InstanceConfigurationRegistry.d.ts +31 -123
- package/dist/types/configuration/keys.d.ts +5 -19
- package/dist/types/configuration/shape.d.ts +12 -29
- package/dist/types/configuration/types.d.ts +59 -87
- package/dist/types/configuration/utils/applyInstanceConfiguration.d.ts +13 -35
- package/dist/types/configuration/utils/copyConfigPatch.d.ts +8 -0
- package/dist/types/configuration/utils/declarativeSlices.d.ts +5 -16
- package/dist/types/configuration/utils/deepFreezeConfig.d.ts +3 -17
- package/dist/types/configuration/utils/serverAuthority.d.ts +9 -29
- package/dist/types/connection/ConnectionIdManager.d.ts +18 -40
- package/dist/types/connection/ConnectionRecoveryManager.d.ts +29 -120
- package/dist/types/connection/networkConnection/NetworkConnectionObserver.d.ts +32 -103
- package/dist/types/connection/networkConnection/reporters.d.ts +9 -32
- package/dist/types/connection/networkConnection/types.d.ts +12 -27
- package/dist/types/connection/wsConnection/StableWSConnection.d.ts +37 -116
- package/dist/types/connection/wsConnection/WSConnection.d.ts +41 -104
- package/dist/types/connection/wsConnection/WSConnectionFallback.d.ts +8 -16
- package/dist/types/connection/wsConnection/config.d.ts +8 -28
- package/dist/types/connection/wsConnection/types.d.ts +17 -70
- package/dist/types/entityStore/EntityStore.d.ts +153 -113
- package/dist/types/entityStore/StoreBackedItemIndex.d.ts +13 -46
- package/dist/types/errors.d.ts +7 -19
- package/dist/types/gen/chat/ChannelApi.d.ts +2 -1
- package/dist/types/gen/chat/ChatApi.d.ts +28 -14
- package/dist/types/gen/models/index.d.ts +769 -86
- package/dist/types/gen/moderation/ModerationApi.d.ts +4 -4
- package/dist/types/index.d.ts +2 -4
- package/dist/types/languageNames.d.ts +9 -24
- package/dist/types/logger.d.ts +1 -3
- package/dist/types/messageComposer/LocationComposer.d.ts +1 -1
- package/dist/types/messageComposer/attachmentIdentity.d.ts +1 -6
- package/dist/types/messageComposer/attachmentManager.d.ts +10 -32
- package/dist/types/messageComposer/attachmentUtils.d.ts +3 -14
- package/dist/types/messageComposer/configuration/configuration.d.ts +2 -5
- package/dist/types/messageComposer/configuration/types.d.ts +29 -56
- package/dist/types/messageComposer/linkPreviewsManager.d.ts +6 -9
- package/dist/types/messageComposer/messageComposer.d.ts +30 -164
- package/dist/types/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.d.ts +4 -14
- package/dist/types/messageComposer/middleware/messageComposer/attachments.d.ts +2 -9
- package/dist/types/messageComposer/middleware/pollComposer/types.d.ts +2 -4
- package/dist/types/messageComposer/middleware/pollComposer/validation.d.ts +7 -19
- package/dist/types/messageComposer/middleware/textComposer/commands.d.ts +9 -0
- package/dist/types/messageComposer/middleware/textComposer/mentions.d.ts +11 -18
- package/dist/types/messageComposer/middleware/textComposer/textMiddlewareUtils.d.ts +9 -3
- package/dist/types/messageComposer/middleware/textComposer/types.d.ts +1 -6
- package/dist/types/messageComposer/pollComposer.d.ts +8 -4
- package/dist/types/messageComposer/textComposer.d.ts +3 -9
- package/dist/types/messageComposer/types.d.ts +5 -8
- package/dist/types/messageDelivery/MessageDeliveryReporter.d.ts +25 -79
- package/dist/types/messageDelivery/MessageReceiptsTracker.d.ts +15 -82
- package/dist/types/messageOperations/FailedSendCache.d.ts +4 -9
- package/dist/types/messageOperations/MessageOperationStatePolicy.d.ts +10 -20
- package/dist/types/messageOperations/MessageOperations.d.ts +20 -40
- package/dist/types/messageOperations/applyReactionLocally.d.ts +5 -22
- package/dist/types/messageOperations/createMessageOperations.d.ts +3 -5
- package/dist/types/messageOperations/reflectReactionEvent.d.ts +5 -13
- package/dist/types/messageOperations/sendOrdering.d.ts +4 -7
- package/dist/types/messageOperations/settlePendingAttachmentUploads.d.ts +7 -15
- package/dist/types/messageOperations/types.d.ts +15 -30
- package/dist/types/middleware.d.ts +7 -5
- package/dist/types/moderation.d.ts +21 -14
- package/dist/types/notifications/NotificationManager.d.ts +8 -31
- package/dist/types/notifications/types.d.ts +19 -59
- package/dist/types/offline-support/offline_support_api.d.ts +145 -539
- package/dist/types/offline-support/offline_sync_manager.d.ts +18 -42
- package/dist/types/offline-support/queueableOperations.d.ts +24 -52
- package/dist/types/offline-support/types.d.ts +24 -187
- package/dist/types/pagination/ItemIndex.d.ts +4 -12
- package/dist/types/pagination/cursorDerivation/aroundPaginationFlags.d.ts +13 -0
- package/dist/types/pagination/cursorDerivation/createdAtAroundPaginationFlags.d.ts +2 -4
- package/dist/types/pagination/cursorDerivation/linearPaginationFlags.d.ts +2 -3
- package/dist/types/pagination/paginators/BasePaginator.d.ts +235 -622
- package/dist/types/pagination/paginators/ChannelPaginator.d.ts +67 -125
- package/dist/types/pagination/paginators/MessageIntervalPaginator.d.ts +133 -292
- package/dist/types/pagination/paginators/MessagePaginator.d.ts +69 -118
- package/dist/types/pagination/paginators/PinnedMessagePaginator.d.ts +7 -26
- package/dist/types/pagination/paginators/ReminderPaginator.d.ts +2 -2
- package/dist/types/pagination/paginators/ThreadPaginator.d.ts +15 -23
- package/dist/types/pagination/paginators/UserGroupPaginator.d.ts +2 -6
- package/dist/types/pagination/sortCompiler.d.ts +11 -22
- package/dist/types/pagination/utility.normalization.d.ts +5 -23
- package/dist/types/pagination/utility.search.d.ts +3 -8
- package/dist/types/poll.d.ts +14 -14
- package/dist/types/poll_manager.d.ts +11 -2
- package/dist/types/poll_optimistic_votes.d.ts +10 -14
- package/dist/types/reminders/Reminder.d.ts +4 -4
- package/dist/types/reminders/ReminderManager.d.ts +7 -20
- package/dist/types/search/BaseSearchSource.d.ts +7 -12
- package/dist/types/search/ChannelSearchSource.d.ts +7 -7
- package/dist/types/search/MessageSearchSource.d.ts +7 -9
- package/dist/types/search/SearchController.d.ts +17 -22
- package/dist/types/search/types.d.ts +6 -10
- package/dist/types/signing.d.ts +12 -3
- package/dist/types/thread.d.ts +23 -71
- package/dist/types/thread_manager.d.ts +45 -70
- package/dist/types/token_manager.d.ts +8 -13
- package/dist/types/types.d.ts +64 -125
- package/dist/types/types.utility.d.ts +3 -3
- package/dist/types/upload-utils.d.ts +5 -10
- package/dist/types/uploadManager.d.ts +17 -45
- package/dist/types/utils/FixedSizeQueueCache.d.ts +6 -11
- package/dist/types/utils/WithSubscriptions.d.ts +5 -15
- package/dist/types/utils/concurrency.d.ts +11 -19
- package/dist/types/utils/mergeWith/mergeWith.d.ts +8 -11
- package/dist/types/utils/mergeWith/mergeWithCore.d.ts +5 -13
- package/dist/types/utils/mergeWith/mergeWithDiff.d.ts +22 -0
- package/dist/types/utils/objectPath.d.ts +6 -41
- package/dist/types/utils/retryable.d.ts +8 -12
- package/dist/types/utils/throttling/throttle.d.ts +8 -23
- package/dist/types/utils/time.d.ts +24 -64
- package/dist/types/utils.d.ts +40 -86
- package/package.json +7 -2
- package/src/ChannelManager.ts +159 -300
- package/src/CooldownTimer.ts +11 -29
- package/src/EventHandlerPipeline.ts +21 -44
- package/src/LiveLocationManager.ts +35 -74
- package/src/api-client.ts +43 -61
- package/src/campaign.ts +1 -1
- package/src/channel.ts +396 -749
- package/src/channel_batch_updater.ts +1 -1
- package/src/channel_state.ts +50 -105
- package/src/client.ts +360 -588
- package/src/client_state.ts +1 -6
- package/src/configuration/ConfigController.ts +41 -157
- package/src/configuration/InstanceConfigurationRegistry.ts +42 -171
- package/src/configuration/index.ts +1 -1
- package/src/configuration/keys.ts +13 -32
- package/src/configuration/shape.ts +67 -70
- package/src/configuration/types.ts +60 -100
- package/src/configuration/utils/applyInstanceConfiguration.ts +21 -53
- package/src/configuration/utils/copyConfigPatch.ts +11 -32
- package/src/configuration/utils/declarativeSlices.ts +9 -39
- package/src/configuration/utils/deepFreezeConfig.ts +3 -17
- package/src/configuration/utils/index.ts +2 -4
- package/src/configuration/utils/serverAuthority.ts +20 -68
- package/src/connection/ConnectionIdManager.ts +20 -49
- package/src/connection/ConnectionRecoveryManager.ts +51 -178
- package/src/connection/networkConnection/NetworkConnectionObserver.ts +37 -114
- package/src/connection/networkConnection/reporters.ts +9 -36
- package/src/connection/networkConnection/types.ts +12 -27
- package/src/connection/wsConnection/StableWSConnection.ts +73 -193
- package/src/connection/wsConnection/WSConnection.ts +56 -131
- package/src/connection/wsConnection/WSConnectionFallback.ts +22 -31
- package/src/connection/wsConnection/config.ts +13 -48
- package/src/connection/wsConnection/types.ts +17 -70
- package/src/constants.ts +4 -4
- package/src/entityStore/EntityStore.ts +162 -167
- package/src/entityStore/StoreBackedItemIndex.ts +14 -47
- package/src/errors.ts +7 -19
- package/src/formatMessageMemo.ts +3 -9
- package/src/gen/chat/ChannelApi.ts +19 -0
- package/src/gen/chat/ChatApi.ts +87 -13
- package/src/gen/models/index.ts +793 -71
- package/src/gen/moderation/ModerationApi.ts +0 -4
- package/src/index.ts +10 -14
- package/src/languageNames.ts +9 -24
- package/src/logger.ts +1 -3
- package/src/messageComposer/LocationComposer.ts +7 -23
- package/src/messageComposer/MessageComposerEffectHandlers.ts +1 -2
- package/src/messageComposer/attachmentIdentity.ts +2 -13
- package/src/messageComposer/attachmentManager.ts +17 -63
- package/src/messageComposer/attachmentUtils.ts +3 -17
- package/src/messageComposer/configuration/configuration.ts +5 -8
- package/src/messageComposer/configuration/types.ts +29 -56
- package/src/messageComposer/fileUtils.ts +1 -1
- package/src/messageComposer/linkPreviewsManager.ts +8 -20
- package/src/messageComposer/messageComposer.ts +54 -222
- package/src/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.ts +4 -16
- package/src/messageComposer/middleware/attachmentManager/postUpload/uploadErrorHandler.ts +1 -2
- package/src/messageComposer/middleware/messageComposer/attachments.ts +14 -40
- package/src/messageComposer/middleware/messageComposer/linkPreviews.ts +3 -4
- package/src/messageComposer/middleware/messageComposer/textComposer.ts +1 -1
- package/src/messageComposer/middleware/messageComposer/userDataInjection.ts +4 -11
- package/src/messageComposer/middleware/pollComposer/state.ts +2 -2
- package/src/messageComposer/middleware/pollComposer/types.ts +2 -4
- package/src/messageComposer/middleware/pollComposer/validation.ts +8 -26
- package/src/messageComposer/middleware/textComposer/TextComposerMiddlewareExecutor.ts +1 -6
- package/src/messageComposer/middleware/textComposer/commandUtils.ts +1 -2
- package/src/messageComposer/middleware/textComposer/commands.ts +11 -16
- package/src/messageComposer/middleware/textComposer/mentions.ts +32 -70
- package/src/messageComposer/middleware/textComposer/textMiddlewareUtils.ts +10 -4
- package/src/messageComposer/middleware/textComposer/types.ts +1 -6
- package/src/messageComposer/pollComposer.ts +11 -8
- package/src/messageComposer/textComposer.ts +9 -59
- package/src/messageComposer/types.ts +9 -12
- package/src/messageDelivery/MessageDeliveryReporter.ts +60 -113
- package/src/messageDelivery/MessageReceiptsTracker.ts +21 -105
- package/src/messageOperations/FailedSendCache.ts +4 -9
- package/src/messageOperations/MessageOperationStatePolicy.ts +40 -67
- package/src/messageOperations/MessageOperations.ts +33 -62
- package/src/messageOperations/applyReactionLocally.ts +9 -28
- package/src/messageOperations/createMessageOperations.ts +17 -33
- package/src/messageOperations/reflectReactionEvent.ts +5 -13
- package/src/messageOperations/sendOrdering.ts +4 -7
- package/src/messageOperations/settlePendingAttachmentUploads.ts +10 -21
- package/src/messageOperations/types.ts +15 -30
- package/src/middleware.ts +7 -10
- package/src/moderation.ts +25 -21
- package/src/notifications/NotificationManager.ts +11 -35
- package/src/notifications/types.ts +19 -59
- package/src/offline-support/offline_support_api.ts +178 -608
- package/src/offline-support/offline_sync_manager.ts +25 -69
- package/src/offline-support/queueableOperations.ts +31 -84
- package/src/offline-support/types.ts +25 -188
- package/src/pagination/ItemIndex.ts +4 -12
- package/src/pagination/cursorDerivation/aroundPaginationFlags.ts +29 -0
- package/src/pagination/cursorDerivation/createdAtAroundPaginationFlags.ts +13 -62
- package/src/pagination/cursorDerivation/idAroundPaginationFlags.ts +11 -39
- package/src/pagination/cursorDerivation/linearPaginationFlags.ts +4 -6
- package/src/pagination/filterCompiler.ts +11 -25
- package/src/pagination/paginators/BasePaginator.ts +422 -976
- package/src/pagination/paginators/ChannelPaginator.ts +130 -190
- package/src/pagination/paginators/MessageIntervalPaginator.ts +226 -457
- package/src/pagination/paginators/MessagePaginator.ts +135 -175
- package/src/pagination/paginators/PinnedMessagePaginator.ts +13 -38
- package/src/pagination/paginators/ReminderPaginator.ts +12 -15
- package/src/pagination/paginators/ThreadPaginator.ts +16 -26
- package/src/pagination/paginators/UserGroupPaginator.ts +6 -17
- package/src/pagination/paginators/stateThrottling.ts +5 -10
- package/src/pagination/sortCompiler.ts +23 -50
- package/src/pagination/utility.normalization.ts +8 -30
- package/src/pagination/utility.search.ts +4 -11
- package/src/poll.ts +19 -19
- package/src/poll_manager.ts +16 -20
- package/src/poll_optimistic_votes.ts +17 -22
- package/src/reminders/Reminder.ts +5 -6
- package/src/reminders/ReminderManager.ts +9 -34
- package/src/reminders/ReminderTimer.ts +1 -2
- package/src/search/BaseSearchSource.ts +14 -29
- package/src/search/ChannelSearchSource.ts +8 -8
- package/src/search/MessageSearchSource.ts +19 -25
- package/src/search/SearchController.ts +18 -24
- package/src/search/types.ts +6 -10
- package/src/segment.ts +1 -1
- package/src/signing.ts +23 -3
- package/src/thread.ts +72 -173
- package/src/thread_manager.ts +59 -76
- package/src/token_manager.ts +11 -21
- package/src/types.ts +78 -126
- package/src/types.utility.ts +3 -3
- package/src/upload-utils.ts +5 -10
- package/src/uploadManager.ts +22 -54
- package/src/utils/FixedSizeQueueCache.ts +13 -21
- package/src/utils/WithSubscriptions.ts +5 -15
- package/src/utils/concurrency.ts +16 -55
- package/src/utils/mergeWith/mergeWith.ts +10 -13
- package/src/utils/mergeWith/mergeWithCore.ts +24 -81
- package/src/utils/mergeWith/mergeWithDiff.ts +15 -33
- package/src/utils/objectPath.ts +6 -41
- package/src/utils/retryable.ts +10 -14
- package/src/utils/throttling/throttle.ts +9 -25
- package/src/utils/time.ts +26 -66
- package/src/utils.ts +72 -128
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
import type { Unsubscribe } from '@stream-io/state-store';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Base class for entities that own event subscriptions. `unregisterSubscriptions` is ref-counted:
|
|
5
|
+
* only the last consumer's call tears the subscriptions down.
|
|
5
6
|
*
|
|
6
7
|
* @internal
|
|
7
8
|
*/
|
|
8
9
|
export abstract class WithSubscriptions {
|
|
9
10
|
private unsubscribeFunctions: Set<Unsubscribe> = new Set();
|
|
10
|
-
/**
|
|
11
|
-
* Workaround for the missing TS keyword - ensures that inheritants
|
|
12
|
-
* overriding `unregisterSubscriptions` call the base method and return
|
|
13
|
-
* its unique symbol value.
|
|
14
|
-
*/
|
|
11
|
+
/** Return brand that makes overrides of `unregisterSubscriptions` return the base result. */
|
|
15
12
|
protected static symbol = Symbol(WithSubscriptions.name);
|
|
16
13
|
private refCount = 0;
|
|
17
14
|
|
|
18
15
|
public abstract registerSubscriptions(): void;
|
|
19
16
|
|
|
20
|
-
/**
|
|
21
|
-
* Returns a boolean, provides information of whether `registerSubscriptions`
|
|
22
|
-
* method has already been called for this instance.
|
|
23
|
-
*/
|
|
17
|
+
/** Whether subscriptions are registered. Guards `registerSubscriptions` against running twice. */
|
|
24
18
|
public get hasSubscriptions() {
|
|
25
19
|
return this.unsubscribeFunctions.size > 0;
|
|
26
20
|
}
|
|
@@ -29,16 +23,12 @@ export abstract class WithSubscriptions {
|
|
|
29
23
|
this.unsubscribeFunctions.add(unsubscribeFunction);
|
|
30
24
|
}
|
|
31
25
|
|
|
32
|
-
/**
|
|
33
|
-
* Increments `refCount` by one and returns new value.
|
|
34
|
-
*/
|
|
35
26
|
protected incrementRefCount() {
|
|
36
27
|
return ++this.refCount;
|
|
37
28
|
}
|
|
38
29
|
|
|
39
30
|
/**
|
|
40
|
-
*
|
|
41
|
-
* make sure to run the original too.
|
|
31
|
+
* Overrides must call `super.unregisterSubscriptions()` and return its result.
|
|
42
32
|
*
|
|
43
33
|
* @example
|
|
44
34
|
* ```ts
|
package/src/utils/concurrency.ts
CHANGED
|
@@ -12,32 +12,24 @@ type AsyncWrapper<P extends unknown[], T> = (
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Runs async functions
|
|
16
|
-
* should never run simultaneously: if marked with the same tag, functions
|
|
17
|
-
* will run one after another.
|
|
15
|
+
* Runs async functions with the same tag one after another.
|
|
18
16
|
*
|
|
19
|
-
* @param tag - Async functions with the same tag
|
|
20
|
-
* with different tags can run in parallel.
|
|
17
|
+
* @param tag - Async functions with the same tag run serially. Different tags run in parallel.
|
|
21
18
|
* @param cb - Async function to run.
|
|
22
|
-
* @returns
|
|
19
|
+
* @returns A promise that resolves with the result of `cb`.
|
|
23
20
|
*/
|
|
24
21
|
export const withoutConcurrency = createRunner(wrapWithContinuationTracking);
|
|
25
22
|
|
|
26
23
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* each other (e.g. enabling and disabling camera).
|
|
24
|
+
* Like {@link withoutConcurrency}, but scheduling a new action with the same tag cancels the
|
|
25
|
+
* earlier ones.
|
|
30
26
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* @
|
|
36
|
-
*
|
|
37
|
-
* @param cb - Async function to run. Receives AbortSignal as the only argument.
|
|
38
|
-
* @returns Promise that resolves when async functions returns. If the function didn't
|
|
39
|
-
* start and was canceled, will resolve with 'canceled'. If the function started to run,
|
|
40
|
-
* it's up to the function to decide how to react to cancelation.
|
|
27
|
+
* @param tag - Async functions with the same tag run serially, and are canceled when a new action
|
|
28
|
+
* with the same tag is scheduled.
|
|
29
|
+
* @param cb - Async function to run. Receives an `AbortSignal` as the only argument, aborted when
|
|
30
|
+
* the running action is canceled; `cb` decides how to react.
|
|
31
|
+
* @returns A promise that resolves with the result of `cb`, or with `'canceled'` when the action
|
|
32
|
+
* was canceled before it started.
|
|
41
33
|
*/
|
|
42
34
|
export const withCancellation = createRunner(wrapWithCancellation);
|
|
43
35
|
|
|
@@ -52,15 +44,8 @@ export async function settled(tag: string | symbol) {
|
|
|
52
44
|
}
|
|
53
45
|
|
|
54
46
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* Before running, async function is "wrapped" using the provided wrapper. This wrapper
|
|
59
|
-
* can add additional steps to run before or after the function.
|
|
60
|
-
*
|
|
61
|
-
* When async function is scheduled to run, the previous function is notified
|
|
62
|
-
* by calling the associated onContinued callback. This behavior of this callback
|
|
63
|
-
* is defined by the wrapper.
|
|
47
|
+
* Chains functions with the same tag serially. `wrapper` adds steps around each function and
|
|
48
|
+
* defines its `onContinued`, which runs on the previous function when a successor is scheduled.
|
|
64
49
|
*/
|
|
65
50
|
function createRunner<P extends unknown[], T>(wrapper: AsyncWrapper<P, T>) {
|
|
66
51
|
return function run(tag: string | symbol, cb: (...args: P) => Promise<T>) {
|
|
@@ -73,11 +58,7 @@ function createRunner<P extends unknown[], T>(wrapper: AsyncWrapper<P, T>) {
|
|
|
73
58
|
};
|
|
74
59
|
}
|
|
75
60
|
|
|
76
|
-
/**
|
|
77
|
-
* Wraps an async function with an additional step run after the function:
|
|
78
|
-
* if the function is the last in the queue, it cleans up the whole chain
|
|
79
|
-
* of promises after finishing.
|
|
80
|
-
*/
|
|
61
|
+
/** The last function in the queue cleans up the whole promise chain when it finishes. */
|
|
81
62
|
function wrapWithContinuationTracking<T>(tag: string | symbol, cb: () => Promise<T>) {
|
|
82
63
|
let hasContinuation = false;
|
|
83
64
|
const wrapped = () =>
|
|
@@ -91,28 +72,8 @@ function wrapWithContinuationTracking<T>(tag: string | symbol, cb: () => Promise
|
|
|
91
72
|
}
|
|
92
73
|
|
|
93
74
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* as an argument. When a new function is scheduled to run after the current
|
|
97
|
-
* one, current signal is aborted.
|
|
98
|
-
* 2. If current function didn't start and was aborted, in will never start.
|
|
99
|
-
* 3. If the function is the last in the queue, it cleans up the whole chain
|
|
100
|
-
* of promises after finishing.
|
|
101
|
-
*
|
|
102
|
-
* The cb is passed the AbortController instance for a given execution.
|
|
103
|
-
* The cb should implement own cancellation logic to reflect that the given AbortController has been aborted.
|
|
104
|
-
*
|
|
105
|
-
* ```
|
|
106
|
-
* const cb = async (signal: AbortSignal) => {
|
|
107
|
-
* await new Promise(resolve => setTimeout(resolve, 50));
|
|
108
|
-
* if (signal.aborted) {
|
|
109
|
-
* abortedSignals.push(signal);
|
|
110
|
-
* return 'canceled';
|
|
111
|
-
* }
|
|
112
|
-
* return 1;
|
|
113
|
-
* };
|
|
114
|
-
* const result = withCancellation('tag-x', cb); // the result variable may acquire value "canceled" or 1
|
|
115
|
-
* ```
|
|
75
|
+
* Passes each function an abort signal, aborted when a successor is scheduled. A function aborted
|
|
76
|
+
* before it starts never runs. The last function in the queue cleans up the promise chain.
|
|
116
77
|
*/
|
|
117
78
|
function wrapWithCancellation<T>(
|
|
118
79
|
tag: string | symbol,
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
+
import type { MergeWithCustomizer } from './mergeWithCore';
|
|
2
|
+
import { createMergeCore } from './mergeWithCore';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* properties. If `customizer` returns `undefined` merging is handled by the
|
|
5
|
-
* method instead. The `customizer` is invoked with seven arguments:
|
|
5
|
+
* Like lodash `_.merge`, but `customizer` produces the merged value of each destination and source
|
|
6
|
+
* property. When it returns `undefined` the default merge applies. It is called with
|
|
6
7
|
* (objValue, srcValue, key, object, source, stack).
|
|
7
8
|
*
|
|
8
|
-
* @
|
|
9
|
-
* @param
|
|
10
|
-
* @param source - A single source object or an array of objects to be merged into the .
|
|
9
|
+
* @param target - The destination object. It is not mutated.
|
|
10
|
+
* @param source - A single source object or an array of objects to be merged into `target`.
|
|
11
11
|
* @param customizer - The function to customize assigned values.
|
|
12
|
-
* @returns
|
|
12
|
+
* @returns A new object with the merged result.
|
|
13
13
|
* @example
|
|
14
14
|
*
|
|
15
15
|
* function customizer(objValue, srcValue) {
|
|
16
|
-
* if (
|
|
16
|
+
* if (Array.isArray(objValue)) {
|
|
17
17
|
* return objValue.concat(srcValue);
|
|
18
18
|
* }
|
|
19
19
|
* }
|
|
@@ -28,12 +28,9 @@
|
|
|
28
28
|
* 'vegetables': ['carrot']
|
|
29
29
|
* };
|
|
30
30
|
*
|
|
31
|
-
*
|
|
31
|
+
* mergeWith(object, other, customizer);
|
|
32
32
|
* // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }
|
|
33
33
|
*/
|
|
34
|
-
import type { MergeWithCustomizer } from './mergeWithCore';
|
|
35
|
-
import { createMergeCore } from './mergeWithCore';
|
|
36
|
-
|
|
37
34
|
export function mergeWith<T extends object>(
|
|
38
35
|
target: T,
|
|
39
36
|
source: object | object[],
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Core utility functions and types for mergeWith functionality.
|
|
3
|
-
* This file contains shared logic used by both mergeWith and mergeWithDiff functions.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
1
|
export type MergeWithCustomizer<T extends object> = (
|
|
7
2
|
objValue: unknown,
|
|
8
3
|
srcValue: unknown,
|
|
@@ -28,19 +23,15 @@ export interface DiffNode {
|
|
|
28
23
|
oldValue?: unknown;
|
|
29
24
|
}
|
|
30
25
|
|
|
26
|
+
/** True for objects of a class other than `Object`. Arrays and plain objects are not. */
|
|
31
27
|
export const isClassInstance = (value: unknown): boolean => {
|
|
32
28
|
if (!value || typeof value !== 'object') return false;
|
|
33
29
|
|
|
34
|
-
// Arrays are not class instances
|
|
35
30
|
if (Array.isArray(value)) return false;
|
|
36
31
|
|
|
37
|
-
// Get the prototype chain
|
|
38
32
|
const proto = Object.getPrototypeOf(value);
|
|
39
|
-
|
|
40
|
-
// If it's null or Object.prototype, it's a plain object
|
|
41
33
|
if (proto === null || proto === Object.prototype) return false;
|
|
42
34
|
|
|
43
|
-
// Check if it has a constructor that's not Object
|
|
44
35
|
return value.constructor && value.constructor !== Object;
|
|
45
36
|
};
|
|
46
37
|
|
|
@@ -55,18 +46,15 @@ function memoHasOrAdd(memo: PairMemo, a: object, b: object): boolean {
|
|
|
55
46
|
}
|
|
56
47
|
|
|
57
48
|
/**
|
|
58
|
-
* Deep
|
|
59
|
-
*
|
|
60
|
-
* - Dates/RegExps compared by value
|
|
61
|
-
* - "Class instances" are treated atomically (unequal unless ===)
|
|
62
|
-
* - NaN equals NaN; -0 equals 0 (same as ===)
|
|
49
|
+
* Deep equality that is cycle-safe and covers symbol keys. Dates and RegExps compare by value, Sets
|
|
50
|
+
* and Maps by content, other class instances only by `===`. `NaN` equals `NaN`, `-0` equals `0`.
|
|
63
51
|
*/
|
|
64
52
|
export const isEqual = (
|
|
65
53
|
value1: unknown,
|
|
66
54
|
value2: unknown,
|
|
67
55
|
pairMemo: PairMemo = new WeakMap(),
|
|
68
56
|
): boolean => {
|
|
69
|
-
if (value1 === value2) return true;
|
|
57
|
+
if (value1 === value2) return true;
|
|
70
58
|
if (value1 == null || value2 == null) return false;
|
|
71
59
|
|
|
72
60
|
const t1 = typeof value1;
|
|
@@ -74,21 +62,18 @@ export const isEqual = (
|
|
|
74
62
|
if (t1 !== t2) return false;
|
|
75
63
|
|
|
76
64
|
if (t1 !== 'object') {
|
|
77
|
-
// NaN handling
|
|
78
65
|
// eslint-disable-next-line no-self-compare
|
|
79
66
|
return value1 !== value1 && value2 !== value2 ? true : value1 === value2;
|
|
80
67
|
}
|
|
81
68
|
|
|
82
|
-
// Objects
|
|
83
69
|
const o1 = value1 as object;
|
|
84
70
|
const o2 = value2 as object;
|
|
85
71
|
|
|
86
|
-
// Fast path for tag mismatch
|
|
87
72
|
const tag1 = Object.prototype.toString.call(o1);
|
|
88
73
|
const tag2 = Object.prototype.toString.call(o2);
|
|
89
74
|
if (tag1 !== tag2) return false;
|
|
90
75
|
|
|
91
|
-
//
|
|
76
|
+
// Dates and RegExps are class instances: compare them before that test.
|
|
92
77
|
if (o1 instanceof Date && o2 instanceof Date) {
|
|
93
78
|
return (o1 as Date).getTime() === (o2 as Date).getTime();
|
|
94
79
|
}
|
|
@@ -98,11 +83,7 @@ export const isEqual = (
|
|
|
98
83
|
return r1.source === r2.source && r1.flags === r2.flags;
|
|
99
84
|
}
|
|
100
85
|
|
|
101
|
-
//
|
|
102
|
-
// Two sets are equal if they have the same size and
|
|
103
|
-
// every value in one has an equivalent value in the
|
|
104
|
-
// other (using deep equality).
|
|
105
|
-
// Cannot use the same item for multiple matches in another set.
|
|
86
|
+
// Sets are equal when every value matches a distinct deep-equal value in the other.
|
|
106
87
|
if (value1 instanceof Set && value2 instanceof Set) {
|
|
107
88
|
if (value1.size !== value2.size) return false;
|
|
108
89
|
if (memoHasOrAdd(pairMemo, value1, value2)) return true;
|
|
@@ -113,7 +94,7 @@ export const isEqual = (
|
|
|
113
94
|
let matched = false;
|
|
114
95
|
for (const v2 of unmatched) {
|
|
115
96
|
if (isEqual(v1, v2, pairMemo)) {
|
|
116
|
-
unmatched.delete(v2);
|
|
97
|
+
unmatched.delete(v2);
|
|
117
98
|
matched = true;
|
|
118
99
|
break;
|
|
119
100
|
}
|
|
@@ -124,13 +105,12 @@ export const isEqual = (
|
|
|
124
105
|
return unmatched.size === 0;
|
|
125
106
|
}
|
|
126
107
|
|
|
127
|
-
// Handle Map comparison
|
|
128
108
|
if (value1 instanceof Map && value2 instanceof Map) {
|
|
129
109
|
if (value1.size !== value2.size) return false;
|
|
130
110
|
|
|
131
111
|
if (memoHasOrAdd(pairMemo, value1, value2)) return true;
|
|
132
112
|
|
|
133
|
-
const unmatched = new Set(value2);
|
|
113
|
+
const unmatched = new Set(value2);
|
|
134
114
|
|
|
135
115
|
for (const [k1, v1] of value1) {
|
|
136
116
|
let matchedEntry: [unknown, unknown] | null = null;
|
|
@@ -143,20 +123,19 @@ export const isEqual = (
|
|
|
143
123
|
}
|
|
144
124
|
}
|
|
145
125
|
|
|
146
|
-
if (!matchedEntry) return false;
|
|
147
|
-
unmatched.delete(matchedEntry);
|
|
126
|
+
if (!matchedEntry) return false;
|
|
127
|
+
unmatched.delete(matchedEntry);
|
|
148
128
|
}
|
|
149
129
|
|
|
150
130
|
return unmatched.size === 0;
|
|
151
131
|
}
|
|
152
132
|
|
|
153
|
-
// Treat non-plain instances atomically (your current rule)
|
|
154
133
|
if (isClassInstance(o1) || isClassInstance(o2)) return false;
|
|
155
134
|
|
|
156
|
-
// Cycle guard
|
|
135
|
+
// Cycle guard: a pair already being compared counts as equal.
|
|
157
136
|
if (memoHasOrAdd(pairMemo, o1, o2)) return true;
|
|
158
137
|
|
|
159
|
-
//
|
|
138
|
+
// A hole differs from `undefined`.
|
|
160
139
|
if (Array.isArray(o1)) {
|
|
161
140
|
const a1 = value1 as unknown[],
|
|
162
141
|
a2 = value2 as unknown[];
|
|
@@ -167,7 +146,6 @@ export const isEqual = (
|
|
|
167
146
|
if (has1 !== has2) return false;
|
|
168
147
|
if (has1 && !isEqual(a1[i], a2[i], pairMemo)) return false;
|
|
169
148
|
}
|
|
170
|
-
// Compare enumerable non-index props as well (to align with objects)
|
|
171
149
|
const extraKeys1 = Reflect.ownKeys(o1)
|
|
172
150
|
.filter((k) => typeof k !== 'string' || isNaN(+k))
|
|
173
151
|
.filter((k) => Object.prototype.propertyIsEnumerable.call(o1, k));
|
|
@@ -183,7 +161,6 @@ export const isEqual = (
|
|
|
183
161
|
return true;
|
|
184
162
|
}
|
|
185
163
|
|
|
186
|
-
// Plain objects (string + symbol enumerable own keys)
|
|
187
164
|
const keys1 = Reflect.ownKeys(o1).filter((k) =>
|
|
188
165
|
Object.prototype.propertyIsEnumerable.call(o1, k),
|
|
189
166
|
);
|
|
@@ -192,7 +169,7 @@ export const isEqual = (
|
|
|
192
169
|
);
|
|
193
170
|
if (keys1.length !== keys2.length) return false;
|
|
194
171
|
|
|
195
|
-
//
|
|
172
|
+
// Same prototype, so `{}` differs from `Object.create(null)`.
|
|
196
173
|
if (Object.getPrototypeOf(o1) !== Object.getPrototypeOf(o2)) return false;
|
|
197
174
|
|
|
198
175
|
for (const k of keys1) {
|
|
@@ -203,42 +180,27 @@ export const isEqual = (
|
|
|
203
180
|
return true;
|
|
204
181
|
};
|
|
205
182
|
|
|
206
|
-
/**
|
|
207
|
-
* Generates a diff between original and modified objects.
|
|
208
|
-
* This is used after the merge operation to track what has changed.
|
|
209
|
-
*/
|
|
183
|
+
/** Diff of the keys added or updated in `modified`, or `null` when there are none. */
|
|
210
184
|
export function generateDiff(original: unknown, modified: unknown): DiffNode | null {
|
|
211
|
-
// Root diff node
|
|
212
185
|
const diffRoot: DiffNode = { children: {} };
|
|
213
186
|
|
|
214
|
-
// Compare the objects and build the diff tree
|
|
215
187
|
compareAndBuildDiff(original, modified, diffRoot);
|
|
216
188
|
|
|
217
|
-
// Clean up the diff tree (remove empty nodes)
|
|
218
189
|
return cleanupDiffTree(diffRoot);
|
|
219
190
|
}
|
|
220
191
|
|
|
221
|
-
/**
|
|
222
|
-
* Helper function to compare and build the diff tree
|
|
223
|
-
*/
|
|
224
192
|
function compareAndBuildDiff(
|
|
225
193
|
original: unknown,
|
|
226
194
|
modified: unknown,
|
|
227
195
|
parentDiffNode: DiffNode,
|
|
228
196
|
key?: string | symbol,
|
|
229
197
|
pairMemo: PairMemo = new WeakMap(),
|
|
230
|
-
/**
|
|
231
|
-
* Tracks individual objects that are being processed in the current traversal path
|
|
232
|
-
* - It's used to detect when we encounter the same object multiple times in a single traversal path
|
|
233
|
-
* - This helps identify self-referential or circular structures within a single object (e.g., when an object references itself)
|
|
234
|
-
* - When an object appears in `objectStack` again, we know it's a circular reference within the same object
|
|
235
|
-
*/
|
|
198
|
+
/** Objects on the current traversal path, to detect an object that references itself. */
|
|
236
199
|
objectStack = new Set<unknown>(),
|
|
237
200
|
): void {
|
|
238
|
-
// If values are equal, no diff to record
|
|
239
201
|
if (isEqual(original, modified)) return;
|
|
240
202
|
|
|
241
|
-
//
|
|
203
|
+
// A null/undefined original counts as an addition.
|
|
242
204
|
if (original === undefined || original === null) {
|
|
243
205
|
if (key !== undefined) {
|
|
244
206
|
parentDiffNode.children[String(key)] = {
|
|
@@ -250,7 +212,6 @@ function compareAndBuildDiff(
|
|
|
250
212
|
return;
|
|
251
213
|
}
|
|
252
214
|
|
|
253
|
-
// Check for circular references in objects
|
|
254
215
|
if (typeof original === 'object' && original !== null) {
|
|
255
216
|
if (objectStack.has(original)) {
|
|
256
217
|
if (key !== undefined) {
|
|
@@ -266,7 +227,6 @@ function compareAndBuildDiff(
|
|
|
266
227
|
objectStack.add(original);
|
|
267
228
|
}
|
|
268
229
|
|
|
269
|
-
// Check if we're dealing with non-objects or special object types that should be treated atomically
|
|
270
230
|
const shouldTreatAtomically =
|
|
271
231
|
typeof original !== 'object' ||
|
|
272
232
|
typeof modified !== 'object' ||
|
|
@@ -286,18 +246,15 @@ function compareAndBuildDiff(
|
|
|
286
246
|
};
|
|
287
247
|
}
|
|
288
248
|
|
|
289
|
-
// Remove from object stack if it was added
|
|
290
249
|
if (typeof original === 'object' && original !== null) {
|
|
291
250
|
objectStack.delete(original);
|
|
292
251
|
}
|
|
293
252
|
return;
|
|
294
253
|
}
|
|
295
254
|
|
|
296
|
-
// Handle objects
|
|
297
255
|
const originalObj = original as Record<string | symbol, unknown>;
|
|
298
256
|
const modifiedObj = modified as Record<string | symbol, unknown>;
|
|
299
257
|
|
|
300
|
-
// Create a diff node for this level if we're processing a property
|
|
301
258
|
const currentDiffNode =
|
|
302
259
|
key !== undefined
|
|
303
260
|
? {
|
|
@@ -312,7 +269,7 @@ function compareAndBuildDiff(
|
|
|
312
269
|
parentDiffNode.children[String(key)] = currentDiffNode;
|
|
313
270
|
}
|
|
314
271
|
|
|
315
|
-
// Pairwise cycle check
|
|
272
|
+
// Pairwise cycle check.
|
|
316
273
|
if (
|
|
317
274
|
typeof original === 'object' &&
|
|
318
275
|
original !== null &&
|
|
@@ -320,14 +277,11 @@ function compareAndBuildDiff(
|
|
|
320
277
|
modified !== null
|
|
321
278
|
) {
|
|
322
279
|
if (memoHasOrAdd(pairMemo, original as object, modified as object)) {
|
|
323
|
-
// already visited this exact pair in this diff traversal
|
|
324
|
-
// (prevents infinite recursion), so stop here
|
|
325
280
|
if (typeof original === 'object') objectStack.delete(original);
|
|
326
281
|
return;
|
|
327
282
|
}
|
|
328
283
|
}
|
|
329
284
|
|
|
330
|
-
// Process all keys from both objects
|
|
331
285
|
const allKeys = new Set([
|
|
332
286
|
...Object.keys(originalObj),
|
|
333
287
|
...Object.getOwnPropertySymbols(originalObj),
|
|
@@ -339,13 +293,11 @@ function compareAndBuildDiff(
|
|
|
339
293
|
const originalValue = originalObj[childKey];
|
|
340
294
|
const modifiedValue = modifiedObj[childKey];
|
|
341
295
|
|
|
342
|
-
//
|
|
296
|
+
// Deletions are not tracked.
|
|
343
297
|
if (!(childKey in modifiedObj)) {
|
|
344
|
-
// Currently we don't track deletions, but could be added here if needed
|
|
345
298
|
continue;
|
|
346
299
|
}
|
|
347
300
|
|
|
348
|
-
// Handle added properties (they exist in modified but not in original)
|
|
349
301
|
if (!(childKey in originalObj)) {
|
|
350
302
|
currentDiffNode.children[String(childKey)] = {
|
|
351
303
|
type: 'added',
|
|
@@ -355,7 +307,6 @@ function compareAndBuildDiff(
|
|
|
355
307
|
continue;
|
|
356
308
|
}
|
|
357
309
|
|
|
358
|
-
// Process properties that exist in both but may have changed
|
|
359
310
|
compareAndBuildDiff(
|
|
360
311
|
originalValue,
|
|
361
312
|
modifiedValue,
|
|
@@ -383,7 +334,6 @@ export function createMergeCore<T extends object>(options: { trackDiff?: boolean
|
|
|
383
334
|
}): { result: T; diff: DiffNode | null } {
|
|
384
335
|
const sources = Array.isArray(source) ? source : [source];
|
|
385
336
|
|
|
386
|
-
// Store the original target if we need to track diffs
|
|
387
337
|
const originalTarget = trackDiff ? structuredClone(target) : undefined;
|
|
388
338
|
|
|
389
339
|
function handleCustomizer(
|
|
@@ -422,21 +372,18 @@ export function createMergeCore<T extends object>(options: { trackDiff?: boolean
|
|
|
422
372
|
return srcValue;
|
|
423
373
|
}
|
|
424
374
|
if (targetValue && typeof targetValue === 'object') {
|
|
425
|
-
// Check if it's a class instance (not a plain object)
|
|
426
375
|
const isTargetClassInstance = isClassInstance(targetValue);
|
|
427
376
|
const isSourceClassInstance = isClassInstance(srcValue);
|
|
428
377
|
|
|
429
|
-
//
|
|
378
|
+
// A class instance on either side is not merged: the source one wins, else the target
|
|
379
|
+
// stays.
|
|
430
380
|
if (isTargetClassInstance || isSourceClassInstance) {
|
|
431
|
-
// If source is a class instance, use it
|
|
432
381
|
if (isSourceClassInstance) {
|
|
433
382
|
return srcValue as object;
|
|
434
383
|
}
|
|
435
|
-
// Otherwise preserve the target
|
|
436
384
|
return targetValue;
|
|
437
385
|
}
|
|
438
386
|
|
|
439
|
-
// For plain objects, use normal merging
|
|
440
387
|
return Array.isArray(targetValue) ? [...targetValue] : { ...targetValue };
|
|
441
388
|
}
|
|
442
389
|
return Array.isArray(srcValue) ? [] : {};
|
|
@@ -501,10 +448,8 @@ export function createMergeCore<T extends object>(options: { trackDiff?: boolean
|
|
|
501
448
|
pendingMerges: PendingMerge[],
|
|
502
449
|
): void {
|
|
503
450
|
if (stack.has(source)) {
|
|
504
|
-
//
|
|
505
|
-
//
|
|
506
|
-
|
|
507
|
-
// If we're tracking diffs, we need to mark this as a circular reference
|
|
451
|
+
// Circular reference in the source: skip the merge. With diff tracking the target is re-set
|
|
452
|
+
// on the parent.
|
|
508
453
|
if (trackDiff && sourceKey && parentTarget) {
|
|
509
454
|
Object.defineProperty(parentTarget, sourceKey, {
|
|
510
455
|
value: target,
|
|
@@ -526,7 +471,7 @@ export function createMergeCore<T extends object>(options: { trackDiff?: boolean
|
|
|
526
471
|
}
|
|
527
472
|
|
|
528
473
|
function baseMerge(object: T, source: object, stack = new Set<unknown>()): T {
|
|
529
|
-
//
|
|
474
|
+
// Prevents infinite recursion.
|
|
530
475
|
if (stack.has(object) || stack.has(source)) {
|
|
531
476
|
return { ...object };
|
|
532
477
|
}
|
|
@@ -553,7 +498,6 @@ export function createMergeCore<T extends object>(options: { trackDiff?: boolean
|
|
|
553
498
|
...target,
|
|
554
499
|
} as T);
|
|
555
500
|
|
|
556
|
-
// If diff tracking is enabled, generate the diff after the merge is complete
|
|
557
501
|
const diff =
|
|
558
502
|
trackDiff && originalTarget ? generateDiff(originalTarget, result) : null;
|
|
559
503
|
|
|
@@ -561,7 +505,7 @@ export function createMergeCore<T extends object>(options: { trackDiff?: boolean
|
|
|
561
505
|
};
|
|
562
506
|
}
|
|
563
507
|
|
|
564
|
-
|
|
508
|
+
/** Prunes nodes that have no type and no children. Returns `null` for an empty tree. */
|
|
565
509
|
export function cleanupDiffTree(diffNode: DiffNode): DiffNode | null {
|
|
566
510
|
const cleanChildren: Record<string | symbol, DiffNode> = {};
|
|
567
511
|
let hasChildren = false;
|
|
@@ -574,7 +518,6 @@ export function cleanupDiffTree(diffNode: DiffNode): DiffNode | null {
|
|
|
574
518
|
}
|
|
575
519
|
}
|
|
576
520
|
|
|
577
|
-
// If this node has a type (added/updated) or has children, keep it
|
|
578
521
|
if (diffNode.type || hasChildren) {
|
|
579
522
|
return {
|
|
580
523
|
...diffNode,
|
|
@@ -1,44 +1,28 @@
|
|
|
1
|
+
import { cleanupDiffTree, createMergeCore } from './mergeWithCore';
|
|
2
|
+
import type { DiffNode, MergeWithCustomizer } from './mergeWithCore';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
5
|
+
* Like `mergeWith`, and also returns a hierarchical diff of the keys that were added or
|
|
6
|
+
* updated.
|
|
4
7
|
*
|
|
5
|
-
* @
|
|
6
|
-
* @param
|
|
7
|
-
* @param source - A single source object or an array of objects to be merged into the object.
|
|
8
|
+
* @param target - The destination object. It is not mutated.
|
|
9
|
+
* @param source - A single source object or an array of objects to be merged into `target`.
|
|
8
10
|
* @param customizer - The function to customize assigned values.
|
|
9
|
-
* @returns
|
|
11
|
+
* @returns The merged result and a hierarchical diff object.
|
|
10
12
|
* @example
|
|
11
13
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* return objValue.concat(srcValue);
|
|
15
|
-
* }
|
|
16
|
-
* }
|
|
17
|
-
*
|
|
18
|
-
* var object = {
|
|
19
|
-
* 'fruits': ['apple'],
|
|
20
|
-
* 'vegetables': ['beet']
|
|
21
|
-
* };
|
|
22
|
-
*
|
|
23
|
-
* var other = {
|
|
24
|
-
* 'fruits': ['banana'],
|
|
25
|
-
* 'vegetables': ['carrot'],
|
|
26
|
-
* 'grains': ['wheat']
|
|
27
|
-
* };
|
|
28
|
-
*
|
|
29
|
-
* const { result, diff } = mergeWithDiff({ target: object, source: other, customizer });
|
|
30
|
-
* // result => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'], 'grains': ['wheat'] }
|
|
14
|
+
* const { result, diff } = mergeWithDiff({ a: 1, b: { c: 2 } }, { b: { c: 3 }, d: 4 });
|
|
15
|
+
* // result => { a: 1, b: { c: 3 }, d: 4 }
|
|
31
16
|
* // diff => {
|
|
32
17
|
* // children: {
|
|
33
|
-
* //
|
|
34
|
-
* //
|
|
35
|
-
* //
|
|
18
|
+
* // b: {
|
|
19
|
+
* // type: 'updated', value: { c: 3 }, oldValue: { c: 2 },
|
|
20
|
+
* // children: { c: { type: 'updated', value: 3, oldValue: 2, children: {} } }
|
|
21
|
+
* // },
|
|
22
|
+
* // d: { type: 'added', value: 4, children: {} }
|
|
36
23
|
* // }
|
|
37
24
|
* // }
|
|
38
25
|
*/
|
|
39
|
-
import { cleanupDiffTree, createMergeCore } from './mergeWithCore';
|
|
40
|
-
import type { DiffNode, MergeWithCustomizer } from './mergeWithCore';
|
|
41
|
-
|
|
42
26
|
export function mergeWithDiff<T extends object>(
|
|
43
27
|
target: T,
|
|
44
28
|
source: object | object[],
|
|
@@ -47,7 +31,5 @@ export function mergeWithDiff<T extends object>(
|
|
|
47
31
|
const mergeCore = createMergeCore<T>({ trackDiff: true });
|
|
48
32
|
const { result, diff } = mergeCore({ target, source, customizer });
|
|
49
33
|
|
|
50
|
-
// Clean up the diff tree to remove empty nodes
|
|
51
|
-
|
|
52
34
|
return { result, diff: cleanupDiffTree(diff ?? { children: {} }) || { children: {} } };
|
|
53
35
|
}
|