threads.js icon indicating copy to clipboard operation
threads.js copied to clipboard

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'registerSerializer')

Open EmptySpace99 opened this issue 4 years ago • 5 comments

Hi everyone, I have a problem with quasar framework and thread.js and I don't know how to solve the following error: "Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'registerSerializer')"

How reproduce this error: create quasar project (with typescript support) npm install threads tiny-worker npm install -D threads-plugin

Then into your quasar.config.js: const ThreadsPlugin = require('threads-plugin');

and extend webpack in build section: extendWebpack (cfg) { cfg.plugins.push(new ThreadsPlugin()) }

Finally tried the following code from thread.js documentation in typescript section:

//App.vue
<template>
  <div><div/>
</template>
<script lang="ts">
import { spawn, Thread, Worker } from "threads"
import { Counter } from "./workers/counter"

export default {
  setup(){
    const counter = await spawn<Counter>(new Worker("./workers/counter"))
    await counter.increment()
    await Thread.terminate(counter)    

    return {}
}
</script>
//workers/counter.ts
import { expose } from "threads/worker"

let currentCount = 0

const counter = {
  getCount() {
    return currentCount
  },
  increment() {
    return ++currentCount
  },
  decrement() {
    return --currentCount
  }
}

export type Counter = typeof counter

expose(counter)

EmptySpace99 avatar Dec 21 '21 12:12 EmptySpace99

Hey @EmptySpace99. Can you please provide the full stack trace?

The code doesn't use registerSerializer at all, so the error must have happened somewhere "on the way".

andywer avatar Dec 24 '21 11:12 andywer

I encountered similar issue in storybook, and it is likely an import issue (even though registerSerializer is not used, I have seen it gets stuck in the comprehension of imports, because the module was somewhat undefined.) I had workaround it by require("threads") though. However, I'm working on minimal reproducible example for this.

sylee957 avatar Nov 10 '23 14:11 sylee957

any updates on this?

inlet avatar Dec 05 '23 15:12 inlet

I tried this monkey patch and it seems like working for me.

diff --git a/node_modules/threads/index.mjs b/node_modules/threads/index.mjs
index 0e7864e..2998e12 100644
--- a/node_modules/threads/index.mjs
+++ b/node_modules/threads/index.mjs
@@ -1,4 +1,4 @@
-import Threads from "./dist/index.js"
+import * as Threads from "./dist/index.js"
 
 export const registerSerializer = Threads.registerSerializer
 export const spawn = Threads.spawn
diff --git a/node_modules/threads/observable.mjs b/node_modules/threads/observable.mjs
index 52dc86f..f765af2 100644
--- a/node_modules/threads/observable.mjs
+++ b/node_modules/threads/observable.mjs
@@ -1,4 +1,4 @@
-import Observables from "./dist/observable.js"
+import * as Observables from "./dist/observable.js"
 
 export const Observable = Observables.Observable
 export const Subject = Observables.Subject
diff --git a/node_modules/threads/worker.mjs b/node_modules/threads/worker.mjs
index c53ac7d..ca822d0 100644
--- a/node_modules/threads/worker.mjs
+++ b/node_modules/threads/worker.mjs
@@ -1,4 +1,4 @@
-import WorkerContext from "./dist/worker/index.js"
+import * as WorkerContext from "./dist/worker/index.js"
 
 export const expose = WorkerContext.expose
 export const registerSerializer = WorkerContext.registerSerializer

sylee957 avatar Jan 10 '24 15:01 sylee957

Any update on fixing this?

arietrouw avatar Feb 12 '24 21:02 arietrouw