Skip to content

Commit

Permalink
feat(tt): 支持 contextType, close #3301
Browse files Browse the repository at this point in the history
alipay, qq, swan 也同步支持
  • Loading branch information
yuche committed Jun 3, 2019
1 parent a9f0a5b commit 9691e02
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/taro-alipay/src/lifecycle.js
Expand Up @@ -8,6 +8,7 @@ import {
import { componentTrigger } from './create-component'
import { shakeFnFromObject, isEmptyObject, diffObjToPath } from './util'
import PropTypes from 'prop-types'
import { enqueueRender } from './render-queue'

const isDEV = typeof process === 'undefined' ||
!process.env ||
Expand Down Expand Up @@ -57,13 +58,31 @@ export function updateComponent (component) {
component.prevState = component.state
}

function injectContextType (component) {
const ctxType = component.constructor.contextType
if (ctxType) {
const context = ctxType.context
const emiter = context.emiter
if (emiter === null) {
component.context = context._defaultValue
return
}
if (!component._hasContext) {
component._hasContext = true
emiter.on(_ => enqueueRender(component))
}
component.context = emiter.value
}
}

function doUpdate (component, prevProps, prevState) {
const { state, props = {} } = component
let data = state || {}
if (component._createData) {
// 返回null或undefined则保持不变
const isRunLoopRef = !component.__mounted
if (component.__isReady) {
injectContextType(component)
Current.current = component
Current.index = 0
invokeEffects(component, true)
Expand Down
19 changes: 19 additions & 0 deletions packages/taro-qq/src/lifecycle.js
Expand Up @@ -8,6 +8,7 @@ import {
// import PropTypes from 'prop-types'
import { componentTrigger } from './create-component'
import { shakeFnFromObject, isEmptyObject, diffObjToPath } from './util'
import { enqueueRender } from './render-queue'

// const isDEV = typeof process === 'undefined' ||
// !process.env ||
Expand Down Expand Up @@ -59,13 +60,31 @@ export function updateComponent (component) {
component.prevState = component.state
}

function injectContextType (component) {
const ctxType = component.constructor.contextType
if (ctxType) {
const context = ctxType.context
const emiter = context.emiter
if (emiter === null) {
component.context = context._defaultValue
return
}
if (!component._hasContext) {
component._hasContext = true
emiter.on(_ => enqueueRender(component))
}
component.context = emiter.value
}
}

function doUpdate (component, prevProps, prevState) {
const { state, props = {} } = component
let data = state || {}
if (component._createData) {
// 返回null或undefined则保持不变
const runLoopRef = !component.__mounted
if (component.__isReady) {
injectContextType(component)
Current.current = component
Current.index = 0
invokeEffects(component, true)
Expand Down
19 changes: 19 additions & 0 deletions packages/taro-swan/src/lifecycle.js
Expand Up @@ -8,6 +8,7 @@ import {
import { componentTrigger } from './create-component'
import { shakeFnFromObject, isEmptyObject, diffObjToPath } from './util'
import PropTypes from 'prop-types'
import { enqueueRender } from './render-queue'

const isDEV = typeof process === 'undefined' ||
!process.env ||
Expand Down Expand Up @@ -57,13 +58,31 @@ export function updateComponent (component) {
component.prevState = component.state
}

function injectContextType (component) {
const ctxType = component.constructor.contextType
if (ctxType) {
const context = ctxType.context
const emiter = context.emiter
if (emiter === null) {
component.context = context._defaultValue
return
}
if (!component._hasContext) {
component._hasContext = true
emiter.on(_ => enqueueRender(component))
}
component.context = emiter.value
}
}

function doUpdate (component, prevProps, prevState) {
const { state, props = {} } = component
let data = state || {}
if (component._createData) {
// 返回null或undefined则保持不变
const isRunLoopRef = !component.__mounted
if (component.__isReady) {
injectContextType(component)
Current.current = component
Current.index = 0
invokeEffects(component, true)
Expand Down
19 changes: 19 additions & 0 deletions packages/taro-tt/src/lifecycle.js
Expand Up @@ -8,6 +8,7 @@ import {
import { componentTrigger } from './create-component'
import { shakeFnFromObject, isEmptyObject, diffObjToPath } from './util'
import PropTypes from 'prop-types'
import { enqueueRender } from './render-queue'

const isDEV = typeof process === 'undefined' ||
!process.env ||
Expand Down Expand Up @@ -57,13 +58,31 @@ export function updateComponent (component) {
component.prevState = component.state
}

function injectContextType (component) {
const ctxType = component.constructor.contextType
if (ctxType) {
const context = ctxType.context
const emiter = context.emiter
if (emiter === null) {
component.context = context._defaultValue
return
}
if (!component._hasContext) {
component._hasContext = true
emiter.on(_ => enqueueRender(component))
}
component.context = emiter.value
}
}

function doUpdate (component, prevProps, prevState) {
const { state, props = {} } = component
let data = state || {}
if (component._createData) {
// 返回null或undefined则保持不变
const isRunLoopRef = !component.__mounted
if (component.__isReady) {
injectContextType(component)
Current.current = component
Current.index = 0
invokeEffects(component, true)
Expand Down

0 comments on commit 9691e02

Please sign in to comment.