Skip to content

Commit 9f798c1

Browse files
committed
refactor: move machine in its own file
1 parent 5a1ee49 commit 9f798c1

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed

src/lib/machine.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { FSMMachineConfig } from "@cicerchie/fsm";
2+
3+
import { cache } from "./cache";
4+
import { decreaseFetching, increaseFetching } from "./indicator";
5+
6+
const isEnabled = (_, event): boolean => event.data.options.enabled;
7+
8+
export const swrMachine: FSMMachineConfig = {
9+
initial: "init",
10+
on: {
11+
revalidate: {
12+
target: "revalidating",
13+
cond: isEnabled,
14+
},
15+
},
16+
states: {
17+
init: {},
18+
revalidating: {
19+
entry: (ctx, event) => {
20+
if (cache.has(event.data.key)) {
21+
ctx = { ...ctx, isLoading: false, data: cache.get(event.data.key) };
22+
} else if (event.data.options.initialData) {
23+
const data = event.data.options.initialData();
24+
if (data) {
25+
ctx = { ...ctx, isLoading: false, data };
26+
}
27+
}
28+
return {
29+
...ctx,
30+
isLoading: !(ctx.data || ctx.error),
31+
isFetching: true,
32+
};
33+
},
34+
on: {
35+
revalidate: null,
36+
},
37+
invoke: {
38+
src: (_, event) => {
39+
increaseFetching();
40+
return event.data.fn();
41+
},
42+
onDone: {
43+
target: "success",
44+
action: (ctx, event) => {
45+
cache.set(event.invokeEvent.data.key, event.data);
46+
return { ...ctx, error: undefined, data: event.data };
47+
},
48+
},
49+
onError: {
50+
target: "failure",
51+
action: (ctx, event) => {
52+
return { ...ctx, error: event.data, data: undefined };
53+
},
54+
},
55+
},
56+
exit: (ctx) => {
57+
decreaseFetching();
58+
return { ...ctx, isLoading: false, isFetching: false };
59+
},
60+
},
61+
failure: {},
62+
success: {},
63+
},
64+
};

src/lib/swr.ts

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import type { FSMMachineConfig } from "@cicerchie/fsm";
21
import type { Readable } from "svelte/store";
32
import { newFSM } from "@cicerchie/fsm";
43
import { writable } from "svelte/store";
54

6-
import { cache } from "./cache";
7-
import { decreaseFetching, increaseFetching } from "./indicator";
5+
import { swrMachine } from "./machine";
86

97
interface SWROptions<T> {
108
enabled?: boolean;
@@ -46,66 +44,6 @@ const defaultSWRParams = {
4644
},
4745
};
4846

49-
const isEnabled = (_, event): boolean => event.data.options.enabled;
50-
51-
const swrMachine: FSMMachineConfig = {
52-
initial: "init",
53-
on: {
54-
revalidate: {
55-
target: "revalidating",
56-
cond: isEnabled,
57-
},
58-
},
59-
states: {
60-
init: {},
61-
revalidating: {
62-
entry: (ctx, event) => {
63-
if (cache.has(event.data.key)) {
64-
ctx = { ...ctx, isLoading: false, data: cache.get(event.data.key) };
65-
} else if (event.data.options.initialData) {
66-
const data = event.data.options.initialData();
67-
if (data) {
68-
ctx = { ...ctx, isLoading: false, data };
69-
}
70-
}
71-
return {
72-
...ctx,
73-
isLoading: !(ctx.data || ctx.error),
74-
isFetching: true,
75-
};
76-
},
77-
on: {
78-
revalidate: null,
79-
},
80-
invoke: {
81-
src: (_, event) => {
82-
increaseFetching();
83-
return event.data.fn();
84-
},
85-
onDone: {
86-
target: "success",
87-
action: (ctx, event) => {
88-
cache.set(event.invokeEvent.data.key, event.data);
89-
return { ...ctx, error: undefined, data: event.data };
90-
},
91-
},
92-
onError: {
93-
target: "failure",
94-
action: (ctx, event) => {
95-
return { ...ctx, error: event.data, data: undefined };
96-
},
97-
},
98-
},
99-
exit: (ctx) => {
100-
decreaseFetching();
101-
return { ...ctx, isLoading: false, isFetching: false };
102-
},
103-
},
104-
failure: {},
105-
success: {},
106-
},
107-
};
108-
10947
export function useSWR<T>(): SWRStore<T> {
11048
// const store = writable<SWRStore<T>>({ ...defaultSWRStore }, () => {
11149
// fsm.setEnabled(true);

0 commit comments

Comments
 (0)