Alpha 3 cleanup (#857)

This commit is contained in:
Carlos Valente
2024-03-28 13:35:36 +01:00
committed by GitHub
parent ce14aabcd7
commit df8b76305e
15 changed files with 167 additions and 173 deletions
@@ -7,7 +7,7 @@ import { failIsNotArray } from '../../utils/routerUtils.js';
export async function getUrlPresets(_req: Request, res: Response<URLPreset[]>) {
const presets = DataProvider.getUrlPresets();
res.status(200).send(presets);
res.status(200).send(presets as URLPreset[]);
}
export async function postUrlPresets(req: Request, res: Response<URLPreset[] | ErrorResponse>) {
@@ -18,32 +18,34 @@ import { data, db } from '../../setup/loadDb.js';
import { safeMerge } from './DataProvider.utils.js';
import { isTest } from '../../setup/index.js';
type ReadonlyPromise<T> = Promise<Readonly<T>>;
export class DataProvider {
static getData() {
return data;
}
static async setProjectData(newData: Partial<ProjectData>) {
static async setProjectData(newData: Partial<ProjectData>): ReadonlyPromise<ProjectData> {
data.project = { ...data.project, ...newData };
this.persist();
return data.project;
}
static getProjectData() {
static getProjectData(): Readonly<ProjectData> {
return data.project;
}
static async setCustomFields(newData: CustomFields): Promise<CustomFields> {
static async setCustomFields(newData: CustomFields): ReadonlyPromise<CustomFields> {
data.customFields = { ...newData };
this.persist();
return data.customFields;
}
static getCustomFields(): CustomFields {
static getCustomFields(): Readonly<CustomFields> {
return data.customFields;
}
static async setRundown(newData: OntimeRundown) {
static async setRundown(newData: OntimeRundown): ReadonlyPromise<OntimeRundown> {
data.rundown = [...newData];
this.persist();
return data.rundown;
@@ -53,64 +55,64 @@ export class DataProvider {
return data.settings;
}
static async setSettings(newData: Settings) {
static async setSettings(newData: Settings): ReadonlyPromise<Settings> {
data.settings = { ...newData };
this.persist();
return data.settings;
}
static getOsc(): OSCSettings {
static getOsc(): Readonly<OSCSettings> {
return data.osc;
}
static getHttp(): HttpSettings {
static getHttp(): Readonly<HttpSettings> {
return data.http;
}
static getUrlPresets(): URLPreset[] {
static getUrlPresets(): Readonly<URLPreset[]> {
return data.urlPresets;
}
static async setUrlPresets(newData: URLPreset[]) {
static async setUrlPresets(newData: URLPreset[]): ReadonlyPromise<URLPreset[]> {
data.urlPresets = newData;
this.persist();
return data.urlPresets;
}
static getViewSettings() {
return { ...data.viewSettings };
static getViewSettings(): Readonly<ViewSettings> {
return data.viewSettings;
}
static async setViewSettings(newData: ViewSettings) {
static async setViewSettings(newData: ViewSettings): ReadonlyPromise<ViewSettings> {
data.viewSettings = { ...newData };
this.persist();
return data.viewSettings;
}
static async setOsc(newData: OSCSettings): Promise<OSCSettings> {
static async setOsc(newData: OSCSettings): ReadonlyPromise<OSCSettings> {
data.osc = { ...newData };
this.persist();
return data.osc;
}
static async setHttp(newData: HttpSettings): Promise<HttpSettings> {
static async setHttp(newData: HttpSettings): ReadonlyPromise<HttpSettings> {
data.http = { ...newData };
this.persist();
return data.http;
}
static getRundown() {
return [...data.rundown];
static getRundown(): Readonly<OntimeRundown> {
return data.rundown;
}
static async persist() {
private static async persist() {
if (isTest) {
return;
}
await db.write();
}
static async mergeIntoData(newData: Partial<DatabaseModel>) {
static async mergeIntoData(newData: Partial<DatabaseModel>): ReadonlyPromise<DatabaseModel> {
const mergedData = safeMerge(data, newData);
data.project = mergedData.project;
data.settings = mergedData.settings;
@@ -234,7 +234,7 @@ function notifyChanges(options: { timer?: boolean | string[]; external?: boolean
* Overrides the rundown with the given
* @param rundown
*/
export async function initRundown(rundown: OntimeRundown, customFields: CustomFields) {
export async function initRundown(rundown: Readonly<OntimeRundown>, customFields: Readonly<CustomFields>) {
await cache.init(rundown, customFields);
// notify runtime that rundown has changed
@@ -55,19 +55,13 @@ const customFieldChangelog = {};
*/
let assignedCustomFields: Record<CustomFieldLabel, EventID[]> = {};
export async function init(initialRundown: OntimeRundown, customFields: CustomFields) {
persistedRundown = structuredClone(initialRundown);
export async function init(initialRundown: Readonly<OntimeRundown>, customFields: Readonly<CustomFields>) {
persistedRundown = structuredClone(initialRundown) as OntimeRundown;
persistedCustomFields = structuredClone(customFields);
generate();
await DataProvider.setRundown(persistedRundown);
}
export async function setRundown(initialRundown: OntimeRundown) {
persistedRundown = structuredClone(initialRundown);
generate();
await DataProvider.setRundown(persistedRundown);
}
/**
* Utility initialises cache
* @param rundown
@@ -74,67 +74,33 @@ export function cellRequestFromEvent(
worksheetId: number,
metadata,
): sheets_v4.Schema$Request {
const returnRows: sheets_v4.Schema$CellData[] = [];
const tmp = Object.entries(metadata)
const rowData = Object.entries(metadata)
.filter(([_, value]) => value !== undefined)
.sort(([_a, a], [_b, b]) => a['col'] - b['col']) as [string, { col: number; row: number }][];
const titleCol = tmp[0][1].col;
const titleCol = rowData[0][1].col;
for (const [index, e] of tmp.entries()) {
for (const [index, e] of rowData.entries()) {
if (index !== 0) {
const prevCol = tmp[index - 1][1].col;
const prevCol = rowData[index - 1][1].col;
const thisCol = e[1].col;
const diff = thisCol - prevCol;
if (diff > 1) {
const fillArr = new Array<(typeof tmp)[0]>(1).fill(['blank', { row: e[1].row, col: prevCol + 1 }]);
tmp.splice(index, 0, ...fillArr);
const fillArr = new Array<(typeof rowData)[0]>(1).fill(['blank', { row: e[1].row, col: prevCol + 1 }]);
rowData.splice(index, 0, ...fillArr);
}
}
}
tmp.forEach(([key, _]) => {
if (isOntimeEvent(event)) {
if (key === 'blank') {
returnRows.push({});
} else if (key === 'colour') {
returnRows.push({
userEnteredValue: { stringValue: event.colour },
});
} else if (typeof event[key] === 'number') {
returnRows.push({
userEnteredValue: { stringValue: millisToString(event[key]) },
});
} else if (typeof event[key] === 'string') {
returnRows.push({
userEnteredValue: { stringValue: event[key] },
});
} else if (typeof event[key] === 'boolean') {
returnRows.push({
userEnteredValue: { boolValue: event[key] },
});
} else {
returnRows.push({});
}
} else if (isOntimeBlock(event)) {
if (key === 'title') {
returnRows.push({
userEnteredValue: { stringValue: event[key] },
});
} else if (key === 'timerType') {
returnRows.push({
userEnteredValue: { stringValue: 'block' },
});
} else {
returnRows.push({});
}
}
const returnRows: sheets_v4.Schema$CellData[] = rowData.map(([key, _]) => {
return getCellData(key, event);
});
return {
updateCells: {
start: {
sheetId: worksheetId,
rowIndex: index + tmp[0][1]['row'] + 1,
rowIndex: index + rowData[0][1]['row'] + 1,
columnIndex: titleCol,
},
fields: 'userEnteredValue',
@@ -146,3 +112,36 @@ export function cellRequestFromEvent(
},
};
}
function getCellData(key: string, event: OntimeRundownEntry) {
if (isOntimeEvent(event)) {
if (key === 'blank') {
return {};
}
if (key === 'colour') {
return { userEnteredValue: { stringValue: event[key] } };
}
const dataType = typeof event[key];
if (dataType === 'number') {
return { userEnteredValue: { stringValue: millisToString(event[key]) } };
}
if (dataType === 'string') {
return { userEnteredValue: { stringValue: event[key] } };
}
if (dataType === 'boolean') {
return { userEnteredValue: { boolValue: event[key] } };
}
}
if (isOntimeBlock(event)) {
if (key === 'title') {
return { userEnteredValue: { stringValue: event[key] } };
}
if (key === 'timerType') {
return { userEnteredValue: { stringValue: 'block' } };
}
}
return {};
}