Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possilble to have Lazy.events (Event sequences) for value change? #49

Closed
ken-okabe opened this issue Dec 8, 2013 · 12 comments
Closed

Comments

@ken-okabe
Copy link

lazy.js is one of the best project I've ever found.
This library integrates undersocre/linq.js and RxJS with lazy evaluation advantage where I feel that a clean and complete implementation of Functional/Declarative programming paradigm is deployed; All the JS programmers should learn from this.
Thank you, Dan Tao.

Having said that, I have a question:
Basically, I want to bind javascript value with DOM element behavior.

To monitor myval change event, I could code something like:

var monitor = function()
{
  if(myval != myval0)
  {
      triggerEach(); // function to manipulate DOM 
  }
  myval0 = myval;
}
var tid = setInterval(function()
{
    monitor();
},100);

Is it possilble to have Lazy.events like this??

@dtao
Copy link
Owner

dtao commented Dec 8, 2013

Hey, thanks for the kind words!

I like that idea. It should definitely be possible with Object.defineProperty (perhaps with a fallback to polling, as you suggested). The result would of course be a Lazy.AsyncSequence.

Maybe something like Lazy.monitor or Lazy.observe? What do you think? I can probably implement it in the near future.

@ken-okabe
Copy link
Author

Dan Tao,
Thanks for the reply.

As I mentioned earlier, what I found very smart is your design of lazy.js is the first and only one which properly integrate Lazy evaluation functional library and Async/Reactive programming.
In this manner, we could code in true Declarative programming paradigm seamlessly ;
Mathematical structure(such as infinite sequence) -> Memory(ToArray evaluation) < - > IO,
therefore, I would love to write a code to declare to bind JS object and DOM(IO) here.
I know there's IO->Object(sequence) is done

DOM events as a sequence

which is
Memory(evaluation) <- IO (Input)
and yes, also, we do need
Memory(evaluation) -> IO (Output)
that can use map etc.

I considered the name of the Lazy generate method, but can't decide, basically I trust your naming sense.

@ken-okabe
Copy link
Author

UPDATE: need to share this

I found some library
http://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript-or-jquery

Object.prototype.watch() (Mozilla/Gekko/Firefox Only)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FObject%2Fwatch

eligrey / object-watch.js (Confirmed to work in the latest Firefox/Chrome)
https://gist.github.com/eligrey/384583

I think watch is a good name, but as you mentioned earlier, we already have Lazy.AsyncSequence, so it would be great you implement there.

@ken-okabe
Copy link
Author

UPDATE2:
https://github.com/melanke/Watch.JS

found a better one

@dtao
Copy link
Owner

dtao commented Dec 10, 2013

Here's what I've got so far:

var object = {},
    values = Lazy(object).watch('foo'),
    truthyValues = values.compact();

var auditLog = [];
truthyValues.each(function(value) {
  auditLog.push(value);
});

object.foo = 100;
object.foo = null;
object.foo = true;
object.foo = 'bar';
object.foo = false;

auditLog.toArray(); // => [100, true, 'bar']

Make sense?

@ken-okabe
Copy link
Author

Slghtly.
Probably, we need a simpler sample code since Lazy.compact() is nothing to do with fundamental feature of Lazy.watch

Does this behave in the same manner of, say,
https://github.com/melanke/Watch.JS ??

@dtao
Copy link
Owner

dtao commented Dec 11, 2013

You're right that compact isn't related to watch; I included it simply to illustrate how what you get back from watch is a Lazy.Sequence with all the same features (map, reduce, etc.) as any other sequence.

This first commit was just a simple PoC. I can also implement the option to watch multiple properties, or all properties, like:

// Watch multiple properties
Lazy(object).watch(['foo', 'bar']).each(callback);

// Watch all (existing) properties
Lazy(object).watch().each(callback);

I could also look into watching for new properties, like Watch.JS does; but that library uses setInterval to constantly poll watched objects which I'd rather not do. It seems (to me) too "big" to justify putting it in Lazy.

As for the multiple/all properties functionality, though, I'll add that in an upcoming commit.

@ken-okabe
Copy link
Author

Dan,

Sounds great.
Especially for

Lazy(object).watch(['foo', 'bar']).each(callback);

None of libraries including Watch.js works for Arrays properly so far, and yes,
it's perhaps related to new properties which also never worked.
I must say Watch.js example code does not work too.
I definitely agree with you to that we should not employ poll hack to implement, that is ugly.

Once, I considered polling to Array/new properties, but I end up with closure so that I can bind jsObject and behavior. Of course, this manner itself is not Functional/Declarative programming style (no Referential transparency), but to make mathematical model separated from IO, we do need this.

 IO.log = function()
    {
        var x;
        return {
            I: function()
            {
                return x;
            },
            O: function(val)
            {
                x = val;
                //--------- 
                $('.log')
                    .append(x + '<br>');
                //-------------
                return true;
            }

        };
    }();

Please keep up the great work.
lazy.js an universal library in JS. We can employ this library from DataBase to DOM reactive programming(the last piece we needed).
In terms of DB, lazy evaluation advantage does matter; I've read this blog, too.
http://adamnengland.wordpress.com/2013/10/10/benchmarks-underscore-js-vs-lodash-js-vs-lazy-js/

@ken-okabe
Copy link
Author

Dan,

I tried Lazy(obj).watch for my own project.
maximum call stack size exceeded error occurred.
Please check if there's some infinite loop in your watch implementation.

So far, I use watch.js, and with that, the code works as it should be, so it must be the lazy().watch probelm.
Thanks.

Ken

@ken-okabe
Copy link
Author

Dan, thanks for the update for this issue.

Just let you know things I had not known when I opened this topic.

ECMAScript 6 seems to have Object.observe and the new API looks sophisticated and complete.

http://wiki.ecmascript.org/doku.php?id=harmony:observe#object.observe
http://wiki.ecmascript.org/doku.php?id=harmony:observe_api_usage

I have informed you watch things since I did not know this api, and I think this observe is rather defacto standard for next generation JS.

Now, I personally use some implementation to work on the current ES5.

https://github.com/jdarling/Object.observe

@dtao
Copy link
Owner

dtao commented Mar 8, 2014

Thanks for bringing that up! As it turns out, I did read about Object.observe around the time we were discussing this; and it's been on my mental to-do list to use that API in Lazy. I just haven't gotten around to it yet ;) It will definitely be incorporated in an upcoming release.

@dtao
Copy link
Owner

dtao commented Feb 5, 2018

Well, it looks like Object.observe is not a thing anymore.

I'm going to close this issue as watch() is a feature that has been documented awhile. It's a bit weird, and I might like to change how it works in the future; but for now, it's there and it works.

@dtao dtao closed this as completed Feb 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants