tunnckocorelabs / elmaz Goto Github PK
View Code? Open in Web Editor NEWUnidirectional, functional and robust state management in 800 bytes, a la Elm Architecture
License: BSD 3-Clause "New" or "Revised" License
Unidirectional, functional and robust state management in 800 bytes, a la Elm Architecture
License: BSD 3-Clause "New" or "Revised" License
Here's the Weekly Digest for tunnckoCoreLabs/elmaz:
Last week, no issues were created.
Last week, no pull requests were created, updated or merged.
Last week there were no commits.
Last week there were no contributors.
Last week there were no stargazers.
Last week there were no releases.
That's all for last week, please ๐ Watch and โญ Star the repository tunnckoCoreLabs/elmaz to receive next weekly updates. ๐
You can also view all Weekly Digests by clicking here.
Your Weekly Digest bot. ๐
Here's the Weekly Digest for tunnckoCoreLabs/elmaz:
Last week, no issues were created.
Last week, no pull requests were created, updated or merged.
Last week there were no commits.
Last week there were no contributors.
Last week there were no stargazers.
Last week there were no releases.
That's all for last week, please ๐ Watch and โญ Star the repository tunnckoCoreLabs/elmaz to receive next weekly updates. ๐
You can also view all Weekly Digests by clicking here.
Your Weekly Digest bot. ๐
Here's the Weekly Digest for tunnckoCoreLabs/elmaz:
Last week, no issues were created.
Last week, no pull requests were created, updated or merged.
Last week there were no commits.
Last week there were no contributors.
Last week there were no stargazers.
Last week there were no releases.
That's all for last week, please ๐ Watch and โญ Star the repository tunnckoCoreLabs/elmaz to receive next weekly updates. ๐
You can also view all Weekly Digests by clicking here.
Your Weekly Digest bot. ๐
Here's the Weekly Digest for tunnckoCoreLabs/elmaz:
This week, no issues have been created or closed.
This week, no pull requests has been proposed by the users.
This week, no user has contributed to this repository.
This week, no user has starred this repository.
This week, there have been no commits.
This week, no releases were published.
That's all for this week, please watch ๐ and star โญ tunnckoCoreLabs/elmaz to receive next weekly updates. ๐
Here's the Weekly Digest for tunnckoCoreLabs/elmaz:
Last week, no issues were created.
Last week, no pull requests were created, updated or merged.
Last week there were no commits.
Last week there were no contributors.
Last week there were no stargazers.
Last week there were no releases.
That's all for last week, please ๐ Watch and โญ Star the repository tunnckoCoreLabs/elmaz to receive next weekly updates. ๐
You can also view all Weekly Digests by clicking here.
Your Weekly Digest bot. ๐
Here's the Weekly Digest for tunnckoCoreLabs/elmaz:
Last week, no issues were created.
Last week, no pull requests were created, updated or merged.
Last week there were no commits.
Last week there were no contributors.
Last week there were no stargazers.
Last week there were no releases.
That's all for last week, please ๐ Watch and โญ Star the repository tunnckoCoreLabs/elmaz to receive next weekly updates. ๐
You can also view all Weekly Digests by clicking here.
Your Weekly Digest bot. ๐
backup
index.js
/**
* @copyright 2017-present, Charlike Mike Reagent <[email protected]>
* @license Apache-2.0
*/
import createElement from 'minscript'
import morph from 'minmorph'
const h = createElement
// state management
import createView from './view.js'
import createStore from './store.js'
import createEffects from './effects.js'
import createReducers from './reducers.js'
export { h, createElement, morph, createView, createStore, createEffects, createReducers }
view.js
/**
* @copyright 2017-present, Charlike Mike Reagent <[email protected]>
* @license Apache-2.0
*/
import assert from 'assert'
import { isObject } from './utils.js'
export default function createView (store, actions, viewFn) {
assert(isObject(store), 'expect `store` to be an object')
assert(isObject(actions), 'expect `actions` to be an object')
assert(typeof viewFn === 'function', 'expect `viewFn` to be a function')
let el = null
let context = {
state: store.getState(),
actions,
done: (er) => {
store.emit('error', er)
},
}
let view = (...args) => {
try {
return viewFn(...args)
} catch (er) {
context.done(er)
}
}
store.off('stateChange')
store.on('stateChange', (ctx) => {
console.log(ctx.state)
context = Object.assign({}, context, ctx)
console.log(context.state)
store.emit('render', view, context, (fn) => (el = fn(el)))
})
store.emit('render', view, context, (fn) => (el = fn(el)))
return el
}
utils.js
const isObject = (val) => val && typeof val === 'object' && !Array.isArray(val)
export { isObject }
store.js
/**
* @copyright 2017-present, Charlike Mike Reagent <[email protected]>
* @license Apache-2.0
*/
import createEmitter from './emitter.js'
export default function createStore (state) {
const store = createEmitter()
store._state = state
// always freezed state object
store.getState = () => Object.freeze(Object.assign({}, store._state))
// 1. if `next` function, pass freezed state to it,
// and "hard update" the whole state from the return
// 2. if `next` not function, "soft update" the whole state
// 3. if `next` nothing, return freezed old state
store.setState = (nextState, hardUpdate) => {
if (!nextState) {
return null
}
if (typeof nextState === 'function') {
return store.setState(nextState({ state: store.getState() }), true)
}
const prevState = store.getState()
const state = hardUpdate
? Object.assign({}, nextState)
: Object.assign({}, prevState, nextState)
store.emit('stateChange', { prevState, state, nextState })
// !! Intentionally after the emitting !!
// User should not expect `store.getState()` from the listener to work.
// You get any kind of state in that listener's arguments, so - don't!
store._state = state
return store._state
}
return store
}
emitter.js
/**
* @copyright 2017-present, Charlike Mike Reagent <[email protected]>
* @license Apache-2.0
*/
export default function createEmitter (events) {
events = Object.assign({}, events)
const app = {
events,
on (name, listener) {
;(events[name] || (events[name] = [])).push(listener)
},
off (name, listener) {
if (events[name] && listener) {
events[name].splice(events[name].indexOf(listener) >>> 0, 1)
} else if (name && !listener) {
app.events[name] = events[name] = []
} else {
app.events = events = {}
}
},
// eslint-disable-next-line
emit(name, ...args) {
;(events[name] || []).map((listener) => {
listener(...args)
})
},
}
return app
}
effects.js
/**
* @copyright 2017-present, Charlike Mike Reagent <[email protected]>
* @license Apache-2.0
*/
import assert from 'assert'
import { isObject } from './utils.js'
// effect signature: ({ state, actions }, a, b) => any
export default function createEffects (store, effects, reducers) {
assert(isObject(store), 'expect `store` to be an object')
assert(isObject(effects), 'expect `effects` to be an object')
const context = { emit: store.emit }
context.actions = Object.assign({}, reducers)
Object.keys(effects).map((name) => {
context.actions[name] = (a, b) => {
store.emit('action', name, a, b)
store.emit('effect', name, a, b)
const effectPromise = new Promise((resolve) => {
context.state = store.getState()
resolve(effects[name](context, a, b))
})
return effectPromise.catch((er) => {
store.emit('error', er)
})
}
})
return context.actions
}
reducers.js
/**
* @copyright 2017-present, Charlike Mike Reagent <[email protected]>
* @license Apache-2.0
*/
import assert from 'assert'
import { isObject } from './utils.js'
// reducer signature: ({ state }, a, b) => Object or Function returning an object
export default function createReducers (store, reducers) {
assert(isObject(store), 'expect `store` to be an object')
assert(isObject(reducers), 'expect `reducers` to be an object')
return Object.keys(reducers).reduce((memo, name) => {
memo[name] = (a, b) => {
const context = { state: store.getState() }
store.emit('action', name, a, b)
store.emit('reducer', name, a, b)
let next = null
try {
next = reducers[name](context, a, b)
} catch (er) {
store.emit('error', er)
return null
}
return store.setState(next)
}
return memo
}, {})
}
Here's the Weekly Digest for tunnckoCoreLabs/elmaz:
Last week, no issues were created.
Last week, no pull requests were created, updated or merged.
Last week there were no commits.
Last week there were no contributors.
Last week there were no stargazers.
Last week there were no releases.
That's all for last week, please ๐ Watch and โญ Star the repository tunnckoCoreLabs/elmaz to receive next weekly updates. ๐
You can also view all Weekly Digests by clicking here.
Your Weekly Digest bot. ๐
Here's the Weekly Digest for tunnckoCoreLabs/elmaz:
Last week, no issues were created.
Last week, no pull requests were created, updated or merged.
Last week there were no commits.
Last week there were no contributors.
Last week there were no stargazers.
Last week there were no releases.
That's all for last week, please ๐ Watch and โญ Star the repository tunnckoCoreLabs/elmaz to receive next weekly updates. ๐
You can also view all Weekly Digests by clicking here.
Your Weekly Digest bot. ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.