GithubHelp home page GithubHelp logo

anthrax3 / redux-bundler-worker-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from henrikjoreteg/redux-bundler-worker-example

0.0 1.0 0.0 133 KB

Redux-bundler with store and bundler running inside a worker

HTML 2.75% JavaScript 97.25%

redux-bundler-worker-example's Introduction

redux-bundler-worker-example

This is a somewhat sloppy but also quite complete example of how you can run an app written with redux-bundler entirely in a Web Worker. The worker maintains the redux store, and all redux-related functionality and code gets bundled into the worker.

The main thread simply runs Preact and handles UI.

This app is nearly a straight fork of the redux-bundler-example the important thing to note is that:

The bundles and UI code written for that app remained entirely unchanged

With very minor changes, things Just Work™ when moving this into a worker.

The main thread runs Preact and UI components and the same redux-bundler-preact bindings.

Notes and additional things demonstrated here:

  • The main thread kicks things off by passing in its URL to the worker.
  • Any locally cached data from indexedDB is read on load (just as the other example app), but this time that happens in the worker.
  • The cached data is combined with the initial data payload from the main thread and the store is created entirely inside the worker.
  • In the main thread we init the worker, and create a "store proxy" that mimics the relevant portions of the store API, but posts messages to the worker instead, where they are applied to the store as needed.
  • The main thread keeps a copy, not of the state but of the result of running all the selectors from the store. From then only deltas as passed to the main thread (this could be made even more efficient by only sending and tracking deltas of results the UI is currently using as indicated by selectors specified in connect()).
  • There are times when you need to be on the main thread, such as when you'd like to register global event listeners, or request geolocation, or any number or other APIs that are not yet available inside worker contexts (this, by the way, is where most examples of this type of thing seem to break down). But, it's possible though, not super pretty, to still keep such code in a redux-bundle. This is demonstrated by adding a "viewport bundle" here that essentially echoes the browser's viewport size to redux state. This type of thing is occasionally useful if we need to change, not just style, but behavior of an application based on viewport size. This is accomplished by serializing a function and passing it to the main thread for execution and allowing a callback to be triggered whenever required. This is done using my worker-proof library as you see used inside /src/bundles/viewport.js

It's deployed here: https://redux-bundler-worker.netlify.com/.

The main redux-bundler repo is here.

There's a lot more I could go into here, but I'll save that for a later time.

Just wanted to demonstrate what's possible with relatively minor modifications when an app is built using redux-bundler.

running it locally

npm i && npm start

Then open http://localhost:3000

license

MIT

redux-bundler-worker-example's People

Contributors

henrikjoreteg avatar

Watchers

 avatar

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.