Skip to content

Commit f9e29be

Browse files
committed
checks to fix loading issues if loaded as SSR
1 parent 4dc43b8 commit f9e29be

File tree

5 files changed

+4642
-80
lines changed

5 files changed

+4642
-80
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ build/module.js: setup globals.bc
3131
cp yarn.lock ./module/yarn.lock
3232
cp ./module-files/* ./module
3333

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

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

module-files/webworker.js

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,68 @@ const { version } = require("./package.json")
22
let lastRequestId = -1
33

44
function wrapAutoseg(urlOrVersion) {
5-
if (urlOrVersion.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
6-
urlOrVersion = `https://unpkg.com/autoseg@${urlOrVersion}/webworker-worker-bundle.js`
7-
}
5+
try {
6+
if (
7+
typeof window === "undefined" ||
8+
!window.URL ||
9+
!window.URL.createObjectURL
10+
) {
11+
console.log(
12+
"autoseg not loading- no window (why are you using the browser version?)"
13+
)
14+
return {}
15+
}
16+
if (urlOrVersion.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
17+
urlOrVersion = `https://unpkg.com/autoseg@${urlOrVersion}/webworker-worker-bundle.js`
18+
}
19+
20+
const blob = new Blob([`importScripts('${urlOrVersion}')`], {
21+
type: "application/javascript",
22+
})
23+
const blobUrl = window.URL.createObjectURL(blob)
24+
const webworker = new Worker(blobUrl)
25+
26+
// This is blocked by a unfixable CORS error
27+
// const webworker = new Worker(urlOrVersion)
828

9-
const blob = new Blob([`importScripts('${urlOrVersion}')`], {
10-
type: "application/javascript",
11-
})
12-
const blobUrl = window.URL.createObjectURL(blob)
13-
const webworker = new Worker(blobUrl)
14-
15-
// This is blocked by a unfixable CORS error
16-
// const webworker = new Worker(urlOrVersion)
17-
18-
const functions = ["setConfig", "loadImage", "getMask"]
19-
20-
const obj = {}
21-
22-
for (const functionName of functions) {
23-
const id = lastRequestId++
24-
obj[functionName] = (...args) => {
25-
webworker.postMessage({ functionName, args, id })
26-
return new Promise((resolve, reject) => {
27-
const possibleResponse = (e) => {
28-
const { data } = e
29-
if (!data) return
30-
if (data.id === id) {
31-
webworker.removeEventListener("message", possibleResponse)
32-
if (data.error) {
33-
reject(new Error(data.error))
34-
} else {
35-
// There's some issue with the serialization of the ImageData
36-
// for getMask
37-
if (functionName === "getMask") {
38-
const { data: pixels, width, height } = data.returnValue
39-
resolve(new ImageData(pixels, width, height))
29+
const functions = ["setConfig", "loadImage", "getMask"]
30+
31+
const obj = {}
32+
33+
for (const functionName of functions) {
34+
const id = lastRequestId++
35+
obj[functionName] = (...args) => {
36+
webworker.postMessage({ functionName, args, id })
37+
return new Promise((resolve, reject) => {
38+
const possibleResponse = (e) => {
39+
const { data } = e
40+
if (!data) return
41+
if (data.id === id) {
42+
webworker.removeEventListener("message", possibleResponse)
43+
if (data.error) {
44+
reject(new Error(data.error))
4045
} else {
41-
resolve(data.returnValue)
46+
// There's some issue with the serialization of the ImageData
47+
// for getMask
48+
if (functionName === "getMask") {
49+
const { data: pixels, width, height } = data.returnValue
50+
resolve(new ImageData(pixels, width, height))
51+
} else {
52+
resolve(data.returnValue)
53+
}
4254
}
4355
}
4456
}
45-
}
46-
webworker.addEventListener("message", possibleResponse)
47-
})
57+
webworker.addEventListener("message", possibleResponse)
58+
})
59+
}
4860
}
49-
}
5061

51-
return obj
62+
return obj
63+
} catch (e) {
64+
console.log(`error loading autoseg: ${e.toString()}`)
65+
return {}
66+
}
5267
}
5368

5469
module.exports = wrapAutoseg(

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "autoseg",
3-
"version": "0.0.10",
3+
"version": "0.0.12",
44
"main": "web.js",
55
"repository": "[email protected]:UniversalDataTool/autoseg.git",
66
"author": "seveibar <[email protected]>",
@@ -19,6 +19,7 @@
1919
"eslint-plugin-react": "7.x",
2020
"eslint-plugin-react-hooks": "2.x",
2121
"lodash": "^4.17.15",
22+
"parcel": "^1.12.4",
2223
"prettier": "^2.0.5",
2324
"regenerator-runtime": "^0.13.7"
2425
}

tests/segmentation/segmentation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const test = require("ava")
22
const fs = require("fs")
33
const path = require("path")
4-
const m = require("../../build/module-wasm-bundle.js")
4+
const m = require("../../build/module.js")
55
const range = require("lodash/range")
66

77
const f = (p) => path.join(__dirname, p)

0 commit comments

Comments
 (0)