-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI: Add EventSource ready for implementing blocking queries (#5070)
- Maintain http headers as JSON-API meta for all API requests (#4946) - Add EventSource ready for implementing blocking queries - EventSource project implementation to enable blocking queries for service and node listings (#5267) - Add setting to enable/disable blocking queries (#5352)
- Loading branch information
Showing
45 changed files
with
1,878 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
app/utils/dom/event-target/event-target-shim/event.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Controller from '@ember/controller'; | ||
import { get, set } from '@ember/object'; | ||
import { inject as service } from '@ember/service'; | ||
|
||
export default Controller.extend({ | ||
repo: service('settings'), | ||
dom: service('dom'), | ||
actions: { | ||
change: function(e, value, item) { | ||
const event = get(this, 'dom').normalizeEvent(e, value); | ||
// TODO: Switch to using forms like the rest of the app | ||
// setting utils/form/builder for things to be done before we | ||
// can do that. For the moment just do things normally its a simple | ||
// enough form at the moment | ||
|
||
const target = event.target; | ||
const blocking = get(this, 'item.client.blocking'); | ||
switch (target.name) { | ||
case 'client[blocking]': | ||
if (typeof blocking === 'undefined') { | ||
set(this, 'item.client', {}); | ||
} | ||
set(this, 'item.client.blocking', !blocking); | ||
this.send('update', get(this, 'item')); | ||
break; | ||
} | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import config from '../config/environment'; | ||
|
||
const enabled = 'CONSUL_UI_DISABLE_REALTIME'; | ||
export function initialize(container) { | ||
if (config[enabled] || window.localStorage.getItem(enabled) !== null) { | ||
return; | ||
} | ||
['node', 'service'] | ||
.map(function(item) { | ||
// create repositories that return a promise resolving to an EventSource | ||
return { | ||
service: `repository/${item}/event-source`, | ||
extend: 'repository/type/event-source', | ||
// Inject our original respository that is used by this class | ||
// within the callable of the EventSource | ||
services: { | ||
content: `repository/${item}`, | ||
}, | ||
}; | ||
}) | ||
.concat([ | ||
// These are the routes where we overwrite the 'default' | ||
// repo service. Default repos are repos that return a promise resovlving to | ||
// an ember-data record or recordset | ||
{ | ||
route: 'dc/nodes/index', | ||
services: { | ||
repo: 'repository/node/event-source', | ||
}, | ||
}, | ||
{ | ||
route: 'dc/services/index', | ||
services: { | ||
repo: 'repository/service/event-source', | ||
}, | ||
}, | ||
]) | ||
.forEach(function(definition) { | ||
if (typeof definition.extend !== 'undefined') { | ||
// Create the class instances that we need | ||
container.register( | ||
`service:${definition.service}`, | ||
container.resolveRegistration(`service:${definition.extend}`).extend({}) | ||
); | ||
} | ||
Object.keys(definition.services).forEach(function(name) { | ||
const servicePath = definition.services[name]; | ||
// inject its dependencies, this could probably detect the type | ||
// but hardcode this for the moment | ||
if (typeof definition.route !== 'undefined') { | ||
container.inject(`route:${definition.route}`, name, `service:${servicePath}`); | ||
} else { | ||
container.inject(`service:${definition.service}`, name, `service:${servicePath}`); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
export default { | ||
initialize, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Mixin from '@ember/object/mixin'; | ||
|
||
export default Mixin.create({ | ||
reset: function(exiting) { | ||
if (exiting) { | ||
Object.keys(this).forEach(prop => { | ||
if (this[prop] && typeof this[prop].close === 'function') { | ||
this[prop].close(); | ||
// ember doesn't delete on 'resetController' by default | ||
delete this[prop]; | ||
} | ||
}); | ||
} | ||
return this._super(...arguments); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Service from '@ember/service'; | ||
import { get } from '@ember/object'; | ||
|
||
export default Service.extend({ | ||
shouldProxy: function(content, method) { | ||
return false; | ||
}, | ||
init: function() { | ||
this._super(...arguments); | ||
const content = get(this, 'content'); | ||
for (let prop in content) { | ||
if (typeof content[prop] === 'function') { | ||
if (this.shouldProxy(content, prop)) { | ||
this[prop] = function() { | ||
return this.execute(content, prop).then(method => { | ||
return method.apply(this, arguments); | ||
}); | ||
}; | ||
} else if (typeof this[prop] !== 'function') { | ||
this[prop] = function() { | ||
return content[prop](...arguments); | ||
}; | ||
} | ||
} | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { inject as service } from '@ember/service'; | ||
import { get } from '@ember/object'; | ||
|
||
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) { | ||
// proxied find*..(id, dc) | ||
const throttle = get(this, 'wait').execute; | ||
const client = get(this, 'client'); | ||
return function() { | ||
const key = `${repo.getModelName()}.${find}.${serialize([...arguments])}`; | ||
const _args = arguments; | ||
const newPromisedEventSource = cache; | ||
return newPromisedEventSource( | ||
function(configuration) { | ||
// take a copy of the original arguments | ||
// this means we don't have any configuration object on it | ||
let args = [..._args]; | ||
if (configuration.settings.enabled) { | ||
// ...and only add our current cursor/configuration if we are blocking | ||
args = args.concat([configuration]); | ||
} | ||
// save a callback so we can conditionally throttle | ||
const cb = () => { | ||
// original find... with configuration now added | ||
return repo[find](...args) | ||
.then(res => { | ||
if (!configuration.settings.enabled) { | ||
// blocking isn't enabled, immediately close | ||
this.close(); | ||
} | ||
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; | ||
}); | ||
}; | ||
// if we have a cursor (which means its at least the second call) | ||
// and we have a throttle setting, wait for so many ms | ||
if (typeof configuration.cursor !== 'undefined' && configuration.settings.throttle) { | ||
return throttle(configuration.settings.throttle).then(cb); | ||
} | ||
return cb(); | ||
}, | ||
{ | ||
key: key, | ||
type: BlockingEventSource, | ||
settings: { | ||
enabled: settings.blocking, | ||
throttle: settings.throttle, | ||
}, | ||
} | ||
); | ||
}; | ||
}; | ||
let cache = null; | ||
export default LazyProxyService.extend({ | ||
store: service('store'), | ||
settings: service('settings'), | ||
wait: service('timeout'), | ||
client: service('client/http'), | ||
init: function() { | ||
this._super(...arguments); | ||
if (cache === null) { | ||
cache = createCache({}); | ||
} | ||
}, | ||
willDestroy: function() { | ||
cache = null; | ||
}, | ||
shouldProxy: function(content, method) { | ||
return method.indexOf('find') === 0; | ||
}, | ||
execute: function(repo, find) { | ||
return get(this, 'settings') | ||
.findBySlug('client') | ||
.then(settings => { | ||
return createProxy.bind(this)(repo, find, settings, cache); | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.