]>
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 | console.log(data); | |
19 | ||
20 | // No old value == add | |
21 | if (!data.old_val) { | |
22 | console.log('Adding new data'); | |
23 | return internals.forums.push(data.new_val); | |
24 | } | |
25 | ||
26 | // We have an old value, let's find it. | |
27 | const index = internals.forums.findIndex((element) => element.id === data.old_val.id); | |
28 | ||
29 | if (index > -1) { | |
30 | console.log(`Found old data at index ${index}`); | |
31 | ||
32 | if (data.new_val) { | |
33 | return internals.forums.splice(index, 1, data.new_val || undefined); | |
34 | } | |
35 | ||
36 | return internals.forums.splice(index, 1); | |
37 | } | |
38 | } | |
39 | }; | |
40 | ||
41 | export const forums = readable(internals.forums, (set) => { | |
42 | ||
43 | onMessage((message) => { | |
44 | ||
45 | if (message.name === internals.kChangeFeedEventName) { | |
46 | internals.handleChangeFeed(message.data); | |
47 | set(internals.forums); | |
48 | } | |
49 | }); | |
50 | }); |