2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2020-06-17 13:19:50 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
2020-07-17 13:42:45 +00:00
|
|
|
import { get, set } from '@ember/object';
|
2020-06-17 13:19:50 +00:00
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
const replace = function (
|
2020-07-17 13:42:45 +00:00
|
|
|
obj,
|
|
|
|
prop,
|
|
|
|
value,
|
|
|
|
destroy = (prev = null, value) => (typeof prev === 'function' ? prev() : null)
|
|
|
|
) {
|
|
|
|
const prev = obj[prop];
|
|
|
|
if (prev !== value) {
|
|
|
|
destroy(prev, value);
|
|
|
|
}
|
|
|
|
return set(obj, prop, value);
|
|
|
|
};
|
2020-06-17 13:19:50 +00:00
|
|
|
export default Component.extend({
|
|
|
|
tagName: '',
|
|
|
|
dom: service('dom'),
|
|
|
|
logger: service('logger'),
|
2020-07-17 13:42:45 +00:00
|
|
|
data: service('data-source/service'),
|
2020-06-17 13:19:50 +00:00
|
|
|
closeOnDestroy: true,
|
2022-09-15 08:43:17 +00:00
|
|
|
onerror: function (e) {
|
2020-06-17 13:19:50 +00:00
|
|
|
this.logger.execute(e.error);
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
init: function () {
|
2020-06-17 13:19:50 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
this._listeners = this.dom.listeners();
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
willDestroyElement: function () {
|
2020-07-17 13:42:45 +00:00
|
|
|
if (this.closeOnDestroy) {
|
|
|
|
this.actions.close.apply(this, []);
|
2020-06-17 13:19:50 +00:00
|
|
|
}
|
|
|
|
this._listeners.remove();
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
didReceiveAttrs: function () {
|
2020-07-17 13:42:45 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
// only close and reopen if the uri changes
|
|
|
|
// otherwise this will fire whenever the proxies data changes
|
|
|
|
if (get(this, 'src.configuration.uri') !== get(this, 'source.configuration.uri')) {
|
|
|
|
this.actions.open.apply(this, []);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
2022-09-15 08:43:17 +00:00
|
|
|
open: function () {
|
2020-07-17 13:42:45 +00:00
|
|
|
replace(this, 'source', this.data.open(this.src, this), (prev, source) => {
|
|
|
|
// Makes sure any previous source (if different) is ALWAYS closed
|
|
|
|
if (typeof prev !== 'undefined') {
|
|
|
|
this.data.close(prev, this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
replace(this, 'proxy', this.src, (prev, proxy) => {
|
|
|
|
// Makes sure any previous proxy (if different) is ALWAYS closed
|
|
|
|
if (typeof prev !== 'undefined') {
|
|
|
|
prev.destroy();
|
|
|
|
}
|
|
|
|
});
|
2022-09-15 08:43:17 +00:00
|
|
|
const error = (err) => {
|
2020-07-17 13:42:45 +00:00
|
|
|
try {
|
|
|
|
const error = get(err, 'error.errors.firstObject');
|
|
|
|
if (get(error || {}, 'status') !== '429') {
|
|
|
|
this.onerror(err);
|
2020-06-17 13:19:50 +00:00
|
|
|
}
|
2020-07-17 13:42:45 +00:00
|
|
|
this.logger.execute(err);
|
|
|
|
} catch (err) {
|
|
|
|
this.logger.execute(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// set up the listeners (which auto cleanup on component destruction)
|
|
|
|
// we only need errors here as this only uses proxies which
|
|
|
|
// automatically update their data
|
|
|
|
const remove = this._listeners.add(this.source, {
|
2022-09-15 08:43:17 +00:00
|
|
|
error: (e) => {
|
2020-07-17 13:42:45 +00:00
|
|
|
error(e);
|
2020-06-17 13:19:50 +00:00
|
|
|
},
|
|
|
|
});
|
2020-07-17 13:42:45 +00:00
|
|
|
replace(this, '_remove', remove);
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
close: function () {
|
2020-07-17 13:42:45 +00:00
|
|
|
if (typeof this.source !== 'undefined') {
|
|
|
|
this.data.close(this.source, this);
|
|
|
|
replace(this, '_remove', undefined);
|
|
|
|
set(this, 'source', undefined);
|
|
|
|
}
|
|
|
|
if (typeof this.proxy !== 'undefined') {
|
|
|
|
this.proxy.destroy();
|
|
|
|
}
|
|
|
|
},
|
2020-06-17 13:19:50 +00:00
|
|
|
},
|
|
|
|
});
|