mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-06-01 20:10:16 -04:00
Since it moved to ES Modules in version 2.3.1, diff-dom stopped including the `diffDOM.js` file in its NPM package. We don't do any kind of bundling in our build yet, just concatenation of our scripts and some minification of the results so we can't take advantage of this yet. The `diffDOM.js` file is still available in the Github release so this moves to referencing that in the `package.json` instead, until we start using a bundler. I opened an issue to check this is what the author intended: https://github.com/fiduswriter/diffDOM/issues/84 The latest version also adds Rollup as a peer dependency.
54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
(function(Modules) {
|
|
"use strict";
|
|
|
|
var queues = {};
|
|
var dd = new diffDOM.DiffDOM();
|
|
|
|
var getRenderer = $component => response => dd.apply(
|
|
$component.get(0),
|
|
dd.diff($component.get(0), $(response[$component.data('key')]).get(0))
|
|
);
|
|
|
|
var getQueue = resource => (
|
|
queues[resource] = queues[resource] || []
|
|
);
|
|
|
|
var flushQueue = function(queue, response) {
|
|
while(queue.length) queue.shift()(response);
|
|
};
|
|
|
|
var clearQueue = queue => (queue.length = 0);
|
|
|
|
var poll = function(renderer, resource, queue, interval, form) {
|
|
|
|
if (document.visibilityState !== "hidden" && queue.push(renderer) === 1) $.ajax(
|
|
resource,
|
|
{
|
|
'method': form ? 'post' : 'get',
|
|
'data': form ? $('#' + form).serialize() : {}
|
|
}
|
|
).done(
|
|
response => flushQueue(queue, response)
|
|
).fail(
|
|
() => poll = function(){}
|
|
);
|
|
|
|
setTimeout(
|
|
() => poll(...arguments), interval
|
|
);
|
|
};
|
|
|
|
Modules.UpdateContent = function() {
|
|
|
|
this.start = component => poll(
|
|
getRenderer($(component)),
|
|
$(component).data('resource'),
|
|
getQueue($(component).data('resource')),
|
|
($(component).data('interval-seconds') || 1.5) * 1000,
|
|
$(component).data('form')
|
|
);
|
|
|
|
};
|
|
|
|
})(window.GOVUK.Modules);
|