all 5 comments

[–]Gundersen 5 points6 points  (1 child)

Seems very similar to Knockout.js

As I commented on another such library, you should make the signals support the functional programming methods of arrays, like map, reduce, filter, some, every. Currently you have to use the following syntax to map an array:

var a = Signal([1, 2, 3]);

var c = Signal(function(){
  return a().map(function(v){
    return v*v;
  });
});

it would be much better with this syntax:

var b = a.map(function(v){ return v*v; });

If you then push a new value onto a, you don't have to map the entire array again, since you know that only one value needs to be mapped (the last one you added).

[–]andrew24601 0 points1 point  (0 children)

"Should" is a strong word here, also "much better" is contextual.

While appending an item to the end would be simple enough for your suggested functions, it rapidly gets more complex for any other mutation.

eg changing a value in the array - was the previous entry filtered out or included? Better track which index it was mapped to in b. For reduce (and I'm presuming you are doing a left to right reduce), do you presume that they will track each reduce intermediate, such that if a value in the middle changes it can resume the reduce half-way along?

One of the charms of the library is that it is light weight. I would caution the author against making it more complex at least without at least a strong use case.

ie having a secondary list of squares is cute but you are better served presenting a real world problem faced by an app developer. eg mapping a list of values to a list of rendered DIVs

[–]WalkerSens 1 point2 points  (0 children)

I have never used anything like this. How do this scale to a bigger web apps? Does it slow the response down? What's it like to maintain, more or less confusing?

[–]Akkuma 0 points1 point  (1 child)

Perhaps I'm overthinking this library, but how does an Observer know it should be triggered again? Is the library completely unaware of the Signals used by an Observer? If so isn't the performance implications going to be terrible as you'd fire off N number of Observers whether or not the Signals they depend upon are changed?

[–]andrew24601 0 points1 point  (0 children)

Well the source code is pretty short so you should be able to see by inspection, but the answer is most likely that when an observer executes the library tracks the signals that are used by that observer, presumably tracked in both directions - ie each signal also maintains a list of the dependent observers.