diff --git a/packages/taro-mobx-common/src/index.js b/packages/taro-mobx-common/src/index.js index 462df39009d0..1df035645f9b 100755 --- a/packages/taro-mobx-common/src/index.js +++ b/packages/taro-mobx-common/src/index.js @@ -4,10 +4,10 @@ export const onError = fn => errorsReporter.on(fn) export { PropTypes } from "./propTypes" -export { observer } from './observer' export { useLocalStore } from './useLocalStore' export { useAsObservableSource } from './useAsObservableSource' export { isUsingStaticRendering, useStaticRendering } from './staticRendering' export { getStore, setStore } from './store' +export { errorsReporter } from './reporter' export { inject, getInjectName, mapStoreToProps } from './inject' diff --git a/packages/taro-mobx-common/src/utils.js b/packages/taro-mobx-common/src/utils.js index 302d440c488a..5ce532751934 100644 --- a/packages/taro-mobx-common/src/utils.js +++ b/packages/taro-mobx-common/src/utils.js @@ -1,5 +1,3 @@ -import Taro from '@tarojs/taro' - export function isPlainObject (value) { if (!value || typeof value !== 'object') { return false @@ -7,7 +5,3 @@ export function isPlainObject (value) { const proto = Object.getPrototypeOf(value) return !proto || proto === Object.prototype } - -export function isMiniPlatform () { - return !(/^WEB|RN$/i.test(Taro.getEnv())) -} diff --git a/packages/taro-mobx-h5/src/index.js b/packages/taro-mobx-h5/src/index.js index e94225869f74..db23620b10b3 100644 --- a/packages/taro-mobx-h5/src/index.js +++ b/packages/taro-mobx-h5/src/index.js @@ -3,7 +3,6 @@ import { useState } from '@tarojs/taro-h5' import { PropTypes, onError, - observer, isUsingStaticRendering, useStaticRendering, useLocalStore as originUseLocalStore, @@ -12,6 +11,7 @@ import { import Provider from './Provider' import { inject } from './inject' +import { observer } from './observer' function useLocalStore (initializer, current) { return originUseLocalStore(initializer, current, useState) diff --git a/packages/taro-mobx-h5/src/observer.js b/packages/taro-mobx-h5/src/observer.js new file mode 100644 index 000000000000..766d347cecd2 --- /dev/null +++ b/packages/taro-mobx-h5/src/observer.js @@ -0,0 +1,50 @@ +import { Reaction } from 'mobx' +import { errorsReporter, isUsingStaticRendering } from '@tarojs/mobx-common' + +export function observer (component) { + if (isUsingStaticRendering()) { + return component + } + + if (component.isMobxInjector === true) { + console.warn( + "Mobx observer: You are trying to use 'observer' on a component that already has 'inject'. Please apply 'observer' before applying 'inject'" + ) + } + + const target = component.prototype + + const originComponentWillUnmount = target.componentWillUnmount + target.componentWillUnmount = function () { + this._reaction.dispose() + originComponentWillUnmount && originComponentWillUnmount.call(this) + } + + const originRender = target.render + target.render = function (...args) { + if (!this._reaction) { + const initialName = this.displayName || this.name + this._reaction = new Reaction(`${initialName}_${Date.now()}`, () => { + this.componentWillReact && this.componentWillReact() + this.forceUpdate() + }) + } + + let result + let exception + this._reaction.track(() => { + try { + result = originRender.call(this, null, null, args[2]) + } catch (e) { + exception = e + } + }) + if (exception) { + errorsReporter.emit(exception) + throw exception + } + return result + } + + return component +} diff --git a/packages/taro-mobx-rn/src/index.js b/packages/taro-mobx-rn/src/index.js index 9b6272e6336f..7a577c4e3c2b 100644 --- a/packages/taro-mobx-rn/src/index.js +++ b/packages/taro-mobx-rn/src/index.js @@ -3,7 +3,6 @@ import { useState } from '@tarojs/taro-rn' import { PropTypes, onError, - observer, isUsingStaticRendering, useStaticRendering, useLocalStore as originUseLocalStore, @@ -12,6 +11,7 @@ import { import Provider from './Provider' import { inject } from './inject' +import { observer } from './observer' function useLocalStore (initializer, current) { return originUseLocalStore(initializer, current, useState) diff --git a/packages/taro-mobx-rn/src/observer.js b/packages/taro-mobx-rn/src/observer.js new file mode 100644 index 000000000000..766d347cecd2 --- /dev/null +++ b/packages/taro-mobx-rn/src/observer.js @@ -0,0 +1,50 @@ +import { Reaction } from 'mobx' +import { errorsReporter, isUsingStaticRendering } from '@tarojs/mobx-common' + +export function observer (component) { + if (isUsingStaticRendering()) { + return component + } + + if (component.isMobxInjector === true) { + console.warn( + "Mobx observer: You are trying to use 'observer' on a component that already has 'inject'. Please apply 'observer' before applying 'inject'" + ) + } + + const target = component.prototype + + const originComponentWillUnmount = target.componentWillUnmount + target.componentWillUnmount = function () { + this._reaction.dispose() + originComponentWillUnmount && originComponentWillUnmount.call(this) + } + + const originRender = target.render + target.render = function (...args) { + if (!this._reaction) { + const initialName = this.displayName || this.name + this._reaction = new Reaction(`${initialName}_${Date.now()}`, () => { + this.componentWillReact && this.componentWillReact() + this.forceUpdate() + }) + } + + let result + let exception + this._reaction.track(() => { + try { + result = originRender.call(this, null, null, args[2]) + } catch (e) { + exception = e + } + }) + if (exception) { + errorsReporter.emit(exception) + throw exception + } + return result + } + + return component +} diff --git a/packages/taro-mobx/src/index.js b/packages/taro-mobx/src/index.js index 6f716295526d..27cd6ae2a37c 100644 --- a/packages/taro-mobx/src/index.js +++ b/packages/taro-mobx/src/index.js @@ -5,7 +5,6 @@ import { onError, getStore, setStore, - observer, isUsingStaticRendering, useStaticRendering, useLocalStore as originUseLocalStore, @@ -13,6 +12,7 @@ import { } from '@tarojs/mobx-common' import { inject } from './inject' +import { observer } from './observer' class Provider {} diff --git a/packages/taro-mobx/src/inject.js b/packages/taro-mobx/src/inject.js index 85f106d133d3..252a2e9c1109 100644 --- a/packages/taro-mobx/src/inject.js +++ b/packages/taro-mobx/src/inject.js @@ -8,19 +8,11 @@ function createStoreInjector (grabStoresFn, injectNames, Component) { super(Object.assign(...arguments, mapStoreToProps(grabStoresFn, props)), isPage) } - componentWillMount () { + _constructor () { Object.assign(this.props, mapStoreToProps(grabStoresFn, this.props)) - if (typeof super.componentWillMount === 'function') { - super.componentWillMount() - } + super._constructor && super._constructor(this.props) } } - const target = Injector.prototype - const originCreateData = target._createData - target._createData = function (...args) { - Object.assign(this.props, mapStoreToProps(grabStoresFn, this.props)) - return originCreateData.call(this, null, null, args[2]) - } return Injector } diff --git a/packages/taro-mobx-common/src/observer.js b/packages/taro-mobx/src/observer.js similarity index 58% rename from packages/taro-mobx-common/src/observer.js rename to packages/taro-mobx/src/observer.js index 645b31e2034e..5aa09a295f46 100644 --- a/packages/taro-mobx-common/src/observer.js +++ b/packages/taro-mobx/src/observer.js @@ -1,7 +1,5 @@ import { Reaction } from 'mobx' -import { isMiniPlatform } from './utils' -import { errorsReporter } from './reporter' -import { isUsingStaticRendering } from './staticRendering' +import { errorsReporter, isUsingStaticRendering } from '@tarojs/mobx-common' export function observer (component) { if (isUsingStaticRendering()) { @@ -15,15 +13,16 @@ export function observer (component) { } const target = component.prototype - const originComponentWillMount = target.componentWillMount - const originComponentWillReact = target.componentWillReact - target.componentWillMount = function () { - const initialName = this.displayName || this.name - this._reaction = new Reaction(`${initialName}_${Date.now()}`, () => { - this.forceUpdate() - originComponentWillReact && originComponentWillReact.call(this) - }) - originComponentWillMount && originComponentWillMount.call(this) + const originConstructor = target._constructor + target._constructor = function () { + if (this.$scope) { + const initialName = this.displayName || this.name + this._reaction = new Reaction(`${initialName}_${Date.now()}`, () => { + this.componentWillReact && this.componentWillReact() + this.forceUpdate() + }) + } + originConstructor && originConstructor.call(this, this.props) } const originComponentWillUnmount = target.componentWillUnmount @@ -32,9 +31,8 @@ export function observer (component) { originComponentWillUnmount && originComponentWillUnmount.call(this) } - const renderMethod = isMiniPlatform() ? '_createData' : 'render' - const originRender = target[renderMethod] - target[renderMethod] = function (...args) { + const originRender = target._createData + target._createData = function (...args) { let result let exception if (this._reaction instanceof Reaction) { diff --git a/packages/taro-mobx/types/index.d.ts b/packages/taro-mobx/types/index.d.ts index 8db533f8ed2d..cb0655348490 100644 --- a/packages/taro-mobx/types/index.d.ts +++ b/packages/taro-mobx/types/index.d.ts @@ -14,10 +14,9 @@ export function useLocalStore, TSource extend export function useAsObservableSource(current: TSource): TSource; export type ITaroComponent

= | Taro.ComponentClass

- | Taro.FunctionComponent

+ | Taro.FunctionComponent

; -export function observer

(component: P): P -// export function observer(component); +export function observer

(component: P): P; export function inject(...stores: string[]); export function inject(fn: (stores: IValueMap, nextProps: IValueMap) => IValueMap);