From 131e843720490b39298fe35caa7ccc868346c8b8 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Mon, 5 Sep 2016 17:22:53 +0530 Subject: [PATCH 1/2] refactor(store): use a custom implementation of redux custom implementation can be easily optimized for performance and smaller library footprint --- package.json | 1 - src/createWCProto.js | 2 +- src/micro-redux.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/micro-redux.js diff --git a/package.json b/package.json index c076547..d09316e 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,6 @@ "validate-commit-msg": "^2.8.0" }, "dependencies": { - "redux": "^3.5.2", "window-or-global": "^1.0.1" } } diff --git a/src/createWCProto.js b/src/createWCProto.js index 51c3efb..7571a98 100644 --- a/src/createWCProto.js +++ b/src/createWCProto.js @@ -6,7 +6,7 @@ */ 'use strict' -import {createStore} from 'redux' +import {createStore} from './micro-redux' import CustomEvent from './CustomEvent' function isArray (i) { diff --git a/src/micro-redux.js b/src/micro-redux.js new file mode 100644 index 0000000..3f7a4e0 --- /dev/null +++ b/src/micro-redux.js @@ -0,0 +1,36 @@ +/** + * Created by tushar.mathur on 05/09/16. + */ + +'use strict' + +const INIT = {type: '@@redux/INIT'} + +export class Store { + constructor (reducer, state) { + this.__reducer = reducer + this.__state = state + this.__listners = [] + this.__dispatching = false + this.dispatch(INIT) + } + + subscribe (listener) { + const i = this.__listners.push(listener) + return () => this.__listners.splice(i - 1, 1) + } + + getState () { + return this.__state + } + + dispatch (action) { + if (this.__dispatching) return + this.__dispatching = true + this.__state = this.__reducer(this.__state, action) + this.__listners.forEach(i => i(this.__state)) + this.__dispatching = false + } +} + +export const createStore = (reducer, state) => new Store(reducer, state) From 42da1041252f3efd07f9093b5327ca09b1b46bb9 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Mon, 5 Sep 2016 17:28:25 +0530 Subject: [PATCH 2/2] docs(readme): updated docs --- README.md | 6 ++++++ docs/README.template.md | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/README.md b/README.md index 6ecc320..4da1cb6 100644 --- a/README.md +++ b/README.md @@ -246,3 +246,9 @@ Creates the prototype for the web component element. | component.update | function | a redux reducer for updating component state. | | component.view | function | takes in the state and returns a dom tree. | + +### Show your support +⭐ this repo + +Would really appreciate if you could suggest improvements in docs. + diff --git a/docs/README.template.md b/docs/README.template.md index 4825c73..8b71ff6 100644 --- a/docs/README.template.md +++ b/docs/README.template.md @@ -228,3 +228,9 @@ update (state, {type, params}) { {{>main}} + +### Show your support +⭐ this repo + +Would really appreciate if you could suggest improvements in docs. +