Skip to content

Middleware support

Compare
Choose a tag to compare
@JonAbrams JonAbrams released this 25 Dec 21:35
· 30 commits to master since this release

Merry X-Mas! The most requested feature of SpaceAce finally lands, and in SpaceAce fashion it's a bit unconventional.

I've been asked many times when SpaceAce would get Middleware support. I looked at how Redux did Middleware, and it of course felt far too user unfriendly for SpaceAce. It took a while, but I think I've come up with a SpaceAcey way to do middleware. It's done by super-charging subscribers.

What is middleware for? What's the easiest way to enable developers to do what's needed, while keeping the library itself as simple as possible? As far as I can tell, Middleware is used for a few things:

  1. Cancelling a change.
  2. Altering a change.
  3. Side effects caused by a change, e.g. logging, triggering an async request, etc.

The third case was already being taken care of by subscribers. With this latest release, subscribers can also cancel a change or alter it.

To cancel a change, have your subscriber call the passed in cancel function. When you do this, no further subscribers will be invoked, and not parent space subscribers will be called either.

To alter a change, have your subscriber return an object that should be used instead. This new object will be turned into a space, inserted at the appropriate spot in the tree, and further subscribers will be called, with the new space instead.

Check out the details in the updated Readme.

Cheers!
-Jon