mirror of
https://github.com/motajs/template.git
synced 2026-11-23 22:14:30 +08:00
feat: Adding should replay flag
This commit is contained in:
1 parent
7743795948
commit
d74cc35c8e
16 files changed
+86
-186
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { shouldReplay } from '@user/data-common';
|
||||
import { IFlagCommonField, IFlagSystem } from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
|
||||
@@ -16,10 +17,12 @@ export class FlagCommonField<T> implements IFlagCommonField<T> {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@shouldReplay('Flag field value set should be replayed.')
|
||||
set(value: T): void {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@shouldReplay('Flag field value set should be replayed.')
|
||||
add(value: number): void {
|
||||
if (typeof this.value !== 'number') {
|
||||
logger.warn(111, String(this.key));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { IFlagCommonField, IFlagSystem, IFlagSystemSave } from './types';
|
||||
import { FlagCommonField } from './field';
|
||||
import { shouldReplay } from '@user/data-common';
|
||||
|
||||
export class FlagSystem implements IFlagSystem {
|
||||
private readonly fieldMap: Map<PropertyKey, IFlagCommonField<any>> =
|
||||
@@ -9,6 +10,7 @@ export class FlagSystem implements IFlagSystem {
|
||||
return this.fieldMap.has(field);
|
||||
}
|
||||
|
||||
@shouldReplay('Flag field set should be replayed.')
|
||||
setField<T>(field: PropertyKey, value: T): IFlagCommonField<T> {
|
||||
return this.getOrInsert(field, value);
|
||||
}
|
||||
@@ -34,6 +36,7 @@ export class FlagSystem implements IFlagSystem {
|
||||
);
|
||||
}
|
||||
|
||||
@shouldReplay('Flag field deleting should be replayed.')
|
||||
deleteField(field: PropertyKey): void {
|
||||
this.fieldMap.delete(field);
|
||||
}
|
||||
@@ -63,26 +66,11 @@ export class FlagSystem implements IFlagSystem {
|
||||
}
|
||||
|
||||
loadState(state: IFlagSystemSave): void {
|
||||
// 按 key 复用现有字段原地写值,使外部持有的引用跨读档仍然有效
|
||||
this.fieldMap.clear();
|
||||
for (const [key, data] of state.fields) {
|
||||
const existing = this.fieldMap.get(key);
|
||||
if (existing) {
|
||||
existing.fromStructured(data);
|
||||
} else {
|
||||
const field = new FlagCommonField<unknown>(
|
||||
this,
|
||||
key,
|
||||
undefined
|
||||
);
|
||||
field.fromStructured(data);
|
||||
this.fieldMap.set(key, field);
|
||||
}
|
||||
}
|
||||
// 以存档为准:存档中不存在的字段一律删除
|
||||
for (const key of this.fieldMap.keys()) {
|
||||
if (!state.fields.has(key)) {
|
||||
this.fieldMap.delete(key);
|
||||
}
|
||||
const field = new FlagCommonField(this, key, void 0);
|
||||
field.fromStructured(data);
|
||||
this.fieldMap.set(key, field);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user