1.0.15 • Published 4 years ago

worker-typescript-connector v1.0.15

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Call worker as simple typed class methods. And make DOM events available for worker.

Requirements:

npm install worker-typescript-connector --save

and worker-loader to start worker:

npm install worker-loader --save-dev

How to use:

For example we have ExampleClass - this is the Class that we want to run inside worker, and it implements example ExampleInterface

example-class.ts:

import { ExampleInterface } from './example-interface.ts';

export class ExampleClass implements ExampleInterface {
  
  myMethodOne(someArgument: number): void {
    ...some code here
  }
  myMethodTwo(someArgument: string, anotherArgument: boolean): void {
     ...some sode here
  }
  
  async myMethodWithSharedData(someArgument: string, offscreenCanvas: OffscreenCanvas): Promise<string> {
    return "some result string";
  }
}

example-interface.ts:

export interface ExampleInterface {
  myMethodOne(someArgument: number): void;
  myMethodTwo(someArgument: string, anotherArgument: boolean): void;
  myMethodWithSharedData(someArgument: string, offscreenCanvas: OffscreenCanvas): Promise<string>;
}

Now create worker client - with just method declarations (empty implementation)

client.ts:

import Worker from 'worker-loader!./worker';
import { WorkerClientBase, AutowiredMethodWithSharedData, AutowiredMethod } from 'worker-typescript-connector';
import { ExampleInterface } from './example-interface';

export class ExampleWorkerClient extends WorkerClientBase implements ExampleInterface {
  constructor() {
    super(new Worker());
  }

  /* 
   *  Call next methods annotated with @AutowiredMethod will be translated to worker calls with all arguments
   *  Implementation do not needed
   *  
   *  Call methods annotated with @AutowiredMethodWithSharedData will also pass shared data as OffscreenCanvas/ArrayBuffers and etc to worker
   */

  @AutowiredMethod
  myMethodOne(): void {}
  
  @AutowiredMethod
  myMethodTwo(): void {}
 
  @AutowiredMethodWithSharedData
  myMethodWithSharedData(): Promise<string> { return null; }

and worker.ts:

 import { startWorkerInterfaceFor } from 'worker-typescript-connector/dist/src/lib/worker-base';
 import { ExampleClass } from './example-class';
 
 const exampleClass = new ExampleClass();
 
 startWorkerInterfaceFor(exampleClass);

Just it, now, when call ExampleWorkerClient methods it will be called inside worker and return results back:

...

async function test() {
    const exampleWorkerClient = new ExampleWorkerClient();
    
    exampleWorkerClient.myMethodOne();

    exampleWorkerClient.myMethodTwo();

    console.log(await exampleWorkerClient.myMethodWithSharedData(...)); // Will print "some result string"
}

...
1.0.11

4 years ago

1.0.10

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago