Skip to content

Instantly share code, notes, and snippets.

@thadeu
Last active March 20, 2019 02:32
Show Gist options
  • Save thadeu/0e155da10245f1150a318a293e8c7a4f to your computer and use it in GitHub Desktop.
Save thadeu/0e155da10245f1150a318a293e8c7a4f to your computer and use it in GitHub Desktop.
šŸ§ Brain JS
export function brainstore() {
let store = {}
let listeners = []
let ownProp = Object.prototype.hasOwnProperty
let allowed = {
string: 1,
number: 1,
boolean: 1,
object: 1,
function: 0,
}
function dispatch(type) {
return listeners.forEach(l => l(store, type))
}
function use(props) {
if (typeof props === 'function') {
return props(store)
}
if (!props || props.length <= 0) {
return store
}
const selecteds = {}
for (let i in store) {
if (props.includes(i) && ownProp.call(store, i)) {
selecteds[i] = store[i]
}
}
return selecteds
}
return {
createStore: function(initialStore) {
store = initialStore
return this
},
is: function(name) {
return ownProp.call(store, name)
},
subscribe: function(callback) {
return listeners.push(callback)
},
map: function(callback) {
return callback && callback(use())
},
getState: function() {
return store
},
set: function(name, value, forceUpdae) {
if (!forceUpdae) {
if (this.is(name)) {
return this
}
}
if (!ownProp.call(allowed, typeof value) || allowed[typeof value] == 0) {
return this
}
store[name] = value
dispatch('set')
return this
},
get: function(prop) {
return use(prop)
},
use: function(props) {
return use(props)
},
clear: function(callback) {
store = {}
return callback && callback(this)
},
setState: function(props) {
let keys = Object.keys(props)
keys.map(key => this.set(key, props[key]))
return this
},
connect: function(props) {
return function(component) {
return component(use(props))
}
},
}
}
const log = console.log
export const store = new brainstore()
store.createStore({ state: 'INITIALIZED' })
store.subscribe(function(state, type) {
console.info(`$$subscribe ${type}`, state)
})
// store.set('maxwidth', 30).get('maxwidth')
// log('is:', store.is('maxwidth'))
// log('get:', store.get('maxwidth'))
// log('update:', store.set('maxwidth', 50).get('maxwidth'))
// store.set('height', { value: 1 }).get('height')
// store.setState({ age: 32, color: 'black', weight: 110 })
// store.map(function(entries) {
// log('map:', entries)
// })
// const { connect } = store
// const App = props => console.log(props)
// function mapToProps(props) {
// return {
// largura: props.maxwidth,
// altura: props.height,
// age: props.age,
// }
// }
// setTimeout(() => {
// store.set('weight', '100kg', true) // forceUpdate
// log('use: ', store.use('weight'))
// store.clear()
// }, 3000)
// console.time('set items using set')
// for (var i = 0; i < 100; i++) {
// store.set(`key_${i}`, i)
// }
// console.timeEnd('set items using set')
// console.time('set items using setState')
// for (var i = 0; i < 100; i++) {
// store.setState({ [`key_${i}`]: i })
// }
// console.timeEnd('set items using setState')
// console.time(`mapToProps`)
// connect(mapToProps)(App)
// console.timeEnd(`mapToProps`)
// console.time(`connect string`)
// connect('maxwidth,height,age')(App)
// console.timeEnd(`connect string`)
// console.time(`connect array`)
// connect(['height'])(App)
// console.timeEnd(`connect array`)
// console.time(`connect all props`)
// connect()(App)
// console.timeEnd(`connect all props`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment