react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { ComponentProvider } from 'react-native';
  2. import { IWrappedComponent } from './ComponentWrapper';
  3. export class Store {
  4. private componentsByName: Record<string, ComponentProvider> = {};
  5. private propsById: Record<string, any> = {};
  6. private componentsInstancesById: Record<string, IWrappedComponent> = {};
  7. updateProps(componentId: string, props: any) {
  8. this.propsById[componentId] = props;
  9. const component = this.componentsInstancesById[componentId];
  10. if (component) {
  11. this.componentsInstancesById[componentId].setProps(props);
  12. }
  13. }
  14. getPropsForId(componentId: string) {
  15. return this.propsById[componentId] || {};
  16. }
  17. clearComponent(componentId: string) {
  18. delete this.propsById[componentId];
  19. delete this.componentsInstancesById[componentId];
  20. }
  21. setComponentClassForName(componentName: string | number, ComponentClass: ComponentProvider) {
  22. this.componentsByName[componentName.toString()] = ComponentClass;
  23. }
  24. getComponentClassForName(componentName: string | number): ComponentProvider | undefined {
  25. return this.componentsByName[componentName.toString()];
  26. }
  27. setComponentInstance(id: string, component: IWrappedComponent): void {
  28. this.componentsInstancesById[id] = component;
  29. }
  30. getComponentInstance(id: string): IWrappedComponent {
  31. return this.componentsInstancesById[id];
  32. }
  33. }