GithubHelp home page GithubHelp logo

developit / preact-worker-demo Goto Github PK

View Code? Open in Web Editor NEW
225.0 8.0 16.0 18 KB

Demo of preact rendering an entire app in a Web Worker.

Home Page: https://preact-worker-demo.surge.sh

JavaScript 92.43% HTML 1.63% CSS 5.94%
web-worker preact workers thread background-thread virtual-dom dom demo webpack

preact-worker-demo's Introduction

A full Preact app rendering in a Web Worker gitter

Have you ever wondered if you could take advantage of Web Workers to render a Virtual DOM app in a background thread? This repo contains the source code of a demo messaging app that does just that! ๐ŸŒˆ

โšก๏ธ A complete Preact app running entirely within a Web Worker.

๐Ÿ’ For a high level overview of the approach, see How It Works.

๐Ÿš€ This means your UI stays interactive at 60FPS, even if your application grinds to a halt in the background.


How It Works

The implementation is split into renderer/dom.js and renderer/worker.js. These modules live outside and inside the Worker (respectively), and communicate with eachother asynchronously via postMessage().

The app's code, components, libraries and DOM are all isolated in a single Worker (background thread). This means even Preact's diff algorithm and component instantiation is done in the worker. The main (UI) thread simply applies a stream of serialized DOM change descriptions (MutationRecords), and proxies events back to the Worker to be handled off the main thread.

As an optimization, when serializing DOM Elements to be published up to the UI thread, any previously-sent Elements are replaced with IDs. These are correlated through a mapping retained on both sides of the thread boundary.

Quick-Start Guide

Installation

1. Clone this repo:

git clone --depth 1 https://github.com/developit/preact-worker-demo.git
cd preact-worker-demo

2. Install the dependencies:

npm install

You're done installing! Now let's get started developing.

Development Workflow

3. Start a live-reload development server:

PORT=8080 npm run dev

4. Generate a production build in ./build:

npm run build

You can now deploy the contents of the build directory to production!

Surge.sh Example: surge ./build -d my-app.surge.sh

5. Start local production server with superstatic:

npm start

This is to simulate a production (CDN) server with gzip. It just serves up the contents of ./build.


License

MIT

preact-worker-demo's People

Contributors

developit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

preact-worker-demo's Issues

How to redo this with Comlink?

Since comlink achive to do the same with redux in a friendly way. I want to do the same with the preact dom, How can I achive this ;c. I'm unable to understand the requirements for this. Can you guide me with the very basic?

Do I need undom?

not compatible with [email protected]

First, thanks to your demo, it really helped me a lot.

I run script in javascriptCore, and get the undom. then I send the message to webview to create real dom or update the dom.
But I find it works with [email protected], but not 8.2.5. Can you help me?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.