npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-rs-simple-worker

v1.0.10

Published

An injectable service framework that provides a streamlined way of dispatching web worker tasks and handling their callbacks.

Downloads

21

Readme

ngx-rs-simple-worker

An injectable service framework that provides a streamlined way of dispatching web worker tasks and handling their callbacks.

Packaged for Angular ^16.2.0

How to use:

First:

  • Create your Worker the angular way, with the Angular Cli
  • This automatically extends your tsconfig with a tsconfig.worker.json, and creates a worker file with the necessary /// reference lib="webworker" heading
  • For example, we will make a worker file called services/workerthread.worker.ts:
ng generate web-worker services/workerthread.worker.ts

Then:

  • Inject the NgxSimpleWorkerService into your application
  • Instantiate your worker on the dtWorker:Worker property of the NgxSimpleWorkerService, preferably in your application's root component ( app.component.ts )
  • Initialize it with the 'bootstrapWebWorker' method
  • Make requests to the worker wherever your NgxSimpleWorkerService is injected with the 'makeWorkerRequest' method
  • Bind calback functions with the 'onWorkerReturn' : Record<string, IWorkerReturn>
//import these
import { NgxSimpleWorkerService } from 'ngx-rs-simple-worker';
import { WorkerReturn } from 'ngx-rs-simple-worker/shared';
...

constructor( _workerSvc: NgxSimpleWorkerService) {
	//set things up
	_workerSvc.dtWorker = new Worker(new URL('./services/workerthread.worker.ts', import.meta.url), { type: 'module' });
	_workerSvc.bootstrapWebWorker();
	
	//run a worker task called "TESTME"
	_workerSvc.makeWorkerRequest({data:"HELLO", directive:"TESTME"});

	//handle the response from "TESTME"
	_workerSvc.onWorkerReturn["TESTME"] = {
		ProcessReturn: (returnData: WorkerReturn) => { 
			console.log("got return from :" + returnData.directive);
			console.log("data is :" + returnData.value)
			console.log( "error is: "+ returnData.err)
		}
	};
}

And, In your Worker file,

  • Instantiate a SimpleWorker instance using the workers 'self'
  • Attach your worker tasks to the SharedNGWorker's 'workerDirectives' : Record<string, IWorkerDirective>
//import these
import { SimpleWorker, WorkerRequest, WorkerReturn } from 'ngx-rs-simple-worker/shared';

var WC = new SimpleWorker(self);

//your "TESTME" task function goes here!
WC.workerDirectives["TESTME"] = { ProcessRequest: (req: WorkerRequest)=>{
		let responseMessage = req.data+" NASTY";
		let retr : WorkerReturn = {directive:"TESTME", value: responseMessage, err: null }
		postMessage(retr);
	}
}

And thats it!

The data exchange intefaces look like this:

/**
 * interface WorkerReturn
 *	- directive: {string} key for the onWorkerReturn {IWorkerReturn} callback function 
 *	- vaule: object of {any} type, returned from worker thread 
 *	- error: object of {error|null} type, returned from worker thread in case of error,	to be checked upon callback return
 */
 export interface WorkerReturn {
	/**  directive: {string} key for the onWorkerReturn {IWorkerReturn} callback function */
	directive: string
	/**  vaule: object of {any} type, returned from worker thread */
	value: any
	/**  error: object of {error|null} type, returned from worker thread in case of error, to be checked upon callback return */
	err: Error | null
}

/**
 * interface IWorkerReturn
 *	- @param {function(WorkerReturn):void} ProcessReturn - function signature for onWorkerReturn callback
 */
export interface IWorkerReturn {
	ProcessReturn(returner: WorkerReturn): void
}

/**
 * interface IWorkerDirective 
 *	- @param {function(WorkerRequest):void} ProcessReturn - function signature for IWorkerDirective handler endpoint. 
 * Maps to, and executes, a handler at workerDirectives[[WorkerRequest.directive]] with the parameter arguments WorkerRequest.data
 * 	- @returns ProcessRequest has no return but must execute postMessage with a {WorkerReturn} object.
 *  I.E. postMessage({directive:"SOME_DIRECTIVE@Caller's_onWorkerReturn", value:"SOME_VALUE", err: null })
 */
 export interface IWorkerDirective {
	ProcessRequest(req: WorkerRequest): void
}


/**
 * interface WorkerRequest
 *	- @param {string} directive - lookup key for callbakc function in worker thread's workerDirectives list of {IWorkerDirective} endpoints
 *	- @param {any} data - data to be supplied by copy to {IWorkerDirective} endpoints. 
 *	Warnging, any in memory references to other data in the UI thread within this data object will be lost.
 */
export interface WorkerRequest {
	directive: string
	data: any
}