How to use worker-rpc - 9 common examples

To help you get started, we’ve selected a few worker-rpc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TypeStrong / fork-ts-checker-webpack-plugin / src / cluster.ts View on Github external
const workerRpcs = workers.map(worker => {
  const rpc = new RpcProvider(message => {
    try {
      worker.send(message);
    } catch (e) {
      // channel closed - something went wrong - close cluster...
      process.exit();
    }
  });
  worker.on('message', message => rpc.dispatch(message));
  return rpc;
});
github TypeStrong / fork-ts-checker-webpack-plugin / src / service.ts View on Github external
IncrementalCheckerInterface,
  ApiIncrementalCheckerParams,
  IncrementalCheckerParams
} from './IncrementalCheckerInterface';
import { ApiIncrementalChecker } from './ApiIncrementalChecker';
import {
  makeCreateNormalizedMessageFromDiagnostic,
  makeCreateNormalizedMessageFromRuleFailure,
  makeCreateNormalizedMessageFromInternalError
} from './NormalizedMessageFactories';
import { RpcProvider } from 'worker-rpc';
import { RunPayload, RunResult, RUN } from './RpcTypes';
import { TypeScriptPatchConfig, patchTypescript } from './patchTypescript';
import { createEslinter } from './createEslinter';

const rpc = new RpcProvider(message => {
  try {
    process.send!(message, undefined, undefined, error => {
      if (error) {
        process.exit();
      }
    });
  } catch (e) {
    // channel closed...
    process.exit();
  }
});
process.on('message', message => rpc.dispatch(message));

const typescript: typeof ts = require(process.env.TYPESCRIPT_PATH!);
const patchConfig: TypeScriptPatchConfig = {
  skipGetSyntacticDiagnostics:
github TypeStrong / fork-ts-checker-webpack-plugin / src / index.ts View on Github external
path.resolve(
        __dirname,
        this.workersNumber > 1 ? './cluster.js' : './service.js'
      ),
      [],
      {
        env,
        execArgv: (this.workersNumber > 1
          ? []
          : ['--max-old-space-size=' + this.memoryLimit]
        ).concat(this.nodeArgs),
        stdio: ['inherit', 'inherit', 'inherit', 'ipc']
      }
    );

    this.serviceRpc = new RpcProvider(message => this.service!.send(message));
    this.service.on('message', message => this.serviceRpc!.dispatch(message));

    if ('hooks' in this.compiler) {
      // webpack 4+
      const forkTsCheckerHooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(
        this.compiler
      );
      forkTsCheckerHooks.serviceStart.call(
        this.tsconfigPath,
        this.tslintPath,
        this.watchPaths,
        this.workersNumber,
        this.memoryLimit
      );
    } else {
      // webpack 2 / 3
github TypeStrong / fork-ts-checker-webpack-plugin / test / integration / helpers / rpc.js View on Github external
exports.getRpcProvider = () => {
  if (!rpc) {
    rpc = new RpcProvider(message => {
      if (process && process.send) {
        return process.send(message);
      }
    });
    process.on('message', message => rpc.dispatch(message));
  }

  return rpc;
};
github TypeStrong / fork-ts-checker-webpack-plugin / test / integration / oldHelpers.js View on Github external
exports.getRpcProvider = () => {
  if (!rpc) {
    rpc = new RpcProvider(message => process.send(message));
    process.on('message', message => rpc.dispatch(message));
  }
  return rpc;
};
github 6502ts / 6502.ts / src / web / stella / service / worker / VideoDriver.ts View on Github external
init(videoPipelinePort?: MessagePort): void {
        this._rpc
            .registerSignalHandler(SIGNAL_TYPE.videoReturnSurface, this._onReturnSurfaceFromHost.bind(this))
            .registerRpcHandler(RPC_TYPE.getVideoParameters, this._onGetVideoParameters.bind(this));

        if (videoPipelinePort) {
            const videoPipelineRpc = new RpcProvider((data: any, transfer?: any) =>
                videoPipelinePort.postMessage(data, transfer)
            );
            videoPipelinePort.onmessage = (e: MessageEvent) => videoPipelineRpc.dispatch(e.data);

            this._videoPipelineClient = new VideoPipelineClient(videoPipelineRpc);

            this._videoPipelineClient.emit.addHandler(VideoDriver._onEmitFromPipeline, this);
        }
    }
github 6502ts / 6502.ts / src / web / stella / service / worker / EmulationService.ts View on Github external
init(): Promise {
        this._worker = new Worker(this._stellaWorkerUri);
        this._rpc = new RpcProvider((message, transfer?) => this._worker.postMessage(message, transfer));

        this._pcmChannel = new PCMAudioProxy(0, this._rpc).init();

        for (let i = 0; i < 2; i++) {
            this._waveformChannels[i] = new WaveformAudioProxy(i, this._rpc).init();
        }

        const videoProxy = new VideoProxy(this._rpc),
            controlProxy = new ControlProxy(this._rpc);

        videoProxy.init();

        this._emulationContext = new EmulationContext(
            videoProxy,
            controlProxy,
            this._waveformChannels,
github 6502ts / 6502.ts / src / video / processing / worker / PipelineClient.ts View on Github external
static spawn(workerUrl = 'video-pipeline.js'): PipelineClient {
        const worker = new Worker(workerUrl),
            rpc = new RpcProvider((message: any, transfer: Array) => worker.postMessage(message, transfer));

        worker.onmessage = messageEvent => rpc.dispatch(messageEvent.data);

        return new PipelineClient(rpc);
    }
github 6502ts / 6502.ts / src / web / stella / service / worker / EmulationService.ts View on Github external
private async _startVideoProcessingPipeline(): Promise {
        let channel: MessageChannel = null;

        if (this._videoWorkerUri) {
            channel = new MessageChannel();

            const worker = new Worker(this._videoWorkerUri),
                rpc = new RpcProvider((payload: any, transfer?: any) => worker.postMessage(payload, transfer));

            worker.onmessage = (e: MessageEvent) => rpc.dispatch(e.data);

            await rpc.rpc('/use-port', channel.port1, [channel.port1]);
        }

        await this._rpc.rpc(
            RPC_TYPE.setup,
            {
                videoProcessorPort: channel && channel.port2
            },
            channel ? [channel.port2] : []
        );
    }

worker-rpc

A simple RPC layer for communicating with web workers and over other transports

MIT
Latest version published 5 years ago

Package Health Score

65 / 100
Full package analysis

Popular worker-rpc functions