chore(workflows-sdk, utils): Prevent unnecessary serialization (#13413)
* chore(workflows-sdk, utils): Prevent unnecessary serialization * Create poor-mugs-cheat.md
This commit is contained in:
1 parent
bd571aca82
commit
55a35e4721
5 files changed
+31
-11
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/event-bus-local": patch
|
||||||
|
"@medusajs/utils": patch
|
||||||
|
"@medusajs/workflows-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(workflows-sdk, utils): Prevent unnecessary serialization
|
||||||
@@ -58,6 +58,7 @@ export * from "./object-to-string-path"
|
|||||||
export * from "./omit-deep"
|
export * from "./omit-deep"
|
||||||
export * from "./optional-numeric-serializer"
|
export * from "./optional-numeric-serializer"
|
||||||
export * from "./parse-cors-origins"
|
export * from "./parse-cors-origins"
|
||||||
|
export * from "./parse-stringify-if-necessary"
|
||||||
export * from "./partition-array"
|
export * from "./partition-array"
|
||||||
export * from "./pick-deep"
|
export * from "./pick-deep"
|
||||||
export * from "./pick-value-from-object"
|
export * from "./pick-value-from-object"
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { isDefined } from "./is-defined"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only apply JSON.parse JSON.stringify when we have objects, arrays, dates, etc..
|
||||||
|
* @param result
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export function parseStringifyIfNecessary(result: unknown) {
|
||||||
|
if (typeof result !== "object") {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
const strResult = JSON.stringify(result)
|
||||||
|
if (isDefined(strResult)) {
|
||||||
|
return JSON.parse(strResult)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
deepCopy,
|
deepCopy,
|
||||||
isDefined,
|
|
||||||
OrchestrationUtils,
|
OrchestrationUtils,
|
||||||
|
parseStringifyIfNecessary,
|
||||||
promiseAll,
|
promiseAll,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
@@ -83,8 +83,5 @@ export async function resolveValue(input, transactionContext) {
|
|||||||
? await resolveProperty(copiedInput, transactionContext)
|
? await resolveProperty(copiedInput, transactionContext)
|
||||||
: await unwrapInput(copiedInput, {})
|
: await unwrapInput(copiedInput, {})
|
||||||
|
|
||||||
const strResult = JSON.stringify(result) // Symbols return undefined
|
return parseStringifyIfNecessary(result)
|
||||||
if (isDefined(strResult)) {
|
|
||||||
return JSON.parse(strResult)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -82,16 +82,13 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
|||||||
// This is useful in the event of a distributed transaction where you'd want to emit
|
// This is useful in the event of a distributed transaction where you'd want to emit
|
||||||
// events only once the transaction ends.
|
// events only once the transaction ends.
|
||||||
private async groupOrEmitEvent<T = unknown>(eventData: Message<T>) {
|
private async groupOrEmitEvent<T = unknown>(eventData: Message<T>) {
|
||||||
const eventData_ = JSON.parse(JSON.stringify(eventData))
|
const { options, ...eventBody } = eventData
|
||||||
const { options, ...eventBody } = eventData_
|
|
||||||
const eventGroupId = eventBody.metadata?.eventGroupId
|
const eventGroupId = eventBody.metadata?.eventGroupId
|
||||||
|
|
||||||
if (eventGroupId) {
|
if (eventGroupId) {
|
||||||
await this.groupEvent(eventGroupId, eventData_)
|
await this.groupEvent(eventGroupId, eventData)
|
||||||
} else {
|
} else {
|
||||||
const { options, ...eventBody } = eventData_
|
const options_ = eventData.options as { delay: number }
|
||||||
|
|
||||||
const options_ = options as { delay: number }
|
|
||||||
const delay = (ms?: number) => (ms ? setTimeout(ms) : Promise.resolve())
|
const delay = (ms?: number) => (ms ? setTimeout(ms) : Promise.resolve())
|
||||||
|
|
||||||
delay(options_?.delay).then(() =>
|
delay(options_?.delay).then(() =>
|
||||||
|
|||||||
Reference in new issue
Block a user