Is there any pattern I can use to handle Event Streams with Fluture? #478
-
I know there are libraries like @most/core, but is seems they don't implement fantasy-land. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
You can use a lazy sequence of Futures to approximate a Stream. You could use, for example, something like https://github.com/francisrstokes/Lazy-Infinite-List, and have a producer that emits Futures, process the stream by mapping, and consuming with const Infinite = require ('lazy-infinite')
const fl = require ('fluture')
const stream = Infinite.of (function*() {
let x = 0
while (true) yield Future.after (300) (x++)
})
// to process items in the stream, we can just map-map:
const processedStream = fl.map (fl.map (x => `This is number ${x}`)) (stream)
// we can also map-chain to process with a side effect or async thing:
const effectStream = fl.map (fl.chain (fl.encase (x => {
console.log (x)
}))) (processedStream)
// and then to consume, we can use go
fl.promise (fl.go (effectStream.gen)) In theory you should also be able to get Stream behaviour by using a List Monad Transformer: const Stream = List.Transformer (Future) But I don't know of any packages that implement the list transformer. 🤷♂️ |
Beta Was this translation helpful? Give feedback.
-
@Avaq Thanks for the quick response. Your advice looks very interesting. I'm not closing the issue for now because it is very fresh. Let's see if there are any other approaches. |
Beta Was this translation helpful? Give feedback.
-
@Avaq I realized that your solution produces a constant stream of data. Could you guide me how to implement a source of real events like button clicks? |
Beta Was this translation helpful? Give feedback.
-
Anyways I found a solution.
Maybe it will help someone. Please note that unsubscribe is not fully implemented. |
Beta Was this translation helpful? Give feedback.
You can use a lazy sequence of Futures to approximate a Stream.
You could use, for example, something like https://github.com/francisrstokes/Lazy-Infinite-List, and have a producer that emits Futures, process the stream by mapping, and consuming with
go
. Like so: