Skip to content

Commit

Permalink
refactor(store): use a custom implementation of redux
Browse files Browse the repository at this point in the history
custom implementation can be easily optimized for performance and smaller library footprint
  • Loading branch information
tusharmath committed Sep 5, 2016
1 parent 9beed86 commit 131e843
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"validate-commit-msg": "^2.8.0"
},
"dependencies": {
"redux": "^3.5.2",
"window-or-global": "^1.0.1"
}
}
2 changes: 1 addition & 1 deletion src/createWCProto.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
'use strict'

import {createStore} from 'redux'
import {createStore} from './micro-redux'
import CustomEvent from './CustomEvent'

function isArray (i) {
Expand Down
36 changes: 36 additions & 0 deletions src/micro-redux.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 131e843

Please sign in to comment.