Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
syntagme.js - 記号化されたFluxを紬ぎ、拡張するフレームワークの構想
import syntagme from 'syntagme'
import repository from 'repository/main'
export function mount () {
return syntagme.ac("MOUNT", function () {
return repository.fetch()
})
}
import syntagme from 'syntagme'
export default syntagme.reducer(function MountReducer (payload, previous) {
return syntagme.utils.respond('MOUNT', payload, previous, {
action: null,
resolve: {result: payload.action.result},
reject: {rejection: payload.action.rejection},
})
})
import store from 'syntagme/store'
import dispatcher from 'syntagme/dispatcher'
import utils from 'syntagme/utils'
var syntagme = {}
syntagme.store = store
syntagme.utils = utils
syntagme.config = {
prefix: {
RESOLVE: '_RESOLVE',
REJECT: '_REJECT',
}
}
syntagme.dispatch = function (payload) {
dispatcher.dispatch(payload)
}
syntagme.ac = function (type, fn) {
this.dispach({source: 'ACTION', action: {type: type}})
return fn().then((result) => {
this.dispatch({source: 'ACTION_RESOLVE', action: {type: type+syntagme.config.prefix.RESOLVE, result}})
}).catch(() => {
this.dispatch({source: 'ACTION_REJECT', action: {type: type+syntagme.config.prefix.RESOLVE, result}})
})
}
syntagme.reducer = function (reducer) {
if ('array' !== typeof reducer) {
reducer = [reducer]
}
// 実行順序を指定できるようにするため、すでに登録されているreducerが存在すれば削除
// 実行順序を考慮して追加
// 循環参照は考慮すべき
// この辺はReduxを参考にしたい
return new Reducer(reducer)
}
// reducerの処理を簡略化
syntagme.utils.respond = function (type, payload, previous, response) {
for (let key in Object.keys(response)) {
if ((key == 'action' || key == 'resolve' || key == 'reject') && response[key]) {
let type_name = type + (key == 'action' ? '' : key = 'resolve' ? syntagme.config.prefix.RESOLVE : syntagme.config.prefix.REJECT)
if (payload.action.type == type_name) {
return Object.assign({}, response[key], previous)
}
} else if (response[key]) {
if (payload.action.type == key) {
return Object.assign({}, response[key], previous)
}
}
return previous
}
}
export default syntagme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.