From 2ebd8c8fa7ad0c8a4588a81851969a1b34e50318 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Tue, 11 Sep 2018 00:01:06 +0800 Subject: [PATCH] refactor(connect): remove sync and async --- src/driver/connect.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/driver/connect.js b/src/driver/connect.js index 49a7ebd..19f1902 100644 --- a/src/driver/connect.js +++ b/src/driver/connect.js @@ -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) + } } }