From 368da3360a8eeaabb9b52997c86c6b3351a083b4 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 22 Aug 2022 22:59:28 -0400 Subject: [PATCH] Extract QueryInput to its own component. Add larger modal text editor. Include JSON representation in JS filtering. --- src/lib/Modal.svelte | 17 ++++--- src/lib/QueryInput.svelte | 62 +++++++++++++++++++++++ src/lib/state.js | 21 +++++--- src/routes/+page.svelte | 100 +++++++++++++++++++++----------------- 4 files changed, 141 insertions(+), 59 deletions(-) create mode 100644 src/lib/QueryInput.svelte diff --git a/src/lib/Modal.svelte b/src/lib/Modal.svelte index 4dfdb19..8fb2bb0 100644 --- a/src/lib/Modal.svelte +++ b/src/lib/Modal.svelte @@ -1,9 +1,10 @@ @@ -40,7 +41,7 @@ /* Modal Content/Box */ .modal-content { background-color: #fefefe; - margin: 15% auto; /* 15% from the top and centered */ + margin: /*15%*/ auto; /* 15% from the top and centered */ padding: 2em; border: 1px solid #888; border-radius: 10px; diff --git a/src/lib/QueryInput.svelte b/src/lib/QueryInput.svelte new file mode 100644 index 0000000..e4fb595 --- /dev/null +++ b/src/lib/QueryInput.svelte @@ -0,0 +1,62 @@ + + +
+
jsFilter = !jsFilter}> + Use JavaScript to filter messages + +
+
+ +{#if jsFilter} +
+
+ Allow JavaScript to mutate objects + +
+ +
+{/if} + + \ No newline at end of file diff --git a/src/lib/state.js b/src/lib/state.js index 6295551..50813e4 100644 --- a/src/lib/state.js +++ b/src/lib/state.js @@ -39,6 +39,7 @@ export const testMode = true export const state = writable({ items: [], itemCount: undefined, + matchCount: undefined, error: 'Connecting to WebSocket...', }) @@ -50,7 +51,7 @@ const updateClearError = updater => state.update(s => { let ws; let itemLimit = Infinity let disconnected = false -let filterFunc = (_message, _value) => true +let filterFunc = (_message, _value, _json) => true let runTestQuery = true const testTimeout = 200 @@ -60,9 +61,13 @@ const testQuery = (mode) => { if (mode === queryMode.REAL_TIME) { const addItem = () => { const item = getRandomFromArray(mockItems) - if (filterFunc(item, item.value)) { + if (filterFunc(item, item.value, JSON.stringify(item))) { item.timestamp = new Date().getTime() - state.update(s => ({ ...s, items: [item, ...s.items].slice(0, itemLimit), itemCount: (s.itemCount || 0) + 1 })) + state.update(s => ({ + ...s, + items: [item, ...s.items].slice(0, itemLimit), + itemCount: (s.itemCount || 0) + 1 + })) } if (runTestQuery) { setTimeout(addItem, testTimeout) @@ -70,7 +75,11 @@ const testQuery = (mode) => { } setTimeout(addItem, testTimeout) } else { - state.update(s => ({ ...s, items: mockItems.filter(item => filterFunc(item, item.value)), itemCount: (s.itemCount || 0) + 1 })) + state.update(s => ({ + ...s, + items: mockItems.filter(item => filterFunc(item, item.value, JSON.stringify(item))), + itemCount: (s.itemCount || 0) + 1 + })) } } catch (e) { console.log('Caught an error:', e.toString()) @@ -89,7 +98,7 @@ export const killQuery = async ({ }) => { // noinspection JSUnusedGlobalSymbols export const query = async ({ cluster, topic, mode, jsFilter, queryCode, maxItems, mutableObjects }) => { if (jsFilter) { - filterFunc = new Function('message', 'value', queryCode) + filterFunc = new Function('message', 'value', 'json', queryCode) } else { filterFunc = () => true } @@ -153,7 +162,7 @@ export const connect = () => { })) break; case 'message': - if (filterFunc(data.message)) { + if (filterFunc(data.message, data.message.value, message.data)) { updateClearError(s => ({ ...s, items: console.log('new item', data.message) || [data.message, ...s.items].slice(0, itemLimit), diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index fde5b86..74e9970 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,9 +1,13 @@