From 0bbfc4cdc3b79aa4f5164aa84504e9e9427aa93d Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Sun, 27 Jun 2021 15:13:45 +1000 Subject: [PATCH] Move to broadcast channel check for worker put asset --- src/contexts/AssetsContext.js | 19 ++++++++++++------- src/helpers/shared.js | 3 --- src/workers/DatabaseWorker.js | 5 ----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/contexts/AssetsContext.js b/src/contexts/AssetsContext.js index ed8a0f3..b6ee893 100644 --- a/src/contexts/AssetsContext.js +++ b/src/contexts/AssetsContext.js @@ -75,13 +75,18 @@ export function AssetsProvider({ children }) { const putAsset = useCallback( async (asset) => { - // Attempt to use worker to put map to avoid UI lockup - const packedAsset = encode(asset); - const success = await worker.putData( - Comlink.transfer(packedAsset, [packedAsset.buffer]), - "assets" - ); - if (!success) { + // Check for broadcast channel and attempt to use worker to put map to avoid UI lockup + // Safari doesn't support BC so fallback to single thread + if (window.BroadcastChannel) { + const packedAsset = encode(asset); + const success = await worker.putData( + Comlink.transfer(packedAsset, [packedAsset.buffer]), + "assets" + ); + if (!success) { + await database.table("assets").put(asset); + } + } else { await database.table("assets").put(asset); } }, diff --git a/src/helpers/shared.js b/src/helpers/shared.js index b45ce17..5940429 100644 --- a/src/helpers/shared.js +++ b/src/helpers/shared.js @@ -75,9 +75,6 @@ export function groupBy(array, key) { } export const isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform); -export const isSafari = /^((?!chrome|android).)*safari/i.test( - navigator.userAgent -); export function shuffle(array) { let temp = [...array]; diff --git a/src/workers/DatabaseWorker.js b/src/workers/DatabaseWorker.js index 7003e93..8b53785 100644 --- a/src/workers/DatabaseWorker.js +++ b/src/workers/DatabaseWorker.js @@ -8,7 +8,6 @@ import { encode, decode } from "@msgpack/msgpack"; import { getDatabase } from "../database"; import blobToBuffer from "../helpers/blobToBuffer"; -import { isSafari } from "../helpers/shared"; // Worker to load large amounts of database data on a separate thread let service = { @@ -46,10 +45,6 @@ let service = { * @param {string} table */ async putData(data, table) { - if (isSafari) { - // Safari is unable to put data into indexedb and have useLiveQuery update - return false; - } try { let db = getDatabase({}); const decoded = decode(data);