add notification service

This commit is contained in:
Géry Debongnie
2019-01-29 14:15:36 +01:00
parent dd75a17448
commit 552d8a0809
3 changed files with 101 additions and 3 deletions
+3 -3
View File
@@ -1,14 +1,14 @@
export interface RPC {
export interface RPCQuery {
model: string;
method: string;
args: any;
}
export interface IAjax {
rpc(rpc: RPC): Promise<any>;
rpc(rpc: RPCQuery): Promise<any>;
}
export class Ajax implements IAjax {
rpc(rpc: RPC): Promise<any> {
rpc(rpc: RPCQuery): Promise<any> {
return Promise.resolve(1);
}
}
@@ -0,0 +1,52 @@
import { EventBus as Bus } from "../core/event_bus";
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
export interface Notification {
id: number;
title: string;
message: string;
type: "notification" | "warning";
sticky: boolean;
}
export type NotificationEvent = "notification_added" | "notification_closed";
export type Callback = (notif: Notification) => void;
export interface INotificationManager {
add(notif: Partial<Notification>): number;
close(id: number): void;
on(event: NotificationEvent, owner: any, callback: Callback): void;
}
//------------------------------------------------------------------------------
// Notification Manager
//------------------------------------------------------------------------------
export class NotificationManager extends Bus implements INotificationManager {
nextID = 0;
notifications: { [key: number]: Notification } = {};
add(notif: Partial<Notification>): number {
const id = this.nextID++;
const defaultVals = {
title: "",
message: "",
type: "notification",
sticky: false
};
const notification = Object.assign(defaultVals, notif, { id });
this.notifications[id] = notification;
this.trigger("notification_added", notification);
return id;
}
close(id: number) {
let notification = this.notifications[id];
if (notification) {
delete this.notifications[id];
this.trigger("notification_closed", notification);
}
}
}