Skip to content

Commit

Permalink
feat(alita wx-react wx-router wx-react-redux): 修改入口文件的转化,更加友好的支持Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ykforerlang committed Jul 1, 2019
1 parent 08071d2 commit 710d810
Show file tree
Hide file tree
Showing 26 changed files with 1,127 additions and 66 deletions.
11 changes: 0 additions & 11 deletions examples/Todo/RNApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ const store = createStore(todoApp, applyMiddleware(thunk, promiseMiddleware))

export default class RNApp extends PureComponent {

static childContextTypes = {
store: PropTypes.object
}

getChildContext() {
return {
store: Platform.OS === 'wx' ? store : {}
}
}


render() {
return (
<Provider store={store}>
Expand Down
35 changes: 21 additions & 14 deletions examples/TodoWP/RNApp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import React, { PureComponent, h } from "@areslabs/wx-react";
import { Platform } from "@areslabs/wx-react-native";
import PropTypes from "@areslabs/wx-prop-types";
import { createStore, applyMiddleware } from "@areslabs/wx-redux";
import promiseMiddleware from "@areslabs/wx-redux-promise";
import thunk from "@areslabs/wx-redux-thunk";
Expand All @@ -9,18 +7,27 @@ import { Provider } from "@areslabs/wx-react-redux";
import { Router, Route } from "@areslabs/wx-router";
const store = createStore(todoApp, applyMiddleware(thunk, promiseMiddleware));
const RNAppClass = class RNApp extends PureComponent {
static childContextTypes = {
store: PropTypes.object
};

getChildContext() {
return {
store: Platform.OS === 'wx' ? store : {}
};
render() {
return h(Provider, {
store: store,
diuu: "DIUU00001"
}, h(Router, {
diuu: "DIUU00002"
}, h(Route, {
diuu: "DIUU00003"
})));
}

};
const RNApp = new RNAppClass({});
RNApp.childContext = RNApp.getChildContext ? RNApp.getChildContext() : {};
export default RNApp;
wx._historyConfig = {...(wx._historyConfig || {}), ...{"Todoinit":"/src/components/index"}};
React.render(h(RNAppClass, {
diuu: React.rootUuid
}), null, {});
const rootContext = React.getRootContext();
export default {
childContext: rootContext
};
wx._historyConfig = { ...(wx._historyConfig || {}),
...{
"Todoinit": "/src/components/index"
}
};
8 changes: 4 additions & 4 deletions examples/TodoWP/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "Todo",
"version": "0.0.1",
"dependencies": {
"@areslabs/wx-router": "^1.0.0",
"@areslabs/wx-router": "beta",
"@areslabs/wx-animated": "^1.0.1",
"@areslabs/wx-react": "^1.0.0",
"@areslabs/wx-react": "beta",
"@areslabs/wx-react-native": "^1.0.0",
"@areslabs/wx-prop-types": "^1.0.0",
"@areslabs/wx-react-redux": "^1.0.0",
"@areslabs/wx-react-redux": "beta",
"@areslabs/wx-redux": "^1.0.0",
"@areslabs/wx-redux-actions": "^1.0.0",
"@areslabs/wx-redux-promise": "^1.0.0",
"@areslabs/wx-redux-thunk": "^1.0.0"
}
}
}
2 changes: 1 addition & 1 deletion examples/TodoWP/project.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"compileType": "miniprogram",
"libVersion": "2.2.2",
"appid": "wx67a52214b440b81b",
"projectname": "TodoListWP",
"projectname": "tmptmp",
"isGameTourist": false,
"condition": {
"search": {
Expand Down
2 changes: 1 addition & 1 deletion examples/TodoWP/src/components/AddTodoTemplate.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<template name="ITNP00004">
<block original="View" diuu="{{DIUU00001}}" tempName="ITNP00004" style="{{tools.getFinalStyle(DIUU00001style)}}">
<WXTextInput autoCapitalize='none' autoFocus="{{DIUU00002autoFocus}}" ref="{{DIUU00002ref}}" value="{{DIUU00002value}}" diuu="{{DIUU00002}}" style="{{tools.getFinalStyle(DIUU00002style)}}" />
<WXTextInput autoCapitalize="none" autoFocus="{{DIUU00002autoFocus}}" ref="{{DIUU00002ref}}" value="{{DIUU00002value}}" diuu="{{DIUU00002}}" style="{{tools.getFinalStyle(DIUU00002style)}}" />
<WXButton title="ADD" diuu="{{DIUU00003}}" style="{{tools.getFinalStyle(DIUU00003style)}}" />
</block>
</template>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"weixin",
"redux"
],
"version": "1.0.1",
"version": "1.0.2-beta.1",
"description": "首个ReactNative转微信小程序引擎",
"main": "./lib/index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions packages/wx-mobx-react/README.md
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
74 changes: 74 additions & 0 deletions packages/wx-mobx-react/miniprogram_dist/Provider.js
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 packages/wx-mobx-react/miniprogram_dist/disposeOnUnmount.js
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
}
}
27 changes: 27 additions & 0 deletions packages/wx-mobx-react/miniprogram_dist/index.js
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"
Loading

0 comments on commit 710d810

Please sign in to comment.