Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into PT-116811
Browse files Browse the repository at this point in the history
  • Loading branch information
jien.li committed Dec 29, 2023
2 parents 832144a + eca2aa0 commit abf43b4
Show file tree
Hide file tree
Showing 87 changed files with 5,403 additions and 1,316 deletions.
106 changes: 76 additions & 30 deletions dist/ReactAli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* 运行于支付宝小程序的React by 司徒正美 Copyright 2021-09-29
* 运行于支付宝小程序的React by 司徒正美 Copyright 2023-12-27
*/

var arrayPush = Array.prototype.push;
Expand Down Expand Up @@ -765,15 +765,11 @@ function dispatchEvent(e) {
value: value
};
if (app && app.onCollectLogs && rbeaconType.test(eventType)) {
app.onCollectLogs(dataset, eventType, fiber.stateNode);
app.onCollectLogs(dataset, eventType, fiber.stateNode, fiber);
}
Renderer.batchedUpdates(function () {
try {
var fn = instance.$$eventCached[eventUid];
fn && fn.call(instance, createEvent(e, safeTarget));
} catch (err) {
console.log(err.stack);
}
var fn = instance.$$eventCached[eventUid];
fn && fn.call(instance, createEvent(e, safeTarget));
}, e);
}
function createEvent(e, target) {
Expand Down Expand Up @@ -1106,6 +1102,10 @@ function useEffectImpl(create, deps, EffectTag, createList, destroyList) {
list.push(create);
}, deps);
}
function useQueryImpl() {
var fiber = getCurrentFiber();
return fiber.props.query;
}
function getCurrentFiber() {
return get(Renderer.currentOwner);
}
Expand Down Expand Up @@ -1601,6 +1601,35 @@ var refStrategy = {
}
};

function registerPageHook(appHooks, pageHook, app, instance, args) {
for (var i = 0; i < 2; i++) {
var method = i ? appHooks[pageHook] : pageHook;
var host = i ? app : instance;
if (host && host[method] && isFn(host[method])) {
var ret = host[method](args);
if (ret !== void 0) {
if (ret && ret.then && ret['catch']) {
continue;
}
return ret;
}
}
if (i === 0 && host && host.__isStateless) {
return callLifecycle(host, method, args);
}
}
}
function callLifecycle(instance, lifecycle, args) {
var callbacks = instance.lifecycleCallback && instance.lifecycleCallback[lifecycle] || [];
var result = void 0;
callbacks.forEach(function (callback) {
result = callback(args);
});
if (result) {
return result;
}
}

var domFns = ['insertElement', 'updateContent', 'updateAttribute'];
var domEffects = [PLACE, CONTENT, ATTR];
var domRemoved = [];
Expand Down Expand Up @@ -1803,6 +1832,7 @@ function disposeFiber(fiber, force) {
Renderer.onDispose(fiber);
if (fiber.hasMounted) {
if (isStateless) {
callLifecycle(stateNode, 'onUnload', null);
safeInvokeHooks(fiber.updateQueue, 'layout', 'unlayout');
safeInvokeHooks(fiber.updateQueue, 'passive', 'unpassive');
}
Expand Down Expand Up @@ -2137,7 +2167,8 @@ var Renderer$1 = createRenderer({
}
}
}
if (!app.$$pageIsReady && instance.componentDidMount) {
var pagePath = instance.instanceUid ? instance.$$pagePath : instance.props.path;
if (app.$$pagePath == pagePath && !app.$$pageIsReady && instance.componentDidMount) {
delayMounts.push({
instance: instance,
fn: instance.componentDidMount
Expand Down Expand Up @@ -2729,6 +2760,9 @@ function onLoad(PageClass, path, query, isLoad) {
isPageComponent: true
}), container);
}
if (pageInstance.__isStateless) {
callLifecycle(pageInstance, 'onLoad', query);
}
if (isLoad) {
callGlobalHook("onGlobalLoad");
}
Expand All @@ -2746,6 +2780,9 @@ function onReady() {
el.fn.call(el.instance);
el.instance.componentDidMount = el.fn;
}
if (this.reactInstance.__isStateless) {
callLifecycle(this.reactInstance, 'onReady', arguments);
}
callGlobalHook("onGlobalReady");
}
function onUnload() {
Expand Down Expand Up @@ -2774,26 +2811,11 @@ function onUnload() {
this.reactContainer = null;
}

function registerPageHook(appHooks, pageHook, app, instance, args) {
for (var i = 0; i < 2; i++) {
var method = i ? appHooks[pageHook] : pageHook;
var host = i ? app : instance;
if (host && host[method] && isFn(host[method])) {
var ret = host[method](args);
if (ret !== void 0) {
if (ret && ret.then && ret['catch']) {
continue;
}
return ret;
}
}
}
}

var appHooks = {
onShow: 'onGlobalShow',
onHide: 'onGlobalHide'
};
var lifeCycleList = ['onShareAppMessage', 'onPageScroll', 'onReachBottom', 'onPullDownRefresh', 'onTabItemTap', 'onResize', 'onShow', 'onHide'];
function registerPage(PageClass, path, testObject) {
PageClass.reactInstances = [];
var config = {
Expand All @@ -2805,7 +2827,7 @@ function registerPage(PageClass, path, testObject) {
onReady: onReady,
onUnload: onUnload
};
Array('onShareAppMessage', 'onPageScroll', 'onReachBottom', 'onPullDownRefresh', 'onTabItemTap', 'onResize', 'onShow', 'onHide').forEach(function (hook) {
lifeCycleList.forEach(function (hook) {
config[hook] = function (e) {
var instance = this.reactInstance,
pageHook = hook,
Expand All @@ -2827,9 +2849,6 @@ function registerPage(PageClass, path, testObject) {
param = instance.props.query;
app.$$page = this;
var path = app.$$pagePath = instance.props.path;
if (this.needReRender) {
onLoad.call(this, PageClass, path, param);
}
}
return registerPageHook(appHooks, pageHook, app, instance, param);
};
Expand All @@ -2856,6 +2875,9 @@ function useEffect(create, deps) {
function useLayoutEffect(create, deps) {
return useEffectImpl(create, deps, HOOK, "layout", "unlayout");
}
function useQuery() {
return useQueryImpl();
}

function createRef() {
return {
Expand All @@ -2876,6 +2898,30 @@ function memo(render, shouldComponentUpdate) {
};
}

var allLifecycle = ['onLoad', 'onReady', 'onUnload'].concat(lifeCycleList);
function usePageEvent(eventName, callback) {
if (!allLifecycle.includes(eventName)) {
console.error('小程序没有' + eventName + '生命周期,请仔细检查');
return;
}
var pageInstance = Renderer.currentOwner;
if (eventName === 'onShareAppMessage') {
pageInstance.onShare = callback;
return;
}
useLayoutEffect(function () {
return registerLifecycle(pageInstance, eventName, callback);
});
}
function registerLifecycle(pageInstance, lifecycle, callback) {
var lifecycleCallback = pageInstance.lifecycleCallback || (pageInstance.lifecycleCallback = {});
pageInstance.lifecycleCallback[lifecycle] = lifecycleCallback[lifecycle] || [];
pageInstance.lifecycleCallback[lifecycle].push(callback);
return function () {
pageInstance.lifecycleCallback[lifecycle].splice(pageInstance.lifecycleCallback[lifecycle].indexOf(callback), 1);
};
}

var render$1 = Renderer$1.render;
var React = getWindow().React = {
eventSystem: {
Expand Down Expand Up @@ -2924,4 +2970,4 @@ if (typeof my != "undefined") {
registerAPIs(React, apiContainer, more);

export default React;
export { Children, createElement, Component, PureComponent, createRef, memo, useState, useReducer, useCallback, useMemo, useEffect, useContext, useComponent, useRef };
export { Children, createElement, Component, PureComponent, createRef, memo, useState, useReducer, useCallback, useMemo, useEffect, useLayoutEffect, useContext, useComponent, useRef, useQuery, usePageEvent };
Loading

0 comments on commit abf43b4

Please sign in to comment.