posipaki 0.6.2 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/xfetch.ts DELETED
@@ -1,103 +0,0 @@
1
- import { ExitMessage, runDispatch } from './util.js';
2
- import type Process from './process.js';
3
- import type { ProcessCtx, Message } from './process.js';
4
-
5
- function isJsonHelper(res : Response) {
6
- const ct = res.headers.get('content-type');
7
- return ct === 'application/json';
8
- }
9
-
10
- export type FetchState<T> = {
11
- code: 'pending' | 'loading' | 'aborted' | 'failed' | 'ok',
12
- data: T | null,
13
- text: string | null,
14
- };
15
- type FetchArgsGet = {
16
- url: URL,
17
- method?: 'GET',
18
- body?: undefined,
19
- };
20
- type FetchArgsSend<T> = {
21
- url: URL,
22
- method?: 'POST' | 'PUT' | 'PATCH',
23
- body: T,
24
- };
25
- export type FetchArgs<T> = FetchArgsGet | FetchArgsSend<T>;
26
-
27
- export type FetchMessage<T> = {
28
- type: 'OK',
29
- data?: T | null,
30
- text?: string | null,
31
- }
32
- | { type: 'ERROR' | 'LOADING' | 'ABORTED' | 'STOP' }
33
- | ExitMessage;
34
-
35
- type FetchGenerator<T, M> = Generator<FetchState<T> | null, void, M>;
36
-
37
- export type FetchProcess<D> = Process<FetchArgs<D>, FetchState<D>, FetchMessage<D>, FetchMessage<D>>;
38
-
39
-
40
- function* xfetch<Type>({ pname, toParent, send } : ProcessCtx<FetchMessage<Type>, FetchMessage<Type>>, { method='GET', url, body } : FetchArgs<Type>) : FetchGenerator<Type, FetchMessage<Type>> {
41
- const state: FetchState<Type> = { code: 'pending', data: null, text: null };
42
- yield state;
43
-
44
- const controller = new AbortController();
45
- const signal = controller.signal;
46
-
47
- const toSelf = send;
48
-
49
- (async function do_request() {
50
- try {
51
- toSelf({ type: 'LOADING'});
52
- const serializedBody = method === 'GET' ? undefined : JSON.stringify(body);
53
- const headers = new Headers({});
54
- if (serializedBody) {
55
- headers.set('content-type', 'application/json');
56
- };
57
- const res: Response = await fetch(url.href, { method, signal, body: serializedBody, headers });
58
- if (isJsonHelper(res)) {
59
- const data = await res.json();
60
- toSelf({ type: 'OK', data });
61
- } else {
62
- const text = await res.text();
63
- toSelf({ type: 'OK', text });
64
- }
65
- } catch (e) {
66
- const isAborted = (e instanceof DOMException && e.name === 'AbortError');
67
- if (isAborted) {
68
- toSelf({ type: 'ABORTED' });
69
- } else {
70
- //console.log('e', e);
71
- toSelf({ type: 'ERROR' });
72
- }
73
- }
74
- })();
75
-
76
- const isDone = ()=> !(state.code === 'pending' || state.code === 'loading')
77
-
78
- yield* runDispatch<FetchMessage<Type>>(pname, (msg : FetchMessage<Type>)=> {
79
- if (msg.type === 'STOP') {
80
- controller.abort();
81
- }
82
- if (msg.type === 'ABORTED') {
83
- toParent(msg);
84
- state.code = 'aborted';
85
- }
86
- if (msg.type === 'ERROR') {
87
- toParent(msg);
88
- state.code = 'failed';
89
- }
90
- if (msg.type === 'LOADING') {
91
- state.code = 'loading';
92
- }
93
-
94
- if (msg.type === 'OK') {
95
- toParent(msg);
96
- state.code = 'ok';
97
- state.data = msg.data || null;
98
- state.text = msg.text || null;
99
- }
100
- }, isDone, true);
101
- }
102
-
103
- export { xfetch };