handler.dart 826 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import 'package:fis_jsonrpc/services/notification.m.dart';
  2. import 'package:flutter/foundation.dart';
  3. import 'core/interface/handler.dart';
  4. abstract class NotificationHandler<T>
  5. implements IHandler<T>, IDistributable<T> {
  6. final Set<ValueChanged<T>> _distributeCbList = {};
  7. /// 通知类型枚举
  8. final NotificationTypeEnum type;
  9. NotificationHandler(this.type);
  10. @override
  11. int get typeInt => type.index;
  12. @override
  13. @protected
  14. @mustCallSuper
  15. void execute(T message) {
  16. if (_distributeCbList.isNotEmpty) {
  17. for (final cb in _distributeCbList) {
  18. cb.call(message);
  19. }
  20. }
  21. }
  22. @override
  23. void subscribe(ValueChanged<T> callback) {
  24. _distributeCbList.add(callback);
  25. }
  26. @override
  27. void unsubscribe(ValueChanged<T> callback) {
  28. _distributeCbList.remove(callback);
  29. }
  30. }