knockout-projections icon indicating copy to clipboard operation
knockout-projections copied to clipboard

Double additions when creating a map

Open evilB opened this issue 11 years ago • 5 comments

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 avatar Feb 04 '14 15:02 evilB

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.

SteveSanderson avatar Feb 18 '14 15:02 SteveSanderson

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/SteveSanderson/knockout-projections/issues/12#issuecomment-35393250 .

evilB avatar Feb 19 '14 19:02 evilB

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 avatar Mar 31 '14 14:03 lieut-data

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 avatar Mar 31 '14 15:03 lieut-data

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.

lieut-data avatar Mar 31 '14 19:03 lieut-data