summaryrefslogtreecommitdiff
path: root/ui/lib/state/remote/channels.svelte.js
blob: b6da31b8b7552edc2b18a85f35e848b5f5d45651 (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
import { DateTime } from 'luxon';
import { SvelteMap } from 'svelte/reactivity';

class Channel {
  static boot({ at, id, name }) {
    return new Channel({
      at: DateTime.fromISO(at),
      id,
      name
    });
  }

  constructor({ at, id, name }) {
    this.at = at;
    this.id = id;
    this.name = name;
  }
}

export class Channels {
  all = $state();

  static boot(channels) {
    const all = new SvelteMap(channels.map((channel) => [channel.id, Channel.boot(channel)]));
    return new Channels({ all });
  }

  constructor({ all }) {
    this.all = all;
  }

  add({ at, id, name }) {
    this.all.set(id, Channel.boot({ at, id, name }));
  }

  remove(id) {
    this.all.delete(id);
  }
}