Multithread emulator. The wrun allows you to dynamically run a function inside a Web Worker on the client side, without the needing of a dedicated file

Overview

wrun

This lib allows you to dynamically run a function inside a Web Worker on the client side, without the needing of a dedicated file. This means that you can execute a JS function as a subprocess, avoiding the slow down, break or freeze of the main thread. With this, you can emulate multithreading on your client side JavaScript code. This lib is also minimal: 811 bytes.

Reason

To create a Web Worker you commonly need a new .js file and a URL that points to this file, then you load this file as a worker: const worker = new Worker('http://site.com/your-file.js');.

With wrun you can dinamically run a function inside a Worker as a just in time subprocess execution, without the needing of a thirty part file: wrun(function() {});. This make way easier to handle background executions and dynamic subprocess.

Getting started

Download, or install wrun:

npm install wrun-fn

Then import, require or directly add it:

CommonJS

const wrun = require('wrun-fn');

ES6 Modules

import wrun from 'wrun-fn';

Browser

<script src="/path/wrun.js" type="application/javascript"></script>

Usage

To run a function inside a new Worker, just do:

const { worker, error } = wrun(function() {
  /** Your code **/
});

On the code above, the const worker will be your created Worker that will be running your function, and error will be false or - in case of error - an object { code: number, message: string }. A variation of this code would be:

function myFunction() {
  /** Your code **/
}

const $w = wrun(myFunction);

In the case above, the variable $w will be an object containing { worker, error } and you can access your worker on the property $w.worker and the error on the property $w.error.

Handling the Worker

The wrun always return an object:

{
  worker: WebWorker | null,
  error: boolean | { code: number, message: string }
}

You must use the worker property returned by the wrun to manage your Worker:

/** Create a worker **/
const { worker, error } = wrun(function() {
  self.addEventListener('message', console.log);
});

/** Send a message to our worker **/
worker.postMessage('Hello!');

The Worker will listen to the message and print the MessageEvent on the console. Your function can also access any Object or API restricted to Workers, as self and caches for example. You must also keep in mind that your function must communicate to your runtime using Worker Messages and vice versa, exactly as a normal Worker.

Handling Errors

When something goes wrong, wrun will return an object with an error property, this property haves the following structure:

{ code: number, message: string }

For example:

/** Try to create a worker with an invalid parameter **/
const { worker, error } = wrun(1);

if (error) {
  console.log('Error code: ', error.code, '. Message: ', error.message);
}

Since 1 is not a function, wrun will return a null worker, and a error property with {code: 4, message: 'The wrun argument must be a function'}. The console.log above will output:

Error code: 4. Message: The wrun argument must be a function

The wrun can return different kind of errors, but always using this pattern.

Use cases

The use cases for wrun are the same of any Web Worker, but with the benefit of "just in time" execution. Since Workers run on their own thread, they wont harm the main thread performance. That is good for highly intensive processing tasks, to load scripts on background, wasm tasks, parallelism, heavy subtasks and any kind of thing that you need to run without directly compromise the application performance. You can also use it to have access to Worker-Only APIs like caches api or global fetch events, for example.

Example

You can see wrun working here: https://felippe-regazio.github.io/wrun/

You might also like...

This plugin allows side-by-side notetaking with videos. Annotate your notes with timestamps to directly control the video and remember where each note comes from.

Obsidian Timestamp Notes Use Case Hello Obsidian users! Like all of you, I love using Obsidian for taking notes. My usual workflow is a video in my br

Jan 2, 2023

This Plugin is For Logseq. If you're using wide monitors, you can place journals, linked references, and journal queries side by side.

This Plugin is For Logseq. If you're using wide monitors, you can place journals, linked references, and journal queries side by side.

Logseq Column-Layout Plugin Journals, linked references, and journal queries can be placed side by side if the minimum screen width is "1850px" or mor

Dec 14, 2022

jQuery Terminal Emulator - JavaScript library for creating web-based terminals with custom commands

jQuery Terminal Emulator - JavaScript library for creating web-based terminals with custom commands

JavaScript Library for Web Based Terminal Emulators Summary jQuery Terminal Emulator is a plugin for creating command line interpreters in your applic

Jan 1, 2023

CLI utility that parses argv, loads your specified file, and passes the parsed argv into your file's exported function. Supports ESM/TypeScript/etc out of the box.

cleffa CLI tool that: Parses argv into an object (of command-line flags) and an array of positional arguments Loads a function from the specified file

Mar 6, 2022

Run RPC over a MessagePort object from a Worker thread (or WebWorker)

thread-rpc Run RPC over a MessagePort object from a Worker thread (or WebWorker) npm install thread-rpc Usage First in the parent thread const Thread

May 31, 2022

With this File Manager prepared for PHP/Js, you can perform all file operations on your server without any problems.

FileManager With this File Manager prepared for PHP/Js, you can perform all file operations on your server without any problems. Instead of downloadin

Sep 23, 2022

A Javascript library to export svg charts from the DOM and download them as an SVG file, PDF, or raster image (JPEG, PNG) format. Can be done all in client-side.

svg-exportJS An easy-to-use client-side Javascript library to export SVG graphics from web pages and download them as an SVG file, PDF, or raster imag

Oct 5, 2022

An adapter where you can define which function to run

Switch Functions An adapter where you can define which function to run Installation This is a Node.js module available through the npm registry. Befor

Jun 17, 2022

A cloudflare worker to use the user-agent for ~~rickrolling~~ without a discord embed

nextcord.gay A cloudflare worker to use the user-agent for rickrolling without a discord embed Build npm run build Find the output in ./dist/worker.mj

Oct 4, 2022
Owner
Felippe Regazio
software engineer, open sourcerer, intp, lifelong learner, linuxer, father, skateboarder. a strange carbon-based lifeform
Felippe Regazio
Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Jarred Sumner 19 Dec 19, 2022
Inside-out promise; lets you call resolve and reject from outside the Promise constructor function.

Inside-out promise; lets you call resolve and reject from outside the Promise constructor function.

Lily Scott 3 Feb 28, 2022
Pack all your node_modules and other files you want inside your project to a zip file.

?? Node Modules Packer Use Cases | Usage | Examples | Headless | Benchmarks | Reference This is a library to package all your node_modules and other f

Vinicius Lourenço 14 Dec 1, 2022
MerLoc is a live AWS Lambda function development and debugging tool. MerLoc allows you to run AWS Lambda functions on your local while they are still part of a flow in the AWS cloud remote.

MerLoc MerLoc is a live AWS Lambda function development and debugging tool. MerLoc allows you to run AWS Lambda functions on your local while they are

Thundra 165 Dec 21, 2022
Make drag-and-drop easier using DropPoint. Drag content without having to open side-by-side windows

Make drag-and-drop easier using DropPoint! DropPoint helps you drag content without having to open side-by-side windows Works on Windows, Linux and Ma

Sudev Suresh Sreedevi 391 Dec 29, 2022
Jonathan Parker 6 Nov 23, 2022
Run a command, watch the filesystem, stop the process on file change and then run the command again...

hubmon Run a command, watch the filesystem, stop the process on file change and then run the command again... Install You can install this command lin

Hubert SABLONNIÈRE 7 Jul 30, 2022
Fast and minimal JS server-side writer and client-side manager.

unihead Fast and minimal JS <head> server-side writer and client-side manager. Nearly every SSR framework out there relies on server-side components t

Jonas Galvez 24 Sep 4, 2022
Easy server-side and client-side validation for FormData, URLSearchParams and JSON data in your Fresh app 🍋

Fresh Validation ??     Easily validate FormData, URLSearchParams and JSON data in your Fresh app server-side or client-side! Validation Fresh Validat

Steven Yung 20 Dec 23, 2022
Cloudflare worker function to update github bio automatically with leetcode and codeforces profile stats 🚀

Stats check ?? Cloudflare worker function to update github bio automatically with leetcode and codeforces profile stats ?? You can see it in action he

Vaibhav Chopra 2 Feb 15, 2022