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

Updating throttled observable #99

Closed
molnarg opened this issue Aug 3, 2012 · 3 comments
Closed

Updating throttled observable #99

molnarg opened this issue Aug 3, 2012 · 3 comments

Comments

@molnarg
Copy link

molnarg commented Aug 3, 2012

I've run into the following issue.

When running this, I get the expected result, the observable is updated and the subscriber function fires when the update happens:

var a = { x : ko.observable() };
a.x.subscribe(function(value){console.log(value)}); 
a.x(1); a.x(2); a.x(9);            // console: 1, 2, 9

ko.mapping.fromJS({x:3}, {}, a);   // console: 3
console.log(a.x())                 // console: 3

But when trying the same thing with a throttle extender, the value is not written to the observable:

var a = { x : ko.observable().extend({throttle:1}) };
a.x.subscribe(function(value){console.log(value)}); 
a.x(1); a.x(2); a.x(9);            // console: 9

ko.mapping.fromJS({x:3}, {}, a);   // console: nothing.
console.log(a.x())                 // console: 9
@molnarg
Copy link
Author

molnarg commented Aug 3, 2012

After debugging the code, I can now see what happens.

The write function of the computed observable returned by the throttle extender gets actually called with the correct value at first by the updateViewModel function. (at line 379 in the 2.3 release)

But then it gets called again with its original value by the anonymus function passed to visitPropertiesOrArrayEntries in updateViewModel. (at line 440 in the 2.3 release)

At first the subscribers are not notified because this is the point of the throttle extender. Writing the actual observable is deferred. But then the other write comes in with the old value. When the throttle computed observable actually writes the original observable it writes the last value it gets, the old value. That's why the value of the observable doesn't change.

I still don't know how to fix the issue in the mapping plugin, but I hope this explanation helps someone who does.

@sagacity
Copy link
Collaborator

sagacity commented Aug 3, 2012

Thanks for the detailed analysis, I'll look into it.

@sagacity
Copy link
Collaborator

sagacity commented Aug 3, 2012

This should be fixed now. Can you confirm?

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