refactor(connect): remove sync and async

This commit is contained in:
EnixCoda 2018-09-11 00:01:06 +08:00
parent d808c8963a
commit 2ebd8c8fa7

View file

@ -2,14 +2,6 @@ import React from 'react'
import { withErrorLog } from "../analytics"
function async(func) {
return new Promise(resolve => setTimeout(() => resolve(func())))
}
function sync(func) {
return func()
}
function link(instance, sources) {
const wrappedMethods = {/* [keyof sources] -> wrappedMethods.method */}
const map = new Map(/* sources.creator -> wrappedMethods.method */)
@ -24,9 +16,17 @@ function link(instance, sources) {
function dispatch(...args) {
const isFromSource = Object.values(sources).includes(args[0])
if (isFromSource) {
sync(withErrorLog(() => map.get(args[0])(...args.slice(1))))
withErrorLog(() => map.get(args[0])(...args.slice(1)))()
} else {
async(() => instance.setState(...args))
// by doing so, no async updater is available anymore
// luckily I don't need them :)
let [updater, callback] = args
if (typeof updater === 'function') {
callback = updater
callback(instance.state, instance.props)
} else {
instance.setState(updater, callback)
}
}
}