|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- const defaultOrderRegistry = {};
-
- /* function notify(orderRegistry) {
- if (!('listeners' in orderRegistry))
- return;
- for (let k in orderRegistry.listeners) {
- orderRegistry.listeners[k](orderRegistry);
- }
- } */
-
- function cursorDecor() {
- let d = $('#cursor-decor');
- if (d.length === 1)
- return $(d[0]);
- d = $('<div id="cursor-decor" style="z-index: 10000; position: absolute; left: 10px; top: 10px;"> \
- <div class="progress" style="height: 8px;"> \
- <div class="progress-bar progress-bar-striped progress-bar-animated" \
- role="progressbar" aria-valuenow="100" aria-valuemin="0" \
- aria-valuemax="100" style="width: 32px;"></div> \
- </div> \
- </div>');
- $(document.body).append(d);
- let pageX = 0, pageY = 0, scrollX = 0, scrollY = 0;
- document.addEventListener('mousemove', e => {
- pageX = e.pageX;
- pageY = e.pageY;
- d.css({ left: (e.pageX + 16) + 'px', top: (e.pageY + 16) + 'px' })
- scrollX = window.scrollX;
- scrollY = window.scrollY;
- });
- document.addEventListener('scroll', e => {
- d.css({ left: (pageX + window.scrollX - scrollX + 16) + 'px',
- top: (pageY + window.scrollY - scrollY + 16) + 'px' });
- });
- return d;
- }
-
- function updateCursorDecor(orderRegistry) {
- const d = cursorDecor();
- if (Object.keys(orderRegistry.pendingCompletion).length === 0)
- d.hide();
- else
- d.show();
- }
-
- function wbApplyPromiseOrdering(prom, orderRegistry) {
- let orderId;
-
- if (!orderRegistry)
- orderRegistry = defaultOrderRegistry;
-
- //if (Object.keys(orderRegistry).length === 0) {
- if (!('started' in orderRegistry)) {
- orderRegistry.started = 0;
- orderRegistry.pendingCompletion = {};
- orderRegistry.completed = { 0: true };
- }
-
- orderRegistry.started += 1;
- orderId = orderRegistry.started;
- // console.log('New orderId: ' + orderId);
- // notify(orderRegistry);
- cursorDecor().show();
-
- const orderCallback = ((isCatch, payload) => {
- // console.log('orderId: ' + orderId +
- // ', pendingCompletion: ' + Object.keys(orderRegistry.pendingCompletion) +
- // ', completed: ' + Object.keys(orderRegistry.completed));
-
- if ((orderId - 1) in orderRegistry.completed) {
- // console.log('Running: ' + orderId);
- orderRegistry.completed[orderId] = true;
- delete orderRegistry.pendingCompletion[orderId];
-
- const keys = Object.keys(orderRegistry.pendingCompletion);
- keys.sort((a, b) => (a - b));
- keys.map(k => {
- if ((k - 1) in orderRegistry.completed) {
- // console.log('Running: ' + k);
- orderRegistry.pendingCompletion[k]();
- orderRegistry.completed[k] = true;
- delete orderRegistry.pendingCompletion[k];
- }
- });
-
- if (orderRegistry.started in orderRegistry.completed) {
- // console.log('Garbage collect');
- orderRegistry.started = 0;
- orderRegistry.completed = { 0: true };
- cursorDecor().hide();
- }
-
- if (isCatch)
- throw payload;
- else
- return payload;
- }
-
- const prom_1 = new Promise((accept, reject) => {
- orderRegistry.pendingCompletion[orderId] = (() =>
- (isCatch ? reject(payload) : accept(payload)));
- });
-
- return prom_1;
- });
-
- prom = prom.then(xhr => orderCallback(false, xhr));
- prom = prom.catch(e => orderCallback(true, e));
-
- return prom;
- }
-
- export default wbApplyPromiseOrdering;
|