Comments (4)
Kefir observables generally shouldn't be thought of as objects supporting both read and write operations. Think of the Kefir observable as just representing the read side.
I frequently do things like this:
class WebSocketConnection {
constructor(socket) {
this._socket = socket;
this.stream = Kefir.stream(emitter => {
socket.on('data', e => emitter.value(e));
socket.on('end', () => emitter.end());
});
}
write(value) {
this._socket.write(value);
}
close() {
this._socket.close();
}
}
const wsc = new WebSocketConnection(...);
wsc.stream
.filter(value => value.type === 'bar')
.onValue(value => {
console.log('got bar event', value);
console.log('responding');
wsc.write({type: foo, data: value.data + 1});
});
Note a small difference between @mAAdhaTTah's code and mine: his closes the socket when no one is listening. Depending on your use-case, that may be what you want. But if you don't always have listeners (generally in a very idiomatic reactive codebase you set up a bunch of listeners at the start that stick around, but if you're just using Kefir here and there in a more classic object-oriented codebase that may not be the case) and still want to write to the connection at any time, then you may not want to automatically close it in that case and instead want an explicit close method like here.
from kefir.
Loosely speaking:
const socket$ = Kefir.stream(emitter => {
const socket = createSocket();
socket.on('event', e => emitter.value(e));
return () => socket.close();
});
from kefir.
I'm going to close this issue, since it's not a bug, but feel free to ask follow-up questions!
from kefir.
I know this is long closed, but I found that the following code also works and I felt it's too cool not to share.
// I imported fromEvents directly from 'kefir'
let myWsStream = fromEvents(websocket, 'message')
from kefir.
Related Issues (20)
- Only `sampledBy` is past tense
- Current state policies HOT 2
- Idea: Usable as AsycInterator
- Proposal to add `fromProperty` static function HOT 15
- Static Land interop `Observable` definition is wrong HOT 5
- Shouldn't a property only emit on "new values"? Proposing `onChange()` HOT 1
- Missing type for `unplug`
- Maintain TypeScript types? HOT 4
- Stop bundling `symbol-observable`
- TypeError: this._emitValue is not a function HOT 1
- Make subscription available inside callback HOT 8
- A big "Thank You" to Kefir developers HOT 1
- How to import Kefir within Rollup library HOT 3
- groupBy function HOT 8
- flatMapConcat should work when a spawned observable ends synchronously on activation
- Add documentation to "Kefir.pool" as "Rx.Subject equivalent" HOT 2
- Synchronous emit in flush method of debounce produces incorrect result HOT 8
- Is it possible to not activate a staled observable when reactivating flatMapLatest? HOT 8
- Russian docs translate
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kefir.