refactor: cuesheet v2 (#435)

* refactor: typescript migration

* chore: remove prop-types package

* refactor: file structure

* refactor: migrate ontime table to tanstack table 8

* refactor: rundown controller uses service as data source

* refactor: convert to typescript

* feat: caching store

* refactor: add delay values to rundown

* feat: toggle past visibility

* chore: update tests

* refactor: add extra fields to CSV

* style: show skipped events

* chore: add route to navigation menu

* style: allow jumping to bottom

* chore: add tests
This commit is contained in:
Carlos Valente
2023-07-22 09:32:40 +02:00
committed by GitHub
parent bee7a8dcb4
commit 3603b836f6
80 changed files with 3188 additions and 2434 deletions
@@ -0,0 +1,67 @@
import { runtimeCacheStore } from '../cachingStore.js';
describe('cachingStore()', () => {
beforeEach(() => {
runtimeCacheStore.clear(); // Clear the cache before each test
});
it('should check if an item is cached', () => {
// Add an item to the cache
runtimeCacheStore.setCached('key', 'value');
// Check if the item is cached
expect(runtimeCacheStore.checkCached('key')).toBe(true);
expect(runtimeCacheStore.checkCached('non-existent-key')).toBe(false);
});
it('should get an item from the cache', () => {
// Add an item to the cache
runtimeCacheStore.setCached('key', 'value');
// Get the item from the cache
const result = runtimeCacheStore.getCached('key', () => 'default-value');
// Check the returned value
expect(result).toBe('value');
});
it('should retrieve default value when item is not cached', () => {
// Get an item that is not in the cache
const result = runtimeCacheStore.getCached('non-existent-key', () => 'default-value');
// Check the returned value
expect(result).toBe('default-value');
});
it('should set an item in the cache', () => {
// Set an item in the cache
runtimeCacheStore.setCached('key', 'value');
// Check if the item is cached
expect(runtimeCacheStore.checkCached('key')).toBe(true);
});
it('should invalidate an item in the cache', () => {
// Add an item to the cache
runtimeCacheStore.setCached('key', 'value');
// Invalidate the item
runtimeCacheStore.invalidate('key');
// Check if the item is no longer cached
expect(runtimeCacheStore.checkCached('key')).toBe(false);
});
it('should clear the cache', () => {
// Add items to the cache
runtimeCacheStore.setCached('key1', 'value1');
runtimeCacheStore.setCached('key2', 'value2');
// Clear the cache
runtimeCacheStore.clear();
// Check if the cache is empty
expect(runtimeCacheStore.checkCached('key1')).toBe(false);
expect(runtimeCacheStore.checkCached('key2')).toBe(false);
});
});
+47
View File
@@ -0,0 +1,47 @@
interface CacheData {
data: unknown;
}
const runtimeCache: Map<string, CacheData> = new Map();
export function checkCached(key: string): boolean {
return runtimeCache.has(key);
}
export function getCached<T>(key: string, callback: () => T): T {
if (!runtimeCache.has(key)) {
try {
const data = callback();
runtimeCache.set(key, { data });
} catch (error) {
console.log(`Failed retrieving data from callback: ${error}`);
}
}
return runtimeCache.get(key).data as T;
}
export function setCached<T>(key: string, value: T): T {
runtimeCache.set(key, { data: value });
return runtimeCache.get(key).data as T;
}
export function invalidate(key) {
runtimeCache.delete(key);
}
export function clear() {
runtimeCache.clear();
}
function createCacheStore() {
return {
checkCached,
getCached,
setCached,
invalidate,
clear,
};
}
export const runtimeCacheStore = createCacheStore();