From 38cd1926716f9ef2db69ec5b136fc862c3c90b87 Mon Sep 17 00:00:00 2001 From: Robin Ricard Date: Fri, 22 Jul 2016 18:44:51 +0200 Subject: [PATCH] Performs a list merging that looks for IDs By looking of ID fields we can handle complex cases such as updating at the same time two nested arrays. --- src/data/writeToStore.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/data/writeToStore.ts b/src/data/writeToStore.ts index 06741a82f1d..219453b9a89 100644 --- a/src/data/writeToStore.ts +++ b/src/data/writeToStore.ts @@ -294,7 +294,7 @@ function writeFieldToStore({ fragmentMap, included: true, }); - value = [].concat(oldValue, value); + value = defaultListMerging(oldValue, value); } value.forEach((item, index) => { @@ -409,3 +409,9 @@ function writeFieldToStore({ } } + +function defaultListMerging(oldValues: any[], newValues: any[]): any[] { + const newIds = newValues.map(value => value.id ? value.id : null) .filter(id => id !== null); + const filteredOldValues = oldValues.filter(value => !value.id || newIds.indexOf(value.id) < 0); + return [].concat(filteredOldValues, newValues); +}