mirror of
https://github.com/maxpozdeev/mytinytodo.git
synced 2026-03-11 08:55:27 +00:00
add simple callbacks to MTTNotificationCenter
This commit is contained in:
parent
02ac0655b0
commit
8d8f542908
1 changed files with 30 additions and 2 deletions
|
|
@ -9,7 +9,7 @@
|
||||||
class MTTNotificationCenter
|
class MTTNotificationCenter
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array<string, MTTNotificationObserverInterface[]>
|
* @var array<string, MTTNotificationObserverInterface[]|callable[]>
|
||||||
*/
|
*/
|
||||||
private static $observers = [];
|
private static $observers = [];
|
||||||
|
|
||||||
|
|
@ -41,6 +41,20 @@ class MTTNotificationCenter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $notification
|
||||||
|
* @param callable $callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function addCallbackForNotification(string $notification, callable $callback)
|
||||||
|
{
|
||||||
|
if (!isset(self::$observers[$notification])) {
|
||||||
|
self::$observers[$notification] = [];
|
||||||
|
}
|
||||||
|
self::$observers[$notification][] = $callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function postNotification(string $notification, $object)
|
public static function postNotification(string $notification, $object)
|
||||||
{
|
{
|
||||||
|
|
@ -48,7 +62,12 @@ class MTTNotificationCenter
|
||||||
return; // No observers for this notification
|
return; // No observers for this notification
|
||||||
}
|
}
|
||||||
foreach (self::$observers[$notification] as $observer) {
|
foreach (self::$observers[$notification] as $observer) {
|
||||||
$observer->notification($notification, $object);
|
if ($observer instanceof MTTNotificationObserverInterface) {
|
||||||
|
$observer->notification($notification, $object);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$observer($object);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,3 +103,12 @@ abstract class MTTNotification
|
||||||
const didCreateList = 'didCreateList';
|
const didCreateList = 'didCreateList';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function add_action(string $notification, callable $callback)
|
||||||
|
{
|
||||||
|
MTTNotificationCenter::addCallbackForNotification($notification, $callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
function do_action(string $notification, $object = null)
|
||||||
|
{
|
||||||
|
MTTNotificationCenter::postNotification($notification, $object);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue