]>
Commit | Line | Data |
---|---|---|
1 | import { readable } from 'svelte/store'; | |
2 | import { onMessage } from '../socket_coordinator'; | |
3 | ||
4 | const internals = { | |
5 | ||
6 | // Constants | |
7 | ||
8 | kChangeFeedEventName: 'changefeed:forums', | |
9 | ||
10 | // The exported data structure | |
11 | ||
12 | forums: [], | |
13 | ||
14 | // Handles messages from the event | |
15 | ||
16 | handleChangeFeed(data) { | |
17 | ||
18 | // No old value == add | |
19 | if (!data.old_val) { | |
20 | return internals.forums.push(data.new_val); | |
21 | } | |
22 | ||
23 | // We have an old value, let's find it. | |
24 | const index = internals.forums.findIndex((element) => element.id === data.old_val.id); | |
25 | ||
26 | if (index > -1) { | |
27 | if (data.new_val) { | |
28 | return internals.forums.splice(index, 1, data.new_val || undefined); | |
29 | } | |
30 | ||
31 | return internals.forums.splice(index, 1); | |
32 | } | |
33 | } | |
34 | }; | |
35 | ||
36 | export const forums = readable(internals.forums, (set) => { | |
37 | ||
38 | onMessage((message) => { | |
39 | ||
40 | if (message.name === internals.kChangeFeedEventName) { | |
41 | internals.handleChangeFeed(message.data); | |
42 | set(internals.forums); | |
43 | } | |
44 | }); | |
45 | }); |