Allow configuring a datasource to poll instead of block (#8805)

This commit is contained in:
John Cowen 2020-10-06 09:31:01 +01:00 committed by GitHub
parent 5da640b287
commit e4a0dcf10e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 10 deletions

View File

@ -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) {

View File

@ -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'),

View File

@ -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;

View File

@ -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;