【发布时间】:2021-12-29 16:47:12
【问题描述】:
我需要轮询服务器并在客户端更新数据。为此,我有一个调度程序,它调度一个名为FRONT_PAGE 的操作。一旦应用程序启动,该操作就会被调度,并且客户端应该每秒发送两次请求。正在发送请求,但出现以下错误。
×
TypeError: Object(...)(...) is not a function
Observable.pipe
A:src/internal/Observable.ts:439
▶ 2 stack frames were collapsed.
doInnerSub
A:internal/operators/mergeInternals.ts:71
outerNext
A:internal/operators/mergeInternals.ts:53
50 | }
51 | }));
52 | };
> 53 | source.subscribe(new OperatorSubscriber(subscriber, outerNext, function () {
| ^ 54 | isComplete = true;
55 | checkComplete();
56 | }));
代码。
import { from, of, timer } from 'rxjs'
import { ajax } from 'rxjs/ajax'
import { map } from 'rxjs/operators'
import { FRONT_PAGE } from './constants'
const poll_server = url => {
timer(0, 500)
.pipe(from(fetch(url))
.pipe((x) => { console.log("Polling server.."); return x })
.pipe(map(response => response.json())))
}
export const server_dispatch = action => {
switch (action.type) {
case FRONT_PAGE: {
poll_server('http://localhost:8080/warnings')
}
default:
return of(action)
}
}
终点
app.get('/warnings', (req, res) => {
console.log("[GET] /warnings")
const baseline = req.query.baseline ?? -1
if (version > baseline) {
res.send(warnings(alerts.filter(a => a.prediction)))
} else if (game) {
res.status(204).send({})
} else {
res.status(404).send();
}
})
【问题讨论】:
标签: javascript reactjs rxjs observable observer-pattern