feat(core): 支持统一设置node.store的初始值

This commit is contained in:
roymondchen 2025-05-20 11:46:25 +08:00
parent d628ffad26
commit abe27db47a
3 changed files with 12 additions and 2 deletions

View File

@ -59,6 +59,7 @@ export interface AppOptionsConfig {
request?: RequestFunction; request?: RequestFunction;
DataSourceObservedData?: ObservedDataClass; DataSourceObservedData?: ObservedDataClass;
dataSourceManagerInitialData?: DataSourceManagerData; dataSourceManagerInitialData?: DataSourceManagerData;
nodeStoreInitialData?: () => any;
errorHandler?: ErrorHandler; errorHandler?: ErrorHandler;
} }
@ -85,6 +86,7 @@ class App extends EventEmitter {
public transformStyle: (style: Record<string, any>) => Record<string, any>; public transformStyle: (style: Record<string, any>) => Record<string, any>;
public eventHelper?: EventHelper; public eventHelper?: EventHelper;
public errorHandler?: ErrorHandler; public errorHandler?: ErrorHandler;
public nodeStoreInitialData?: () => any;
private flexible?: Flexible; private flexible?: Flexible;
@ -103,6 +105,7 @@ class App extends EventEmitter {
this.codeDsl = options.config?.codeBlocks; this.codeDsl = options.config?.codeBlocks;
options.platform && (this.platform = options.platform); options.platform && (this.platform = options.platform);
options.jsEngine && (this.jsEngine = options.jsEngine); options.jsEngine && (this.jsEngine = options.jsEngine);
options.nodeStoreInitialData && (this.nodeStoreInitialData = options.nodeStoreInitialData);
if (options.pageFragmentContainerType) { if (options.pageFragmentContainerType) {
const pageFragmentContainerType = Array.isArray(options.pageFragmentContainerType) const pageFragmentContainerType = Array.isArray(options.pageFragmentContainerType)
@ -336,6 +339,8 @@ class App extends EventEmitter {
this.dataSourceManager = undefined; this.dataSourceManager = undefined;
this.codeDsl = undefined; this.codeDsl = undefined;
this.components.clear(); this.components.clear();
this.nodeStoreInitialData = undefined;
} }
} }

View File

@ -53,7 +53,7 @@ class Node extends EventEmitter {
public page?: Page; public page?: Page;
public parent?: Node; public parent?: Node;
public app: TMagicApp; public app: TMagicApp;
public store = new Store(); public store;
public eventKeys = new Map<string, symbol>(); public eventKeys = new Map<string, symbol>();
private eventQueue: EventCache[] = []; private eventQueue: EventCache[] = [];
@ -61,6 +61,7 @@ class Node extends EventEmitter {
constructor(options: NodeOptions) { constructor(options: NodeOptions) {
super(); super();
this.store = new Store({ initialData: options.app.nodeStoreInitialData?.() || {} });
this.page = options.page; this.page = options.page;
this.parent = options.parent; this.parent = options.parent;
this.app = options.app; this.app = options.app;

View File

@ -1,5 +1,9 @@
export default class Store { export default class Store {
private data: Record<string, any> = {}; private data: any;
constructor({ initialData = {} }: { initialData?: any } = {}) {
this.data = initialData;
}
public set(key: string, value: any) { public set(key: string, value: any) {
this.data[key] = value; this.data[key] = value;