Skip to content

Commit

Permalink
update: enable manually handle cross resolving context
Browse files Browse the repository at this point in the history
  • Loading branch information
FME849 committed Jun 13, 2024
1 parent 4311cf1 commit 882ecb8
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/core/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class Kernel<
requestId: string;
resolve: (value: unknown) => void;
reject: (error: Error | string) => void;
resolveCallback?: Middleware;
}
> = {};
private channelsMap: Record<ChannelId, ChannelContext<EventType>> =
Expand Down Expand Up @@ -247,7 +248,11 @@ export class Kernel<
await execute(request, middlewares);
}

createCrossResolvingRequest(requestId: string, timeout: number = 1000) {
createCrossResolvingRequest(
requestId: string,
timeout: number = 1000,
resolveCallback?: Middleware,
) {
const resolveId = crypto.randomUUID();

const resolve = async <T>() => {
Expand All @@ -269,6 +274,7 @@ export class Kernel<
clearTimeout(timerId);
delete this.crossResolvingContext[resolveId];
},
resolveCallback,
};
}) as T;
};
Expand Down Expand Up @@ -311,7 +317,7 @@ export class Kernel<
clearInterval(this.timer as never);
}

handleCrossResolvingMiddleware: Middleware = (request, respond) => {
handleCrossResolvingMiddleware: Middleware = async (request, respond) => {
const { resolveId } = request;
if (!resolveId) {
respond({ error: 'Can not find resolveId in request' });
Expand All @@ -322,9 +328,13 @@ export class Kernel<
if (!resolvingContext) {
respond({ error: 'Can not find context for cross-resolving' });
} else {
const { resolve } = resolvingContext;
resolve(request);
respond({ message: 'ok' });
const { resolve, resolveCallback } = resolvingContext;
if (!resolveCallback) {
resolve(request);
respond({ message: 'ok' });
} else {
await resolveCallback(request, respond, resolve);
}
}
};

Expand Down

0 comments on commit 882ecb8

Please sign in to comment.