From 9c5e8d42e743fa373ae684031dc505764ba2443f Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Fri, 20 Jul 2018 00:49:20 -0500 Subject: [PATCH] Split out backgroundtask in prep for attachmentProxy separation. --- packages/gallery/package.json | 1 + packages/gallery/src/data/image.js | 2 +- packages/gallery/src/utils/attachmentProxy.js | 3 +- packages/gallery/src/utils/event.js | 57 ------------------- 4 files changed, 4 insertions(+), 59 deletions(-) diff --git a/packages/gallery/package.json b/packages/gallery/package.json index 84ee163..627f6b4 100644 --- a/packages/gallery/package.json +++ b/packages/gallery/package.json @@ -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", diff --git a/packages/gallery/src/data/image.js b/packages/gallery/src/data/image.js index 3316ad1..f63d5ed 100644 --- a/packages/gallery/src/data/image.js +++ b/packages/gallery/src/data/image.js @@ -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'; diff --git a/packages/gallery/src/utils/attachmentProxy.js b/packages/gallery/src/utils/attachmentProxy.js index 7ad64b3..71d29ba 100644 --- a/packages/gallery/src/utils/attachmentProxy.js +++ b/packages/gallery/src/utils/attachmentProxy.js @@ -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'; diff --git a/packages/gallery/src/utils/event.js b/packages/gallery/src/utils/event.js index d145e3d..addb245 100644 --- a/packages/gallery/src/utils/event.js +++ b/packages/gallery/src/utils/event.js @@ -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(),