From b2b65431572b30e6d7309e65149fdbb01178a855 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Fri, 24 Apr 2020 14:37:55 +0100 Subject: [PATCH] ui: Reuse repo.reconcile and restartWhenAvailable (#7703) --- .../services/data-source/protocols/http.js | 4 +- .../services/repository/type/event-source.js | 55 +++++-------------- 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/ui-v2/app/services/data-source/protocols/http.js b/ui-v2/app/services/data-source/protocols/http.js index 8c26fcb362..6a90796b08 100644 --- a/ui-v2/app/services/data-source/protocols/http.js +++ b/ui-v2/app/services/data-source/protocols/http.js @@ -25,9 +25,7 @@ export default Service.extend({ type: 'message', data: result, }; - if (repo.reconcile === 'function') { - repo.reconcile(get(event, 'data.meta') || {}); - } + repo.reconcile(get(event, 'data.meta')); return event; }; } diff --git a/ui-v2/app/services/repository/type/event-source.js b/ui-v2/app/services/repository/type/event-source.js index 94aeaf13cd..65bd874f84 100644 --- a/ui-v2/app/services/repository/type/event-source.js +++ b/ui-v2/app/services/repository/type/event-source.js @@ -1,41 +1,27 @@ import { inject as service } from '@ember/service'; import { get } from '@ember/object'; +import { restartWhenAvailable } from 'consul-ui/services/client/http'; import LazyProxyService from 'consul-ui/services/lazy-proxy'; import { cache as createCache, BlockingEventSource } from 'consul-ui/utils/dom/event-source'; const createProxy = function(repo, find, settings, cache, serialize = JSON.stringify) { const client = this.client; - const store = this.store; // custom createEvent, here used to reconcile the ember-data store for each tick - const createEvent = function(result, configuration) { - const event = { - type: 'message', - data: result, + let createEvent; + if (typeof repo.reconcile === 'function') { + createEvent = function(result = {}, configuration) { + const event = { + type: 'message', + data: result, + }; + if (repo.reconcile === 'function') { + repo.reconcile(get(event, 'data.meta')); + } + return event; }; - const meta = get(event.data || {}, 'meta') || {}; - if (typeof meta.date !== 'undefined') { - // unload anything older than our current sync date/time - const checkNspace = meta.nspace !== ''; - store.peekAll(repo.getModelName()).forEach(function(item) { - const dc = get(item, 'Datacenter'); - if (dc === meta.dc) { - if (checkNspace) { - const nspace = get(item, 'Namespace'); - if (nspace !== meta.namespace) { - return; - } - } - const date = get(item, 'SyncTime'); - if (typeof date !== 'undefined' && date != meta.date) { - store.unloadRecord(item); - } - } - }); - } - return event; - }; + } // proxied find*..(id, dc) return function() { const key = `${repo.getModelName()}.${find}.${serialize([...arguments])}`; @@ -58,18 +44,7 @@ const createProxy = function(repo, find, settings, cache, serialize = JSON.strin } return res; }) - .catch(function(e) { - // setup the aborted connection restarting - // this should happen here to avoid cache deletion - const status = get(e, 'errors.firstObject.status'); - if (status === '0') { - // Any '0' errors (abort) should possibly try again, depending upon the circumstances - // whenAvailable returns a Promise that resolves when the client is available - // again - return client.whenAvailable(e); - } - throw e; - }); + .catch(restartWhenAvailable(client)); }, { key: key, @@ -85,9 +60,7 @@ const createProxy = function(repo, find, settings, cache, serialize = JSON.strin let cache = null; let cacheMap = null; export default LazyProxyService.extend({ - store: service('store'), settings: service('settings'), - wait: service('timeout'), client: service('client/http'), init: function() { this._super(...arguments);