From 23395452046288d82b20c50d2d9a840f8556e833 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Tue, 6 Oct 2020 09:31:01 +0100 Subject: [PATCH] Allow configuring a datasource to poll instead of block (#8805) --- ui-v2/app/serializers/application.js | 14 ++++++++------ ui-v2/app/services/client/http.js | 2 +- .../data-source/protocols/http/blocking.js | 5 +++-- ui-v2/app/utils/dom/event-source/blocking.js | 3 ++- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/ui-v2/app/serializers/application.js b/ui-v2/app/serializers/application.js index fb7397df8..1eeefdda4 100644 --- a/ui-v2/app/serializers/application.js +++ b/ui-v2/app/serializers/application.js @@ -107,10 +107,6 @@ export default Serializer.extend({ // say `normalizeQueryResponse` // TODO: consider creating a method for each one of the `normalize...Response` family normalizeResponse: function(store, modelClass, payload, id, requestType) { - // Pick the meta/headers back off the payload and cleanup - // before we go through serializing - const headers = payload[HTTP_HEADERS_SYMBOL] || {}; - delete payload[HTTP_HEADERS_SYMBOL]; const normalizedPayload = this.normalizePayload(payload, id, requestType); // put the meta onto the response, here this is ok // as JSON-API allows this and our specific data is now in @@ -119,7 +115,7 @@ export default Serializer.extend({ // (which was the reason for the Symbol-like property earlier) // use a method modelled on ember-data methods so we have the opportunity to // do this on a per-model level - const meta = this.normalizeMeta(store, modelClass, headers, normalizedPayload, id, requestType); + const meta = this.normalizeMeta(store, modelClass, normalizedPayload, id, requestType); if (requestType !== 'query') { normalizedPayload.meta = meta; } @@ -148,7 +144,10 @@ export default Serializer.extend({ timestamp: function() { return new Date().getTime(); }, - normalizeMeta: function(store, modelClass, headers, payload, id, requestType) { + normalizeMeta: function(store, modelClass, payload, id, requestType) { + // Pick the meta/headers back off the payload and cleanup + const headers = payload[HTTP_HEADERS_SYMBOL] || {}; + delete payload[HTTP_HEADERS_SYMBOL]; const meta = { cacheControl: headers[HTTP_HEADERS_CACHE_CONTROL.toLowerCase()], cursor: headers[HTTP_HEADERS_INDEX.toLowerCase()], @@ -158,6 +157,9 @@ export default Serializer.extend({ if (typeof headers['x-range'] !== 'undefined') { meta.range = headers['x-range']; } + if (typeof headers['refresh'] !== 'undefined') { + meta.interval = headers['refresh'] * 1000; + } if (requestType === 'query') { meta.date = this.timestamp(); payload.forEach(function(item) { diff --git a/ui-v2/app/services/client/http.js b/ui-v2/app/services/client/http.js index 64c892580..967e88242 100644 --- a/ui-v2/app/services/client/http.js +++ b/ui-v2/app/services/client/http.js @@ -70,7 +70,7 @@ const parseBody = function(strs, ...values) { return [body, ...values]; }; -const CLIENT_HEADERS = [CACHE_CONTROL, 'X-Request-ID', 'X-Range']; +const CLIENT_HEADERS = [CACHE_CONTROL, 'X-Request-ID', 'X-Range', 'Refresh']; export default Service.extend({ dom: service('dom'), connections: service('client/connections'), diff --git a/ui-v2/app/services/data-source/protocols/http/blocking.js b/ui-v2/app/services/data-source/protocols/http/blocking.js index df072d8cf..3026aad98 100644 --- a/ui-v2/app/services/data-source/protocols/http/blocking.js +++ b/ui-v2/app/services/data-source/protocols/http/blocking.js @@ -17,8 +17,9 @@ export default Service.extend({ return maybeCall(deleteCursor, ifNotBlocking(this.settings))().then(() => { return find(configuration) .then(maybeCall(close, ifNotBlocking(this.settings))) - .then(function(res) { - if (typeof get(res || {}, 'meta.cursor') === 'undefined') { + .then(function(res = {}) { + const meta = get(res, 'meta') || {}; + if (typeof meta.cursor === 'undefined' && typeof meta.interval === 'undefined') { close(); } return res; diff --git a/ui-v2/app/utils/dom/event-source/blocking.js b/ui-v2/app/utils/dom/event-source/blocking.js index fb68934c5..e0d9ef5cb 100644 --- a/ui-v2/app/utils/dom/event-source/blocking.js +++ b/ui-v2/app/utils/dom/event-source/blocking.js @@ -44,7 +44,7 @@ const throttle = function(configuration, prev, current) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(obj); - }, pause); + }, configuration.interval || pause); }); }; }; @@ -104,6 +104,7 @@ export default function(EventSource, backoff = createErrorBackoff()) { // along with cursor validation configuration.cursor = validateCursor(meta.cursor, configuration.cursor); configuration.cacheControl = meta.cacheControl; + configuration.interval = meta.interval; } if ((configuration.cacheControl || '').indexOf('no-store') === -1) { this.currentEvent = event;