Split out backgroundtask in prep for attachmentProxy separation.
This commit is contained in:
parent
faec6dcc69
commit
9c5e8d42e7
@ -12,6 +12,7 @@
|
|||||||
"dev": "webpack-dev-server"
|
"dev": "webpack-dev-server"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"backgroundtask": "~1.0.0",
|
||||||
"body-parser": "~1.18.3",
|
"body-parser": "~1.18.3",
|
||||||
"date-fns": "~1.29.0",
|
"date-fns": "~1.29.0",
|
||||||
"domvm": "~3.2.1",
|
"domvm": "~3.2.1",
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { TypeHandler } from 'pouchtype';
|
import { TypeHandler } from 'pouchtype';
|
||||||
|
import { backgroundTask } from 'backgroundtask';
|
||||||
|
|
||||||
import { db } from '../services/db.js';
|
import { db } from '../services/db.js';
|
||||||
import { blobToArrayBuffer, deepAssign } from '../utils/conversion.js';
|
import { blobToArrayBuffer, deepAssign } from '../utils/conversion.js';
|
||||||
import { backgroundTask } from '../utils/event.js';
|
|
||||||
import { FileType } from './file.js';
|
import { FileType } from './file.js';
|
||||||
import { error } from '../utils/console.js';
|
import { error } from '../utils/console.js';
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import core from 'pouchdb-core';
|
import core from 'pouchdb-core';
|
||||||
import { backgroundTask } from '../utils/event.js';
|
import { backgroundTask } from 'backgroundtask';
|
||||||
|
|
||||||
import { deepAssign, blobToObj } from '../utils/conversion.js';
|
import { deepAssign, blobToObj } from '../utils/conversion.js';
|
||||||
import { error, log } from '../utils/console.js';
|
import { error, log } from '../utils/console.js';
|
||||||
|
|
||||||
|
|||||||
@ -1,62 +1,5 @@
|
|||||||
import { log, group, groupEnd } from '../utils/console.js';
|
import { log, group, groupEnd } from '../utils/console.js';
|
||||||
|
|
||||||
// requestIdleCallback sortof-polyfill
|
|
||||||
if (!global.requestIdleCallback) {
|
|
||||||
const IDLE_TIMEOUT = 10;
|
|
||||||
global.requestIdleCallback = cb => {
|
|
||||||
let start = Date.now();
|
|
||||||
return setTimeout(
|
|
||||||
() =>
|
|
||||||
cb({
|
|
||||||
timeRemaining: () => Math.max(0, IDLE_TIMEOUT - (Date.now() - start))
|
|
||||||
}),
|
|
||||||
1
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function backgroundTask(fn, initialDelay = 500) {
|
|
||||||
let id = null;
|
|
||||||
const params = [];
|
|
||||||
|
|
||||||
async function runTask({ didTimeout }) {
|
|
||||||
if (didTimeout) {
|
|
||||||
id = requestIdleCallback(runTask);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const start = Date.now();
|
|
||||||
group(fn.name);
|
|
||||||
const p = params.shift();
|
|
||||||
if (p.length) {
|
|
||||||
log(`${fn.name} params: `, ...p);
|
|
||||||
}
|
|
||||||
await fn(...p);
|
|
||||||
const executionTime = Date.now() - start;
|
|
||||||
log(`${fn.name} execution time: ${executionTime}ms`);
|
|
||||||
groupEnd(fn.name);
|
|
||||||
if (params.length) {
|
|
||||||
id = requestIdleCallback(runTask);
|
|
||||||
} else {
|
|
||||||
id = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const wrapper = (...args) => {
|
|
||||||
params.push(args);
|
|
||||||
if (id !== null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
id = requestIdleCallback(runTask);
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (initialDelay) {
|
|
||||||
setTimeout(wrapper, initialDelay);
|
|
||||||
}
|
|
||||||
|
|
||||||
return wrapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const streamConfig = {
|
export const streamConfig = {
|
||||||
is: s => s && typeof s.subscribe === 'function',
|
is: s => s && typeof s.subscribe === 'function',
|
||||||
val: s => s(),
|
val: s => s(),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user