Skip to content

Commit

Permalink
chore: Bumped minimum Dart SDK and remove observers getter
Browse files Browse the repository at this point in the history
Also updated docs and did some tidying up
  • Loading branch information
pongloongyeat committed Jan 23, 2024
1 parent 1202c41 commit 3a3280e
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ build/
# Directory created by dartdoc
doc/api/

# fvm
.fvm/

# VSCode
.vscode/
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!--
## Version
### Breaking
### Features
### Fixes
Expand All @@ -8,12 +9,20 @@
### Others
-->

## 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
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
3 changes: 3 additions & 0 deletions example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
2 changes: 2 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A sample command-line application with an entrypoint in `bin/`, library code
in `lib/`, and example unit test in `test/`.
30 changes: 30 additions & 0 deletions example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
15 changes: 15 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion lib/notification_dispatcher.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library notification_dispatcher;

export 'src/notification_dispatcher.dart';
export 'src/notification_dispatcher.dart'
hide NotificationDispatcherTestExtension;
export 'src/typedefs.dart';
74 changes: 32 additions & 42 deletions lib/src/notification_dispatcher.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
/// 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<String, dynamic>? 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].
static final instance = NotificationDispatcher._();

final _observers = <Object, Map<String, NotificationCallback>>{};

/// 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<Object, Map<String, NotificationCallback>> 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,
Expand All @@ -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<String, dynamic>? 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<Object, Map<String, NotificationCallback>> get observers => _observers;
void clearAll() => observers.clear();
}
15 changes: 15 additions & 0 deletions lib/src/notification_message.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic>? info;
}
4 changes: 4 additions & 0 deletions lib/src/typedefs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'notification_dispatcher.dart';

/// Callback signature when receiving a notification.
typedef NotificationCallback = void Function(NotificationMessage message);
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/notification_dispatcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ class TestHelper {
final String name;
}

extension on NotificationDispatcher {
void clearAll() => observers.clear();
}

void main() {
group('NotificationDispatcher', () {
final instance = TestHelper('1');
Expand Down

0 comments on commit 3a3280e

Please sign in to comment.