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

deliverChangeRecords delivers changes which are under delivery #18

Closed
miyconst opened this issue Oct 16, 2015 · 2 comments
Closed

deliverChangeRecords delivers changes which are under delivery #18

miyconst opened this issue Oct 16, 2015 · 2 comments
Assignees
Labels

Comments

@miyconst
Copy link

If deliverChangeRecords method was called from an observer, it should not try to deliver the changes which were sent to the observer.

Test case:

it("should not deliver changes already being under delivery", function (done) {
    var count = 0;
    var obj = { value: 1 };
    var callback = function () {
        count++;

        if (count > 1) {
            done(new Error("deliverChangeRecords should not deliver same changes more then once!"));
            return;
        }

        Object.deliverChangeRecords(callback);
    };

    Object.observe(obj, callback);
    obj.value++;

    setTimeout(function () {
        done();
    }, 10);
});

Note: this test case works fine under npm, but fails under FF and IE.

The fix is very simple.
Here https://github.com/MaxArt2501/object-observe/blob/master/dist/object-observe.js#L514
replace:

if (hdata.changeRecords.length) {
    handler(hdata.changeRecords);
    hdata.changeRecords = [];
}

with:

if (hdata.changeRecords.length) {
    handler(hdata.changeRecords.splice(0, hdata.changeRecords.length));
}
@MaxArt2501
Copy link
Owner

Another good catch, thank you.
About the fix: to avoid using a slow method like splice, let's just keep a reference to the array:

if (hdata.changeRecords.length) {
    var records = hdata.changeRecords;
    hdata.changeRecords = [];
    handler(records);
}

I'll implement this later.

@MaxArt2501 MaxArt2501 added the bug label Oct 16, 2015
@MaxArt2501 MaxArt2501 self-assigned this Oct 16, 2015
@miyconst
Copy link
Author

Sure, I just didn't investigate whether it is safe to replace entire array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants