1

I am looking for a pub/sub mechanism that behaves like a promise but can resolve multiple times, and behaves like an event except if you subscribe after a notification has happened it triggers with the most recent value.

I am aware of notify, but deferred.notify is order-sensitive, so in that way it behaves just like an event. eg:

d.notify('notify before'); // Not observed :(
d.promise.progress(function(data){ console.log(data) });
d.notify('notify after');  // Observed
setTimeout(function(){ d.notify('notify much later') }, 100); // Also Observed

fiddle: http://jsfiddle.net/foLhag3b/

The notification system I'd like is a good fit for a UI component that should update to reflect the state of the data behind it. In these cases, you don't want to care about whether the data has arrived yet or not, and you want updates when they come in.

Maybe this is similar to Immediate mode UIs, but is distinct because it is message based.

The state of the art for message based UI updating, as far as I'm aware, is something which uses a promise or callback to initialize, then also binds an update event:

myUIComponent.gotData(model.data);
model.onUpdate(myUIComponent.gotData); // doing two things is 2x teh workz :(

I don't want to have to do both. I don't think anyone should have to, the use case is common enough to abstract.

model.whenever(myUIComponent.gotData); // yay one intention === one line of code

I could build a mechanism to do what I want, but I wanted to see if a pub/sub mechanism like this already exists. A lot of smart people have done a lot in CS and I figure this concept must exist, I just am looking for the name of it.

To be clear, I'm not looking to change an entire framework, say to Angular or React. I'm looking only for a pub/sub mechanism. Preferably an implementation of a known concept with a snazzy name like notifr or lemme-kno or touch-base.

SimplGy
  • 20,079
  • 15
  • 107
  • 144
  • related: [One time event handling using promises?](http://stackoverflow.com/q/23115272/1048572) – Bergi Apr 30 '15 at 04:22
  • SimplGy, before making major implementation decisions, I think you should at least read about [jQuery Callbacks](https://api.jquery.com/jQuery.Callbacks/). The page actually includes a working example for Pub/Sub. – Roamer-1888 Apr 30 '15 at 08:10
  • @Roamer-1888: I didn't know jquery exposed their callback helper method, and that's interesting. It is just an event management system however, and you have to subscribe *before* publishing in order to receive the message. Avoiding the order of operations problem is important for a modular UI. – SimplGy Apr 30 '15 at 08:33
  • I think that what you want *could* be done with jQuery Callbacks, and it would be an interesting exercise. You would need to establish a "generalised observer" to observe and memorize all topics. On subscription to a topic, as well as registering itself for future updates, a subscriber would be given access to the topic's history to date. – Roamer-1888 Apr 30 '15 at 09:44
  • @Roamer-1888: Yes, I would file this under "implementing it myself". I think the concept of "Streams", particularly observable properties representing the most recent value of a stream, is the existing concept I want to latch on to. I could write my own pub/sub that memorizes most recent responses per channel and pushes them whenever a subscription happens but it's been done better by others, sounds like. – SimplGy Apr 30 '15 at 12:14
  • SimplGy, if streams make the most recent value available, and that is all you want then, yes, they sound like a good way ahead. If you want more history than the most recent value, then it seems you would be into a different territory. – Roamer-1888 Apr 30 '15 at 13:05

1 Answers1

3

You'll want to have a look at (functional) reactive programming. The concept you are looking for is known as a Behavior or Signal in FRP. It models the change of a value over time, and can be inspected at any time (continuously holds a value, in contrast to a stream that discretely fires events).

var ui = state.map(render); // whenever state updates, so does ui with render() result

Some popular libraries in JS are Bacon and Rx, which use their own terminology however: you'll find properties and observables.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • This is an awesome and thoughtful answer, thanks @Bergi. I'll take a look. – SimplGy Apr 30 '15 at 05:16
  • 2
    The wikipedia article wasn't a great intro for me, but this was: http://stackoverflow.com/questions/1028250/what-is-functional-reactive-programming/1030631#1030631 – SimplGy Apr 30 '15 at 05:41