在aor-realtime readme
中提供的示例中import realtimeSaga from 'aor-realtime';
const observeRequest = (fetchType, resource, params) => {
// Use your apollo client methods here or sockets or whatever else including the following very naive polling mechanism
return {
subscribe(observer) {
const intervalId = setInterval(() => {
fetchData(fetchType, resource, params)
.then(results => observer.next(results)) // New data received, notify the observer
.catch(error => observer.error(error)); // Ouch, an error occured, notify the observer
}, 5000);
const subscription = {
unsubscribe() {
// Clean up after ourselves
clearInterval(intervalId);
// Notify the saga that we cleaned up everything
observer.complete();
}
};
return subscription;
},
};
};
const customSaga = realtimeSaga(observeRequest);
提到了 fetchData
函数,但它无法从该范围访问,它只是一个符号/抽象调用吗?
如果我真的想根据一些实时触发器刷新数据,我怎么能从这个范围调度数据获取命令?
答案 0 :(得分:2)
你是对的,这是一个象征/抽象的呼唤。您可以通过使用restClient初始化observeRequest
并使用fetchType
,resource
和params
参数相应地调用客户端方法来实现function foo({a = 'asdf', b = 3.5, c = true} = {}) {
return a + ' ' + b + ' ' + c;
}
console.log(foo());
console.log(foo({a: 'qwerty', b: 123, c: false}));
console.log(foo({b: 567}));
console.log(foo({c: true, a: 'qwerty', b: 789}));
。
我们目前只在aor-simple-graphql-client客户端
中使用此传奇