inline-iso-worker
v0.4.2
Published
Worker que trabalha com callbacks no lugar de arquivos
Downloads
7
Readme
inline-iso-worker 🇧🇷
Isomorfic worker that works with inline callbacks instead of files.
Installation
npm install inline-iso-worker
yarn add inline-iso-worker
Inline worker
Default package export.
Constructors
constructor<TScope, TCallback>(scope: TScope, callback: TCallback)
Creates a worker with the specified scope and callabck. The scope will be assigned to the function's lexical this
. An exception will occur if a arrow function is provided. The rest of the arguments will be the arguments provided to the run()
method.
constructor<TScope>(callback: TCallback)
Creates a worker without scope and with the specified callback. An arrow function can also be provided.
Methods
async run(...args: Parameters<TCallback>): Promise<ReturnType<TCallback>>
Performs the callback provided in the constructor with the given arguments and returns the resulting value from the callback.
Example
import InlineWorker from 'inline-iso-worker'
const worker = new InlineWorker({ prefix: 'Hello ', suffix: '. Be welcome', }, function(name: string) {
return this.prefix + name + this.suffix
})
async someConcurrentFunction() {
const return1 = await worker.run('Filipe')
const return2 = await worker.run('Roberto')
const return3 = await worker.run('Beck')
console.log(return1) // Prints "Hello Filipe. Be welcome"
console.log(return2) // Prints "Hello Roberto. Be welcome"
console.log(return3) // Prints "Hello Beck. Be welcome"
}
Cluster
Handles multiple workers who performs the same callback synchronously.
Constructors
constructor<TScope, TCallback>(scopes: Scope[], callback: TCallback)
Creates a cluster containing a worker for each provided scope, all sharing the specified callback.
Methods
async run(...args: Parameters<TCallback>): Promise<ReturnType<TCallback>[]>
Performs the callback provided in the constructor with the provided arguments for each worker and returns an array with the resulting values from each worker. If the return value is a Promise
, returns the resolved value.
Example
import { Cluster } from 'inline-iso-worker'
const scopes = [{
prefix: 'Hello ', suffix: '. Be welcome',
prefix: 'Bye ', suffix: '',
preffix: '', suffix: ''
}]
const cluster = new Cluster(scopes, function(name: string) {
return this.prefix + name + this.suffix
})
async someConcurrentFunction() {
const [return1, return2, return3] = await cluster.run('Filipe')
console.log(return1) // Prints "Hello Filipe. Be welcome"
console.log(return2) // Prints "By Filipe"
console.log(return3) // Prints "Filipe"
}
🇧🇷
[pt-BR]
Worker isomórfico que trabalha com callbacks inline no lugar de arquivos.
Instalação
npm install inline-iso-worker
yarn add inline-iso-worker
Inline worker
Exportação default do pacote.
Construtores
constructor<TScope, TCallback>(scope: TScope, callback: TCallback)
Cria um Worker com o escopo e callback especificados. O escopo será atribuido ao this
léxico da função. Ocorrerá uma exceção se uma arrow function for fornecida. Os demais argumentos serão os argumentos fornecidos ao método run()
.
constructor<TScope>(callback: TCallback)
Cria um worker sem escopo e com o callback especificado. Uma arrow function também pode ser fornecida.
Métodos
async run(...args: Parameters<TCallback>): Promise<ReturnType<TCallback>>
Executa o callback fornecido no construtor com os argumentos fornecidos e retorna o valor resultante do callback.
Exemplo
import InlineWorker from 'inline-iso-worker'
const worker = new InlineWorker({ prefix: 'Olá ', suffix: '. Seja bem vindo', }, function(name: string) {
return this.prefix + name + this.suffix
})
async someConcurrentFunction() {
const return1 = await worker.run('Filipe')
const return2 = await worker.run('Roberto')
const return3 = await worker.run('Beck')
console.log(return1) // Imprime "Olá Filipe. Seja bem vindo"
console.log(return2) // Imprime "Olá Roberto. Seja bem vindo"
console.log(return3) // Imprime "Olá Beck. Seja bem vindo"
}
Cluster
Manipula múltiplos workers que executam o mesmo callback de forma sincronizada.
Construtores
constructor<TScope, TCallback>(scopes: Scope[], callback: TCallback)
Cria um Cluster contendo um worker para cada escopo fornecido, todos compartilhando o callback especificado.
Métodos
async run(...args: Parameters<TCallback>): Promise<ReturnType<TCallback>[]>
Executa o callback fornecido no construtor com os argumentos fornecidos para cada worker e retorna um array com os valores resultantes de cada worker. Se o valor de retorno for uma Promise
, retorna o valor resolvido.
Exemplo
import { Cluster } from 'inline-iso-worker'
const scopes = [{
prefix: 'Olá ', suffix: '. Seja bem vindo',
prefix: 'Tchau ', suffix: '',
preffix: '', suffix: ''
}]
const cluster = new Cluster(scopes, function(name: string) {
return this.prefix + name + this.suffix
})
async someConcurrentFunction() {
const [return1, return2, return3] = await cluster.run('Filipe')
console.log(return1) // Imprime "Olá Filipe. Seja bem vindo"
console.log(return2) // Imprime "Tchau Filipe"
console.log(return3) // Imprime "Filipe"
}