All Projects → mutebg → WorkerStore

mutebg / WorkerStore

Licence: MIT license
Small React state container running inside WebWorker

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to WorkerStore

moonwave
🌗 A small web application framework.
Stars: ✭ 14 (-56.25%)
Mutual labels:  preact
kvs
Lightweight key-value storage library for Browser, Node.js, and In-Memory.
Stars: ✭ 126 (+293.75%)
Mutual labels:  webworker
fpreact
fpreact provides an alternative api for creating preact components, heavily inspired by elm.
Stars: ✭ 47 (+46.88%)
Mutual labels:  preact
maji
Maji is a framework to build great hybrid mobile apps.
Stars: ✭ 18 (-43.75%)
Mutual labels:  preact
merkur
tiny extensible javascript library for front-end microservices
Stars: ✭ 45 (+40.63%)
Mutual labels:  preact
preact-cli-plugin-netlify
Preact cli plugin for generating h2push headers and redirects rules for netlify
Stars: ✭ 25 (-21.87%)
Mutual labels:  preact
PreactSimpleStarter
PWA Simple Starter with Preact, Preact-mdl and Webpack2 🔥🔥🔥
Stars: ✭ 65 (+103.13%)
Mutual labels:  preact
bassdrum
reactive, type safe components with preact and rxjs.
Stars: ✭ 44 (+37.5%)
Mutual labels:  preact
anonymous-web
💬 A PreactJS powered progressive web (chat) application (Not active)
Stars: ✭ 28 (-12.5%)
Mutual labels:  preact
preact-journal
14k offline-capable journaling PWA using preact, node, MySQL, and IndexedDB.
Stars: ✭ 33 (+3.13%)
Mutual labels:  preact
rainbow-explorer
🌈 A 20kb Preact & Redux based Progressive Web App that translates real life color to digital color.
Stars: ✭ 26 (-18.75%)
Mutual labels:  preact
go-preact-starter
Starter for combining Go and Preact in any web project.
Stars: ✭ 19 (-40.62%)
Mutual labels:  preact
luck-or-hardwork
Simple web-app to provide illustration about a take on luck and hard work.
Stars: ✭ 29 (-9.37%)
Mutual labels:  preact
agrippa
The CLI for frontend component generation
Stars: ✭ 555 (+1634.38%)
Mutual labels:  preact
progressive-web-app-starter
Preact based starter kit for making a Progressive Web App (PWA).
Stars: ✭ 19 (-40.62%)
Mutual labels:  preact
browser-extension
Browser Extension Template with ESbuild builds, support for React, Preact, Typescript, Tailwind, Manifest V3/V2 support and multi browser build including Chrome, Firefox, Safari, Edge, Brave.
Stars: ✭ 535 (+1571.88%)
Mutual labels:  preact
preact-native
client native implement of preactjs(https://preactjs.com/)
Stars: ✭ 17 (-46.87%)
Mutual labels:  preact
tic-tac-toe-app
Online multiplayer Tic Tac Toe game for iOS, Android, and web.
Stars: ✭ 34 (+6.25%)
Mutual labels:  preact
preact-styled-jsx-demo
Preact + styled-jsx = 💞
Stars: ✭ 16 (-50%)
Mutual labels:  preact
tailwind-layouts
Collection of Tailwind Layouts
Stars: ✭ 53 (+65.63%)
Mutual labels:  preact

Worker-Store

1kb state container running inside WebWorker. A similar idea to Redux, besides the action-reducers run inside WebWorker. To do that, dispatch sends only the name of the actions and payload.

Offload expensive work to a WebWorker might improve significant performance. While Main ( UI ) thread can be blocked from expensive operations, the WebWorker solves that like messaging results ( state ) of the operations to UI thread.

Examples

React example

Live demo

Installation

npm install --save worker-store

Documentaion

API documentation

Usage

You need worker loader, something like https://github.com/webpack-contrib/worker-loader

In your app.js

import { createStore } from 'worker-store';
import { Provider, connect  } from 'worker-store/react';
import Worker from './store.worker.js';

// initial store state
const initState = {count: 0, news: []};

// create store
const store = createStore( initState , new Worker() );

// Select state from the store
const mapStateToProps = ( state, ownProps) => ({
    count: state.count,
    news: state.news,
});

//Connects React/Preact component to the store.
const App = connect(mapStateToProps)(({count, news, dispatch}) => {
  return (
    <div>
      <h1>{count}</h1>
      <button onClick={ () => dispatch('inc')}>+1</button>
      <button onClick={ () => dispatch('dec')}>-1</button>
      <button onClick={ () => dispatch('fetch', 5)}>fetch news</button>
      <button onClick={ () => dispatch('generator')}>generator</button>
    </div>
  )
});

// Makes the store available to the connect() calls in the component hierarchy below.
export default () => (
  <Provider store={store}>
    <App />
  </Provider>,
)

Inside your web worker: store.worker.js

import { runStore, put } from "worker-store/worker";

// runStore receive objet of actions
// every action receive current state as first parameter
// and rest as parameters from dispatch function
// action must return the next state  or part of it
runStore({
  inc: state => ({ count: state.count + 1 }),
  dec: state => ({ count: state.count - 1 }),

  // can return Promise which resolves into next state
  fetch: (state, payload) =>
    fetch("https://jsonplaceholder.typicode.com/posts/" + payload)
      .then(res => res.json())
      .then(json => ({ news: json })),

  // or generator function which yield next state
  generator: function*(state) {
    try {
      // using yield and put to update the state
      yield put({ status: "loading" });
      const response = yield fetch(
        "https://jsonplaceholder.typicode.com/posts/1"
      );
      const news = yield response.json();
      yield put({ news: news, status: "done" });
    } catch (err) {
      yield put({ status: "loaded", error: true });
    }
  }
});

Debug

You can use kuker to debug state and actions, very similar way redux-dev-tools does

store.subscribe(({ state, action }) => {
  window.postMessage(
    {
      kuker: true,
      type: "change state",
      origin: "none",
      label: action,
      time: new Date().getTime(),
      state: state
    },
    "*"
  );
});

Inspiration

Inspired by https://github.com/developit/unistore https://github.com/developit/stockroom

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].