]> git.r.bdr.sh - rbdr/forum/blob - src/stores/forums.js
e7acb606cf536db7f227018da308eff532a3136f
[rbdr/forum] / src / stores / forums.js
1 import { readable } from 'svelte/store';
2
3 const internals = {
4
5 // Constants
6
7 kChangeFeedEventName: 'changefeed:forums',
8
9 // The exported data structure
10
11 forums: [],
12
13 // Handles messages from the event
14
15 handleChangeFeed(data) {
16
17 // No old value == add
18 if (!data.old_val) {
19 return internals.forums.push(data.new_val);
20 }
21
22 // We have an old value, let's find it.
23 const index = internals.forums.findIndex((element) => element.id === data.old_val.id);
24
25 if (index > -1) {
26 if (data.new_val) {
27 return internals.forums.splice(index, 1, data.new_val || undefined);
28 }
29
30 return internals.forums.splice(index, 1);
31 }
32 }
33 };
34
35 export const forums = readable(internals.forums, (set) => {
36 });