SaveUtil.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Autowired } from "../../Bean/Bean"
  2. import { GlobalSaveData } from "../annotation/DataAnnotation"
  3. export class SaveUtil {
  4. protected map: Map<string, any> = new Map
  5. @Autowired(GlobalSaveData)
  6. protected globalSaveData: GlobalSaveData
  7. init(data: any) {
  8. data = data || {}
  9. let map = this.globalSaveData.getClassMap()
  10. this.getCompentKey().forEach((key) => {
  11. let component = this.globalSaveData.unserialize(map.get(key), data[key])
  12. this.map.set(key, component)
  13. })
  14. }
  15. protected getCompentByName(name: string) {
  16. return this.map.get(name)
  17. }
  18. protected getCompentByClass<T>(constructor: new (...args: any[]) => T) {
  19. let name = this.globalSaveData.getNameByConstructor(constructor)
  20. return this.map.get(name)
  21. }
  22. protected getCompentKey(): string[] {
  23. return Array.from(this.globalSaveData.getClassMap().keys())
  24. }
  25. serialize() {
  26. let res = {}
  27. this.getCompentKey().forEach((key) => {
  28. let component = this.globalSaveData.serialize(this.map.get(key))
  29. res[key] = component
  30. })
  31. return res
  32. }
  33. }
  34. enum saveCompoent {
  35. }