From 3a3280e23f07772ee55a249c819522e6edbf00e6 Mon Sep 17 00:00:00 2001 From: Pong Loong Yeat Date: Tue, 23 Jan 2024 22:27:08 +0800 Subject: [PATCH] chore: Bumped minimum Dart SDK and remove observers getter Also updated docs and did some tidying up --- .gitignore | 3 + CHANGELOG.md | 11 ++- LICENSE | 2 +- example/.gitignore | 3 + example/CHANGELOG.md | 3 + example/README.md | 2 + example/analysis_options.yaml | 30 ++++++++ .../example.dart} | 0 example/pubspec.yaml | 15 ++++ lib/notification_dispatcher.dart | 4 +- lib/src/notification_dispatcher.dart | 74 ++++++++----------- lib/src/notification_message.dart | 15 ++++ lib/src/typedefs.dart | 4 + pubspec.yaml | 4 +- test/notification_dispatcher_test.dart | 4 - 15 files changed, 123 insertions(+), 51 deletions(-) create mode 100644 example/.gitignore create mode 100644 example/CHANGELOG.md create mode 100644 example/README.md create mode 100644 example/analysis_options.yaml rename example/{notification_dispatcher_example.dart => bin/example.dart} (100%) create mode 100644 example/pubspec.yaml create mode 100644 lib/src/notification_message.dart create mode 100644 lib/src/typedefs.dart diff --git a/.gitignore b/.gitignore index 270cccf..fbc4477 100644 --- a/.gitignore +++ b/.gitignore @@ -12,5 +12,8 @@ build/ # Directory created by dartdoc doc/api/ +# fvm +.fvm/ + # VSCode .vscode/ diff --git a/CHANGELOG.md b/CHANGELOG.md index bc07da1..a526ded 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ +## 0.5.0 + +### Breaking +- Removed `NotificationDispatcher.observers`. + +### Documentation +- Tidied up documentation. + ## 0.4.0 ### Breaking - Removed support for [Equatable](https://pub.dev/packages/equatable). - Removed support for `Future`-based callbacks. -- Removed `MockNotificationDispatcher` +- Removed `MockNotificationDispatcher`. - `remove` has been renamed to `removeObserverWith` with the `Object observer` parameter made positional. ### Documentation diff --git a/LICENSE b/LICENSE index 0de14db..a19c6cb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Pong Loong Yeat +Copyright (c) 2024 Pong Loong Yeat Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..3a85790 --- /dev/null +++ b/example/.gitignore @@ -0,0 +1,3 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ diff --git a/example/CHANGELOG.md b/example/CHANGELOG.md new file mode 100644 index 0000000..effe43c --- /dev/null +++ b/example/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..3816eca --- /dev/null +++ b/example/README.md @@ -0,0 +1,2 @@ +A sample command-line application with an entrypoint in `bin/`, library code +in `lib/`, and example unit test in `test/`. diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/example/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/example/notification_dispatcher_example.dart b/example/bin/example.dart similarity index 100% rename from example/notification_dispatcher_example.dart rename to example/bin/example.dart diff --git a/example/pubspec.yaml b/example/pubspec.yaml new file mode 100644 index 0000000..985a28d --- /dev/null +++ b/example/pubspec.yaml @@ -0,0 +1,15 @@ +name: example +description: A sample command-line application. +version: 1.0.0 +publish_to: none + +environment: + sdk: ^3.0.0 + +dependencies: + notification_dispatcher: + path: ../ + +dev_dependencies: + lints: ^2.1.0 + test: ^1.24.0 diff --git a/lib/notification_dispatcher.dart b/lib/notification_dispatcher.dart index d968cbb..add44e5 100644 --- a/lib/notification_dispatcher.dart +++ b/lib/notification_dispatcher.dart @@ -1,3 +1,5 @@ library notification_dispatcher; -export 'src/notification_dispatcher.dart'; +export 'src/notification_dispatcher.dart' + hide NotificationDispatcherTestExtension; +export 'src/typedefs.dart'; diff --git a/lib/src/notification_dispatcher.dart b/lib/src/notification_dispatcher.dart index 07bed20..fe11fde 100644 --- a/lib/src/notification_dispatcher.dart +++ b/lib/src/notification_dispatcher.dart @@ -1,24 +1,10 @@ -/// Callback signature when receiving a notification. -typedef NotificationCallback = void Function(NotificationMessage); +import 'typedefs.dart'; -/// The class used to store a notification's payload. -class NotificationMessage { - const NotificationMessage({ - required this.sender, - required this.info, - }); +part 'notification_message.dart'; - /// The sender posting the notification - final Object? sender; - - /// The info being posted with the notification. - final Map? info; -} - -/// The [NotificationDispatcher] class. Passes information around -/// to registered observers. The class comes with a default instance -/// named [NotificationDispatcher.instance]. -class NotificationDispatcher { +/// Passes notifications around to registered observers. The class comes with a +/// default instance named [NotificationDispatcher.instance]. +final class NotificationDispatcher { NotificationDispatcher._(); /// The current instance of [NotificationDispatcher]. @@ -26,17 +12,12 @@ class NotificationDispatcher { final _observers = >{}; - /// The observer map. When filled, it looks like this + /// {@template NotificationDispatcher.addObserver} + /// Adds an observer to the current [NotificationDispatcher] instance with + /// its callback. /// - /// { - /// InstanceOfYourObject: { - /// "notificationName": () {}, - /// }, - /// ... - /// } - Map> get observers => _observers; - - /// Adds an observer with its registered callback. + /// When [post] is called, all observers matching [name] will be called. + /// {@endtemplate} void addObserver( Object observer, { required String name, @@ -50,33 +31,42 @@ class NotificationDispatcher { _observers[observer] = {name: callback}; } - /// Removes all callbacks associated to [observer]. + /// {@template NotificationDispatcher.removeObserver} + /// Removes the [observer] from the current [NotificationDispatcher] instance. + /// {@endtemplate} void removeObserver(Object observer) { - _observers.removeWhere((key, _) => identical(key, observer)); + _observers.removeWhere((key, _) => key == observer); } - /// Removes all callbacks associated to [observer] with notification name - /// [name]. - void removeObserverWith(Object observer, {required String name}) { + /// {@template NotificationDispatcher.removeObserverWith} + /// Removes all callbacks associated to [observer] with [name] from the + /// current [NotificationDispatcher] instance. + /// {@endtemplate} + void removeObserverWith( + Object observer, { + required String name, + }) { _observers[observer]?.removeWhere((key, _) => key == name); } - /// Posts a notification, running all callbacks registered - /// with [name] while passing an optional [sender] and [info]. + /// {@template NotificationDispatcher.post} + /// Posts a notification to the current [NotificationDispatcher] instance, + /// running all callbacks registered with [name]. + /// {@endtemplate} void post({ Object? sender, required String name, Map? info, }) { for (final callback in _observers.values.toList()) { - callback[name]?.call( - NotificationMessage( - sender: sender, - info: info, - ), - ); + callback[name]?.call(NotificationMessage._(sender: sender, info: info)); } return; } } + +extension NotificationDispatcherTestExtension on NotificationDispatcher { + Map> get observers => _observers; + void clearAll() => observers.clear(); +} diff --git a/lib/src/notification_message.dart b/lib/src/notification_message.dart new file mode 100644 index 0000000..05ef2e2 --- /dev/null +++ b/lib/src/notification_message.dart @@ -0,0 +1,15 @@ +part of 'notification_dispatcher.dart'; + +/// The class used to store a notification's payload. +final class NotificationMessage { + const NotificationMessage._({ + required this.sender, + required this.info, + }); + + /// The sender posting the notification + final Object? sender; + + /// The info being posted with the notification. + final Map? info; +} diff --git a/lib/src/typedefs.dart b/lib/src/typedefs.dart new file mode 100644 index 0000000..b89fbf1 --- /dev/null +++ b/lib/src/typedefs.dart @@ -0,0 +1,4 @@ +import 'notification_dispatcher.dart'; + +/// Callback signature when receiving a notification. +typedef NotificationCallback = void Function(NotificationMessage message); diff --git a/pubspec.yaml b/pubspec.yaml index e64b7a4..36997a7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: notification_dispatcher description: Inspired by Apple's NotificationCenter. Passes information around to registered observers. -version: 0.4.0 +version: 0.5.0 homepage: https://github.com/pongloongyeat/notification_dispatcher environment: - sdk: '>=2.12.0 <3.0.0' + sdk: ^3.0.0 dev_dependencies: lints: ^1.0.1 diff --git a/test/notification_dispatcher_test.dart b/test/notification_dispatcher_test.dart index c87a407..bce295b 100644 --- a/test/notification_dispatcher_test.dart +++ b/test/notification_dispatcher_test.dart @@ -7,10 +7,6 @@ class TestHelper { final String name; } -extension on NotificationDispatcher { - void clearAll() => observers.clear(); -} - void main() { group('NotificationDispatcher', () { final instance = TestHelper('1');