适配了oak-domain声明的简化

This commit is contained in:
Xu Chang 2023-01-19 14:57:24 +08:00
parent abc5533b3e
commit f8bfec396e
11 changed files with 39 additions and 43 deletions

View File

@ -1,4 +1,4 @@
import { EntityDict, StorageSchema, OpRecord, DeduceSorterItem, AspectWrapper } from "oak-domain/lib/types"; import { EntityDict, StorageSchema, OpRecord, AspectWrapper } 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 { CommonAspectDict } from 'oak-common-aspect'; import { CommonAspectDict } from 'oak-common-aspect';
import { NamedFilterItem, NamedSorterItem } from "../types/NamedCondition"; import { NamedFilterItem, NamedSorterItem } from "../types/NamedCondition";
@ -37,7 +37,7 @@ declare abstract class Node<ED extends EntityDict & BaseEntityDict, T extends ke
isExecuting(): boolean; isExecuting(): boolean;
setExecuting(executing: boolean): void; setExecuting(executing: boolean): void;
getParent(): SingleNode<ED, keyof ED, Cxt, FrontCxt, AD> | ListNode<ED, T, Cxt, FrontCxt, AD> | VirtualNode<ED, Cxt, FrontCxt, AD> | undefined; getParent(): SingleNode<ED, keyof ED, Cxt, FrontCxt, AD> | ListNode<ED, T, Cxt, FrontCxt, AD> | VirtualNode<ED, Cxt, FrontCxt, AD> | undefined;
protected getProjection(): ED[T]["Selection"]["data"]; protected getProjection(): any;
protected judgeRelation(attr: string): string | 0 | 1 | 2 | string[]; protected judgeRelation(attr: string): string | 0 | 1 | 2 | string[];
protected contains(filter: ED[T]['Selection']['filter'], conditionalFilter: ED[T]['Selection']['filter']): boolean; protected contains(filter: ED[T]['Selection']['filter'], conditionalFilter: ED[T]['Selection']['filter']): boolean;
protected repel(filter1: ED[T]['Selection']['filter'], filter2: ED[T]['Selection']['filter']): boolean; protected repel(filter1: ED[T]['Selection']['filter'], filter2: ED[T]['Selection']['filter']): boolean;
@ -97,7 +97,7 @@ declare class ListNode<ED extends EntityDict & BaseEntityDict, T extends keyof E
constructSelection(withParent?: true): { constructSelection(withParent?: true): {
data: ED[T]["Selection"]["data"]; data: ED[T]["Selection"]["data"];
filter: ED[T]["Selection"]["filter"] | {}; filter: ED[T]["Selection"]["filter"] | {};
sorter: DeduceSorterItem<ED[T]["Schema"]>[]; sorter: ED[T]["Selection"]["sorter"];
validParentFilter: boolean; validParentFilter: boolean;
}; };
refresh(pageNumber?: number, getCount?: true, append?: boolean): Promise<void>; refresh(pageNumber?: number, getCount?: true, append?: boolean): Promise<void>;
@ -134,7 +134,7 @@ declare class SingleNode<ED extends EntityDict & BaseEntityDict, T extends keyof
entity: T; entity: T;
operation: ED[T]['Operation']; operation: ED[T]['Operation'];
}> | undefined; }> | undefined;
getProjection(withDecendants?: boolean): ED[T]["Selection"]["data"]; getProjection(withDecendants?: boolean): any;
refresh(): Promise<void>; refresh(): Promise<void>;
clean(): void; clean(): void;
getFilter(disableOperation?: boolean): ED[T]['Selection']['filter'] | undefined; getFilter(disableOperation?: boolean): ED[T]['Selection']['filter'] | undefined;

View File

@ -1,10 +1,9 @@
import { EntityDict } from "oak-domain/lib/types"; import { EntityDict } from "oak-domain/lib/types";
import { DeduceSorterItem } from "oak-domain/lib/types/Entity";
export declare type NamedFilterItem<ED extends EntityDict, T extends keyof ED> = { export declare type NamedFilterItem<ED extends EntityDict, T extends keyof ED> = {
filter: ED[T]['Selection']['filter'] | (() => ED[T]['Selection']['filter'] | undefined); filter: ED[T]['Selection']['filter'] | (() => ED[T]['Selection']['filter'] | undefined);
['#name']?: string; ['#name']?: string;
}; };
export declare type NamedSorterItem<ED extends EntityDict, T extends keyof ED> = { export declare type NamedSorterItem<ED extends EntityDict, T extends keyof ED> = {
sorter: DeduceSorterItem<ED[T]['Schema']> | (() => DeduceSorterItem<ED[T]['Schema']> | undefined); sorter: NonNullable<ED[T]['Selection']['sorter']>[number] | (() => NonNullable<ED[T]['Selection']['sorter']>[number] | undefined);
['#name']?: string; ['#name']?: string;
}; };

8
lib/types/Page.d.ts vendored
View File

@ -1,6 +1,6 @@
/// <reference types="wechat-miniprogram" /> /// <reference types="wechat-miniprogram" />
/// <reference types="react" /> /// <reference types="react" />
import { Aspect, EntityDict, DeduceSorterItem, CheckerType, AggregationResult } from "oak-domain/lib/types"; import { Aspect, EntityDict, CheckerType, AggregationResult } 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 { CommonAspectDict } from 'oak-common-aspect'; import { CommonAspectDict } from 'oak-common-aspect';
import { Feature } from './Feature'; import { Feature } from './Feature';
@ -31,11 +31,11 @@ interface ComponentOption<ED extends EntityDict & BaseEntityDict, T extends keyo
'#name'?: string; '#name'?: string;
}>; }>;
sorters?: Array<{ sorters?: Array<{
sorter: DeduceSorterItem<ED[T]['Schema']> | ((options: { sorter: NonNullable<ED[T]['Selection']['sorter']>[number] | ((options: {
features: BasicFeatures<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>> & FD; features: BasicFeatures<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>> & FD;
props: Partial<WechatMiniprogram.Component.PropertyOptionToData<TProperty>>; props: Partial<WechatMiniprogram.Component.PropertyOptionToData<TProperty>>;
state: Record<string, any>; state: Record<string, any>;
}) => DeduceSorterItem<ED[T]['Schema']>); }) => NonNullable<ED[T]['Selection']['sorter']>[number]);
'#name'?: string; '#name'?: string;
}>; }>;
formData?: (options: { formData?: (options: {
@ -175,7 +175,7 @@ export declare type OakListComponentMethods<ED extends EntityDict & BaseEntityDi
removeNamedFilterByName: (name: string, refresh?: boolean) => void; removeNamedFilterByName: (name: string, refresh?: boolean) => void;
setNamedSorters: (sorters: NamedSorterItem<ED, T>[]) => void; setNamedSorters: (sorters: NamedSorterItem<ED, T>[]) => void;
getSorters: () => ED[T]['Selection']['sorter'] | undefined; getSorters: () => ED[T]['Selection']['sorter'] | undefined;
getSorterByName: (name: string) => DeduceSorterItem<ED[T]['Schema']> | undefined; getSorterByName: (name: string) => NonNullable<ED[T]['Selection']['sorter']>[number] | undefined;
addNamedSorter: (filter: NamedSorterItem<ED, T>, refresh?: boolean) => void; addNamedSorter: (filter: NamedSorterItem<ED, T>, refresh?: boolean) => void;
removeNamedSorter: (filter: NamedSorterItem<ED, T>, refresh?: boolean) => void; removeNamedSorter: (filter: NamedSorterItem<ED, T>, refresh?: boolean) => void;
removeNamedSorterByName: (name: string, refresh?: boolean) => void; removeNamedSorterByName: (name: string, refresh?: boolean) => void;

View File

@ -82,7 +82,7 @@ export default class CheckerExecutor<ED extends EntityDict & BaseEntityDict,Cxt
if (filter) { if (filter) {
const filterr = typeof filter === 'function' ? filter(operation, context, {}) : filter; const filterr = typeof filter === 'function' ? filter(operation, context, {}) : filter;
assert(!(filter instanceof Promise), `${entity as string}的动作${action}上定义的checker其filter返回了Promise请注意将同步和异步的返回区分对待`); assert(!(filter instanceof Promise), `${entity as string}的动作${action}上定义的checker其filter返回了Promise请注意将同步和异步的返回区分对待`);
const isRepel = checkFilterRepel(entity, context, filterr, operation.filter, true); const isRepel = checkFilterRepel<ED, T, Cxt>(entity, context, filterr, operation.filter, true);
assert(typeof isRepel === 'boolean'); assert(typeof isRepel === 'boolean');
if (isRepel) { if (isRepel) {
continue; continue;

View File

@ -147,8 +147,8 @@ function initializeWatchers<ED extends EntityDict & BaseEntityDict, Cxt extends
try { try {
if (w.hasOwnProperty('actionData')) { if (w.hasOwnProperty('actionData')) {
const { entity, action, filter, actionData } = <BBWatcher<ED, keyof ED>>w; const { entity, action, filter, actionData } = <BBWatcher<ED, keyof ED>>w;
const filter2 = typeof filter === 'function' ? await filter() : filter; const filter2 = typeof filter === 'function' ? await (filter as Function)() : filter;
const data = typeof actionData === 'function' ? await (actionData as any)() : actionData; // 这里有个奇怪的编译错误,不理解 by Xc const data = typeof actionData === 'function' ? await (actionData as Function)() : actionData; // 这里有个奇怪的编译错误,不理解 by Xc
const result = await store.operate(entity, { const result = await store.operate(entity, {
id: await generateNewIdAsync(), id: await generateNewIdAsync(),
action, action,
@ -162,8 +162,8 @@ function initializeWatchers<ED extends EntityDict & BaseEntityDict, Cxt extends
} }
else { else {
const { entity, projection, fn, filter } = <WBWatcher<ED, keyof ED, Cxt>>w; const { entity, projection, fn, filter } = <WBWatcher<ED, keyof ED, Cxt>>w;
const filter2 = typeof filter === 'function' ? await filter() : filter; const filter2 = typeof filter === 'function' ? await (filter as Function)() : filter;
const projection2 = typeof projection === 'function' ? await projection() : projection; const projection2 = typeof projection === 'function' ? await (projection as Function)() : projection;
const rows = await store.select(entity, { const rows = await store.select(entity, {
data: projection2 as any, data: projection2 as any,
filter: filter2, filter: filter2,

View File

@ -3,7 +3,7 @@ import { cloneDeep, pull, unset, merge, uniq } from "oak-domain/lib/utils/lodash
import { addFilterSegment, combineFilters, contains, repel, same } from "oak-domain/lib/store/filter"; import { addFilterSegment, combineFilters, contains, repel, same } from "oak-domain/lib/store/filter";
import { createOperationsFromModies } from 'oak-domain/lib/store/modi'; import { createOperationsFromModies } from 'oak-domain/lib/store/modi';
import { judgeRelation } from "oak-domain/lib/store/relation"; import { judgeRelation } from "oak-domain/lib/store/relation";
import { EntityDict, StorageSchema, OpRecord, CreateOpResult, RemoveOpResult, DeduceSorterItem, AspectWrapper } from "oak-domain/lib/types"; import { EntityDict, StorageSchema, OpRecord, CreateOpResult, RemoveOpResult, AspectWrapper } 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 { CommonAspectDict } from 'oak-common-aspect'; import { CommonAspectDict } from 'oak-common-aspect';
@ -156,7 +156,7 @@ abstract class Node<
} }
protected getProjection() { protected getProjection() {
return typeof this.projection === 'function' ? this.projection() : cloneDeep(this.projection); return typeof this.projection === 'function' ? (this.projection as Function)() : cloneDeep(this.projection);
} }
protected judgeRelation(attr: string) { protected judgeRelation(attr: string) {
@ -822,7 +822,7 @@ class ListNode<
if (operation.action === 'create') { if (operation.action === 'create') {
const { data } = operation; const { data } = operation;
assert(!(data instanceof Array)); assert(!(data instanceof Array));
createdIds.push(data.id); createdIds.push(data.id!);
} }
} }
@ -989,7 +989,7 @@ class ListNode<
if (!this.dirty) { if (!this.dirty) {
return; return;
} }
const childOperations: Record<string, ED[T]['Update']> = {}; const childOperations: Record<string, ED[T]['Operation']> = {};
for (const id in this.children) { for (const id in this.children) {
const childOperation = this.children[id].composeOperations(); const childOperation = this.children[id].composeOperations();
if (childOperation) { if (childOperation) {
@ -1037,14 +1037,14 @@ class ListNode<
const sorterArr = sorters.map((ele) => { const sorterArr = sorters.map((ele) => {
const { sorter } = ele; const { sorter } = ele;
if (typeof sorter === 'function') { if (typeof sorter === 'function') {
return sorter(); return (sorter as Function)();
} }
return sorter; return sorter;
}).filter((ele) => !!ele) as DeduceSorterItem<ED[T]['Schema']>[]; }).filter((ele) => !!ele) as ED[T]['Selection']['sorter'];
const filterArr = filters.map((ele) => { const filterArr = filters.map((ele) => {
const { filter } = ele; const { filter } = ele;
if (typeof filter === 'function') { if (typeof filter === 'function') {
return filter(); return (filter as Function)();
} }
return filter; return filter;
}); });
@ -1434,7 +1434,7 @@ class SingleNode<ED extends EntityDict & BaseEntityDict,
return; return;
} }
const filter = this.getFilter(); const filter = this.getFilter();
const operation: ED[T]['Update'] = this.operation ? cloneDeep(this.operation.operation) : { const operation: ED[T]['Operation'] = this.operation ? cloneDeep(this.operation.operation) : {
id: generateNewId(), id: generateNewId(),
action: 'update', action: 'update',
data: {}, data: {},

View File

@ -43,7 +43,7 @@ export async function onPathSet<
filter: filter:
typeof filter === 'function' typeof filter === 'function'
? () => ? () =>
filter.call(this, { (filter as Function).call(this, {
features, features,
props: this.props, props: this.props,
state: this.state, state: this.state,
@ -57,7 +57,7 @@ export async function onPathSet<
if (!proj && projection) { if (!proj && projection) {
proj = typeof projection === 'function' proj = typeof projection === 'function'
? () => ? () =>
projection.call(this, { (projection as Function).call(this, {
features, features,
props: this.props, props: this.props,
state: this.state, state: this.state,
@ -76,7 +76,7 @@ export async function onPathSet<
sorter: sorter:
typeof sorter === 'function' typeof sorter === 'function'
? () => ? () =>
sorter.call(this, { (sorter as Function).call(this, {
features, features,
props: this.props, props: this.props,
state: this.state, state: this.state,

View File

@ -1,7 +1,7 @@
/// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" /> /// <reference path="../node_modules/@types/wechat-miniprogram/index.d.ts" />
import assert from 'assert'; import assert from 'assert';
import { CommonAspectDict } from 'oak-common-aspect'; import { CommonAspectDict } from 'oak-common-aspect';
import { Aspect, Context, DeduceSorterItem, EntityDict } from 'oak-domain/lib/types'; import { Aspect, EntityDict } 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';
@ -439,10 +439,8 @@ const oakBehavior = Behavior<
} }
return sorter; return sorter;
}) })
.filter((ele) => !!ele) as DeduceSorterItem< .filter((ele) => !!ele);
EDD[keyof EDD]['Schema'] return sorters as EDD[keyof EDD]['Selection']['sorter'];
>[];
return sorters;
} }
}, },

View File

@ -4,7 +4,7 @@ import React from 'react';
import { withRouter, PullToRefresh } from './platforms/web'; import { withRouter, PullToRefresh } from './platforms/web';
import { get } from 'oak-domain/lib/utils/lodash'; import { get } from 'oak-domain/lib/utils/lodash';
import { CommonAspectDict } from 'oak-common-aspect'; import { CommonAspectDict } from 'oak-common-aspect';
import { Action, Aspect, CheckerType, DeduceSorterItem, EntityDict } from 'oak-domain/lib/types'; import { Action, Aspect, CheckerType, EntityDict } 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';
@ -374,7 +374,7 @@ abstract class OakComponentBase<
); );
const filters = namedFilters.map(({ filter }) => { const filters = namedFilters.map(({ filter }) => {
if (typeof filter === 'function') { if (typeof filter === 'function') {
return filter(); return (filter as Function)();
} }
return filter; return filter;
}); });
@ -390,7 +390,7 @@ abstract class OakComponentBase<
); );
if (filter?.filter) { if (filter?.filter) {
if (typeof filter.filter === 'function') { if (typeof filter.filter === 'function') {
return filter.filter(); return (filter.filter as Function)();
} }
return filter.filter; return filter.filter;
} }
@ -436,11 +436,11 @@ abstract class OakComponentBase<
const sorters = namedSorters const sorters = namedSorters
.map(({ sorter }) => { .map(({ sorter }) => {
if (typeof sorter === 'function') { if (typeof sorter === 'function') {
return sorter(); return (sorter as Function)();
} }
return sorter; return sorter;
}) })
.filter((ele) => !!ele) as DeduceSorterItem<ED[T]['Schema']>[]; .filter((ele) => !!ele) as ED[T]['Selection']['sorter'][];
return sorters; return sorters;
} }
} }
@ -453,7 +453,7 @@ abstract class OakComponentBase<
); );
if (sorter?.sorter) { if (sorter?.sorter) {
if (typeof sorter.sorter === 'function') { if (typeof sorter.sorter === 'function') {
return sorter.sorter(); return (sorter.sorter as Function)();
} }
return sorter.sorter; return sorter.sorter;
} }

View File

@ -1,5 +1,4 @@
import { EntityDict } from "oak-domain/lib/types"; import { EntityDict } from "oak-domain/lib/types";
import { DeduceSorterItem } from "oak-domain/lib/types/Entity";
export type NamedFilterItem<ED extends EntityDict, T extends keyof ED> = { export type NamedFilterItem<ED extends EntityDict, T extends keyof ED> = {
filter: ED[T]['Selection']['filter'] | (() => ED[T]['Selection']['filter'] | undefined); filter: ED[T]['Selection']['filter'] | (() => ED[T]['Selection']['filter'] | undefined);
@ -7,6 +6,6 @@ export type NamedFilterItem<ED extends EntityDict, T extends keyof ED> = {
}; };
export type NamedSorterItem<ED extends EntityDict, T extends keyof ED> = { export type NamedSorterItem<ED extends EntityDict, T extends keyof ED> = {
sorter: DeduceSorterItem<ED[T]['Schema']> | (() => DeduceSorterItem<ED[T]['Schema']> | undefined); sorter: NonNullable<ED[T]['Selection']['sorter']>[number] | (() => NonNullable<ED[T]['Selection']['sorter']>[number] | undefined);
['#name']?: string; ['#name']?: string;
}; };

View File

@ -1,4 +1,4 @@
import { Aspect, EntityDict, DeduceSorterItem, CheckerType, AggregationResult } from "oak-domain/lib/types"; import { Aspect, EntityDict, CheckerType, AggregationResult } 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 { CommonAspectDict } from 'oak-common-aspect'; import { CommonAspectDict } from 'oak-common-aspect';
import { Feature } from './Feature'; import { Feature } from './Feature';
@ -41,11 +41,11 @@ interface ComponentOption<
'#name'?: string; '#name'?: string;
}>; }>;
sorters?: Array<{ sorters?: Array<{
sorter: DeduceSorterItem<ED[T]['Schema']> | ((options: { sorter: NonNullable<ED[T]['Selection']['sorter']>[number] | ((options: {
features: BasicFeatures<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>> & FD; features: BasicFeatures<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>> & FD;
props: Partial<WechatMiniprogram.Component.PropertyOptionToData<TProperty>>; props: Partial<WechatMiniprogram.Component.PropertyOptionToData<TProperty>>;
state: Record<string, any>; state: Record<string, any>;
}) => DeduceSorterItem<ED[T]['Schema']>) }) => NonNullable<ED[T]['Selection']['sorter']>[number])
'#name'?: string; '#name'?: string;
}>; }>;
formData?: (options: { formData?: (options: {
@ -293,7 +293,7 @@ export type OakListComponentMethods<ED extends EntityDict & BaseEntityDict, T ex
getSorters: () => ED[T]['Selection']['sorter'] | undefined; getSorters: () => ED[T]['Selection']['sorter'] | undefined;
getSorterByName: ( getSorterByName: (
name: string name: string
) => DeduceSorterItem<ED[T]['Schema']> | undefined; ) => NonNullable<ED[T]['Selection']['sorter']>[number] | undefined;
addNamedSorter: (filter: NamedSorterItem<ED, T>, refresh?: boolean) => void; addNamedSorter: (filter: NamedSorterItem<ED, T>, refresh?: boolean) => void;
removeNamedSorter: ( removeNamedSorter: (
filter: NamedSorterItem<ED, T>, filter: NamedSorterItem<ED, T>,