refactor: remove link

This commit is contained in:
EnixCoda 2018-06-22 21:42:44 +08:00
parent 1de478a83d
commit 84d1859991
3 changed files with 30 additions and 25 deletions

View file

@ -1,16 +1,43 @@
import React from 'react'
export default function connect(core) {
function isObject(target) {
return typeof target === 'object' && target !== null
}
function link(instance, sources) {
const wrappedMethods = {/* sources[key] -> wrappedMethods.method */}
const map = new Map(/* sources.creator -> wrappedMethods.method */)
function dispatch(...args) {
if (Object.values(sources).includes(args[0])) {
map.get(args[0])(...args.slice(1))
} else {
setTimeout(
instance.setState.bind(instance, ...args),
)
}
}
Object.entries(sources).forEach(([key, createMethod]) => {
const method = createMethod(dispatch)
wrappedMethods[key] = method
map.set(createMethod, method)
})
return wrappedMethods
}
export default function connect(mapping) {
return function linkComponent(ComponentClass) {
return class AwesomeApp extends React.PureComponent {
static displayName = `Driven${ComponentClass.name}`
state = {}
boundCore = core(this)
boundCore = link(this, mapping)
render() {
return (
<ComponentClass {...this.boundCore} {...this.state} />
<ComponentClass {...this.props} {...this.boundCore} {...this.state}/>
)
}
}

View file

@ -1,5 +1,3 @@
import link from './link'
import DOMHelper, { REPO_TYPE_PRIVATE } from '../utils/DOMHelper'
import GitHubHelper, { NOT_FOUND, BAD_CREDENTIALS } from '../utils/GitHubHelper'
import storageHelper from '../utils/storageHelper'

View file

@ -1,20 +0,0 @@
export default function link(setState, sources) {
const wrappedMethods = {/* sources[key] -> wrappedMethods.method */}
const map = new Map(/* sources.creator -> wrappedMethods.method */)
function dispatch(...args) {
if (Object.values(sources).includes(args[0])) {
map.get(args[0])(...args.slice(1))
} else {
setState(...args)
}
}
Object.entries(sources).forEach(([key, createMethod]) => {
const method = createMethod(dispatch)
wrappedMethods[key] = method
map.set(createMethod, method)
})
return wrappedMethods
}