Software engineer & creator of internet things. Node.js aficionado since 2011, React lover since 2014.
Head of solarwallet.io at SatoshiPay (satoshipay.io).
Hi! Andy here, the author of observable-fns and threads.js.
Just stumbled upon this blog post. Nice job!
About the cancellation: Observables are kind-of cancellable β you can unsubscribe from them.
Observables are "cold" by default which means every new subscriber will get their own fresh instance of this observable, which also means that the code in new Observable(/* ... */) will be run on every .subscribe() call (!). If a subscriber unsubscribes, the upstream subscription is terminated (cancellation) β just that there may be numerous more subscriptions of the same sort.
You can make observables "hot" by using the multicast() function. Then there will only ever be one upstream subscription that all downstream subscribers share. You can unsubscribe, too, just that the single shared upstream subscription will live on until the last subscriber has unsubscribed.
How do I listen to .once('all-unsubscribed') event? Like this one package - p-cancelable.
Otherwise, it is possible to use Subject, as in thread.js? (I wouldn't want to use Worker, as I write to SQLite as well, and it is to supposed to be written from multiple threads.)
Software engineer & creator of internet things. Node.js aficionado since 2011, React lover since 2014.
Head of solarwallet.io at SatoshiPay (satoshipay.io).
How do I listen to .once('all-unsubscribed') event?
functionsubscribeToThings(){returnmulticast(newObservable(observer=>{// <Subscribe to things>constterminateSubscription=()=>{// <-- THIS IS THE "all-unsubscribed" CODE// Remember? multicast() makes that all downstream subscriptions are bundled into one,// so this is the callback function that will be run when this bundled subscription is terminated}returnterminateSubscription}))}subscribeToThings().subscribe(/* ... */)
Otherwise, it is possible to use Subject, as in thread.js?
Software engineer & creator of internet things. Node.js aficionado since 2011, React lover since 2014.
Head of solarwallet.io at SatoshiPay (satoshipay.io).
That doesn't make much sense. Why would you want to emit a new cancellation function for every new value?
You shouldn't have this non-standard custom cancellation approach when there is already a standardized one (observable.subscribe(), subscription.unsubscribe()). Keep in mind that emitting functions is generally considered to be an anti-pattern.
Maybe the observable-fns readme lacks a link to some documentation about observables in generalβ¦ I guess this is one of your first times using observables? (No offense β I'm seriously interested :) )
Software engineer & creator of internet things. Node.js aficionado since 2011, React lover since 2014.
Head of solarwallet.io at SatoshiPay (satoshipay.io).
Software engineer & creator of internet things. Node.js aficionado since 2011, React lover since 2014.
Head of solarwallet.io at SatoshiPay (satoshipay.io).
Hi! Andy here, the author of observable-fns and threads.js.
Just stumbled upon this blog post. Nice job!
About the cancellation: Observables are kind-of cancellable β you can unsubscribe from them.
Observables are "cold" by default which means every new subscriber will get their own fresh instance of this observable, which also means that the code in
new Observable(/* ... */)
will be run on every.subscribe()
call (!). If a subscriber unsubscribes, the upstream subscription is terminated (cancellation) β just that there may be numerous more subscriptions of the same sort.You can make observables "hot" by using the
multicast()
function. Then there will only ever be one upstream subscription that all downstream subscribers share. You can unsubscribe, too, just that the single shared upstream subscription will live on until the last subscriber has unsubscribed.What does that mean for your use case?
multicast()
π.unsubscribe()
acting as cancellationHope that helps π
How do I listen to
.once('all-unsubscribed')
event? Like this one package - p-cancelable.Otherwise, it is possible to use
Subject
, as inthread.js
? (I wouldn't want to use Worker, as I write to SQLite as well, and it is to supposed to be written from multiple threads.)Sure, who would stop you? ;)
Currently, I use the signature,
Will it work?
That doesn't make much sense. Why would you want to emit a new cancellation function for every new value?
You shouldn't have this non-standard custom cancellation approach when there is already a standardized one (
observable.subscribe()
,subscription.unsubscribe()
). Keep in mind that emitting functions is generally considered to be an anti-pattern.Maybe the observable-fns readme lacks a link to some documentation about observables in generalβ¦ I guess this is one of your first times using observables? (No offense β I'm seriously interested :) )
Yes. Only promises and eventemitters.
Should I start with RxJS?
Yeah, maybe that's the best to get started. You should definitely start with RxJS's documentation β it's pretty good!
Which library you then use doesn't really matter too much as their APIs are very similar to almost identical.
Do you actually mean
Yes, my bad! I updated my code sample accordingly.