]> git.r.bdr.sh - rbdr/forum/blame - src/stores/forums.js
Update to SvelteKit
[rbdr/forum] / src / stores / forums.js
CommitLineData
38416066 1import { readable } from 'svelte/store';
890274a7 2
e749c95b
BB
3const 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
e749c95b
BB
17 // No old value == add
18 if (!data.old_val) {
e749c95b
BB
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) {
e749c95b
BB
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 }
890274a7 32 }
e749c95b 33};
890274a7 34
38416066 35export const forums = readable(internals.forums, (set) => {
38416066 36});