重构了frontend级别的上下文

This commit is contained in:
Xu Chang 2023-09-18 19:37:12 +08:00
parent 4b6937cf93
commit fdc8b64070
29 changed files with 240 additions and 8 deletions

View File

@ -1,6 +1,6 @@
import { ActionDef, WebComponentProps } from '../../types/Page';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { ED } from '../../types/AbstractComponent';
import { ED, OakExtraActionProps } from '../../types/AbstractComponent';
import { EntityDict } from 'oak-domain/lib/types/Entity';
import { StorageSchema } from 'oak-domain/lib/types/Storage';
import { CascadeActionProps } from '../../types/AbstractComponent';
@ -19,6 +19,7 @@ export default function Render(props: WebComponentProps<ED, keyof EntityDict, fa
entity: string;
actions: ActionDef<ED, keyof EntityDict>[];
cascadeActions: CascadeActionDef;
extraActions: OakExtraActionProps[];
onAction: (action?: string, cascadeAction?: CascadeActionProps) => void;
}, {
makeItems: () => void;

View File

@ -17,11 +17,11 @@ function ItemComponent(props) {
export default function Render(props) {
const { methods, data } = props;
const { t, makeItems } = methods;
const { schema, actions, onAction, entity, cascadeActions, items, i18n, } = data;
const { schema, actions, onAction, entity, cascadeActions, items, i18n, extraActions, } = data;
const zhCNKeys = i18n?.store?.data?.zh_CN && Object.keys(i18n.store.data.zh_CN).length;
useEffect(() => {
makeItems();
}, [zhCNKeys, actions, cascadeActions]);
}, [zhCNKeys, actions, cascadeActions, extraActions]);
return (_jsx("div", { className: Style.panelContainer, children: _jsx(Space, { align: 'center', style: { width: '100%' }, children: _jsx(Space, { align: 'center', size: 12, children: _jsx(_Fragment, { children: items?.map((ele, index) => {
return (_jsx(ItemComponent, { label: ele.label, type: "a", onClick: ele.onClick }));
}) }) }) }) }));

12
es/context/BackendRuntimeContext.d.ts vendored Normal file
View File

@ -0,0 +1,12 @@
/// <reference types="node" />
import { EntityDict } from 'oak-domain/lib/base-app-domain';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
import { SerializedData } from './FrontendRuntimeContext';
import { IncomingHttpHeaders } from 'http';
export declare abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDict> extends AsyncContext<ED> {
private subscriberId?;
constructor(store: AsyncRowStore<ED, BackendRuntimeContext<ED>>, data?: SerializedData, headers?: IncomingHttpHeaders);
getSubscriberId(): string | undefined;
protected initialized(): Promise<void>;
}

View File

@ -0,0 +1,15 @@
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
export class BackendRuntimeContext extends AsyncContext {
subscriberId;
constructor(store, data, headers) {
super(store, headers);
if (data) {
this.subscriberId = data.sid;
}
}
getSubscriberId() {
return this.subscriberId;
}
async initialized() {
}
}

16
es/context/FrontendRuntimeContext.d.ts vendored Normal file
View File

@ -0,0 +1,16 @@
import { EntityDict } from 'oak-domain/lib/base-app-domain';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
import { SyncContext, SyncRowStore } from 'oak-domain/lib/store/SyncRowStore';
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
import { CommonAspectDict } from 'oak-common-aspect';
import { Aspect } from 'oak-domain/lib/types';
import { SubScriber } from '../features/subscriber';
import { BasicFeatures } from '../features';
export type SerializedData = {
sid?: string;
};
export declare abstract class FrontendRuntimeContext<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>, AD extends CommonAspectDict<ED, Cxt> & Record<string, Aspect<ED, Cxt>>> extends SyncContext<ED> {
subscriber: SubScriber<ED, Cxt, FrontendRuntimeContext<ED, Cxt, AD>, AD>;
constructor(store: SyncRowStore<ED, FrontendRuntimeContext<ED, Cxt, AD>>, features: BasicFeatures<ED, Cxt, FrontendRuntimeContext<ED, Cxt, AD>, AD>);
protected getSerializedData(): SerializedData;
}

View File

@ -0,0 +1,14 @@
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
export class FrontendRuntimeContext extends SyncContext {
subscriber;
constructor(store, features) {
super(store);
this.subscriber = features.subscriber;
}
getSerializedData() {
const sid = this.subscriber.getSubscriberId();
return {
sid,
};
}
}

View File

@ -28,5 +28,6 @@ export declare class SubScriber<ED extends EntityDict & BaseEntityDict, Cxt exte
private connect;
sub(data: SubDataDef<ED, keyof ED>[], callback?: (records: OpRecord<ED>[], ids: string[]) => void): Promise<void>;
unsub(ids: string[]): Promise<void>;
getSubscriberId(): string | undefined;
}
export {};

View File

@ -16,5 +16,6 @@ export declare class SubScriber<ED extends EntityDict & BaseEntityDict, Cxt exte
off(event: SubscribeEvent, callback: () => void): void;
sub(data: SubDataDef<ED, keyof ED>[], callback: (records: OpRecord<ED>[], ids: string[]) => void): Promise<void>;
unsub(ids: string[]): Promise<void>;
getSubscriberId(): undefined;
}
export {};

View File

@ -18,4 +18,7 @@ export class SubScriber extends Feature {
console.log('data subscribe 在dev模式下不起作用');
}
async unsub(ids) { }
getSubscriberId() {
return undefined;
}
}

View File

@ -158,4 +158,9 @@ export class SubScriber extends Feature {
}
}
}
getSubscriberId() {
if (this.socket) {
return this.socket.id;
}
}
}

2
es/index.d.ts vendored
View File

@ -15,3 +15,5 @@ export * from './utils/upload';
export { CacheStore } from './cacheStore/CacheStore';
export { default as SyncTriggerExecutor } from './cacheStore/SyncTriggerExecutor';
export { DebugStore } from './debugStore/DebugStore';
export { FrontendRuntimeContext, SerializedData } from './context/FrontendRuntimeContext';
export { BackendRuntimeContext } from './context/BackendRuntimeContext';

View File

@ -14,3 +14,5 @@ export * from './utils/upload';
export { CacheStore } from './cacheStore/CacheStore';
export { default as SyncTriggerExecutor } from './cacheStore/SyncTriggerExecutor';
export { DebugStore } from './debugStore/DebugStore';
export { FrontendRuntimeContext } from './context/FrontendRuntimeContext';
export { BackendRuntimeContext } from './context/BackendRuntimeContext';

View File

@ -1,6 +1,6 @@
import { ActionDef, WebComponentProps } from '../../types/Page';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
import { ED } from '../../types/AbstractComponent';
import { ED, OakExtraActionProps } from '../../types/AbstractComponent';
import { EntityDict } from 'oak-domain/lib/types/Entity';
import { StorageSchema } from 'oak-domain/lib/types/Storage';
import { CascadeActionProps } from '../../types/AbstractComponent';
@ -19,6 +19,7 @@ export default function Render(props: WebComponentProps<ED, keyof EntityDict, fa
entity: string;
actions: ActionDef<ED, keyof EntityDict>[];
cascadeActions: CascadeActionDef;
extraActions: OakExtraActionProps[];
onAction: (action?: string, cascadeAction?: CascadeActionProps) => void;
}, {
makeItems: () => void;

View File

@ -21,11 +21,11 @@ function Render(props) {
var _a, _b;
var methods = props.methods, data = props.data;
var t = methods.t, makeItems = methods.makeItems;
var schema = data.schema, actions = data.actions, onAction = data.onAction, entity = data.entity, cascadeActions = data.cascadeActions, items = data.items, i18n = data.i18n;
var schema = data.schema, actions = data.actions, onAction = data.onAction, entity = data.entity, cascadeActions = data.cascadeActions, items = data.items, i18n = data.i18n, extraActions = data.extraActions;
var zhCNKeys = ((_b = (_a = i18n === null || i18n === void 0 ? void 0 : i18n.store) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.zh_CN) && Object.keys(i18n.store.data.zh_CN).length;
(0, react_1.useEffect)(function () {
makeItems();
}, [zhCNKeys, actions, cascadeActions]);
}, [zhCNKeys, actions, cascadeActions, extraActions]);
return ((0, jsx_runtime_1.jsx)("div", { className: web_module_less_1.default.panelContainer, children: (0, jsx_runtime_1.jsx)(antd_1.Space, { align: 'center', style: { width: '100%' }, children: (0, jsx_runtime_1.jsx)(antd_1.Space, { align: 'center', size: 12, children: (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: items === null || items === void 0 ? void 0 : items.map(function (ele, index) {
return ((0, jsx_runtime_1.jsx)(ItemComponent, { label: ele.label, type: "a", onClick: ele.onClick }));
}) }) }) }) }));

12
lib/context/BackendRuntimeContext.d.ts vendored Normal file
View File

@ -0,0 +1,12 @@
/// <reference types="node" />
import { EntityDict } from 'oak-domain/lib/base-app-domain';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
import { SerializedData } from './FrontendRuntimeContext';
import { IncomingHttpHeaders } from 'http';
export declare abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDict> extends AsyncContext<ED> {
private subscriberId?;
constructor(store: AsyncRowStore<ED, BackendRuntimeContext<ED>>, data?: SerializedData, headers?: IncomingHttpHeaders);
getSubscriberId(): string | undefined;
protected initialized(): Promise<void>;
}

View File

@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackendRuntimeContext = void 0;
var tslib_1 = require("tslib");
var AsyncRowStore_1 = require("oak-domain/lib/store/AsyncRowStore");
var BackendRuntimeContext = /** @class */ (function (_super) {
tslib_1.__extends(BackendRuntimeContext, _super);
function BackendRuntimeContext(store, data, headers) {
var _this = _super.call(this, store, headers) || this;
if (data) {
_this.subscriberId = data.sid;
}
return _this;
}
BackendRuntimeContext.prototype.getSubscriberId = function () {
return this.subscriberId;
};
BackendRuntimeContext.prototype.initialized = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/];
});
});
};
return BackendRuntimeContext;
}(AsyncRowStore_1.AsyncContext));
exports.BackendRuntimeContext = BackendRuntimeContext;

16
lib/context/FrontendRuntimeContext.d.ts vendored Normal file
View File

@ -0,0 +1,16 @@
import { EntityDict } from 'oak-domain/lib/base-app-domain';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
import { SyncContext, SyncRowStore } from 'oak-domain/lib/store/SyncRowStore';
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
import { CommonAspectDict } from 'oak-common-aspect';
import { Aspect } from 'oak-domain/lib/types';
import { SubScriber } from '../features/subscriber';
import { BasicFeatures } from '../features';
export type SerializedData = {
sid?: string;
};
export declare abstract class FrontendRuntimeContext<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>, AD extends CommonAspectDict<ED, Cxt> & Record<string, Aspect<ED, Cxt>>> extends SyncContext<ED> {
subscriber: SubScriber<ED, Cxt, FrontendRuntimeContext<ED, Cxt, AD>, AD>;
constructor(store: SyncRowStore<ED, FrontendRuntimeContext<ED, Cxt, AD>>, features: BasicFeatures<ED, Cxt, FrontendRuntimeContext<ED, Cxt, AD>, AD>);
protected getSerializedData(): SerializedData;
}

View File

@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FrontendRuntimeContext = void 0;
var tslib_1 = require("tslib");
var SyncRowStore_1 = require("oak-domain/lib/store/SyncRowStore");
var FrontendRuntimeContext = /** @class */ (function (_super) {
tslib_1.__extends(FrontendRuntimeContext, _super);
function FrontendRuntimeContext(store, features) {
var _this = _super.call(this, store) || this;
_this.subscriber = features.subscriber;
return _this;
}
FrontendRuntimeContext.prototype.getSerializedData = function () {
var sid = this.subscriber.getSubscriberId();
return {
sid: sid,
};
};
return FrontendRuntimeContext;
}(SyncRowStore_1.SyncContext));
exports.FrontendRuntimeContext = FrontendRuntimeContext;

View File

@ -28,5 +28,6 @@ export declare class SubScriber<ED extends EntityDict & BaseEntityDict, Cxt exte
private connect;
sub(data: SubDataDef<ED, keyof ED>[], callback?: (records: OpRecord<ED>[], ids: string[]) => void): Promise<void>;
unsub(ids: string[]): Promise<void>;
getSubscriberId(): string | undefined;
}
export {};

View File

@ -16,5 +16,6 @@ export declare class SubScriber<ED extends EntityDict & BaseEntityDict, Cxt exte
off(event: SubscribeEvent, callback: () => void): void;
sub(data: SubDataDef<ED, keyof ED>[], callback: (records: OpRecord<ED>[], ids: string[]) => void): Promise<void>;
unsub(ids: string[]): Promise<void>;
getSubscriberId(): undefined;
}
export {};

View File

@ -33,6 +33,9 @@ var SubScriber = /** @class */ (function (_super) {
return [2 /*return*/];
}); });
};
SubScriber.prototype.getSubscriberId = function () {
return undefined;
};
return SubScriber;
}(Feature_1.Feature));
exports.SubScriber = SubScriber;

View File

@ -210,6 +210,11 @@ var SubScriber = /** @class */ (function (_super) {
});
});
};
SubScriber.prototype.getSubscriberId = function () {
if (this.socket) {
return this.socket.id;
}
};
return SubScriber;
}(Feature_1.Feature));
exports.SubScriber = SubScriber;

2
lib/index.d.ts vendored
View File

@ -15,3 +15,5 @@ export * from './utils/upload';
export { CacheStore } from './cacheStore/CacheStore';
export { default as SyncTriggerExecutor } from './cacheStore/SyncTriggerExecutor';
export { DebugStore } from './debugStore/DebugStore';
export { FrontendRuntimeContext, SerializedData } from './context/FrontendRuntimeContext';
export { BackendRuntimeContext } from './context/BackendRuntimeContext';

View File

@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DebugStore = exports.SyncTriggerExecutor = exports.CacheStore = exports.LocalStorage = exports.Cache = void 0;
exports.BackendRuntimeContext = exports.FrontendRuntimeContext = exports.DebugStore = exports.SyncTriggerExecutor = exports.CacheStore = exports.LocalStorage = exports.Cache = void 0;
var tslib_1 = require("tslib");
var cache_1 = require("./features/cache");
Object.defineProperty(exports, "Cache", { enumerable: true, get: function () { return cache_1.Cache; } });
@ -23,3 +23,7 @@ var SyncTriggerExecutor_1 = require("./cacheStore/SyncTriggerExecutor");
Object.defineProperty(exports, "SyncTriggerExecutor", { enumerable: true, get: function () { return tslib_1.__importDefault(SyncTriggerExecutor_1).default; } });
var DebugStore_1 = require("./debugStore/DebugStore");
Object.defineProperty(exports, "DebugStore", { enumerable: true, get: function () { return DebugStore_1.DebugStore; } });
var FrontendRuntimeContext_1 = require("./context/FrontendRuntimeContext");
Object.defineProperty(exports, "FrontendRuntimeContext", { enumerable: true, get: function () { return FrontendRuntimeContext_1.FrontendRuntimeContext; } });
var BackendRuntimeContext_1 = require("./context/BackendRuntimeContext");
Object.defineProperty(exports, "BackendRuntimeContext", { enumerable: true, get: function () { return BackendRuntimeContext_1.BackendRuntimeContext; } });

View File

@ -0,0 +1,23 @@
import { EntityDict } from 'oak-domain/lib/base-app-domain';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
import { SerializedData } from './FrontendRuntimeContext';
import { IncomingHttpHeaders } from 'http';
export abstract class BackendRuntimeContext<ED extends EntityDict & BaseEntityDict> extends AsyncContext<ED> {
private subscriberId?: string;
constructor(store: AsyncRowStore<ED, BackendRuntimeContext<ED>>, data?: SerializedData, headers?: IncomingHttpHeaders) {
super(store, headers);
if (data) {
this.subscriberId = data.sid;
}
}
getSubscriberId() {
return this.subscriberId;
}
protected async initialized() {
}
}

View File

@ -0,0 +1,32 @@
import { EntityDict } from 'oak-domain/lib/base-app-domain';
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
import { SyncContext, SyncRowStore } from 'oak-domain/lib/store/SyncRowStore';
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
import { CommonAspectDict } from 'oak-common-aspect';
import { Aspect } from 'oak-domain/lib/types';
import { SubScriber } from '../features/subscriber';
import { BasicFeatures } from '../features';
export type SerializedData = {
sid?: string;
};
export abstract class FrontendRuntimeContext<
ED extends EntityDict & BaseEntityDict,
Cxt extends AsyncContext<ED>,
AD extends CommonAspectDict<ED, Cxt> & Record<string, Aspect<ED, Cxt>>
> extends SyncContext<ED> {
subscriber: SubScriber<ED, Cxt, FrontendRuntimeContext<ED, Cxt, AD>, AD>;
constructor(store: SyncRowStore<ED, FrontendRuntimeContext<ED, Cxt, AD>>, features: BasicFeatures<ED, Cxt, FrontendRuntimeContext<ED, Cxt, AD>, AD>) {
super(store);
this.subscriber = features.subscriber;
}
protected getSerializedData(): SerializedData {
const sid = this.subscriber.getSubscriberId();
return {
sid,
};
}
}

View File

@ -48,4 +48,8 @@ export class SubScriber<
}
async unsub(ids: string[]) {}
getSubscriberId() {
return undefined;
}
}

View File

@ -221,4 +221,10 @@ export class SubScriber<
}
}
}
getSubscriberId() {
if (this.socket) {
return this.socket.id;
}
}
}

View File

@ -17,4 +17,6 @@ export * from './utils/upload';
export { CacheStore } from './cacheStore/CacheStore';
export { default as SyncTriggerExecutor } from './cacheStore/SyncTriggerExecutor';
export { DebugStore } from './debugStore/DebugStore';
export { DebugStore } from './debugStore/DebugStore';
export { FrontendRuntimeContext, SerializedData } from './context/FrontendRuntimeContext';
export { BackendRuntimeContext } from './context/BackendRuntimeContext';