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

diffhtml-middleware-worker

v1.0.0-beta.30

Published

Provides worker rendering for client and server

Downloads

29

Readme

<±/> diffHTML Worker Middleware

Stable Version: 1.0.0-beta.23

Used to separate the diffing and patching stages of rendering across threads. The main thread receives payloads containing mutations to apply, all UI logic is isolated within a worker thread.

Can be used with Node.js servers and pass patches to the client using Web Sockets.

Installation

npm install diffhtml-middleware-worker

Usage

When you create a worker, you bind it to a mount point and all innerHTML or outerHTML calls that get made in the worker are intercepted and passed to the main thread.

In a browser

To create a web worker in the browser, import the createWebWorker function. Invoke this with a file path pointing to the location of your module that contains the rendering logic. This file path will be explained more after the following code snippet.

You must import either the core or lite version in the main thread to do the actual patching. The lite version is preferable due to the smaller filesize. Then register the middleware into the main thread.

import { use } from 'https://diffhtml.org/core/lite';
import { mainTask, createWebWorker } from 'https://diffhtml.org/middleware-worker';

use(mainTask());

Once the middleware is registered, you can create web workers using the helper function. If you already have a worker you can pass it instead of a string to have the events hooked up.

const mount = document.body;
createWebWorker('./path/to/worker.js', { mount });

The above code will create a worker that exists at worker.js and proxy all rendering from it into the mount point, in this case the <body> tag. You must register the workerTask middleware inside the worker to ensure patches are properly passed to the main thread.

import { use, html, createTree, innerHTML } from 'https://diffhtml.org/core';
import { workerTask } from 'https://diffhtml.org/middleware-worker';

use(workerTask());

In the worker you must create a placeholder to render into. This will emulate the mount in the main thread.

// Create an empty fragment to render into, this represents the body tag
// in the main thread.
const placeholder = createTree();

// Any outerHTML or innerHTML calls will be proxied to the main thread mount
// point.
innerHTML(placeholder, html`
  <h1>Hello world from a worker thread!</h1>
`);

With Node.js