Skip to content

Commit

Permalink
checks to fix loading issues if loaded as SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Dec 18, 2020
1 parent 4dc43b8 commit f9e29be
Show file tree
Hide file tree
Showing 5 changed files with 4,642 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ build/module.js: setup globals.bc
cp yarn.lock ./module/yarn.lock
cp ./module-files/* ./module

parcel build ./module/webworker-worker.js -d module -o /webworker-worker-bundle.js
npx parcel build ./module/webworker-worker.js -d module -o /webworker-worker-bundle.js

# cd module && npm version patch && npm publish

Expand Down
95 changes: 55 additions & 40 deletions module-files/webworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,68 @@ const { version } = require("./package.json")
let lastRequestId = -1

function wrapAutoseg(urlOrVersion) {
if (urlOrVersion.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
urlOrVersion = `https://unpkg.com/autoseg@${urlOrVersion}/webworker-worker-bundle.js`
}
try {
if (
typeof window === "undefined" ||
!window.URL ||
!window.URL.createObjectURL
) {
console.log(
"autoseg not loading- no window (why are you using the browser version?)"
)
return {}
}
if (urlOrVersion.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
urlOrVersion = `https://unpkg.com/autoseg@${urlOrVersion}/webworker-worker-bundle.js`
}

const blob = new Blob([`importScripts('${urlOrVersion}')`], {
type: "application/javascript",
})
const blobUrl = window.URL.createObjectURL(blob)
const webworker = new Worker(blobUrl)

// This is blocked by a unfixable CORS error
// const webworker = new Worker(urlOrVersion)

const blob = new Blob([`importScripts('${urlOrVersion}')`], {
type: "application/javascript",
})
const blobUrl = window.URL.createObjectURL(blob)
const webworker = new Worker(blobUrl)

// This is blocked by a unfixable CORS error
// const webworker = new Worker(urlOrVersion)

const functions = ["setConfig", "loadImage", "getMask"]

const obj = {}

for (const functionName of functions) {
const id = lastRequestId++
obj[functionName] = (...args) => {
webworker.postMessage({ functionName, args, id })
return new Promise((resolve, reject) => {
const possibleResponse = (e) => {
const { data } = e
if (!data) return
if (data.id === id) {
webworker.removeEventListener("message", possibleResponse)
if (data.error) {
reject(new Error(data.error))
} else {
// There's some issue with the serialization of the ImageData
// for getMask
if (functionName === "getMask") {
const { data: pixels, width, height } = data.returnValue
resolve(new ImageData(pixels, width, height))
const functions = ["setConfig", "loadImage", "getMask"]

const obj = {}

for (const functionName of functions) {
const id = lastRequestId++
obj[functionName] = (...args) => {
webworker.postMessage({ functionName, args, id })
return new Promise((resolve, reject) => {
const possibleResponse = (e) => {
const { data } = e
if (!data) return
if (data.id === id) {
webworker.removeEventListener("message", possibleResponse)
if (data.error) {
reject(new Error(data.error))
} else {
resolve(data.returnValue)
// There's some issue with the serialization of the ImageData
// for getMask
if (functionName === "getMask") {
const { data: pixels, width, height } = data.returnValue
resolve(new ImageData(pixels, width, height))
} else {
resolve(data.returnValue)
}
}
}
}
}
webworker.addEventListener("message", possibleResponse)
})
webworker.addEventListener("message", possibleResponse)
})
}
}
}

return obj
return obj
} catch (e) {
console.log(`error loading autoseg: ${e.toString()}`)
return {}
}
}

module.exports = wrapAutoseg(
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "autoseg",
"version": "0.0.10",
"version": "0.0.12",
"main": "web.js",
"repository": "[email protected]:UniversalDataTool/autoseg.git",
"author": "seveibar <[email protected]>",
Expand All @@ -19,6 +19,7 @@
"eslint-plugin-react": "7.x",
"eslint-plugin-react-hooks": "2.x",
"lodash": "^4.17.15",
"parcel": "^1.12.4",
"prettier": "^2.0.5",
"regenerator-runtime": "^0.13.7"
}
Expand Down
2 changes: 1 addition & 1 deletion tests/segmentation/segmentation.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require("ava")
const fs = require("fs")
const path = require("path")
const m = require("../../build/module-wasm-bundle.js")
const m = require("../../build/module.js")
const range = require("lodash/range")

const f = (p) => path.join(__dirname, p)
Expand Down
Loading

0 comments on commit f9e29be

Please sign in to comment.