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

Double additions when creating a map #12

Open
evilB opened this issue Feb 4, 2014 · 5 comments
Open

Double additions when creating a map #12

evilB opened this issue Feb 4, 2014 · 5 comments

Comments

@evilB
Copy link

evilB commented Feb 4, 2014

When creating a map in my program, for example:
original array: [1,2] to [1_1,2_2]

I sometimes get the unexpected resulting array [1,1,4,4]. When adding a 3rd element (3) to the original array the resulting array becomes [1,1,4,4,9].

I've managed to track down the issue to the function StateItem

function StateItem(ko, inputItem, initialStateArrayIndex, initialOutputArrayIndex, mapping, arrayOfState, outputObservableArray) {
// ... omitted for brevity
this.mappedValueComputed = ko.computed(this.mappingEvaluator, this);
this.mappedValueComputed.subscribe(this.onMappingResultChanged, this);
this.previousMappedValue = this.mappedValueComputed.peek();
}

In my use case, for whatever reason (i don't remember changing it myself), the computed is not calculated until the first look with peek().

onMappingResultChanged is called when setting previousMappedValue and replaces the element the outputObservableArray with slice(). However, there is nothing in the Array to be removed so the element is just added to the array, which causes the duplicates.

I've resolved the issue by setting the subscription after setting
this.previousMappedValue.

Best Regards,

Evert

evilB added a commit to evilB/knockout-projections that referenced this issue Feb 4, 2014
Set the subscription to this.mappedValueComputed after setting of this.previousMappedValue

see issue SteveSanderson#12
@SteveSanderson
Copy link
Owner

Thanks for reporting this. I know you've suggested a fix, but I'd love to see the problem at work too. To be sure I've understood the problem properly, would you be able to post a repro?

Ideally if you can put a minimal demonstration of the issue on jsFiddle.net, I'll then be able to understand exactly what I missed in the original implementation.

@evilB
Copy link
Author

evilB commented Feb 19, 2014

Hij Steve,

It toon me a while tot figure it out. I think it may be a combination with
knockout-sortable plug in. I'll see about creating a jsfiddle at lunch
tomorrow to recreate the issue, it's the first time I'll do jsfiddle so it
may take longer than expected.

Best regards

Evert
On 18 Feb 2014 16:10, "Steven Sanderson" [email protected] wrote:

Thanks for reporting this. I know you've suggested a fix, but I'd love to
see the problem at work too. To be sure I've understood the problem
properly, would you be able to post a repro?

Ideally if you can put a minimal demonstration of the issue on
jsFiddle.net, I'll then be able to understand exactly what I missed in the
original implementation.

Reply to this email directly or view it on GitHubhttps://github.com//issues/12#issuecomment-35393250
.

@lieut-data
Copy link

I can reproduce this issue in my application, but am too having difficulty isolating it into a reproducible jsFiddle. I haven't tested it thoroughly, but evilB's proposed patch also addresses the duplication I'm seeing.

I'll continue to try to isolate the issue in a reproducible jsFiddle.

@lieut-data
Copy link

Here's a jsFiddle that reproduces the issue: http://jsfiddle.net/47Xar/

Note that, just like my application, it relies on the mapping plugin being applied to a (simplified) nested hierarchy.

@lieut-data
Copy link

Unfortunately, I'm now also seeing issues where sometimes a nested, throttled filter introduces null values into the resulting array where the original had none, independent of evilB's proposed patch above. My application consists of a series of checked items and I currently reproduce this issue after toggling several checkbox states in a row.

I suspect the same underlying issue, but if needs be, can try to isolate into another jsFiddle.

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

3 participants