How to use the threads.Worker function in threads

To help you get started, we’ve selected a few threads 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 ChainSafe / lodestar / packages / lodestar / src / chain / factory / block / index.ts View on Github external
if(slot > currentState.slot) {
    processSlots(config, currentState, slot);
  }
  const merkleTree = await db.merkleTree.getProgressiveMerkleTree(config, currentState.eth1DepositIndex);
  const parentHeader: BeaconBlockHeader = blockToHeader(config, parentBlock);
  const block: BeaconBlock = {
    slot,
    parentRoot: signingRoot(parentHeader, config.types.BeaconBlockHeader),
    // @ts-ignore
    signature: undefined,
    // @ts-ignore
    stateRoot: undefined,
    body: await assembleBody(config, opPool, eth1, merkleTree, currentState, randao),
  };

  const stat = await spawn(new Worker("../../../../../eth2.0-state-transition/src/stateTransition"))

  block.stateRoot = hashTreeRoot(
    await stat(config, currentState, block, false, false),
    config.types.BeaconState
  );

  await Thread.terminate(stat)
  return block;
}
github satoshipay / solar / src / lib / stellar-expert.ts View on Github external
export async function fetchWellknownAccounts(testnet: boolean): Promise {
  const cacheKey = testnet ? "known-accounts:testnet" : "known-accounts:mainnet"

  const cachedAccountsString = localStorage.getItem(cacheKey)
  const timestamp = localStorage.getItem("known-accounts:timestamp")

  if (cachedAccountsString && timestamp && +timestamp > Date.now() - 24 * 60 * 60 * 1000) {
    // use cached accounts if they are not older than 24h
    return JSON.parse(cachedAccountsString)
  } else {
    const fetcher = await spawn(new Worker("../workers/fetch-worker.ts"))
    const knownAccounts = await fetcher.fetchWellknownAccounts(testnet)
    await Thread.terminate(fetcher)

    localStorage.setItem(cacheKey, JSON.stringify(knownAccounts))
    localStorage.setItem("known-accounts:timestamp", Date.now().toString())
    return knownAccounts
  }
}
github higlass / higlass / app / scripts / data-fetchers / bam-fetcher.js View on Github external
constructor(dataConfig) {
    this.dataConfig = dataConfig;
    this.uid = slugid.nice();

    this.worker = spawn(
      new Worker('./bam-fetcher-worker.js')
    );

    this.initPromise = this.worker.then((tileFunctions) => {
      console.log('tileFunctions:', tileFunctions);
    
      return tileFunctions.init(
        this.uid, dataConfig.url, dataConfig.chromSizesUrl
      ).then(() => this.worker);
    });

    console.log('constructor');
  }