Need standaloneflow example #479
Answered
by
xaviergonz
aboyer1013
asked this question in
Q&A
-
I'm struggling with how to correctly use Is this okay to use? Would like an example that is TS friendly. Appreciate the help! import {
_async,
_await,
model,
Model,
modelFlow,
idProp,
standaloneFlow
} from "mobx-keystone";
import axios from "axios";
console.clear();
const fetchData = standaloneFlow("actions/fetchData", function* (target) {
console.log("start standloneflow");
const promise = yield* _await(axios.get("https://catfact.ninja/fact"));
console.log("promise", promise);
yield* _await(promise);
});
@model("RootModel")
class RootModel extends Model({ id: idProp }) {
@modelFlow
fetchCatFacts = _async(function* (this: RootModel) {
// I want to call standaloneflow function "fetchData" within this method and do more stuff after.
const foo = yield* fetchData(this);
// Why is foo undefined?
console.log("foo", foo);
});
}
const root = new RootModel({});
root.fetchCatFacts(); |
Beta Was this translation helpful? Give feedback.
Answered by
xaviergonz
Aug 4, 2022
Replies: 1 comment 1 reply
-
The typings were wrong, I just published v0.69.8 with a fix. Hopefully the following test can give you an insight on how to do it: test("standaloneFlow", async () => {
@model("standaloneFlow/Data")
class DataModel extends Model({ x: prop(0) }) {
@modelFlow
fetchX = _async(function* (this: DataModel, y: number) {
const data = this.x + y
const flowResult = yield* _await(fetchData(this, y))
assert(flowResult, _ as number)
expect(flowResult).toBe(data)
return flowResult
})
}
const fetchData = standaloneFlow("actions/fetchData", function* (target: DataModel, y: number) {
const data = target.x + y
const promiseResult = yield* _await(Promise.resolve(data))
assert(promiseResult, _ as number)
expect(promiseResult).toBe(data)
return promiseResult
})
assert(fetchData, _ as (target: DataModel, y: number) => Promise<number>)
const root = new DataModel({})
const pr = fetchData(root, 3)
assert(pr, _ as Promise<number>)
const r = await pr
assert(r, _ as number)
expect(r).toBe(3)
const pr2 = root.fetchX(4)
assert(pr2, _ as Promise<number>)
const r2 = await pr2
assert(r2, _ as number)
expect(r2).toBe(4)
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
aboyer1013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The typings were wrong, I just published v0.69.8 with a fix.
Hopefully the following test can give you an insight on how to do it: