| Hi, I'm also working on a minimalistic reactive programming concept; https://github.com/azer/attr I have a library called "attr", which mixes pubsub (npm.im/pubsub) and new-prop (npm.im/new-prop) And you simply get; foo = attr(3)
bar = attr().getter(function(){ return foo() + 5 })
foo()
// => 3
bar()
// => 8
foo(5)
bar()
// => 10
They are simply based on a very simple pubsub interface (publish,subscribe). So you can also do; foo = attr(3)
bar = bar()
foo.subscribe(function(update){
bar(update + 5)
})
bar.subscribe(function(update){
update
// => 10
})
foo(5)
The advantage of this concept is, it's really easy to extend and optimize. Here is an example that I use new-list (npm.im/new-list) and new-object new-object (npm.im/new-object) to create some lists and objects, and use subscribe (npm.im/subscribe) module to create one callback for observing all changes: people = newObject({ 'jack': 23, 'smith': 27 })
fruits = newList('apple', 'orange')
foo = attr()
bar = attr()
foo.subscribe(function(update){
bar( update + 5 )
})
subscribe(people, fruits, bar, function(updates){
updates[0].params
// => { add: { John: 21 }, rm: ['smith'] }
updates[1].params
// => 12
})
people.rm('smith')
people('John', 21)
foo(7)
My work is done until here. And here is the part that I'm actually working on: http://github.com/azer/new-reactiveIt's a library for creating your own HTML binding abstractions like AngularJS. But your own that you can share, that can be extended by somebody else or can be mixed with another namespace of abstractions. Which means, if your company is named "foo" and you're using a library called "bar", you can have bindings like; <button foo-play="song" bar-content="i18n.play"></button> |
Just as some extra background, I've read the README with the parent library this comment thread is based on. I've read the linked Stack Overflow question and answer linked from it about reactive programming. I worked my way partly through the 1998 article with the bouncing kids heads. I recently did some work with Knockout for a client. I've had some exposure to reactive programming, but I still haven't had a light bulb moment that demonstrates why this style is better.
With Knockout, I find having a variable automatically update its value interesting. I like the pub/sub concept of having a variable publish its changes to any subscribers. This is useful in many frameworks including Knockout, Backbone, and more. I think in Knockout simply executing an observable inside a function makes that function subscribe to the variable is cute. But just in my opinion, it feels like to much magic and magic is fragile.
But to ask the most pertinent question, can you provide a real-world situation of at least moderate complexity where using a programming model like your "attr" is going to be better than a more traditional way of solving it?
Thanks in advance! I'm looking forward to learning something new.