Skip to content

Commit

Permalink
setDurableRequestForEvent, #72
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancook committed Sep 2, 2023
1 parent 4bdfdce commit c5fde45
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/data/durable-request/get-durable-request.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import {getDurableRequestStore} from "./store";
import {DurableEvent} from "../durable-event";
import {DurableEventData} from "../durable-event";
import {ok} from "../../is";

export function getDurableRequest(durableRequestId: string) {
const store = getDurableRequestStore();
return store.get(durableRequestId);
}

export function getDurableRequestIdForEvent(event: DurableEvent) {
export function getDurableRequestIdForEvent(event: DurableEventData) {
ok(event.durableEventId, "Expected durableEventId");
return `${event.type}:request:${event.durableEventId}`;
}

export function getDurableRequestForEvent(event: DurableEvent) {
export function getDurableRequestForEvent(event: DurableEventData) {
return getDurableRequest(
getDurableRequestIdForEvent(event)
);
Expand Down
4 changes: 2 additions & 2 deletions src/data/durable-request/set-durable-request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {v4} from "uuid";
import {DurableRequest, PartialDurableRequest} from "./types";
import {getDurableRequestStore} from "./store";
import {DurableEvent} from "../durable-event";
import {DurableEventData} from "../durable-event";
import {getDurableRequestIdForEvent} from "./get-durable-request";


Expand All @@ -19,7 +19,7 @@ export async function setDurableRequest(data: PartialDurableRequest) {
return durableRequest;
}

export function setDurableRequestForEvent(data: PartialDurableRequest, event: DurableEvent) {
export function setDurableRequestForEvent(data: PartialDurableRequest, event: DurableEventData) {
return setDurableRequest({
...data,
durableRequestId: getDurableRequestIdForEvent(event)
Expand Down
7 changes: 2 additions & 5 deletions src/fetch/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {setDurableRequest, DurableEventData, DurableRequestData} from "../data";
import {setDurableRequest, DurableEventData, DurableRequestData, setDurableRequestForEvent} from "../data";
import {on} from "../events";
import {dispatcher} from "../events/schedule/schedule";
import {defer} from "@virtualstate/promise";
Expand Down Expand Up @@ -110,10 +110,7 @@ export const removeFetchDispatcherFunction = dispatcher(FETCH, async (event, dis
});
const response = await handled;
const durableRequest = await fromRequestResponse(request, response);
await setDurableRequest({
...durableRequest,
durableRequestId: `${event.type}:request:${event.durableEventId}`
});
await setDurableRequestForEvent(durableRequest, event);
} catch (error) {
if (!signal.aborted) {
controller?.abort(error);
Expand Down

0 comments on commit c5fde45

Please sign in to comment.