Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NoticeManager.ts « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f49b65350153bfb99df4882177427e3372b4b575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Storage from './Storage';
import SortedPersistentMap from './util/SortedPersistentMap';
import Roster from './ui/Roster';
import { INoticeData, Notice } from './Notice';
import Client from './Client';

(<any>window).removeNotice = function (notice) {
   Client.getNoticeManager().removeNotice(notice);
};

(<any>window).addNotice = function (title, description, fnName) {
   return Client.getNoticeManager().addNotice({
      title,
      description,
      fnName,
   });
};

export class NoticeManager {
   private notices: SortedPersistentMap;

   public static removeAll() {
      Client.getNoticeManager().removeAll();

      Client.getAccountManager()
         .getAccounts()
         .forEach(account => {
            account.getNoticeManager().removeAll();
         });
   }

   constructor(private storage: Storage) {
      this.notices = new SortedPersistentMap(this.storage, 'notices');

      this.notices.setRemoveHook(id => {
         Roster.get().removeNotice(this, id);
      });

      this.notices.setPushHook(id => {
         let notice = new Notice(this.storage, id);

         Roster.get().addNotice(this, notice);

         return notice;
      });

      this.notices.init();
   }

   public getId(): string {
      return this.storage.getName();
   }

   public addNotice(noticeData: INoticeData) {
      let notice = new Notice(this.storage, noticeData);

      if (this.notices.get(notice.getId())) {
         return;
      }

      this.notices.push(notice);

      return notice;
   }

   public removeNotice(notice: Notice) {
      this.notices.remove(notice);
   }

   public removeAll() {
      this.notices.empty(id => {
         Roster.get().removeNotice(this, id);
      });
   }
}