





















@@ -89,6 +89,41 @@ function resolveGatewayInflightMap(params: { context: GatewayRequestContext; ded
8989return { kind: "ready", inflightMap };
9090}
919192+function resolveGatewayInflightStart(params: {
93+context: GatewayRequestContext;
94+dedupeKey: string;
95+respond: RespondFn;
96+}):
97+| {
98+kind: "ready";
99+inflightMap: Map<string, Promise<InflightResult>>;
100+}
101+| {
102+kind: "handled";
103+done: Promise<void>;
104+} {
105+const inflight = resolveGatewayInflightMap({
106+context: params.context,
107+dedupeKey: params.dedupeKey,
108+});
109+if (inflight.kind === "cached") {
110+params.respond(inflight.cached.ok, inflight.cached.payload, inflight.cached.error, {
111+cached: true,
112+});
113+return { kind: "handled", done: Promise.resolve() };
114+}
115+if (inflight.kind === "inflight") {
116+return {
117+kind: "handled",
118+done: inflight.inflight.then((result) => {
119+const meta = result.meta ? { ...result.meta, cached: true } : { cached: true };
120+params.respond(result.ok, result.payload, result.error, meta);
121+}),
122+};
123+}
124+return { kind: "ready", inflightMap: inflight.inflightMap };
125+}
126+92127async function runGatewayInflightWork(params: {
93128inflightMap: Map<string, Promise<InflightResult>>;
94129dedupeKey: string;
@@ -349,23 +384,12 @@ export const sendHandlers: GatewayRequestHandlers = {
349384};
350385const idem = request.idempotencyKey;
351386const dedupeKey = `message.action:${idem}`;
352-const inflight = resolveGatewayInflightMap({ context, dedupeKey });
353-if (inflight.kind === "cached") {
354-respond(inflight.cached.ok, inflight.cached.payload, inflight.cached.error, {
355-cached: true,
356-});
357-return;
358-}
359-if (inflight.kind === "inflight") {
360-const result = await inflight.inflight;
361-const meta = result.meta ? { ...result.meta, cached: true } : { cached: true };
362-respond(result.ok, result.payload, result.error, meta);
363-return;
364-}
365-if (inflight.kind !== "ready") {
387+const inflightStart = resolveGatewayInflightStart({ context, dedupeKey, respond });
388+if (inflightStart.kind === "handled") {
389+await inflightStart.done;
366390return;
367391}
368-const inflightMap = inflight.inflightMap;
392+const inflightMap = inflightStart.inflightMap;
369393const work = (async (): Promise<InflightResult> => {
370394const resolvedChannel = await resolveRequestedChannel({
371395requestChannel: request.channel,
@@ -480,20 +504,12 @@ export const sendHandlers: GatewayRequestHandlers = {
480504};
481505const idem = request.idempotencyKey;
482506const dedupeKey = `send:${idem}`;
483-const inflight = resolveGatewayInflightMap({ context, dedupeKey });
484-if (inflight.kind === "cached") {
485-respond(inflight.cached.ok, inflight.cached.payload, inflight.cached.error, {
486-cached: true,
487-});
488-return;
489-}
490-if (inflight.kind === "inflight") {
491-const result = await inflight.inflight;
492-const meta = result.meta ? { ...result.meta, cached: true } : { cached: true };
493-respond(result.ok, result.payload, result.error, meta);
507+const inflightStart = resolveGatewayInflightStart({ context, dedupeKey, respond });
508+if (inflightStart.kind === "handled") {
509+await inflightStart.done;
494510return;
495511}
496-const inflightMap = inflight.inflightMap;
512+const inflightMap = inflightStart.inflightMap;
497513const to = normalizeOptionalString(request.to) ?? "";
498514const message = normalizeOptionalString(request.message) ?? "";
499515const mediaUrl = normalizeOptionalString(request.mediaUrl);
@@ -700,23 +716,12 @@ export const sendHandlers: GatewayRequestHandlers = {
700716};
701717const idem = request.idempotencyKey;
702718const dedupeKey = `poll:${idem}`;
703-const inflight = resolveGatewayInflightMap({ context, dedupeKey });
704-if (inflight.kind === "cached") {
705-respond(inflight.cached.ok, inflight.cached.payload, inflight.cached.error, {
706-cached: true,
707-});
708-return;
709-}
710-if (inflight.kind === "inflight") {
711-const result = await inflight.inflight;
712-const meta = result.meta ? { ...result.meta, cached: true } : { cached: true };
713-respond(result.ok, result.payload, result.error, meta);
714-return;
715-}
716-if (inflight.kind !== "ready") {
719+const inflightStart = resolveGatewayInflightStart({ context, dedupeKey, respond });
720+if (inflightStart.kind === "handled") {
721+await inflightStart.done;
717722return;
718723}
719-const inflightMap = inflight.inflightMap;
724+const inflightMap = inflightStart.inflightMap;
720725const work = (async (): Promise<InflightResult> => {
721726const resolvedChannel = await resolveRequestedChannel({
722727requestChannel: request.channel,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。