更新在初始化生命周期里的报错机制
This commit is contained in:
parent
9b10f99d1b
commit
8184de966c
|
|
@ -498,10 +498,11 @@ export function reRender(option, extra) {
|
||||||
catch (err) {
|
catch (err) {
|
||||||
rollback();
|
rollback();
|
||||||
console.error(err);
|
console.error(err);
|
||||||
assert(false);
|
// assert(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
try {
|
||||||
const data = formData
|
const data = formData
|
||||||
? formData.call(this, {
|
? formData.call(this, {
|
||||||
features,
|
features,
|
||||||
|
|
@ -535,6 +536,10 @@ export function reRender(option, extra) {
|
||||||
}); // 有些环境下如果传空值不触发判断
|
}); // 有些环境下如果传空值不触发判断
|
||||||
this.setState(data);
|
this.setState(data);
|
||||||
}
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
export async function refresh() {
|
export async function refresh() {
|
||||||
if (this.state.oakFullpath) {
|
if (this.state.oakFullpath) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
/// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" />
|
/// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" />
|
||||||
import { assert } from 'oak-domain/lib/utils/assert';
|
import { assert } from 'oak-domain/lib/utils/assert';
|
||||||
|
import { OakException } from 'oak-domain/lib/types';
|
||||||
import { onPathSet, reRender, refresh, loadMore, execute, destroyNode, getFreshValue, } from './page.common';
|
import { onPathSet, reRender, refresh, loadMore, execute, destroyNode, getFreshValue, } from './page.common';
|
||||||
import { cloneDeep, pull } from 'oak-domain/lib/utils/lodash';
|
import { cloneDeep, pull } from 'oak-domain/lib/utils/lodash';
|
||||||
const OakProperties = {
|
const OakProperties = {
|
||||||
|
|
@ -720,7 +721,8 @@ export function createComponent(option, features) {
|
||||||
this.features = features;
|
this.features = features;
|
||||||
this.featuresSubscribed = [];
|
this.featuresSubscribed = [];
|
||||||
created && created.call(this);
|
created && created.call(this);
|
||||||
this.lifetime = 'created';
|
this._readyCalled = false;
|
||||||
|
this.oakLifetime = 'created';
|
||||||
},
|
},
|
||||||
attached() {
|
attached() {
|
||||||
if (typeof data === 'function') {
|
if (typeof data === 'function') {
|
||||||
|
|
@ -785,27 +787,55 @@ export function createComponent(option, features) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
attached && attached.call(this);
|
||||||
if (this.props.oakPath ||
|
if (this.props.oakPath ||
|
||||||
(this.iAmThePage() && this.oakOption.path)) {
|
(this.iAmThePage() && this.oakOption.path)) {
|
||||||
const pathState = onPathSet.call(this, this.oakOption, this.iAmThePage());
|
const pathState = onPathSet.call(this, this.oakOption, this.iAmThePage());
|
||||||
this.setState(pathState, () => {
|
this.setState(pathState, async () => {
|
||||||
if (this.lifetime === 'detached') {
|
if (this.oakLifetime === 'detached') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { oakFullpath } = this.state;
|
const { oakFullpath } = this.state;
|
||||||
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
||||||
this.refresh();
|
try {
|
||||||
|
await this.refresh();
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.reRender();
|
this.reRender();
|
||||||
}
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
|
if (this._readyCalled) {
|
||||||
|
// 小程序生命周期里的ready已经调用过了,在这里调用ready
|
||||||
|
try {
|
||||||
|
ready && await ready.call(this);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (!this.oakOption.entity) {
|
else if (!this.oakOption.entity) {
|
||||||
this.reRender();
|
this.reRender();
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
}
|
}
|
||||||
attached && attached.call(this);
|
|
||||||
this.lifetime = 'attached';
|
|
||||||
},
|
},
|
||||||
detached() {
|
detached() {
|
||||||
this.unsubscribeAll();
|
this.unsubscribeAll();
|
||||||
|
|
@ -813,20 +843,37 @@ export function createComponent(option, features) {
|
||||||
(this.iAmThePage() || !this.props.oakZombie) &&
|
(this.iAmThePage() || !this.props.oakZombie) &&
|
||||||
destroyNode.call(this, this.iAmThePage());
|
destroyNode.call(this, this.iAmThePage());
|
||||||
detached && detached.call(this);
|
detached && detached.call(this);
|
||||||
this.lifetime = 'detached';
|
this.oakLifetime = 'detached';
|
||||||
},
|
},
|
||||||
ready() {
|
async ready() {
|
||||||
// 等oakFullpath构建完成后再ready
|
/* // 等oakFullpath构建完成后再ready
|
||||||
|
// 这代码已经看不懂,感觉没用。 by Xc 20240701
|
||||||
if (this.state.oakFullpath) {
|
if (this.state.oakFullpath) {
|
||||||
if (this.props.oakId) {
|
if (this.props.oakId) {
|
||||||
this.features.runningTree.setId(this.state.oakFullpath, this.props.oakId);
|
this.features.runningTree.setId(
|
||||||
|
this.state.oakFullpath,
|
||||||
|
this.props.oakId
|
||||||
|
);
|
||||||
}
|
}
|
||||||
ready && ready.call(this);
|
ready && ready.call(this);
|
||||||
}
|
}
|
||||||
else if (!this.oakOption.entity) {
|
else if (!this.oakOption.entity) {
|
||||||
ready && ready.call(this);
|
ready && ready.call(this);
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
} */
|
||||||
|
if (this.oakLifetime === 'attached') {
|
||||||
|
try {
|
||||||
|
ready && await ready.call(this);
|
||||||
}
|
}
|
||||||
this.lifetime = 'ready';
|
catch (err) {
|
||||||
|
if (err instanceof OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
|
this._readyCalled = true;
|
||||||
},
|
},
|
||||||
moved() {
|
moved() {
|
||||||
moved && moved.call(this);
|
moved && moved.call(this);
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,10 @@ export declare function createComponent<IsList extends boolean, ED extends Entit
|
||||||
new (props: ComponentProps<ED, T, TProperty>): {
|
new (props: ComponentProps<ED, T, TProperty>): {
|
||||||
features: BasicFeatures<ED> & FD;
|
features: BasicFeatures<ED> & FD;
|
||||||
oakOption: OakComponentOption<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData, TProperty, TMethod>;
|
oakOption: OakComponentOption<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData, TProperty, TMethod>;
|
||||||
|
oakLifetime: string;
|
||||||
isReachBottom: boolean;
|
isReachBottom: boolean;
|
||||||
methodProps: Record<string, Function>;
|
methodProps: Record<string, Function>;
|
||||||
defaultProperties: Record<string, any>;
|
defaultProperties: Record<string, any>;
|
||||||
unmounted: boolean;
|
|
||||||
iAmThePage(): boolean;
|
iAmThePage(): boolean;
|
||||||
isMobile(): boolean;
|
isMobile(): boolean;
|
||||||
supportPullDownRefresh(): boolean;
|
supportPullDownRefresh(): boolean;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { assert } from 'oak-domain/lib/utils/assert';
|
import { assert } from 'oak-domain/lib/utils/assert';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { get, pull } from 'oak-domain/lib/utils/lodash';
|
import { get, pull } from 'oak-domain/lib/utils/lodash';
|
||||||
|
import { OakException } from 'oak-domain/lib/types';
|
||||||
import { onPathSet, reRender, refresh, loadMore, execute, destroyNode, getFreshValue, } from './page.common';
|
import { onPathSet, reRender, refresh, loadMore, execute, destroyNode, getFreshValue, } from './page.common';
|
||||||
class OakComponentBase extends React.PureComponent {
|
class OakComponentBase extends React.PureComponent {
|
||||||
featuresSubscribed = [];
|
featuresSubscribed = [];
|
||||||
|
|
@ -408,10 +409,10 @@ export function createComponent(option, features) {
|
||||||
class OakComponentWrapper extends OakComponentBase {
|
class OakComponentWrapper extends OakComponentBase {
|
||||||
features = features;
|
features = features;
|
||||||
oakOption = option;
|
oakOption = option;
|
||||||
|
oakLifetime = 'born';
|
||||||
isReachBottom = false;
|
isReachBottom = false;
|
||||||
methodProps;
|
methodProps;
|
||||||
defaultProperties;
|
defaultProperties;
|
||||||
unmounted = false;
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
const methodProps = {
|
const methodProps = {
|
||||||
|
|
@ -567,7 +568,9 @@ export function createComponent(option, features) {
|
||||||
this.defaultProperties[property] = properties[property];
|
this.defaultProperties[property] = properties[property];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lifetimes?.created && lifetimes.created.call(this);
|
const createResult = lifetimes?.created && lifetimes.created.call(this);
|
||||||
|
assert(!(createResult instanceof Promise), 'created方法不能是异步');
|
||||||
|
this.oakLifetime = 'created';
|
||||||
}
|
}
|
||||||
// 编译器只会在page层注入path,component依赖父亲设置的oakPath
|
// 编译器只会在page层注入path,component依赖父亲设置的oakPath
|
||||||
iAmThePage() {
|
iAmThePage() {
|
||||||
|
|
@ -589,30 +592,6 @@ export function createComponent(option, features) {
|
||||||
this.addFeatureSub('cache', () => this.reRender());
|
this.addFeatureSub('cache', () => this.reRender());
|
||||||
}
|
}
|
||||||
lifetimes?.attached && lifetimes.attached.call(this);
|
lifetimes?.attached && lifetimes.attached.call(this);
|
||||||
const { oakPath } = this.props;
|
|
||||||
if (oakPath || path) {
|
|
||||||
const pathState = onPathSet.call(this, this.oakOption, this.iAmThePage());
|
|
||||||
if (this.unmounted) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.setState(pathState, () => {
|
|
||||||
lifetimes?.ready && lifetimes.ready.call(this);
|
|
||||||
lifetimes?.show && lifetimes.show.call(this);
|
|
||||||
const { oakFullpath } = this.state;
|
|
||||||
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
|
||||||
this.refresh();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.reRender();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (!this.oakOption.entity) {
|
|
||||||
// 如果没有entity,也不需要onPathSet,直接走ready
|
|
||||||
lifetimes?.ready && lifetimes.ready.call(this);
|
|
||||||
lifetimes?.show && lifetimes.show.call(this);
|
|
||||||
this.reRender();
|
|
||||||
}
|
|
||||||
if (option.features) {
|
if (option.features) {
|
||||||
option.features.forEach(ele => {
|
option.features.forEach(ele => {
|
||||||
if (typeof ele === 'string') {
|
if (typeof ele === 'string') {
|
||||||
|
|
@ -645,22 +624,63 @@ export function createComponent(option, features) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
|
const { oakPath } = this.props;
|
||||||
|
if (oakPath || path) {
|
||||||
|
const pathState = onPathSet.call(this, this.oakOption, this.iAmThePage());
|
||||||
|
this.setState(pathState, async () => {
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const { oakFullpath } = this.state;
|
||||||
|
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
||||||
|
await this.refresh();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.reRender();
|
||||||
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lifetimes?.ready && await lifetimes.ready.call(this);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lifetimes?.show && lifetimes.show.call(this);
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (!this.oakOption.entity) {
|
||||||
|
// 如果没有entity,也不需要onPathSet,直接走ready
|
||||||
|
lifetimes?.ready && await lifetimes.ready.call(this);
|
||||||
|
lifetimes?.show && lifetimes.show.call(this);
|
||||||
|
this.reRender();
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
this.unsubscribeAll();
|
this.unsubscribeAll();
|
||||||
this.state.oakFullpath && (this.iAmThePage() || !this.props.oakZombie) && destroyNode.call(this, this.iAmThePage());
|
this.state.oakFullpath && (this.iAmThePage() || !this.props.oakZombie) && destroyNode.call(this, this.iAmThePage());
|
||||||
lifetimes?.detached && lifetimes.detached.call(this);
|
lifetimes?.detached && lifetimes.detached.call(this);
|
||||||
this.unmounted = true;
|
this.oakLifetime = 'detached';
|
||||||
}
|
}
|
||||||
async componentDidUpdate(prevProps, prevState) {
|
async componentDidUpdate(prevProps, prevState) {
|
||||||
if (prevProps.oakPath !== this.props.oakPath) {
|
if (prevProps.oakPath !== this.props.oakPath) {
|
||||||
// oakPath如果是用变量初始化,在这里再执行onPathSet,如果有entity的结点在此执行ready
|
// oakPath如果是用变量初始化,在这里再执行onPathSet,如果有entity的结点在此执行ready
|
||||||
assert(this.props.oakPath && this.oakOption.entity);
|
assert(this.props.oakPath && this.oakOption.entity);
|
||||||
const pathState = onPathSet.call(this, this.oakOption, this.iAmThePage());
|
const pathState = onPathSet.call(this, this.oakOption, this.iAmThePage());
|
||||||
if (this.unmounted) {
|
this.setState(pathState, async () => {
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.setState(pathState, async () => {
|
|
||||||
if (prevProps.oakPath === undefined) {
|
if (prevProps.oakPath === undefined) {
|
||||||
// 如果每个页面都在oakFullpath形成后再渲染子结点,这个if感觉是不应该命中的
|
// 如果每个页面都在oakFullpath形成后再渲染子结点,这个if感觉是不应该命中的
|
||||||
console.warn('发生了结点先形成再配置oakPath的情况,请检查代码修正');
|
console.warn('发生了结点先形成再配置oakPath的情况,请检查代码修正');
|
||||||
|
|
|
||||||
|
|
@ -95,18 +95,20 @@ export type ComponentPublicThisType<ED extends EntityDict & BaseEntityDict, T ex
|
||||||
setState: (data: Partial<ComponentData<ED, T, FormedData, TData>>, callback?: () => void) => void;
|
setState: (data: Partial<ComponentData<ED, T, FormedData, TData>>, callback?: () => void) => void;
|
||||||
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
|
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
|
||||||
} & TMethod & EMethod & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
} & TMethod & EMethod & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
||||||
|
export type OakLifetime = 'born' | 'created' | 'attached' | 'ready' | 'detached';
|
||||||
export type ComponentFullThisType<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = {
|
export type ComponentFullThisType<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = {
|
||||||
features: BasicFeatures<ED>;
|
features: BasicFeatures<ED>;
|
||||||
state: OakComponentData<ED, T>;
|
state: OakComponentData<ED, T>;
|
||||||
props: ComponentProps<ED, T, {}>;
|
props: ComponentProps<ED, T, {}>;
|
||||||
setState: (data: Partial<OakComponentData<ED, T>>, callback?: () => void) => void;
|
setState: (data: Partial<OakComponentData<ED, T>>, callback?: () => void) => void;
|
||||||
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
|
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
|
||||||
|
oakLifetime: OakLifetime;
|
||||||
} & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
} & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
||||||
export type OakComponentOption<IsList extends boolean, ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>, FrontCxt extends SyncContext<ED>, AD extends Record<string, Aspect<ED, AsyncContext<ED>>>, FD extends Record<string, Feature>, FormedData extends Record<string, any>, TData extends Record<string, any>, TProperty extends DataOption, TMethod extends Record<string, Function>, EMethod extends Record<string, Function> = {}> = ComponentOption<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData, TProperty, TMethod, EMethod> & Partial<{
|
export type OakComponentOption<IsList extends boolean, ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>, FrontCxt extends SyncContext<ED>, AD extends Record<string, Aspect<ED, AsyncContext<ED>>>, FD extends Record<string, Feature>, FormedData extends Record<string, any>, TData extends Record<string, any>, TProperty extends DataOption, TMethod extends Record<string, Function>, EMethod extends Record<string, Function> = {}> = ComponentOption<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData, TProperty, TMethod, EMethod> & Partial<{
|
||||||
lifetimes: {
|
lifetimes: {
|
||||||
created?(): void;
|
created?(): void;
|
||||||
attached?(): void;
|
attached?(): void;
|
||||||
ready?(): void;
|
ready?(): any;
|
||||||
moved?(): void;
|
moved?(): void;
|
||||||
detached?(): void;
|
detached?(): void;
|
||||||
error?(err: Error): void;
|
error?(err: Error): void;
|
||||||
|
|
|
||||||
|
|
@ -502,10 +502,11 @@ function reRender(option, extra) {
|
||||||
catch (err) {
|
catch (err) {
|
||||||
rollback();
|
rollback();
|
||||||
console.error(err);
|
console.error(err);
|
||||||
(0, assert_1.assert)(false);
|
// assert(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
try {
|
||||||
const data = formData
|
const data = formData
|
||||||
? formData.call(this, {
|
? formData.call(this, {
|
||||||
features,
|
features,
|
||||||
|
|
@ -539,6 +540,10 @@ function reRender(option, extra) {
|
||||||
}); // 有些环境下如果传空值不触发判断
|
}); // 有些环境下如果传空值不触发判断
|
||||||
this.setState(data);
|
this.setState(data);
|
||||||
}
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
exports.reRender = reRender;
|
exports.reRender = reRender;
|
||||||
async function refresh() {
|
async function refresh() {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.createComponent = void 0;
|
exports.createComponent = void 0;
|
||||||
/// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" />
|
/// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" />
|
||||||
const assert_1 = require("oak-domain/lib/utils/assert");
|
const assert_1 = require("oak-domain/lib/utils/assert");
|
||||||
|
const types_1 = require("oak-domain/lib/types");
|
||||||
const page_common_1 = require("./page.common");
|
const page_common_1 = require("./page.common");
|
||||||
const lodash_1 = require("oak-domain/lib/utils/lodash");
|
const lodash_1 = require("oak-domain/lib/utils/lodash");
|
||||||
const OakProperties = {
|
const OakProperties = {
|
||||||
|
|
@ -723,7 +724,8 @@ function createComponent(option, features) {
|
||||||
this.features = features;
|
this.features = features;
|
||||||
this.featuresSubscribed = [];
|
this.featuresSubscribed = [];
|
||||||
created && created.call(this);
|
created && created.call(this);
|
||||||
this.lifetime = 'created';
|
this._readyCalled = false;
|
||||||
|
this.oakLifetime = 'created';
|
||||||
},
|
},
|
||||||
attached() {
|
attached() {
|
||||||
if (typeof data === 'function') {
|
if (typeof data === 'function') {
|
||||||
|
|
@ -788,27 +790,55 @@ function createComponent(option, features) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
attached && attached.call(this);
|
||||||
if (this.props.oakPath ||
|
if (this.props.oakPath ||
|
||||||
(this.iAmThePage() && this.oakOption.path)) {
|
(this.iAmThePage() && this.oakOption.path)) {
|
||||||
const pathState = page_common_1.onPathSet.call(this, this.oakOption, this.iAmThePage());
|
const pathState = page_common_1.onPathSet.call(this, this.oakOption, this.iAmThePage());
|
||||||
this.setState(pathState, () => {
|
this.setState(pathState, async () => {
|
||||||
if (this.lifetime === 'detached') {
|
if (this.oakLifetime === 'detached') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { oakFullpath } = this.state;
|
const { oakFullpath } = this.state;
|
||||||
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
||||||
this.refresh();
|
try {
|
||||||
|
await this.refresh();
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof types_1.OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.reRender();
|
this.reRender();
|
||||||
}
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
|
if (this._readyCalled) {
|
||||||
|
// 小程序生命周期里的ready已经调用过了,在这里调用ready
|
||||||
|
try {
|
||||||
|
ready && await ready.call(this);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof types_1.OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (!this.oakOption.entity) {
|
else if (!this.oakOption.entity) {
|
||||||
this.reRender();
|
this.reRender();
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
}
|
}
|
||||||
attached && attached.call(this);
|
|
||||||
this.lifetime = 'attached';
|
|
||||||
},
|
},
|
||||||
detached() {
|
detached() {
|
||||||
this.unsubscribeAll();
|
this.unsubscribeAll();
|
||||||
|
|
@ -816,20 +846,37 @@ function createComponent(option, features) {
|
||||||
(this.iAmThePage() || !this.props.oakZombie) &&
|
(this.iAmThePage() || !this.props.oakZombie) &&
|
||||||
page_common_1.destroyNode.call(this, this.iAmThePage());
|
page_common_1.destroyNode.call(this, this.iAmThePage());
|
||||||
detached && detached.call(this);
|
detached && detached.call(this);
|
||||||
this.lifetime = 'detached';
|
this.oakLifetime = 'detached';
|
||||||
},
|
},
|
||||||
ready() {
|
async ready() {
|
||||||
// 等oakFullpath构建完成后再ready
|
/* // 等oakFullpath构建完成后再ready
|
||||||
|
// 这代码已经看不懂,感觉没用。 by Xc 20240701
|
||||||
if (this.state.oakFullpath) {
|
if (this.state.oakFullpath) {
|
||||||
if (this.props.oakId) {
|
if (this.props.oakId) {
|
||||||
this.features.runningTree.setId(this.state.oakFullpath, this.props.oakId);
|
this.features.runningTree.setId(
|
||||||
|
this.state.oakFullpath,
|
||||||
|
this.props.oakId
|
||||||
|
);
|
||||||
}
|
}
|
||||||
ready && ready.call(this);
|
ready && ready.call(this);
|
||||||
}
|
}
|
||||||
else if (!this.oakOption.entity) {
|
else if (!this.oakOption.entity) {
|
||||||
ready && ready.call(this);
|
ready && ready.call(this);
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
} */
|
||||||
|
if (this.oakLifetime === 'attached') {
|
||||||
|
try {
|
||||||
|
ready && await ready.call(this);
|
||||||
}
|
}
|
||||||
this.lifetime = 'ready';
|
catch (err) {
|
||||||
|
if (err instanceof types_1.OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
|
this._readyCalled = true;
|
||||||
},
|
},
|
||||||
moved() {
|
moved() {
|
||||||
moved && moved.call(this);
|
moved && moved.call(this);
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,10 @@ export declare function createComponent<IsList extends boolean, ED extends Entit
|
||||||
new (props: ComponentProps<ED, T, TProperty>): {
|
new (props: ComponentProps<ED, T, TProperty>): {
|
||||||
features: BasicFeatures<ED> & FD;
|
features: BasicFeatures<ED> & FD;
|
||||||
oakOption: OakComponentOption<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData, TProperty, TMethod>;
|
oakOption: OakComponentOption<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData, TProperty, TMethod>;
|
||||||
|
oakLifetime: string;
|
||||||
isReachBottom: boolean;
|
isReachBottom: boolean;
|
||||||
methodProps: Record<string, Function>;
|
methodProps: Record<string, Function>;
|
||||||
defaultProperties: Record<string, any>;
|
defaultProperties: Record<string, any>;
|
||||||
unmounted: boolean;
|
|
||||||
iAmThePage(): boolean;
|
iAmThePage(): boolean;
|
||||||
isMobile(): boolean;
|
isMobile(): boolean;
|
||||||
supportPullDownRefresh(): boolean;
|
supportPullDownRefresh(): boolean;
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ const jsx_runtime_1 = require("react/jsx-runtime");
|
||||||
const assert_1 = require("oak-domain/lib/utils/assert");
|
const assert_1 = require("oak-domain/lib/utils/assert");
|
||||||
const react_1 = tslib_1.__importDefault(require("react"));
|
const react_1 = tslib_1.__importDefault(require("react"));
|
||||||
const lodash_1 = require("oak-domain/lib/utils/lodash");
|
const lodash_1 = require("oak-domain/lib/utils/lodash");
|
||||||
|
const types_1 = require("oak-domain/lib/types");
|
||||||
const page_common_1 = require("./page.common");
|
const page_common_1 = require("./page.common");
|
||||||
class OakComponentBase extends react_1.default.PureComponent {
|
class OakComponentBase extends react_1.default.PureComponent {
|
||||||
featuresSubscribed = [];
|
featuresSubscribed = [];
|
||||||
|
|
@ -413,10 +414,10 @@ function createComponent(option, features) {
|
||||||
class OakComponentWrapper extends OakComponentBase {
|
class OakComponentWrapper extends OakComponentBase {
|
||||||
features = features;
|
features = features;
|
||||||
oakOption = option;
|
oakOption = option;
|
||||||
|
oakLifetime = 'born';
|
||||||
isReachBottom = false;
|
isReachBottom = false;
|
||||||
methodProps;
|
methodProps;
|
||||||
defaultProperties;
|
defaultProperties;
|
||||||
unmounted = false;
|
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
const methodProps = {
|
const methodProps = {
|
||||||
|
|
@ -572,7 +573,9 @@ function createComponent(option, features) {
|
||||||
this.defaultProperties[property] = properties[property];
|
this.defaultProperties[property] = properties[property];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lifetimes?.created && lifetimes.created.call(this);
|
const createResult = lifetimes?.created && lifetimes.created.call(this);
|
||||||
|
(0, assert_1.assert)(!(createResult instanceof Promise), 'created方法不能是异步');
|
||||||
|
this.oakLifetime = 'created';
|
||||||
}
|
}
|
||||||
// 编译器只会在page层注入path,component依赖父亲设置的oakPath
|
// 编译器只会在page层注入path,component依赖父亲设置的oakPath
|
||||||
iAmThePage() {
|
iAmThePage() {
|
||||||
|
|
@ -594,30 +597,6 @@ function createComponent(option, features) {
|
||||||
this.addFeatureSub('cache', () => this.reRender());
|
this.addFeatureSub('cache', () => this.reRender());
|
||||||
}
|
}
|
||||||
lifetimes?.attached && lifetimes.attached.call(this);
|
lifetimes?.attached && lifetimes.attached.call(this);
|
||||||
const { oakPath } = this.props;
|
|
||||||
if (oakPath || path) {
|
|
||||||
const pathState = page_common_1.onPathSet.call(this, this.oakOption, this.iAmThePage());
|
|
||||||
if (this.unmounted) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.setState(pathState, () => {
|
|
||||||
lifetimes?.ready && lifetimes.ready.call(this);
|
|
||||||
lifetimes?.show && lifetimes.show.call(this);
|
|
||||||
const { oakFullpath } = this.state;
|
|
||||||
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
|
||||||
this.refresh();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.reRender();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (!this.oakOption.entity) {
|
|
||||||
// 如果没有entity,也不需要onPathSet,直接走ready
|
|
||||||
lifetimes?.ready && lifetimes.ready.call(this);
|
|
||||||
lifetimes?.show && lifetimes.show.call(this);
|
|
||||||
this.reRender();
|
|
||||||
}
|
|
||||||
if (option.features) {
|
if (option.features) {
|
||||||
option.features.forEach(ele => {
|
option.features.forEach(ele => {
|
||||||
if (typeof ele === 'string') {
|
if (typeof ele === 'string') {
|
||||||
|
|
@ -650,22 +629,63 @@ function createComponent(option, features) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
|
const { oakPath } = this.props;
|
||||||
|
if (oakPath || path) {
|
||||||
|
const pathState = page_common_1.onPathSet.call(this, this.oakOption, this.iAmThePage());
|
||||||
|
this.setState(pathState, async () => {
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const { oakFullpath } = this.state;
|
||||||
|
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
||||||
|
await this.refresh();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.reRender();
|
||||||
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lifetimes?.ready && await lifetimes.ready.call(this);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof types_1.OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lifetimes?.show && lifetimes.show.call(this);
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (!this.oakOption.entity) {
|
||||||
|
// 如果没有entity,也不需要onPathSet,直接走ready
|
||||||
|
lifetimes?.ready && await lifetimes.ready.call(this);
|
||||||
|
lifetimes?.show && lifetimes.show.call(this);
|
||||||
|
this.reRender();
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
this.unsubscribeAll();
|
this.unsubscribeAll();
|
||||||
this.state.oakFullpath && (this.iAmThePage() || !this.props.oakZombie) && page_common_1.destroyNode.call(this, this.iAmThePage());
|
this.state.oakFullpath && (this.iAmThePage() || !this.props.oakZombie) && page_common_1.destroyNode.call(this, this.iAmThePage());
|
||||||
lifetimes?.detached && lifetimes.detached.call(this);
|
lifetimes?.detached && lifetimes.detached.call(this);
|
||||||
this.unmounted = true;
|
this.oakLifetime = 'detached';
|
||||||
}
|
}
|
||||||
async componentDidUpdate(prevProps, prevState) {
|
async componentDidUpdate(prevProps, prevState) {
|
||||||
if (prevProps.oakPath !== this.props.oakPath) {
|
if (prevProps.oakPath !== this.props.oakPath) {
|
||||||
// oakPath如果是用变量初始化,在这里再执行onPathSet,如果有entity的结点在此执行ready
|
// oakPath如果是用变量初始化,在这里再执行onPathSet,如果有entity的结点在此执行ready
|
||||||
(0, assert_1.assert)(this.props.oakPath && this.oakOption.entity);
|
(0, assert_1.assert)(this.props.oakPath && this.oakOption.entity);
|
||||||
const pathState = page_common_1.onPathSet.call(this, this.oakOption, this.iAmThePage());
|
const pathState = page_common_1.onPathSet.call(this, this.oakOption, this.iAmThePage());
|
||||||
if (this.unmounted) {
|
this.setState(pathState, async () => {
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.setState(pathState, async () => {
|
|
||||||
if (prevProps.oakPath === undefined) {
|
if (prevProps.oakPath === undefined) {
|
||||||
// 如果每个页面都在oakFullpath形成后再渲染子结点,这个if感觉是不应该命中的
|
// 如果每个页面都在oakFullpath形成后再渲染子结点,这个if感觉是不应该命中的
|
||||||
console.warn('发生了结点先形成再配置oakPath的情况,请检查代码修正');
|
console.warn('发生了结点先形成再配置oakPath的情况,请检查代码修正');
|
||||||
|
|
|
||||||
|
|
@ -95,18 +95,20 @@ export type ComponentPublicThisType<ED extends EntityDict & BaseEntityDict, T ex
|
||||||
setState: (data: Partial<ComponentData<ED, T, FormedData, TData>>, callback?: () => void) => void;
|
setState: (data: Partial<ComponentData<ED, T, FormedData, TData>>, callback?: () => void) => void;
|
||||||
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
|
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
|
||||||
} & TMethod & EMethod & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
} & TMethod & EMethod & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
||||||
|
export type OakLifetime = 'born' | 'created' | 'attached' | 'ready' | 'detached';
|
||||||
export type ComponentFullThisType<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = {
|
export type ComponentFullThisType<ED extends EntityDict & BaseEntityDict, T extends keyof ED> = {
|
||||||
features: BasicFeatures<ED>;
|
features: BasicFeatures<ED>;
|
||||||
state: OakComponentData<ED, T>;
|
state: OakComponentData<ED, T>;
|
||||||
props: ComponentProps<ED, T, {}>;
|
props: ComponentProps<ED, T, {}>;
|
||||||
setState: (data: Partial<OakComponentData<ED, T>>, callback?: () => void) => void;
|
setState: (data: Partial<OakComponentData<ED, T>>, callback?: () => void) => void;
|
||||||
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
|
triggerEvent: <DetailType = any>(name: string, detail?: DetailType, options?: WechatMiniprogram.Component.TriggerEventOption) => void;
|
||||||
|
oakLifetime: OakLifetime;
|
||||||
} & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
} & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
||||||
export type OakComponentOption<IsList extends boolean, ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>, FrontCxt extends SyncContext<ED>, AD extends Record<string, Aspect<ED, AsyncContext<ED>>>, FD extends Record<string, Feature>, FormedData extends Record<string, any>, TData extends Record<string, any>, TProperty extends DataOption, TMethod extends Record<string, Function>, EMethod extends Record<string, Function> = {}> = ComponentOption<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData, TProperty, TMethod, EMethod> & Partial<{
|
export type OakComponentOption<IsList extends boolean, ED extends EntityDict & BaseEntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>, FrontCxt extends SyncContext<ED>, AD extends Record<string, Aspect<ED, AsyncContext<ED>>>, FD extends Record<string, Feature>, FormedData extends Record<string, any>, TData extends Record<string, any>, TProperty extends DataOption, TMethod extends Record<string, Function>, EMethod extends Record<string, Function> = {}> = ComponentOption<IsList, ED, T, Cxt, FrontCxt, AD, FD, FormedData, TData, TProperty, TMethod, EMethod> & Partial<{
|
||||||
lifetimes: {
|
lifetimes: {
|
||||||
created?(): void;
|
created?(): void;
|
||||||
attached?(): void;
|
attached?(): void;
|
||||||
ready?(): void;
|
ready?(): any;
|
||||||
moved?(): void;
|
moved?(): void;
|
||||||
detached?(): void;
|
detached?(): void;
|
||||||
error?(err: Error): void;
|
error?(err: Error): void;
|
||||||
|
|
|
||||||
|
|
@ -610,9 +610,10 @@ export function reRender<
|
||||||
catch (err) {
|
catch (err) {
|
||||||
rollback();
|
rollback();
|
||||||
console.error(err);
|
console.error(err);
|
||||||
assert(false);
|
// assert(false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
try {
|
||||||
const data: Record<string, any> = formData
|
const data: Record<string, any> = formData
|
||||||
? formData.call(this, {
|
? formData.call(this, {
|
||||||
features,
|
features,
|
||||||
|
|
@ -648,6 +649,10 @@ export function reRender<
|
||||||
}); // 有些环境下如果传空值不触发判断
|
}); // 有些环境下如果传空值不触发判断
|
||||||
this.setState(data);
|
this.setState(data);
|
||||||
}
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function refresh<
|
export async function refresh<
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" />
|
/// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" />
|
||||||
import { assert } from 'oak-domain/lib/utils/assert';
|
import { assert } from 'oak-domain/lib/utils/assert';
|
||||||
import { Aspect, CheckerType, EntityDict } from 'oak-domain/lib/types';
|
import { Aspect, CheckerType, EntityDict, OakException } from 'oak-domain/lib/types';
|
||||||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||||
import { BasicFeatures } from './features';
|
import { BasicFeatures } from './features';
|
||||||
import { Feature } from './types/Feature';
|
import { Feature } from './types/Feature';
|
||||||
|
|
@ -910,7 +910,8 @@ export function createComponent<
|
||||||
WechatMiniprogram.Component.PropertyOption,
|
WechatMiniprogram.Component.PropertyOption,
|
||||||
MethodOption,
|
MethodOption,
|
||||||
{
|
{
|
||||||
lifetime: 'created' | 'attached' | 'ready' | 'detached';
|
oakLifetime: 'created' | 'attached' | 'ready' | 'detached';
|
||||||
|
_readyCalled: boolean;
|
||||||
prevState: Record<string, any>;
|
prevState: Record<string, any>;
|
||||||
state: Record<string, any>;
|
state: Record<string, any>;
|
||||||
featuresSubscribed: Array<{
|
featuresSubscribed: Array<{
|
||||||
|
|
@ -1046,7 +1047,8 @@ export function createComponent<
|
||||||
this.features = features;
|
this.features = features;
|
||||||
this.featuresSubscribed = [];
|
this.featuresSubscribed = [];
|
||||||
created && created.call(this);
|
created && created.call(this);
|
||||||
this.lifetime = 'created';
|
this._readyCalled = false;
|
||||||
|
this.oakLifetime = 'created';
|
||||||
},
|
},
|
||||||
attached() {
|
attached() {
|
||||||
if (typeof data === 'function') {
|
if (typeof data === 'function') {
|
||||||
|
|
@ -1111,6 +1113,7 @@ export function createComponent<
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attached && attached.call(this);
|
||||||
if (this.props.oakPath ||
|
if (this.props.oakPath ||
|
||||||
(this.iAmThePage() && this.oakOption.path)) {
|
(this.iAmThePage() && this.oakOption.path)) {
|
||||||
const pathState = onPathSet.call(
|
const pathState = onPathSet.call(
|
||||||
|
|
@ -1118,22 +1121,49 @@ export function createComponent<
|
||||||
this.oakOption as any,
|
this.oakOption as any,
|
||||||
this.iAmThePage()
|
this.iAmThePage()
|
||||||
);
|
);
|
||||||
this.setState(pathState as any, () => {
|
this.setState(pathState as any, async () => {
|
||||||
if (this.lifetime === 'detached') {
|
if (this.oakLifetime === 'detached') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { oakFullpath } = this.state;
|
const { oakFullpath } = this.state;
|
||||||
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
||||||
this.refresh();
|
try {
|
||||||
|
await this.refresh();
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.reRender();
|
this.reRender();
|
||||||
}
|
}
|
||||||
|
if (this.oakLifetime as any === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
|
if (this._readyCalled) {
|
||||||
|
// 小程序生命周期里的ready已经调用过了,在这里调用ready
|
||||||
|
try {
|
||||||
|
ready && await ready.call(this);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
if (this.oakLifetime as any === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else if (!this.oakOption.entity) {
|
} else if (!this.oakOption.entity) {
|
||||||
this.reRender();
|
this.reRender();
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
}
|
}
|
||||||
attached && attached.call(this);
|
|
||||||
this.lifetime = 'attached';
|
|
||||||
},
|
},
|
||||||
detached() {
|
detached() {
|
||||||
this.unsubscribeAll();
|
this.unsubscribeAll();
|
||||||
|
|
@ -1142,10 +1172,11 @@ export function createComponent<
|
||||||
destroyNode.call(this as any, this.iAmThePage());
|
destroyNode.call(this as any, this.iAmThePage());
|
||||||
|
|
||||||
detached && detached.call(this);
|
detached && detached.call(this);
|
||||||
this.lifetime = 'detached';
|
this.oakLifetime = 'detached';
|
||||||
},
|
},
|
||||||
ready() {
|
async ready() {
|
||||||
// 等oakFullpath构建完成后再ready
|
/* // 等oakFullpath构建完成后再ready
|
||||||
|
// 这代码已经看不懂,感觉没用。 by Xc 20240701
|
||||||
if (this.state.oakFullpath) {
|
if (this.state.oakFullpath) {
|
||||||
if (this.props.oakId) {
|
if (this.props.oakId) {
|
||||||
this.features.runningTree.setId(
|
this.features.runningTree.setId(
|
||||||
|
|
@ -1157,8 +1188,21 @@ export function createComponent<
|
||||||
}
|
}
|
||||||
else if (!this.oakOption.entity) {
|
else if (!this.oakOption.entity) {
|
||||||
ready && ready.call(this);
|
ready && ready.call(this);
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
} */
|
||||||
|
if (this.oakLifetime === 'attached') {
|
||||||
|
try {
|
||||||
|
ready && await ready.call(this);
|
||||||
}
|
}
|
||||||
this.lifetime = 'ready';
|
catch (err) {
|
||||||
|
if (err instanceof OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
|
this._readyCalled = true;
|
||||||
},
|
},
|
||||||
moved() {
|
moved() {
|
||||||
moved && moved.call(this);
|
moved && moved.call(this);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { assert } from 'oak-domain/lib/utils/assert';
|
import { assert } from 'oak-domain/lib/utils/assert';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { get, pull } from 'oak-domain/lib/utils/lodash';
|
import { get, pull } from 'oak-domain/lib/utils/lodash';
|
||||||
import { Aspect, CheckerType, EntityDict, OpRecord } from 'oak-domain/lib/types';
|
import { Aspect, CheckerType, EntityDict, OakException, OpRecord } from 'oak-domain/lib/types';
|
||||||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||||
import { BasicFeatures } from './features';
|
import { BasicFeatures } from './features';
|
||||||
import { NamedFilterItem, NamedSorterItem } from './types/NamedCondition';
|
import { NamedFilterItem, NamedSorterItem } from './types/NamedCondition';
|
||||||
|
|
@ -691,10 +691,10 @@ export function createComponent<
|
||||||
class OakComponentWrapper extends OakComponentBase<ED, T, Cxt, FrontCxt, AD, FD, FormedData, IsList, TData, TProperty, TMethod> {
|
class OakComponentWrapper extends OakComponentBase<ED, T, Cxt, FrontCxt, AD, FD, FormedData, IsList, TData, TProperty, TMethod> {
|
||||||
features = features;
|
features = features;
|
||||||
oakOption = option;
|
oakOption = option;
|
||||||
|
oakLifetime = 'born';
|
||||||
isReachBottom = false;
|
isReachBottom = false;
|
||||||
methodProps: Record<string, Function>;
|
methodProps: Record<string, Function>;
|
||||||
defaultProperties: Record<string, any>;
|
defaultProperties: Record<string, any>;
|
||||||
unmounted: boolean = false;
|
|
||||||
|
|
||||||
constructor(props: ComponentProps<ED, T, TProperty>) {
|
constructor(props: ComponentProps<ED, T, TProperty>) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
@ -885,7 +885,9 @@ export function createComponent<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lifetimes?.created && lifetimes.created.call(this);
|
const createResult = lifetimes?.created && lifetimes.created.call(this);
|
||||||
|
assert(!(createResult as any instanceof Promise), 'created方法不能是异步');
|
||||||
|
this.oakLifetime = 'created';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 编译器只会在page层注入path,component依赖父亲设置的oakPath
|
// 编译器只会在page层注入path,component依赖父亲设置的oakPath
|
||||||
|
|
@ -914,32 +916,6 @@ export function createComponent<
|
||||||
this.addFeatureSub('cache', () => this.reRender());
|
this.addFeatureSub('cache', () => this.reRender());
|
||||||
}
|
}
|
||||||
lifetimes?.attached && lifetimes.attached.call(this);
|
lifetimes?.attached && lifetimes.attached.call(this);
|
||||||
const { oakPath } = this.props;
|
|
||||||
if (oakPath || path) {
|
|
||||||
const pathState = onPathSet.call(this as any, this.oakOption as any, this.iAmThePage());
|
|
||||||
if (this.unmounted) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.setState(pathState as any, () => {
|
|
||||||
lifetimes?.ready && lifetimes.ready.call(this);
|
|
||||||
lifetimes?.show && lifetimes.show.call(this);
|
|
||||||
|
|
||||||
const { oakFullpath } = this.state;
|
|
||||||
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
|
||||||
this.refresh();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.reRender();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (!this.oakOption.entity) {
|
|
||||||
// 如果没有entity,也不需要onPathSet,直接走ready
|
|
||||||
lifetimes?.ready && lifetimes.ready.call(this);
|
|
||||||
lifetimes?.show && lifetimes.show.call(this);
|
|
||||||
|
|
||||||
this.reRender();
|
|
||||||
}
|
|
||||||
if (option.features) {
|
if (option.features) {
|
||||||
option.features.forEach(
|
option.features.forEach(
|
||||||
ele => {
|
ele => {
|
||||||
|
|
@ -974,6 +950,53 @@ export function createComponent<
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.oakLifetime = 'attached';
|
||||||
|
const { oakPath } = this.props;
|
||||||
|
if (oakPath || path) {
|
||||||
|
const pathState = onPathSet.call(this as any, this.oakOption as any, this.iAmThePage());
|
||||||
|
this.setState(pathState as any, async () => {
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { oakFullpath } = this.state;
|
||||||
|
if (oakFullpath && !features.runningTree.checkIsModiNode(oakFullpath) && !features.runningTree.isListDescandent(oakFullpath)) {
|
||||||
|
await this.refresh();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.reRender();
|
||||||
|
}
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lifetimes?.ready && await lifetimes.ready.call(this);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof OakException) {
|
||||||
|
err.tag2 = true;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lifetimes?.show && lifetimes.show.call(this);
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (!this.oakOption.entity) {
|
||||||
|
// 如果没有entity,也不需要onPathSet,直接走ready
|
||||||
|
lifetimes?.ready && await lifetimes.ready.call(this);
|
||||||
|
lifetimes?.show && lifetimes.show.call(this);
|
||||||
|
|
||||||
|
this.reRender();
|
||||||
|
this.oakLifetime = 'ready';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
|
|
@ -983,7 +1006,7 @@ export function createComponent<
|
||||||
this.iAmThePage()
|
this.iAmThePage()
|
||||||
);
|
);
|
||||||
lifetimes?.detached && lifetimes.detached.call(this);
|
lifetimes?.detached && lifetimes.detached.call(this);
|
||||||
this.unmounted = true;
|
this.oakLifetime = 'detached';
|
||||||
}
|
}
|
||||||
|
|
||||||
async componentDidUpdate(prevProps: Record<string, any>, prevState: Record<string, any>) {
|
async componentDidUpdate(prevProps: Record<string, any>, prevState: Record<string, any>) {
|
||||||
|
|
@ -991,10 +1014,10 @@ export function createComponent<
|
||||||
// oakPath如果是用变量初始化,在这里再执行onPathSet,如果有entity的结点在此执行ready
|
// oakPath如果是用变量初始化,在这里再执行onPathSet,如果有entity的结点在此执行ready
|
||||||
assert(this.props.oakPath && this.oakOption.entity);
|
assert(this.props.oakPath && this.oakOption.entity);
|
||||||
const pathState = onPathSet.call(this as any, this.oakOption as any, this.iAmThePage());
|
const pathState = onPathSet.call(this as any, this.oakOption as any, this.iAmThePage());
|
||||||
if (this.unmounted) {
|
this.setState(pathState as any, async () => {
|
||||||
|
if (this.oakLifetime === 'detached') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.setState(pathState as any, async () => {
|
|
||||||
if (prevProps.oakPath === undefined) {
|
if (prevProps.oakPath === undefined) {
|
||||||
// 如果每个页面都在oakFullpath形成后再渲染子结点,这个if感觉是不应该命中的
|
// 如果每个页面都在oakFullpath形成后再渲染子结点,这个if感觉是不应该命中的
|
||||||
console.warn('发生了结点先形成再配置oakPath的情况,请检查代码修正');
|
console.warn('发生了结点先形成再配置oakPath的情况,请检查代码修正');
|
||||||
|
|
|
||||||
|
|
@ -245,6 +245,7 @@ export type ComponentPublicThisType<
|
||||||
) => void;
|
) => void;
|
||||||
} & TMethod & EMethod & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
} & TMethod & EMethod & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
||||||
|
|
||||||
|
export type OakLifetime = 'born' | 'created' | 'attached' | 'ready' | 'detached';
|
||||||
export type ComponentFullThisType<
|
export type ComponentFullThisType<
|
||||||
ED extends EntityDict & BaseEntityDict,
|
ED extends EntityDict & BaseEntityDict,
|
||||||
T extends keyof ED
|
T extends keyof ED
|
||||||
|
|
@ -261,6 +262,7 @@ export type ComponentFullThisType<
|
||||||
detail?: DetailType,
|
detail?: DetailType,
|
||||||
options?: WechatMiniprogram.Component.TriggerEventOption
|
options?: WechatMiniprogram.Component.TriggerEventOption
|
||||||
) => void;
|
) => void;
|
||||||
|
oakLifetime: OakLifetime;
|
||||||
} & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
} & OakCommonComponentMethods<ED, T> & OakListComponentMethods<ED, T> & OakSingleComponentMethods<ED, T>;
|
||||||
|
|
||||||
export type OakComponentOption<
|
export type OakComponentOption<
|
||||||
|
|
@ -281,7 +283,7 @@ export type OakComponentOption<
|
||||||
lifetimes: {
|
lifetimes: {
|
||||||
created?(): void;
|
created?(): void;
|
||||||
attached?(): void;
|
attached?(): void;
|
||||||
ready?(): void;
|
ready?(): any;
|
||||||
moved?(): void;
|
moved?(): void;
|
||||||
detached?(): void;
|
detached?(): void;
|
||||||
error?(err: Error): void;
|
error?(err: Error): void;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue