Currently, I am immersed in a project that combines nativescript-vue and typescript, requiring me to interact with workers. Even though I've delved into the NS documentation and implemented the recommended approach for working with workers, I'm facing a persistent challenge. Initially, everything seems to function as intended, but after invoking the worker management method a few times, the app mysteriously crashes without indicating any errors. It's unclear whether the issue lies in the worker not properly closing after completing its task or if there is a flaw within the script itself...
Here is a snippet of code from the main.ts file:
import "tns-core-modules/globals"
export class Main extends Vue {
worker: Worker
onTaskReceived(task){
this.manageWorker(task, this.worker)
}
manageWorker(task: any, worker: Worker){
const NewWorker = require('nativescript-worker-loader!./worker.ts')
worker = new Worker()
worker.postMessage({
type: "task",
value: task
})
worker.onerror = await function (err) {
console.log(`An unhandled error occurred in worker: ${err.filename}, line: ${err.lineno} :`);
console.log(err.message);
worker.terminate()
}
worker.onmessage = function (message) {
console.log('{onmessage}')
worker.terminate()
}
}
}
Additionally, below is an excerpt from the worker file (worker.ts):
import "tns-core-modules/globals"
import { error } from "tns-core-modules/trace";
const context: Worker = self as any;
context.onmessage = function(task) {
const request = task.data
console.log('[WORKER]: data from main received')
console.log(request)
# This segment represents the actual operation performed by the worker when triggered
if (result.state === 'done') {
console.log('[WORKER]: work done - sending data back')
context.postMessage({
type: 'result',
succeed: true,
value: result
})
}
}
context.onerror = function (error) {
console.log('[WORKER]{ERROR} '+error)
}
export default {} as typeof Worker & (new () => Worker)
I would greatly appreciate any assistance you can provide. Thank you!