2018-10-19 15:17:02 +00:00
|
|
|
import Service from '@ember/service';
|
|
|
|
import Evented from '@ember/object/evented';
|
2020-05-11 15:37:11 +00:00
|
|
|
let buffers;
|
|
|
|
// TODO: This all should be replaced with {{#in-element}} if possible
|
2018-10-19 15:17:02 +00:00
|
|
|
export default Service.extend(Evented, {
|
2020-05-11 15:37:11 +00:00
|
|
|
init: function() {
|
|
|
|
this._super(...arguments);
|
|
|
|
buffers = {};
|
|
|
|
},
|
|
|
|
willDestroy: function() {
|
|
|
|
Object.entries(buffers).forEach(function([key, items]) {
|
|
|
|
items.forEach(function(item) {
|
|
|
|
item.remove();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
buffers = null;
|
|
|
|
},
|
2018-10-19 15:17:02 +00:00
|
|
|
// TODO: Consider renaming this and/or
|
|
|
|
// `delete`ing the buffer (but not the DOM element)
|
|
|
|
// flush should flush, but maybe being able to re-flush
|
|
|
|
// after you've flushed could be handy
|
|
|
|
flush: function(name) {
|
2020-05-11 15:37:11 +00:00
|
|
|
return buffers[name];
|
2018-10-19 15:17:02 +00:00
|
|
|
},
|
|
|
|
add: function(name, dom) {
|
|
|
|
this.trigger('add', dom);
|
2020-05-11 15:37:11 +00:00
|
|
|
if (typeof buffers[name] === 'undefined') {
|
|
|
|
buffers[name] = [];
|
2019-05-08 09:01:26 +00:00
|
|
|
}
|
2020-05-11 15:37:11 +00:00
|
|
|
buffers[name].push(dom);
|
2018-10-19 15:17:02 +00:00
|
|
|
return dom;
|
|
|
|
},
|
2020-05-11 15:37:11 +00:00
|
|
|
remove: function(name, dom) {
|
|
|
|
if (typeof buffers[name] !== 'undefined') {
|
|
|
|
const buffer = buffers[name];
|
|
|
|
const i = buffer.findIndex(item => item === dom);
|
|
|
|
if (i !== -1) {
|
|
|
|
const item = buffer.splice(i, 1)[0];
|
2019-05-08 09:01:26 +00:00
|
|
|
item.remove();
|
2020-05-11 15:37:11 +00:00
|
|
|
}
|
|
|
|
if (buffer.length === 0) {
|
|
|
|
delete buffers[name];
|
|
|
|
}
|
2019-04-08 10:18:55 +00:00
|
|
|
}
|
2018-10-19 15:17:02 +00:00
|
|
|
},
|
|
|
|
});
|