I need to poll the server and update data on the client side. For that I have a dispatcher that dispatches an action that is called FRONT_PAGE. The action is dispatched once the app starts and the client should send requests twice a second. The requests are being sent but I am getting the following error.
×
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 | }));
The code.
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)
}
}
the end point
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();
}
})
1. poll_server is a function that has almsot no effects and then returns nothing/void.
For example:
// return arg + 1
const lambdaFunction = arg => arg + 1;
// returns nothing
const lambdaFunction = arg => {arg + 1;};
// return arg + 1
const lambdaFunction = arg => {return arg + 1;};
2. .pipe(from(fetch(url)).pipe(... What are you attempting here?
from not is not a pipeable operator. You can use it to create an observable from a promise, observableLike, or iterator. Not as a composable operator.
3. The following actually works because operators are actually just functions (hens why they can be composed so well!). So the identity function here is fine, but I'm pretty sure it's not doing what you think it is.
.pipe((x) => { console.log("Polling server.."); return x })
console.log("Polling server..") will be run before your observable is subscribed to. In case this, your observable here is never subscribed to (as it's never returned), but I imagine you might see "Polling server.." in the console anyway.
4.
.pipe(x => { console.log("Polling server.."); return x })
.pipe(map(response => response.json())))
is the same as:
.pipe(
x => { console.log("Polling server.."); return x },
map(response => response.json())
)
That's because the chaining applications is the same thing as composition.
5. server_dispatch only return an observable for the default case in the switch. Otherwise it returns undefined. Is that really the behavior you want?