]> git.r.bdr.sh - rbdr/forum/blobdiff - app/stores/forums.js
Update dependencies
[rbdr/forum] / app / stores / forums.js
index adc8758d1fc9e20a1efda07f1058b1d4bc09c880..5c7c6e69368eac18f93ebf6a080c9a95e4f6f18d 100644 (file)
@@ -1,85 +1,50 @@
-import { writable } from 'svelte/store';
-
-const internals = {};
-
-internals.forums = [
-  {
-    id: 'life',
-    glyph: '☆',
-    label: 'Life'
-  },
-  {
-    id: 'the-world',
-    glyph: '◯',
-    label: 'The World'
-  },
-  {
-    id: 'online',
-    glyph: '⏀',
-    label: 'Online'
-  },
-  {
-    id: 'experience',
-    glyph: '♢',
-    label: 'Experience'
-  },
-  {
-    id: 'belief',
-    glyph: '⏃',
-    label: 'Belief'
-  },
-  {
-    id: 'movement',
-    glyph: '▷',
-    label: 'Movement'
-  },
-  {
-    id: 'emotion',
-    glyph: '☽',
-    label: 'Emotion'
-  },
-  {
-    id: 'interaction',
-    glyph: '〒',
-    label: 'Interaction'
-  },
-  {
-    id: 'structure',
-    glyph: '▢',
-    label: 'Structure'
-  },
-  {
-    id: 'sound',
-    glyph: '〰',
-    label: 'Sound'
-  },
-  {
-    id: 'words',
-    glyph: '╳',
-    label: 'Words'
-  },
-  {
-    id: 'us',
-    glyph: '╱',
-    label: 'Us'
-  },
-  {
-    id: 'everything',
-    glyph: '♡',
-    label: 'Everything'
-  }
-];
+import { readable } from 'svelte/store';
+import { onMessage } from '../socket_coordinator';
+
+const internals = {
+
+  // Constants
+
+  kChangeFeedEventName: 'changefeed:forums',
+
+  // The exported data structure
 
-export const forums = writable(internals.forums);
+  forums: [],
 
-export function addForum() {
-  const id = Math.random();
+  // Handles messages from the event
 
-  forums.update((forums) => ([...forums,
-    {
-      id,
-      glyph: 'の',
-      label: `Woah ${id}`
+  handleChangeFeed(data) {
+
+    console.log(data);
+
+    // No old value == add
+    if (!data.old_val) {
+      console.log('Adding new data');
+      return internals.forums.push(data.new_val);
     }
-  ]));
+
+    // We have an old value, let's find it.
+    const index = internals.forums.findIndex((element) => element.id === data.old_val.id);
+
+    if (index > -1) {
+      console.log(`Found old data at index ${index}`);
+
+      if (data.new_val) {
+        return internals.forums.splice(index, 1, data.new_val || undefined);
+      }
+
+      return internals.forums.splice(index, 1);
+    }
+  }
 };
+
+export const forums = readable(internals.forums, (set) => {
+
+  onMessage((message) => {
+
+    if (message.name === internals.kChangeFeedEventName) {
+      internals.handleChangeFeed(message.data);
+      set(internals.forums);
+    }
+  });
+});