Split out backgroundtask in prep for attachmentProxy separation.

This commit is contained in:
Timothy Farrell 2018-07-20 00:49:20 -05:00
parent faec6dcc69
commit 9c5e8d42e7
4 changed files with 4 additions and 59 deletions

View File

@ -12,6 +12,7 @@
"dev": "webpack-dev-server"
},
"dependencies": {
"backgroundtask": "~1.0.0",
"body-parser": "~1.18.3",
"date-fns": "~1.29.0",
"domvm": "~3.2.1",

View File

@ -1,8 +1,8 @@
import { TypeHandler } from 'pouchtype';
import { backgroundTask } from 'backgroundtask';
import { db } from '../services/db.js';
import { blobToArrayBuffer, deepAssign } from '../utils/conversion.js';
import { backgroundTask } from '../utils/event.js';
import { FileType } from './file.js';
import { error } from '../utils/console.js';

View File

@ -1,5 +1,6 @@
import core from 'pouchdb-core';
import { backgroundTask } from '../utils/event.js';
import { backgroundTask } from 'backgroundtask';
import { deepAssign, blobToObj } from '../utils/conversion.js';
import { error, log } from '../utils/console.js';

View File

@ -1,62 +1,5 @@
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 = {
is: s => s && typeof s.subscribe === 'function',
val: s => s(),