agzam / spacehammer

Hammerspoon config inspired by Spacemacs
MIT License
548 stars 69 forks source link

Create a small, focused FRP stream library #51

Open jaidetree opened 4 years ago

jaidetree commented 4 years ago

There's quite a lot of async code in this library. Currently, it's managed with callbacks, but we all know how much fun those are, especially if you come from node.

I really like working with FRP streams such as RxJS, Highland JS, and Bacon so creating a more focused version for this project (and other people's fennel projects) could help us model complex interactions very trivially.

Intended interface

(local stream {:require stream})

(-> (stream.fromCallback fs.timer.doAfter 3)
    (stream.map (fn [_] "hello"))
    (stream.subscribe print))

With a few more constructors and some higher-order operations, we can do some really cool things to tie two or more otherwise unrelated async operations together.

For example:

(-> (stream.fromWatcher app-focus-watcher)
    (stream.combine (stream.fromCallback ts.eventtap.new [events.keyup]))
    (stream.map hs.inspect)
    (stream.subscribe print))

This would give us a stream of events where a user switches app focus and presses a key which is typically very complicated to model with callbacks and typically requires a lot of mutable state.

This will likely help improving more complex features like vim mode as well as even opening up greater possibilities for animation.

Animation with RxJS

I've started the research phase, learning about the Observer pattern which seems to be at the core of most stream implementations I've worked with.

If anyone can recommend an existing library or even tutorials on how to implement a library like that, it would be greatly appreciated.

agzam commented 4 years ago

whoa.. that would be awesome! Observables make so much sense since we're dealing with lots of things we can simplify with that pattern. Maybe also take a look at https://github.com/bjornbytes/RxLua