Skip to content

Commit 4725ea2

Browse files
authored
Merge pull request #13 from easyops-cn/steve/async
fix(): support resource as context value
2 parents b3852e6 + 6d5929c commit 4725ea2

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

apps/test/src/Pages/About.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
callApi,
3+
createContext,
34
handleHttpError,
45
useContext,
56
useRef,
@@ -8,17 +9,19 @@ import {
89
} from "@next-tsx/core";
910
import { LayoutContext } from "../Contexts/LayoutContext";
1011

12+
const AboutContext = createContext();
13+
1114
export default function About() {
1215
const { handleClick } = useContext(LayoutContext);
1316
const params = useSearchParams();
14-
const [res, refetch] = useResource(
17+
const [aboutInfo, refetch] = useResource(
1518
() => callApi("my.api@getAboutInfo:1.0.0", { id: params.get("id") }),
1619
{ async: true, fallback: null }
1720
);
1821
const modalRef = useRef();
1922

2023
return (
21-
<>
24+
<AboutContext.Provider value={{ aboutInfo, refetch }}>
2225
<div
2326
onMount={handleClick}
2427
onClick={() => {
@@ -31,10 +34,10 @@ export default function About() {
3134
);
3235
}}
3336
>
34-
About {params.get("id")} {res}
37+
About {params.get("id")} {aboutInfo}
3538
</div>
36-
<div>{res ? <span slot="a">1</span> : <span slot="b">2</span>}</div>
39+
<div>{aboutInfo ? <span slot="a">1</span> : <span slot="b">2</span>}</div>
3740
<eo-modal ref={modalRef} />
38-
</>
41+
</AboutContext.Provider>
3942
);
4043
}

packages/builder/bin/builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function buildApp(watchMode) {
6565
for (const err of app.errors) {
6666
console.error(`[${err.severity}] ${err.message}`);
6767
console.error(
68-
` at ${err.filePath}:${err.node?.loc?.start.line}:${err.node?.loc?.start.column}`
68+
` at ${srcDir}${err.filePath}${err.node ? `:${err.node.loc.start.line}:${err.node.loc.start.column}` : ""}`
6969
);
7070
if (
7171
!shouldBailout &&

packages/parser/src/modules/parseContextProvider.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export function parseContextProvider(
9595
const properties = expression.get("properties");
9696
const stateSetters = new Map<string, string>();
9797
const stateGetters = new Map<string, string>();
98+
const refetchList = new Map<string, string>();
9899
const eventCallbacks = new Map<string, BindingInfo>();
99100
for (const prop of properties) {
100101
if (!prop.isObjectProperty()) {
@@ -140,8 +141,12 @@ export function parseContextProvider(
140141
stateSetters.set(value.node.name, binding.id.name);
141142
break;
142143
case "state":
144+
case "resource":
143145
stateGetters.set(value.node.name, binding.id.name);
144146
break;
147+
case "refetch":
148+
refetchList.set(value.node.name, binding.resourceId!.name);
149+
break;
145150
case "eventCallback": {
146151
const params = binding.callback!.get("params");
147152
if (params.length > 0) {
@@ -168,6 +173,7 @@ export function parseContextProvider(
168173
if (
169174
stateSetters.size === 0 &&
170175
stateGetters.size === 0 &&
176+
refetchList.size === 0 &&
171177
eventCallbacks.size === 0
172178
) {
173179
return elements;
@@ -200,6 +206,21 @@ export function parseContextProvider(
200206
},
201207
})
202208
),
209+
...[...refetchList].map<EventHandler>(
210+
([refetchName, stateName]) => ({
211+
action: "conditional",
212+
payload: {
213+
test: `<% EVENT.detail.name === ${JSON.stringify(refetchName)} %>`,
214+
consequent: {
215+
action: "refresh_data_source",
216+
payload: {
217+
name: stateName,
218+
},
219+
},
220+
alternate: null,
221+
},
222+
})
223+
),
203224
...[...eventCallbacks].map<EventHandler>(
204225
([callbackName, binding]) => ({
205226
action: "conditional",

0 commit comments

Comments
 (0)