From a607d50e90ea7c088c54b4fe4682ec8af2a4e04f Mon Sep 17 00:00:00 2001 From: yuche Date: Sun, 28 Apr 2019 14:41:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(tramsformer):=20=E6=8A=8A=20hooks=20?= =?UTF-8?q?=E6=8A=BD=E7=A6=BB=E5=88=B0=20@tarojs/taro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-weapp/src/create-component.js | 3 +- packages/taro-weapp/src/index.js | 20 ++++++++-- packages/taro-weapp/src/lifecycle.js | 4 +- packages/taro-weapp/src/util.js | 2 +- .../current-owner.js => taro/src/current.js} | 0 packages/{taro-weapp => taro}/src/hooks.js | 7 ++-- packages/taro/src/index.js | 40 +++++++++++++++++-- packages/taro/src/util.js | 18 +++++++++ 8 files changed, 79 insertions(+), 15 deletions(-) rename packages/{taro-weapp/src/current-owner.js => taro/src/current.js} (100%) rename packages/{taro-weapp => taro}/src/hooks.js (95%) diff --git a/packages/taro-weapp/src/create-component.js b/packages/taro-weapp/src/create-component.js index 87ad53442ede..aa6a734b4c28 100644 --- a/packages/taro-weapp/src/create-component.js +++ b/packages/taro-weapp/src/create-component.js @@ -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_' diff --git a/packages/taro-weapp/src/index.js b/packages/taro-weapp/src/index.js index f5cf4ce754e0..6ce6a5c9478b 100644 --- a/packages/taro-weapp/src/index.js +++ b/packages/taro-weapp/src/index.js @@ -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' @@ -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, @@ -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 diff --git a/packages/taro-weapp/src/lifecycle.js b/packages/taro-weapp/src/lifecycle.js index af59e2ece87a..1b597c7a288a 100644 --- a/packages/taro-weapp/src/lifecycle.js +++ b/packages/taro-weapp/src/lifecycle.js @@ -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' || diff --git a/packages/taro-weapp/src/util.js b/packages/taro-weapp/src/util.js index 89f198be5591..8ab203943646 100644 --- a/packages/taro-weapp/src/util.js +++ b/packages/taro-weapp/src/util.js @@ -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)) { diff --git a/packages/taro-weapp/src/current-owner.js b/packages/taro/src/current.js similarity index 100% rename from packages/taro-weapp/src/current-owner.js rename to packages/taro/src/current.js diff --git a/packages/taro-weapp/src/hooks.js b/packages/taro/src/hooks.js similarity index 95% rename from packages/taro-weapp/src/hooks.js rename to packages/taro/src/hooks.js index 6b1d00e6e2e5..810f8825bdbb 100644 --- a/packages/taro-weapp/src/hooks.js +++ b/packages/taro/src/hooks.js @@ -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) { @@ -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 diff --git a/packages/taro/src/index.js b/packages/taro/src/index.js index 53db623066ee..68e9b9d64fe1 100644 --- a/packages/taro/src/index.js +++ b/packages/taro/src/index.js @@ -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() @@ -34,7 +50,16 @@ export { commitAttachRef, detachAllRef, Link, - interceptors + interceptors, + Current, + useEffect, + useLayoutEffect, + useReducer, + useState, + useRef, + useCallback, + useMemo, + useImperativeHandle } export default { @@ -56,5 +81,14 @@ export default { commitAttachRef, detachAllRef, Link, - interceptors + interceptors, + Current, + useEffect, + useLayoutEffect, + useReducer, + useState, + useRef, + useCallback, + useMemo, + useImperativeHandle } diff --git a/packages/taro/src/util.js b/packages/taro/src/util.js index b8e267af7442..37cb821cd1de 100644 --- a/packages/taro/src/util.js +++ b/packages/taro/src/util.js @@ -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对象