-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alita wx-react wx-router wx-react-redux): 修改入口文件的转化,更加友好的支持Provider
- Loading branch information
1 parent
08071d2
commit 710d810
Showing
26 changed files
with
1,127 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## wx-mobx-react | ||
|
||
The code is a copy from [mobx-react](https://github.com/mobxjs/mobx-react). We made some adjustments for Wechat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Children, Component } from "react" | ||
import { polyfill } from "react-lifecycles-compat" | ||
import * as PropTypes from "./propTypes" | ||
|
||
const specialReactKeys = { children: true, key: true, ref: true } | ||
|
||
class Provider extends Component { | ||
static contextTypes = { | ||
mobxStores: PropTypes.objectOrObservableObject | ||
} | ||
|
||
static childContextTypes = { | ||
mobxStores: PropTypes.objectOrObservableObject.isRequired | ||
} | ||
|
||
constructor(props, context) { | ||
super(props, context) | ||
this.state = {} | ||
copyStores(props, this.state) | ||
} | ||
|
||
render() { | ||
return Children.only(this.props.children) | ||
} | ||
|
||
getChildContext() { | ||
const stores = {} | ||
// inherit stores | ||
copyStores(this.context.mobxStores, stores) | ||
// add own stores | ||
copyStores(this.props, stores) | ||
return { | ||
mobxStores: stores | ||
} | ||
} | ||
|
||
static getDerivedStateFromProps(nextProps, prevState) { | ||
if (!nextProps) return null | ||
if (!prevState) return nextProps | ||
|
||
// Maybe this warning is too aggressive? | ||
if ( | ||
Object.keys(nextProps).filter(validStoreName).length !== | ||
Object.keys(prevState).filter(validStoreName).length | ||
) | ||
console.warn( | ||
"MobX Provider: The set of provided stores has changed. Please avoid changing stores as the change might not propagate to all children" | ||
) | ||
if (!nextProps.suppressChangedStoreWarning) | ||
for (let key in nextProps) | ||
if (validStoreName(key) && prevState[key] !== nextProps[key]) | ||
console.warn( | ||
"MobX Provider: Provided store '" + | ||
key + | ||
"' has changed. Please avoid replacing stores as the change might not propagate to all children" | ||
) | ||
|
||
return nextProps | ||
} | ||
} | ||
|
||
function copyStores(from, to) { | ||
if (!from) return | ||
for (let key in from) if (validStoreName(key)) to[key] = from[key] | ||
} | ||
|
||
function validStoreName(key) { | ||
return !specialReactKeys[key] && key !== "suppressChangedStoreWarning" | ||
} | ||
|
||
// TODO: kill in next major | ||
polyfill(Provider) | ||
|
||
export default Provider |
56 changes: 56 additions & 0 deletions
56
packages/wx-mobx-react/miniprogram_dist/disposeOnUnmount.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as React from "react" | ||
import { patch, newSymbol } from "./utils/utils" | ||
|
||
const storeKey = newSymbol("disposeOnUnmount") | ||
|
||
function runDisposersOnWillUnmount() { | ||
if (!this[storeKey]) { | ||
// when disposeOnUnmount is only set to some instances of a component it will still patch the prototype | ||
return | ||
} | ||
this[storeKey].forEach(propKeyOrFunction => { | ||
const prop = | ||
typeof propKeyOrFunction === "string" ? this[propKeyOrFunction] : propKeyOrFunction | ||
if (prop !== undefined && prop !== null) { | ||
if (typeof prop !== "function") { | ||
throw new Error( | ||
"[mobx-react] disposeOnUnmount only works on functions such as disposers returned by reactions, autorun, etc." | ||
) | ||
} | ||
prop() | ||
} | ||
}) | ||
this[storeKey] = [] | ||
} | ||
|
||
export function disposeOnUnmount(target, propertyKeyOrFunction) { | ||
if (Array.isArray(propertyKeyOrFunction)) { | ||
return propertyKeyOrFunction.map(fn => disposeOnUnmount(target, fn)) | ||
} | ||
|
||
if (!target instanceof React.Component) { | ||
throw new Error("[mobx-react] disposeOnUnmount only works on class based React components.") | ||
} | ||
|
||
if (typeof propertyKeyOrFunction !== "string" && typeof propertyKeyOrFunction !== "function") { | ||
throw new Error( | ||
"[mobx-react] disposeOnUnmount only works if the parameter is either a property key or a function." | ||
) | ||
} | ||
|
||
// add property key / function we want run (disposed) to the store | ||
const componentWasAlreadyModified = !!target[storeKey] | ||
const store = target[storeKey] || (target[storeKey] = []) | ||
|
||
store.push(propertyKeyOrFunction) | ||
|
||
// tweak the component class componentWillUnmount if not done already | ||
if (!componentWasAlreadyModified) { | ||
patch(target, "componentWillUnmount", runDisposersOnWillUnmount) | ||
} | ||
|
||
// return the disposer as is if invoked as a non decorator | ||
if (typeof propertyKeyOrFunction !== "string") { | ||
return propertyKeyOrFunction | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { spy, configure, getDebugName } from "mobx" | ||
import { Component } from "@areslabs/wx-react" | ||
|
||
export { | ||
observer, | ||
Observer, | ||
renderReporter, | ||
componentByNodeRegistry as componentByNodeRegistery, | ||
componentByNodeRegistry, | ||
trackComponents, | ||
} from "./observer" | ||
|
||
export { default as Provider } from "./Provider" | ||
export { default as inject } from "./inject" | ||
export { disposeOnUnmount } from "./disposeOnUnmount" | ||
|
||
import * as propTypes from "./propTypes" | ||
export { propTypes } | ||
export { propTypes as PropTypes } | ||
|
||
import { errorsReporter } from "./observer" | ||
export const onError = fn => errorsReporter.on(fn) | ||
|
||
/* DevTool support */ | ||
// See: https://github.com/andykog/mobx-devtools/blob/d8976c24b8cb727ed59f9a0bc905a009df79e221/src/backend/installGlobalHook.js | ||
|
||
import { renderReporter, componentByNodeRegistry, trackComponents } from "./observer" |
Oops, something went wrong.