2019-10-30 09:48:24 -04:00
|
|
|
import { scheduleOnce } from "@ember/runloop";
|
2016-10-24 15:21:44 -04:00
|
|
|
// Ember 2.0 removes buffered rendering, but we can still implement it ourselves.
|
|
|
|
// In the long term we'll want to remove this.
|
|
|
|
|
|
|
|
const Mixin = {
|
|
|
|
_customRender() {
|
2018-06-15 11:03:24 -04:00
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-24 15:21:44 -04:00
|
|
|
|
|
|
|
const buffer = [];
|
|
|
|
this.buildBuffer(buffer);
|
2018-06-15 11:03:24 -04:00
|
|
|
this.element.innerHTML = buffer.join("");
|
2016-10-24 15:21:44 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
rerenderBuffer() {
|
2019-10-30 09:48:24 -04:00
|
|
|
scheduleOnce("render", this, this._customRender);
|
2016-10-24 15:21:44 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function bufferedRender(obj) {
|
|
|
|
if (!obj.buildBuffer) {
|
2019-01-16 14:53:13 -05:00
|
|
|
Ember.warn("Missing `buildBuffer` method", {
|
|
|
|
id: "discourse.buffered-render.missing-build-buffer"
|
|
|
|
});
|
2016-10-24 15:21:44 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
const caller = {};
|
2016-10-24 15:21:44 -04:00
|
|
|
|
2016-11-25 15:38:45 -05:00
|
|
|
caller.didRender = function() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2016-11-25 15:38:45 -05:00
|
|
|
this._customRender();
|
|
|
|
};
|
2016-10-24 15:21:44 -04:00
|
|
|
|
|
|
|
const triggers = obj.rerenderTriggers;
|
|
|
|
if (triggers) {
|
|
|
|
caller.init = function() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2016-10-24 15:21:44 -04:00
|
|
|
triggers.forEach(k => this.addObserver(k, this.rerenderBuffer));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
delete obj.rerenderTriggers;
|
|
|
|
|
|
|
|
return Ember.Mixin.create(Mixin, caller, obj);
|
|
|
|
}
|