react-native-navigation的迁移库

CommandsObserver.ts 602B

123456789101112131415161718192021
  1. import * as _ from 'lodash';
  2. import { EventSubscription } from '../interfaces/EventSubscription';
  3. export type CommandsListener = (name: string, params: {}) => void;
  4. export class CommandsObserver {
  5. private readonly listeners = {};
  6. public register(listener: CommandsListener): EventSubscription {
  7. const id = _.uniqueId();
  8. _.set(this.listeners, id, listener);
  9. return {
  10. remove: () => _.unset(this.listeners, id)
  11. };
  12. }
  13. public notify(commandName: string, params: {}): void {
  14. _.forEach(this.listeners, (listener: CommandsListener) => listener(commandName, params));
  15. }
  16. }