能否将ES6类传递给Workerize工具?
Short answer: No, you can't directly pass an ES6 class to Workerize and call instance methods like worker.getClassInfo() as you've shown. Here's why, plus how to make it work:
Why Your Approach Won't Work
Workerize works by packaging functions (or exported functions from a module) into a separate Worker script, then proxying method calls via asynchronous message passing between the main thread and the Worker.
Your code runs into two critical issues:
- ES6 classes need to be instantiated with
newbefore you can call instance methods likegetClassInfo(). When you passSampleClassdirectly to Workerize, the Worker doesn't create an instance of the class—so there's no instance context for the method to run against. - Even if you tried to instantiate the class in the main thread first, the class instance (including private properties like
classInfo) can't be serialized and sent to the Worker. Worker communication relies on structured cloning, which doesn't preserve class instance context or private properties.
Working Solutions
1. Export a Factory Function from Your Worker Module
The most reliable approach is to export a function that creates an instance of your class inside the Worker and returns an object with the methods you want to expose. This keeps the class instance and its state contained within the Worker.
Update worker.ts:
export interface SampleInterface { getClassInfo(): string; } export class SampleClass implements SampleInterface { private classInfo: string; constructor(initialInfo: string) { this.classInfo = initialInfo; } public getClassInfo() { return this.classInfo; } } // Factory function to create and expose the class instance export function createSample(initialInfo: string) { const instance = new SampleClass(initialInfo); return { getClassInfo: () => instance.getClassInfo() }; }
Then in index.ts:
const worker = workerize('./worker.ts'); // Create the instance inside the Worker (returns a proxy) const sampleInstance = await worker.createSample('Hello from Worker!'); // Call the method asynchronously const info = await sampleInstance.getClassInfo(); console.log(info); // Logs "Hello from Worker!"
2. Use Static Methods (For Stateless Scenarios)
If your class doesn't need instance-specific state, you can use static methods and export them directly. This works because static methods don't rely on an instance context.
Update worker.ts:
export interface SampleInterface { getClassInfo(): string; } export class SampleClass implements SampleInterface { private static classInfo: string; public static setClassInfo(info: string) { this.classInfo = info; } public static getClassInfo() { return this.classInfo; } } // Export static methods directly export const setClassInfo = SampleClass.setClassInfo; export const getClassInfo = SampleClass.getClassInfo;
Then in index.ts:
const worker = workerize('./worker.ts'); await worker.setClassInfo('Static info from Worker!'); const info = await worker.getClassInfo(); console.log(info); // Logs "Static info from Worker!"
Key Note
All methods returned by Workerize are asynchronous—even if your original method is synchronous. You must use await or .then() when calling them, since cross-Worker communication is always async.
内容的提问来源于stack exchange,提问作者jbk

