aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2020-03-23 12:12:50 -0600
committerBen Beltran <ben@nsovocal.com>2020-03-23 12:12:50 -0600
commite749c95bf63e22ce81f8d0f933d44e0f56c24618 (patch)
treeb1b7ba37ab5efc2b6f56d0a50161dc4e21718888
parent3841606684ee3d233266ad490905076a3562842c (diff)
Connect forum store to changefeed
-rw-r--r--app/stores/forums.js106
1 files changed, 38 insertions, 68 deletions
diff --git a/app/stores/forums.js b/app/stores/forums.js
index 1928783..5c7c6e6 100644
--- a/app/stores/forums.js
+++ b/app/stores/forums.js
@@ -1,80 +1,50 @@
import { readable } from 'svelte/store';
import { onMessage } from '../socket_coordinator';
-const internals = {};
+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'
+ // Constants
+
+ kChangeFeedEventName: 'changefeed:forums',
+
+ // The exported data structure
+
+ forums: [],
+
+ // Handles messages from the event
+
+ 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) => {
- console.log(message, typeof message);
+ if (message.name === internals.kChangeFeedEventName) {
+ internals.handleChangeFeed(message.data);
+ set(internals.forums);
+ }
});
});