mirror of
https://github.com/maxpozdeev/mytinytodo.git
synced 2026-03-11 08:55:27 +00:00
add internal notifications for taskCreated and listCreated (experimental)
This commit is contained in:
parent
2980fcb949
commit
f31c173db4
4 changed files with 98 additions and 4 deletions
|
|
@ -62,7 +62,9 @@ class ListsController extends ApiController {
|
|||
$id = $db->lastInsertId();
|
||||
$t['total'] = 1;
|
||||
$r = $db->sqa("SELECT * FROM {$db->prefix}lists WHERE id=$id");
|
||||
$t['list'][] = $this->prepareList($r, true);
|
||||
$oo = $this->prepareList($r, true);
|
||||
MTTNotificationCenter::postNotification(MTTNotification::didCreateList, $oo);
|
||||
$t['list'][] = $oo;
|
||||
return $t;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -250,7 +250,9 @@ class TasksController extends ApiController {
|
|||
}
|
||||
$db->ex("COMMIT");
|
||||
$r = $db->sqa("SELECT * FROM {$db->prefix}todolist WHERE id=$id");
|
||||
$t['list'][] = $this->prepareTaskRow($r);
|
||||
$oo = $this->prepareTaskRow($r);
|
||||
MTTNotificationCenter::postNotification(MTTNotification::didCreateTask, $oo);
|
||||
$t['list'][] = $oo;
|
||||
$t['total'] = 1;
|
||||
return $t;
|
||||
}
|
||||
|
|
@ -288,7 +290,9 @@ class TasksController extends ApiController {
|
|||
}
|
||||
$db->ex("COMMIT");
|
||||
$r = $db->sqa("SELECT * FROM {$db->prefix}todolist WHERE id=$id");
|
||||
$t['list'][] = $this->prepareTaskRow($r);
|
||||
$oo = $this->prepareTaskRow($r);
|
||||
MTTNotificationCenter::postNotification(MTTNotification::didCreateTask, $oo);
|
||||
$t['list'][] = $oo;
|
||||
$t['total'] = 1;
|
||||
return $t;
|
||||
}
|
||||
|
|
|
|||
86
src/includes/notifications.php
Normal file
86
src/includes/notifications.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
class MTTNotificationCenter
|
||||
{
|
||||
/**
|
||||
* @var array<string, MTTNotificationObserverInterface[]>
|
||||
*/
|
||||
private static $observers = [];
|
||||
|
||||
/**
|
||||
* @var array<array>
|
||||
*/
|
||||
private static $notificationStore = [];
|
||||
|
||||
/**
|
||||
* @param string $notification
|
||||
* @param MTTNotificationObserverInterface $observer
|
||||
* @return void
|
||||
*/
|
||||
public static function addObserverForNotification(string $notification, MTTNotificationObserverInterface $observer)
|
||||
{
|
||||
if (!isset(self::$observers[$notification])) {
|
||||
self::$observers[$notification] = [];
|
||||
}
|
||||
self::$observers[$notification][] = $observer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $notifications
|
||||
* @param MTTNotificationObserverInterface $observer
|
||||
* @return void
|
||||
*/
|
||||
public static function addObserverForNotifications(array $notifications, MTTNotificationObserverInterface $observer)
|
||||
{
|
||||
foreach ($notifications as $notification) {
|
||||
self::addObserverForNotification($notification, $observer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static function postNotification(string $notification, $object)
|
||||
{
|
||||
self::$notificationStore[] = [
|
||||
'notification' => $notification,
|
||||
'object' => $object
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this near exit()
|
||||
* @return void
|
||||
*/
|
||||
public static function notifyDelayedObservers()
|
||||
{
|
||||
if (count(self::$notificationStore) == 0) {
|
||||
return; // No notifications
|
||||
}
|
||||
if (session_status() == PHP_SESSION_ACTIVE) {
|
||||
session_write_close(); // Close active session
|
||||
}
|
||||
if (function_exists('fastcgi_finish_request')) {
|
||||
fastcgi_finish_request();
|
||||
}
|
||||
foreach (self::$notificationStore as $notificationItem) {
|
||||
$notification = $notificationItem['notification'];
|
||||
$object = $notificationItem['object'];
|
||||
$observers = self::$observers[$notification] ?? [];
|
||||
foreach ($observers as $observer) {
|
||||
$observer->notification($notification, $object);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface MTTNotificationObserverInterface
|
||||
{
|
||||
function notification(string $notification, $object);
|
||||
}
|
||||
|
||||
// Enum
|
||||
abstract class MTTNotification
|
||||
{
|
||||
const didCreateTask = 'didCreateTask';
|
||||
const didCreateList = 'didCreateList';
|
||||
}
|
||||
|
||||
|
|
@ -25,10 +25,11 @@ else {
|
|||
}
|
||||
|
||||
require_once(MTTINC. 'common.php');
|
||||
require_once(MTTINC. 'version.php');
|
||||
require_once(MTTINC. 'class.dbconnection.php');
|
||||
require_once(MTTINC. 'class.dbcore.php');
|
||||
require_once(MTTINC. 'class.config.php');
|
||||
require_once(MTTINC. 'version.php');
|
||||
require_once(MTTINC. 'notifications.php');
|
||||
|
||||
requireConfig();
|
||||
configureDbConnection();
|
||||
|
|
@ -322,6 +323,7 @@ function jsonExit($data)
|
|||
header('Pragma: no-cache'); // for old HTTP/1.0 intermediate caches
|
||||
header_remove('Expires');
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
MTTNotificationCenter::notifyDelayedObservers();
|
||||
exit;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue