From ddc4576f94536f4d9d9b9209bec25c7ea68850fc Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 6 Sep 2022 21:01:40 -0400 Subject: [PATCH] Better mockItems. --- src/lib/state.js | 64 ++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/src/lib/state.js b/src/lib/state.js index f7d8402..8c6fc94 100644 --- a/src/lib/state.js +++ b/src/lib/state.js @@ -1,6 +1,6 @@ // noinspection JSCheckFunctionSignatures -import { writable } from 'svelte/store' +import { get, writable } from 'svelte/store' import { backendAddressAndPort, queryMode, queryState } from './constants.js' const getRandomFromArray = array => array[Math.floor(Math.random() * array.length)] @@ -16,25 +16,34 @@ const addMetadata = mockItem => ({ key: null, }) -const mockItems = () => [ - { - eventType: "purchase", - userName: "sagev", - itemName: "Bean Enchilada", - quantity: getRandomFromArray([1, 2, 3]) - }, - { - eventType: "purchase", - userName: "cameronl", - itemName: "Tacos", - quantity: getRandomFromArray([1, 2, 3]) - }, - { - eventType: "refund", - userName: "sagev", - purchaseId: crypto.randomUUID(), - }, -].map(addMetadata) +const mockUsernames = [ + 'sagev', 'cameronl', 'westonm', 'connorl', 'cameronf', 'thomase' +] + +const mockEventTypes = [ + 'purchase', 'refund', +] + +const mockItemTypes = [ + 'Taco', 'Enchilada', 'Soda', 'Chalupa', 'Tostada' +] + +const buildMockItem = () => { + const purchasedBy = getRandomFromArray(mockUsernames) + let transactionHandledBy = getRandomFromArray(mockUsernames) + while (transactionHandledBy === purchasedBy) { + transactionHandledBy = getRandomFromArray(mockUsernames) + } + return addMetadata({ + eventType: getRandomFromArray(mockEventTypes), + itemType: getRandomFromArray(mockItemTypes), + purchasedBy, + transactionHandledBy, + purchaseId: crypto.randomUUID() + }); +} + +const mockItems = Array(100).fill(buildMockItem, 0).map(mocker => mocker()) export const testMode = true @@ -71,7 +80,7 @@ const testQuery = (mode) => { queryStartTime: new Date() })) const addItem = () => { - const item = getRandomFromArray(mockItems()) + const item = buildMockItem() if (filterFunc(item, item.value, JSON.stringify(item))) { item.timestamp = new Date().getTime() state.update(s => ({ @@ -85,19 +94,16 @@ const testQuery = (mode) => { } } setTimeout(addItem, testTimeout) - } else { - state.update(s => ({ - ...s, - })) + } else { // ONE_SHOT setTimeout(() => { - const items = mockItems().filter(item => filterFunc(item, item.value, JSON.stringify(item))) + const filteredItems = mockItems.filter(item => filterFunc(item, item.value, JSON.stringify(item))).slice(0, itemLimit) state.update(s => ({ ...s, - items, - itemCount: items.length, + items: filteredItems, + itemCount: filteredItems.length, queryState: queryState.DONE })) - }, testTimeout * 10) + }, 2000) } } catch (e) { console.log('Caught an error:', e.toString())