arguiot / DisplayJS

A simple JavaScript framework for building ambitious UIs [DEPRECATED]
https://display.js.org
MIT License
591 stars 59 forks source link

infinite loop inside of live() #6

Closed guyjacks closed 7 years ago

guyjacks commented 7 years ago

https://codepen.io/guyjacks/pen/eRwadE

This is probably more of a javascript issue than an issue with DJS, but I can't figure out how to prevent an infinite loop from occurring when I update the variable that I'm watching.

I don't see the difference between my example and the dog example in the docs.

I love this little framework... Cheers!

arguiot commented 7 years ago

I see ๐Ÿค”, donโ€™t worry ๐Ÿ˜‰, Iโ€™ll try to fix that for you as soon as possible. The $.live() function will always have some bugs because Iโ€™m trying to implement non-standards functions (in this case, Object.prototype.watch()). Iโ€™m nearly sure that the problem comes from the function itself.

In the next week, Iโ€™ll try to fix every function to deliver the v1.0.0 of DisplayJS as soon as possible.

Thank you ๐Ÿ™ for submitting this issue ๐Ÿ˜Š.

arguiot commented 7 years ago

Just to make sure, why are you using self in the ห‹$.live()ห‹ function? I think ๐Ÿค” there is an issue here, because the ห‹selfห‹ object is not ment to be use here ๐Ÿ˜Š.

arguiot commented 7 years ago

For the moment, you can use:

var cart = {
  amount: 0.0,
  charge: 0.0
};

var $djs = new DisplayJS(cart);
$djs.var();
$djs.target(function () {
  var charge = calcCharge(parseInt(cart.amount));
  cart["charge"] = charge;
   $djs.var()
});

function calcCharge(goal) {
  var stripeFixed = 0.3;
  var stripePercent = 0.029;
  var numerator = goal + stripeFixed;
  var denominator = 1.0 - stripePercent;
  var charge = numerator / denominator;
  return charge;
}
guyjacks commented 7 years ago

I didn't mean to leave the "self" in there. I was trying something else and forgot to remove it. It's gone now.

arguiot commented 7 years ago

@guyjacks Also, the $.live() is not a function that should be used often, especially in small projects. DisplayJS have a lot of way to dynamically update values. So, I simply used a callback in the $.target() function to update everything ๐Ÿ˜Š

guyjacks commented 7 years ago

I just got it working with the code you shared above. Thank you.

arguiot commented 7 years ago

The problem was that you triggered 2 things in the exact moment (which is technically impossible in JavaScript, but because I'm dealing with low-level APIs, there is bugs), but because JavaScript is a working on single core, your browser that simply doesn't want to crash skip this step ๐Ÿ˜Š