UNPKG

@intuitionrobotics/push-pub-sub

Version:
52 lines 2.1 kB
import { ThunderDispatcher } from "@intuitionrobotics/thunderstorm/app-frontend/core/thunder-dispatcher"; import { Minute, Module, removeItemFromArray } from "@intuitionrobotics/ts-common"; import {} from "../../index.js"; import { XhrHttpModule } from "@intuitionrobotics/thunderstorm/frontend"; import { HttpMethod } from "@intuitionrobotics/thunderstorm"; export const dispatch_NotificationsUpdated = new ThunderDispatcher('__onNotificationsUpdated'); export class NotificationsModule_Class extends Module { constructor() { super("NotificationsModule"); } notifications = []; getNotifications() { return this.notifications; } setNotificationList = (notifications) => { this.notifications = notifications; dispatch_NotificationsUpdated.dispatchUI(); }; addNotification(newNotification) { this.notifications.push(newNotification); dispatch_NotificationsUpdated.dispatchUI(); } removeNotification(notification) { removeItemFromArray(this.notifications, notification); dispatch_NotificationsUpdated.dispatchUI(); return notification._id; } read = (notification, read) => { const readNotification = this.notifications.find(_notification => _notification._id === notification._id); if (!readNotification || !readNotification.persistent) return; readNotification.read = read; this.readNotification(notification._id, read); }; readNotification = (id, read) => { const body = { _id: id, read }; XhrHttpModule .createRequest(HttpMethod.POST, 'read-notification') .setRelativeUrl("/v1/push/read") .setJsonBody(body) .setOnError('Something went wrong while reading your notification') .setTimeout(Minute) .execute(() => { dispatch_NotificationsUpdated.dispatchUI(); }); }; } export const NotificationsModule = new NotificationsModule_Class(); //# sourceMappingURL=NotificationModule.js.map