export default null ; function promisify < T , A > ( f ) { return ( arg ) => new Promise < T > ( ( resolve , reject ) => f ( arg , ( error , result ) => { if ( error ) { return reject ( error ) ; } if ( result === null ) { return reject ( null ) ; } resolve ( result ) ; } ) ) ; } import { readFile } from 'fs' ; let readFilePromise = promisify ( readFile ) ; readFilePromise ( __dirname + '/exercises.js' ) . then ( result => console . log ( 'done!' , result . toString ( ) ) ) ; type Matrix = number [ ] [ ] ; type MatrixProtocol = { determinant : { in : [ Matrix ] ; out : number ; } ; 'dot-product' : { in : [ Matrix , Matrix ] ; out : Matrix ; } ; invert : { in : [ Matrix ] ; out : Matrix ; } ; } ; type Protocol = { [ command ] : { in : unknown [ ] ; out : unknown ; } ; } ; function createProtocol < P extends Protocol > ( script ) { return < K extends keyof P > ( command ) => ( ... args ) => new Promise < P [ K ] [ 'out' ] > ( ( resolve , reject ) => { let worker = new Worker ( script ) ; worker . onerror = reject ; worker . onmessage = event => resolve ( event . data . data ) ; worker . postMessage ( { command , args } ) ; } ) ; } let runWithMatrixProtocol = createProtocol < MatrixProtocol > ( 'MatrixWorkerScript.js' ) ; let parallelDeterminant = runWithMatrixProtocol ( 'determinant' ) ; parallelDeterminant ( [ [ 1 , 2 ] , [ 3 , 4 ] ] ) . then ( determinant => console . log ( determinant ) ) ; let handlers = { determinant ( matrix ) { return determinant ( matrix ) ; } , [ 'dot-product' ] ( a , b ) { return dotProduct ( a , b ) ; } , invert ( matrix ) { return invert ( matrix ) ; } } ; onmessage = < C extends keyof MatrixProtocol > ( { data : { command , args } } ) => { let handler = handlers [ command ] ; let result = handler ( ... args ) ; postMessage ( result ) ; } ; declare function determinant ( matrix ) ; declare function dotProduct ( matrixA , matrixB ) ; declare function invert ( matrix ) ;