I finally finished adding event listeners to the Holster API, and decided to stick with GunDB's idea of on and off functions.
It took me longer than I expected because on callbacks are long-lived, so the context chain I was using needed to stick around somehow. I also realised that I couldn't do much else once there was an event listener because there was only one context. Or if there was another request the on callback would lose what it was referring to when called.
This meant that the API need to store each context while it was being used, and only remove them when a callback had finished. This took a few attempts, as holster.get().get() previously made sense, but having another request start a new holster.get() meant the two requests couldn't be distinguished from each other and would overwrite the single context available. The solution ended up being that each holster.get() starts a new context, and to differentiate new requests I've added then() to provide the chaining API.
Now Holster can do queries such as:
holster.get("hello").then("world!").on(console.log)
// Which will get called by this update.
holster.get("hello").then("world!").put("update", console.log)
I figured that was enough API changes to add a new page to the Github Wiki, so I've also added a new API page with more details.
Listening to updates
on
andoff
functions.It took me longer than I expected because
on
callbacks are long-lived, so the context chain I was using needed to stick around somehow. I also realised that I couldn't do much else once there was an event listener because there was only one context. Or if there was another request theon
callback would lose what it was referring to when called.This meant that the API need to store each context while it was being used, and only remove them when a callback had finished. This took a few attempts, as
holster.get().get()
previously made sense, but having another request start a newholster.get()
meant the two requests couldn't be distinguished from each other and would overwrite the single context available. The solution ended up being that eachholster.get()
starts a new context, and to differentiate new requests I've addedthen()
to provide the chaining API.Now Holster can do queries such as: I figured that was enough API changes to add a new page to the Github Wiki, so I've also added a new API page with more details.