Comments (11)
Actually .once()
was removed in master
branch, and next version probably won't have it.
I still don't sure synchronous sources (.fromArray
and .once
) a good thing to have in an FRP library. The problem is those sources don't fit well into original FRP semantics. This topic was briefly discussed here baconjs/bacon.js#383 (comment) , and Bacon also has an issue about this specific problem baconjs/bacon.js#367 .
For now I try to avoid adding synchronous sources. It would be good to have here some use case examples of how .fromArray()
/ .once()
could be useful.
from kefir.
I find it mostly useful in combination with flatMap. Let's say I have a function that accepts a Stream a single input and can return any number of outputs. If I want to flatMap these, it's trivial with .once()
and .fromArray()
.
stream.map(function (index) {
var options = [null, "single", ["array1", "array2"]];
return options[index];
}).flatMap(function (option) {
if (option === null) {
return Kefir.never();
} else if (typeof option.length !== 'undefined') {
return Kefir.fromArray(option);
} else {
return kefir.once(option);
}
}).log();
Envision inputStream = [2, 0, 1]::
<value> array1
<value> array2
<value> single
<end>
Without kefir.once(), how would you handle a case like this?
from kefir.
You can use .constant
instead of .once
.
But for .fromArray
there is no alternative now. In some cases .sequentially(0, arr)
may be used, but it not quite same thing as .fromArray(arr)
.
I also consider adding support for simple arrays in methods like .flatMap
or .zip
, so you can return just an array from mapping function and all its items will be emitted. But I don't sure if it better than just have .fromArray
method.
Also, although it a bit awkward, you can use .withHandler
when you need .flatMap
with arrays support:
stream.map(function (index) {
var options = [null, "single", ["array1", "array2"]];
return options[index];
}).withHandler(function(emitter, event) {
if (event.type === 'end') {
emitter.end();
}
if (event.type === 'value') {
var option = event.value;
if (option === null) {
// nothing to do here
} else if (typeof option.length !== 'undefined') {
option.forEach(emitter.emit);
} else {
emitter.emit(option);
}
}
}).log();
I hope to write docs for .withHandler soon :)
from kefir.
.withHandler()
may actually work better than .flatMap()
for my purposes. I'll toy around with it.
That's my only real use-case for .fromArray()
so I'll close this out - thank!
from kefir.
surely that withHandler configuration is a common case? i think there is need for this or something like pluckElements
.
use case might be getting a list of change notifications, splitting them up into individual change notifications, then getting the changes outlined in the change notifications
with flatMap
and fromArray
:
changes.pluck('results')
.flatMapLatest(function (results) { Kefir.fromArray(results); })
.pluck('id')
.flatMap(getDoc);
with pluckElements
changes.pluckElements('results')
.pluck('id')
.flatMap(getDoc);
from kefir.
basically any time you get a list as input and want to operate on an stream of its elements currently you have to use the handler function above.
from kefir.
Take a look at the .transform method. I think it is what you need.
Btw .transform
will be renamed to .flatten
in v0.2.10
which I will probably release later today.
from kefir.
oh i see!
Kefir.sequentially(2000, [[1,2,3],[4,5,6],[7,8,9]]).transform(function (xs) { return xs; }).log()
this is even better than using fromArray
with flatMap
. renaming this to flatten is really going to help people find this feature. i'd recommend including an example like the one above in the docs too.
thanks!
from kefir.
Thanks, I'll add example like this.
You can also omit handler function argument, it's function(x) {return x}
by default.
Kefir.sequentially(2000, [[1,2,3],[4,5,6],[7,8,9]]).transform().log()
from kefir.
thanks! i was going to suggest that it should have that behaviour as defauly but then it and it worked lol!
thanks for all the great work. kefir is a really elegant library : )
from kefir.
I came to this repo to find some issue about fromArray
not being available, or to post that issue myself. But I have to say that after reading this discussion and the referenced issue in the bacon library, and after doing some tests around all that, I'm glad this function is not included (or any other synchronous sources for that matter). It forces other developers to dig into the issue and find about the drawbacks of synchronous streams, just like I did. As for a workaround, you can always call sequentially
with a very low interval value, perhaps even zero.
from kefir.
Related Issues (20)
- KefirJS and WebSockets HOT 4
- 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.