From 4c85481c8fc47d50cc9ff8de31f04599e1f0b425 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Mon, 13 Jul 2015 17:46:38 -0700 Subject: [PATCH] fix(nylas-api): Handle model responses with duplicate entries and warn, avoid guard assertion failure in DatabaseStore --- src/flux/nylas-api.coffee | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/flux/nylas-api.coffee b/src/flux/nylas-api.coffee index b3003459a..10ae3fbbf 100644 --- a/src/flux/nylas-api.coffee +++ b/src/flux/nylas-api.coffee @@ -283,12 +283,18 @@ class NylasAPI Promise.settle(destroyPromises) - _handleModelResponse: (json) -> + _handleModelResponse: (jsons) -> new Promise (resolve, reject) => - reject(new Error("handleModelResponse with no JSON provided")) unless json + reject(new Error("handleModelResponse with no JSON provided")) unless jsons - json = [json] unless json instanceof Array - async.filter json + jsons = [jsons] unless jsons instanceof Array + + uniquedJSONs = _.uniq jsons, false, (model) -> model.id + if uniquedJSONs.length < jsons.length + console.warn("NylasAPI.handleModelResponse: called with non-unique object set. + Maybe an API request returned the same object more than once?") + + async.filter uniquedJSONs , (item, filterCallback) => @_shouldAcceptModel(item.object, item).then (accept) -> filterCallback(accept)