ui: Reuse repo.reconcile and restartWhenAvailable (#7703)

This commit is contained in:
John Cowen 2020-04-24 14:37:55 +01:00 committed by John Cowen
parent 3d9a55c28b
commit b2b6543157
2 changed files with 15 additions and 44 deletions

View File

@ -25,9 +25,7 @@ export default Service.extend({
type: 'message', type: 'message',
data: result, data: result,
}; };
if (repo.reconcile === 'function') { repo.reconcile(get(event, 'data.meta'));
repo.reconcile(get(event, 'data.meta') || {});
}
return event; return event;
}; };
} }

View File

@ -1,41 +1,27 @@
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import { get } from '@ember/object'; import { get } from '@ember/object';
import { restartWhenAvailable } from 'consul-ui/services/client/http';
import LazyProxyService from 'consul-ui/services/lazy-proxy'; import LazyProxyService from 'consul-ui/services/lazy-proxy';
import { cache as createCache, BlockingEventSource } from 'consul-ui/utils/dom/event-source'; import { cache as createCache, BlockingEventSource } from 'consul-ui/utils/dom/event-source';
const createProxy = function(repo, find, settings, cache, serialize = JSON.stringify) { const createProxy = function(repo, find, settings, cache, serialize = JSON.stringify) {
const client = this.client; const client = this.client;
const store = this.store;
// custom createEvent, here used to reconcile the ember-data store for each tick // custom createEvent, here used to reconcile the ember-data store for each tick
const createEvent = function(result, configuration) { let createEvent;
const event = { if (typeof repo.reconcile === 'function') {
type: 'message', createEvent = function(result = {}, configuration) {
data: result, 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) // proxied find*..(id, dc)
return function() { return function() {
const key = `${repo.getModelName()}.${find}.${serialize([...arguments])}`; const key = `${repo.getModelName()}.${find}.${serialize([...arguments])}`;
@ -58,18 +44,7 @@ const createProxy = function(repo, find, settings, cache, serialize = JSON.strin
} }
return res; return res;
}) })
.catch(function(e) { .catch(restartWhenAvailable(client));
// 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;
});
}, },
{ {
key: key, key: key,
@ -85,9 +60,7 @@ const createProxy = function(repo, find, settings, cache, serialize = JSON.strin
let cache = null; let cache = null;
let cacheMap = null; let cacheMap = null;
export default LazyProxyService.extend({ export default LazyProxyService.extend({
store: service('store'),
settings: service('settings'), settings: service('settings'),
wait: service('timeout'),
client: service('client/http'), client: service('client/http'),
init: function() { init: function() {
this._super(...arguments); this._super(...arguments);