Skip to content

Commit

Permalink
refactor(tramsformer): 把 hooks 抽离到 @tarojs/taro
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Apr 28, 2019
1 parent 7e46194 commit a607d50
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 15 deletions.
3 changes: 1 addition & 2 deletions packages/taro-weapp/src/create-component.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { getCurrentPageUrl } from '@tarojs/utils'
import { commitAttachRef, detachAllRef } from '@tarojs/taro'
import { commitAttachRef, detachAllRef, Current } from '@tarojs/taro'
import { isEmptyObject, isFunction } from './util'
import { updateComponent } from './lifecycle'
import { cacheDataSet, cacheDataGet, cacheDataHas } from './data-cache'
import propsManager from './propsManager'
import { Current } from './current-owner'

const anonymousFnNamePreffix = 'funPrivate'
const routerParamsPrivateKey = '__key_'
Expand Down
20 changes: 17 additions & 3 deletions packages/taro-weapp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ import {
internal_safe_set,
internal_inline_style,
internal_get_original,
interceptors
interceptors,
useEffect,
useLayoutEffect,
useReducer,
useState,
useRef,
useCallback,
useMemo,
useImperativeHandle
} from '@tarojs/taro'

import Component from './component'
Expand All @@ -20,7 +28,6 @@ import createComponent from './create-component'
import initNativeApi from './native-api'
import propsManager from './propsManager'
import { getElementById, genCompid } from './util'
import { useEffect, useLayoutEffect, useReducer, useState, useRef, useCallback, useMemo, useImperativeHandle } from './hooks'

export const Taro = {
Component,
Expand All @@ -42,7 +49,14 @@ export const Taro = {
propsManager,
interceptors,
genCompid,
useEffect, useLayoutEffect, useReducer, useState, useRef, useCallback, useMemo, useImperativeHandle
useEffect,
useLayoutEffect,
useReducer,
useState,
useRef,
useCallback,
useMemo,
useImperativeHandle
}

export default Taro
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-weapp/src/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
internal_safe_get as safeGet,
internal_safe_set as safeSet,
commitAttachRef
commitAttachRef,
Current
} from '@tarojs/taro'
// import PropTypes from 'prop-types'
import { componentTrigger } from './create-component'
import { shakeFnFromObject, isEmptyObject, diffObjToPath } from './util'
import { Current } from './current-owner'
import { invokeEffects } from './hooks'

// const isDEV = typeof process === 'undefined' ||
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-weapp/src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isPlainObject from 'lodash/isPlainObject'
import { Current } from './current-owner'
import { Current } from '@tarojs/taro'

export function isEmptyObject (obj) {
if (!obj || !isPlainObject(obj)) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isFunction, isUndefined, isArray, isNullOrUndef } from './util'
import { Current } from './current-owner'
import nextTick from './next-tick'
import { isFunction, isUndefined, isArray, isNullOrUndef, defer } from './util'
import { Current } from './current'

function getHooks (index) {
if (Current.current === null) {
Expand Down Expand Up @@ -88,7 +87,7 @@ function invokeScheduleEffects (component) {
component._afterScheduleEffect = true
scheduleEffectComponents.push(component)
if (scheduleEffectComponents.length === 1) {
nextTick(() => {
defer(() => {
setTimeout(() => {
scheduleEffectComponents.forEach((c) => {
c._afterScheduleEffect = false
Expand Down
40 changes: 37 additions & 3 deletions packages/taro/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ import render from './render'
import { createRef, commitAttachRef, detachAllRef } from './ref'
import Link from './interceptor'
import * as interceptors from './interceptor/interceptors'
import { noPromiseApis, onAndSyncApis, otherApis, initPxTransform } from './native-apis'
import {
noPromiseApis,
onAndSyncApis,
otherApis,
initPxTransform
} from './native-apis'
import {
useEffect,
useLayoutEffect,
useReducer,
useState,
useRef,
useCallback,
useMemo,
useImperativeHandle
} from './hooks'
import { Current } from './current'

const eventCenter = new Events()

Expand All @@ -34,7 +50,16 @@ export {
commitAttachRef,
detachAllRef,
Link,
interceptors
interceptors,
Current,
useEffect,
useLayoutEffect,
useReducer,
useState,
useRef,
useCallback,
useMemo,
useImperativeHandle
}

export default {
Expand All @@ -56,5 +81,14 @@ export default {
commitAttachRef,
detachAllRef,
Link,
interceptors
interceptors,
Current,
useEffect,
useLayoutEffect,
useReducer,
useState,
useRef,
useCallback,
useMemo,
useImperativeHandle
}
18 changes: 18 additions & 0 deletions packages/taro/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ export function isEmptyObject (obj) {
return true
}

export function isFunction (arg) {
return typeof arg === 'function'
}

export const defer = typeof Promise === 'function' ? Promise.prototype.then.bind(Promise.resolve()) : setTimeout

export function isUndefined (o) {
return o === undefined
}

export function isArray (arg) {
return Array.isArray(arg)
}

export function isNullOrUndef (o) {
return isUndefined(o) || o === null
}

/**
* JSON 克隆
* @param {Object | Json} jsonObj json对象
Expand Down

0 comments on commit a607d50

Please sign in to comment.