1- export default class Thread < T > {
1+ /**
2+ * > Type T -> return type
3+ *
4+ * > Type K -> data type of MessageEvent
5+ */
6+ export default class Thread < T = unknown , K = unknown > {
27 public worker : Promise < Worker > ;
38 private imports : Array < string > ;
49 private blob : Promise < Blob > ;
5- private blobURL : string = "" ;
10+ private blobURL = "" ;
611 /**
712 * Tells if the worker has been stopped
813 */
@@ -13,7 +18,7 @@ export default class Thread<T> {
1318 * @param imports Modules to import in the worker. only JS files allowed (over the net import allowed)
1419 */
1520 constructor (
16- operation : ( e : MessageEvent , globalObject ?:{ } ) => T ,
21+ operation : ( e : MessageEvent < K > , globalObject ?: Record < string , unknown > ) => T | Promise < T > ,
1722 type ?: "classic" | "module" ,
1823 imports ?: Array < string > ,
1924 ) {
@@ -24,7 +29,6 @@ export default class Thread<T> {
2429 } ) ;
2530 this . imports = imports || [ ] ;
2631 this . blob = this . populateFile ( operation ) ;
27- this . blob . then ( async ( b ) => console . log ( await b . text ( ) ) ) ;
2832 this . worker = this . makeWorker ( type ) ;
2933 }
3034
@@ -38,16 +42,17 @@ export default class Thread<T> {
3842 ) ;
3943 }
4044
45+ // deno-lint-ignore ban-types
4146 private async populateFile ( code : Function ) {
42- let imported = this . imports ?. flatMap ( async ( val ) => ( await this . copyDep ( val ) ) . join ( "\n" ) ) ;
47+ const imported = this . imports ?. flatMap ( async ( val ) => ( await this . copyDep ( val ) ) . join ( "\n" ) ) ;
4348 return new Blob ( [ `
4449 ${ ( await Promise . all ( imported ) ) . join ( "\n" ) }
4550
4651 var global = {};
4752 var userCode = ${ code . toString ( ) }
4853
49- onmessage = function(e) {
50- postMessage(userCode(e, global));
54+ onmessage = async function(e) {
55+ postMessage(await userCode(e, global));
5156 }
5257
5358 ` ] ) ;
@@ -58,11 +63,11 @@ export default class Thread<T> {
5863 * @param str the import line (eg: import {som} from "lorem/ipsum.js";)
5964 */
6065 private async copyDep ( str : string ) {
61- var importPathRegex = / ( ' | " | ` ) ( .+ \. j s ) ( \1) / ig; // for the path string ("lorem/ipsum.js")
62- var importInsRegex = / ( i m p o r t ( | ) ) ( { .+ } | .+ ) ( f r o m ( | ) ) / ig; // for the instruction before the path (import {som} from)
63- var matchedPath = importPathRegex . exec ( str ) || "" ;
64- var file = false ;
65- var fqfn = "" ;
66+ const importPathRegex = / ( ' | " | ` ) ( .+ \. j s ) ( \1) / ig; // for the path string ("lorem/ipsum.js")
67+ const importInsRegex = / ( i m p o r t ( | ) ) ( { .+ } | .+ ) ( f r o m ( | ) ) / ig; // for the instruction before the path (import {som} from)
68+ const matchedPath = importPathRegex . exec ( str ) || "" ;
69+ let file = false ;
70+ let fqfn = "" ;
6671
6772 if (
6873 ! matchedPath [ 0 ] . includes ( "http://" ) &&
@@ -71,7 +76,7 @@ export default class Thread<T> {
7176 file = true ;
7277 fqfn = matchedPath [ 0 ] . replaceAll ( / ( ' | " | ` ) / ig, "" ) ;
7378 }
74- var matchedIns = importInsRegex . exec ( str ) || "" ; // matchedIns[0] > import {sss} from
79+ const matchedIns = importInsRegex . exec ( str ) || "" ; // matchedIns[0] > import {sss} from
7580
7681 if ( ! matchedIns ) {
7782 throw new Error (
@@ -82,10 +87,10 @@ export default class Thread<T> {
8287
8388
8489 if ( file ) {
85- let x = await import ( fqfn ) ; //Deno.realPathSync(fqfn)
90+ const x = await import ( fqfn ) ; //Deno.realPathSync(fqfn)
8691 return Object . keys ( x ) . map ( ( v ) => x [ v ] . toString ( ) )
8792 } else {
88- let x = await import ( matchedPath [ 0 ] . replaceAll ( / ' | " / g, "" ) ) ;
93+ const x = await import ( matchedPath [ 0 ] . replaceAll ( / ' | " / g, "" ) ) ;
8994 return Object . keys ( x ) . map ( ( v ) => x [ v ] . toString ( ) )
9095 }
9196 }
@@ -94,7 +99,7 @@ export default class Thread<T> {
9499 * Sends data to the Thread
95100 * @param msg
96101 */
97- public postMessage ( msg : any ) : this {
102+ public postMessage ( msg : K ) : this {
98103 this . worker . then ( w => w . postMessage ( msg ) ) ;
99104 return this ;
100105 }
0 commit comments