generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ChannelFetch util to relay fetch requests over the channel,…
… and have URQL use it
- Loading branch information
1 parent
a701631
commit 3b95879
Showing
5 changed files
with
171 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { Channel } from "@storybook/channels"; | ||
|
||
export const FETCH_ABORTED = "ChannelFetch/aborted"; | ||
export const FETCH_REQUEST = "ChannelFetch/request"; | ||
export const FETCH_RESPONSE = "ChannelFetch/response"; | ||
|
||
type ChannelLike = Pick<Channel, "emit" | "on" | "off">; | ||
|
||
const instances = new Map<string, ChannelFetch>(); | ||
|
||
export class ChannelFetch { | ||
channel: ChannelLike; | ||
|
||
abortControllers: Map<string, AbortController>; | ||
|
||
constructor(channel: ChannelLike) { | ||
this.channel = channel; | ||
this.abortControllers = new Map<string, AbortController>(); | ||
|
||
this.channel.on(FETCH_ABORTED, ({ requestId }) => { | ||
this.abortControllers.get(requestId)?.abort(); | ||
this.abortControllers.delete(requestId); | ||
}); | ||
|
||
this.channel.on(FETCH_REQUEST, async ({ requestId, input, init }) => { | ||
const controller = new AbortController(); | ||
this.abortControllers.set(requestId, controller); | ||
|
||
try { | ||
const res = await fetch(input as RequestInfo, { ...init, signal: controller.signal }); | ||
const body = await res.text(); | ||
const headers = Array.from(res.headers as any); | ||
const response = { body, headers, status: res.status, statusText: res.statusText }; | ||
this.channel.emit(FETCH_RESPONSE, { requestId, response }); | ||
} catch (error) { | ||
this.channel.emit(FETCH_RESPONSE, { requestId, error }); | ||
} finally { | ||
this.abortControllers.delete(requestId); | ||
} | ||
}); | ||
} | ||
|
||
static subscribe<T>(key: string, channel: ChannelLike) { | ||
const instance = instances.get(key) || new ChannelFetch(channel); | ||
if (!instances.has(key)) instances.set(key, instance); | ||
return instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useChannel } from "@storybook/manager-api"; | ||
|
||
const FETCH_ABORTED = "ChannelFetch/aborted"; | ||
const FETCH_REQUEST = "ChannelFetch/request"; | ||
const FETCH_RESPONSE = "ChannelFetch/response"; | ||
|
||
type SerializedResponse = { | ||
status: number; | ||
statusText: string; | ||
headers: [string, string][]; | ||
body: string; | ||
}; | ||
|
||
const pendingRequests = new Map< | ||
string, | ||
{ resolve: (value: Response) => void; reject: (reason?: any) => void } | ||
>(); | ||
|
||
export const useChannelFetch: () => typeof fetch = () => { | ||
const emit = useChannel({ | ||
[FETCH_RESPONSE]: ( | ||
data: | ||
| { requestId: string; response: SerializedResponse } | ||
| { requestId: string; error: string } | ||
) => { | ||
const request = pendingRequests.get(data.requestId); | ||
if (!request) return; | ||
|
||
pendingRequests.delete(data.requestId); | ||
if ("error" in data) { | ||
request.reject(new Error(data.error)); | ||
} else { | ||
const { body, headers, status, statusText } = data.response; | ||
const res = new Response(body, { headers, status, statusText }); | ||
request.resolve(res); | ||
} | ||
}, | ||
}); | ||
|
||
return async (input: string | URL | Request, { signal, ...init }: RequestInit = {}) => { | ||
const requestId = Math.random().toString(36).slice(2); | ||
emit(FETCH_REQUEST, { requestId, input, init }); | ||
|
||
signal?.addEventListener("abort", () => emit(FETCH_ABORTED, { requestId })); | ||
|
||
return new Promise((resolve, reject) => { | ||
pendingRequests.set(requestId, { resolve, reject }); | ||
setTimeout(() => { | ||
reject(new Error("Request timed out")); | ||
pendingRequests.delete(requestId); | ||
}, 30000); | ||
}); | ||
}; | ||
}; |