From 5963791d85ec7172cba0d81ff4766113f1641387 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sun, 27 Feb 2022 08:35:07 +0100 Subject: [PATCH] Consider using --force-message for ts/protobuf.bzl (#1694) * Use --force-message in ts/protobuf * Remove some now unnecessary type assertions in deck-options/lib * Satisfy formatter --- ts/card-info/CardInfo.svelte | 6 +++-- ts/change-notetype/index.ts | 12 +++++---- ts/change-notetype/lib.ts | 10 ++++--- ts/deck-options/index.ts | 4 +-- ts/deck-options/lib.test.ts | 8 ++---- ts/deck-options/lib.ts | 38 +++++++++++++++------------ ts/editor/tag-editor/TagEditor.svelte | 6 +++-- ts/graphs/WithGraphData.svelte | 8 +++--- ts/lib/i18n/utils.ts | 4 +-- ts/lib/proto.ts | 2 +- ts/protobuf.bzl | 2 ++ 11 files changed, 55 insertions(+), 45 deletions(-) diff --git a/ts/card-info/CardInfo.svelte b/ts/card-info/CardInfo.svelte index d16b0e6aa..2a3e1f450 100644 --- a/ts/card-info/CardInfo.svelte +++ b/ts/card-info/CardInfo.svelte @@ -6,7 +6,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import Container from "../components/Container.svelte"; import Row from "../components/Row.svelte"; import type { Stats } from "../lib/proto"; - import { stats as statsService } from "../lib/proto"; + import { Cards, stats as statsService } from "../lib/proto"; import CardInfoPlaceholder from "./CardInfoPlaceholder.svelte"; import CardStats from "./CardStats.svelte"; import Revlog from "./Revlog.svelte"; @@ -19,7 +19,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html async function updateStats(cardId: number): Promise { const requestedCardId = cardId; - const cardStats = await statsService.cardStats({ cid: requestedCardId }); + const cardStats = await statsService.cardStats( + Cards.CardId.create({ cid: requestedCardId }), + ); /* Skip if another update has been triggered in the meantime. */ if (requestedCardId === cardId) { diff --git a/ts/change-notetype/index.ts b/ts/change-notetype/index.ts index 4c0251e09..dfa6dcc22 100644 --- a/ts/change-notetype/index.ts +++ b/ts/change-notetype/index.ts @@ -5,7 +5,7 @@ import "./change-notetype-base.css"; import { ModuleName, setupI18n } from "../lib/i18n"; import { checkNightMode } from "../lib/nightmode"; -import { empty, notetypes } from "../lib/proto"; +import { empty, Notetypes, notetypes } from "../lib/proto"; import ChangeNotetypePage from "./ChangeNotetypePage.svelte"; import { ChangeNotetypeState } from "./lib"; @@ -18,10 +18,12 @@ export async function setupChangeNotetypePage( oldNotetypeId: number, newNotetypeId: number, ): Promise { - const changeNotetypeInfo = notetypes.getChangeNotetypeInfo({ - oldNotetypeId, - newNotetypeId, - }); + const changeNotetypeInfo = notetypes.getChangeNotetypeInfo( + Notetypes.GetChangeNotetypeInfoRequest.create({ + oldNotetypeId, + newNotetypeId, + }), + ); const [names, info] = await Promise.all([notetypeNames, changeNotetypeInfo, i18n]); checkNightMode(); diff --git a/ts/change-notetype/lib.ts b/ts/change-notetype/lib.ts index 92faa799b..a0802b0b5 100644 --- a/ts/change-notetype/lib.ts +++ b/ts/change-notetype/lib.ts @@ -144,10 +144,12 @@ export class ChangeNotetypeState { this.info_.input().newNotetypeId = this.notetypeNames.entries[idx].id!; this.notetypesSetter(this.buildNotetypeList()); const { oldNotetypeId, newNotetypeId } = this.info_.input(); - const newInfo = await notetypes.getChangeNotetypeInfo({ - oldNotetypeId, - newNotetypeId, - }); + const newInfo = await notetypes.getChangeNotetypeInfo( + Notetypes.GetChangeNotetypeInfoRequest.create({ + oldNotetypeId, + newNotetypeId, + }), + ); this.info_ = new ChangeNotetypeInfoWrapper(newInfo); this.info_.unusedItems(MapContext.Field); diff --git a/ts/deck-options/index.ts b/ts/deck-options/index.ts index 9841e237d..d9bc2adbd 100644 --- a/ts/deck-options/index.ts +++ b/ts/deck-options/index.ts @@ -11,7 +11,7 @@ import "./deck-options-base.css"; import { modalsKey, touchDeviceKey } from "../components/context-keys"; import { ModuleName, setupI18n } from "../lib/i18n"; import { checkNightMode } from "../lib/nightmode"; -import { deckConfig } from "../lib/proto"; +import { deckConfig, Decks } from "../lib/proto"; import DeckOptionsPage from "./DeckOptionsPage.svelte"; import { DeckOptionsState } from "./lib"; @@ -26,7 +26,7 @@ const i18n = setupI18n({ export async function setupDeckOptions(did: number): Promise { const [info] = await Promise.all([ - deckConfig.getDeckConfigsForUpdate({ did }), + deckConfig.getDeckConfigsForUpdate(Decks.DeckId.create({ did })), i18n, ]); diff --git a/ts/deck-options/lib.test.ts b/ts/deck-options/lib.test.ts index 69bb2d0bb..c54fedcb1 100644 --- a/ts/deck-options/lib.test.ts +++ b/ts/deck-options/lib.test.ts @@ -304,12 +304,8 @@ test("aux data", () => { // ensure changes serialize const out = state.dataForSaving(true); expect(out.configs!.length).toBe(2); - const json = out.configs!.map( - (c) => - JSON.parse(new TextDecoder().decode((c.config as any).other)) as Record< - string, - unknown - >, + const json = out.configs!.map((c) => + JSON.parse(new TextDecoder().decode(c.config!.other)), ); expect(json).toStrictEqual([ // other deck comes first diff --git a/ts/deck-options/lib.ts b/ts/deck-options/lib.ts index 80f2e1d4d..c3a3d02a0 100644 --- a/ts/deck-options/lib.ts +++ b/ts/deck-options/lib.ts @@ -28,15 +28,14 @@ export interface ConfigListEntry { current: boolean; } -type ConfigInner = DeckConfig.DeckConfig.Config; export class DeckOptionsState { - readonly currentConfig: Writable; + readonly currentConfig: Writable; readonly currentAuxData: Writable>; readonly configList: Readable; readonly parentLimits: Readable; readonly cardStateCustomizer: Writable; readonly currentDeck: DeckConfig.DeckConfigsForUpdate.CurrentDeck; - readonly defaults: ConfigInner; + readonly defaults: DeckConfig.DeckConfig.Config; readonly addonComponents: Writable; readonly v3Scheduler: boolean; readonly haveAddons: boolean; @@ -52,11 +51,11 @@ export class DeckOptionsState { constructor(targetDeckId: number, data: DeckConfig.DeckConfigsForUpdate) { this.targetDeckId = targetDeckId; - this.currentDeck = - data.currentDeck as DeckConfig.DeckConfigsForUpdate.CurrentDeck; - this.defaults = data.defaults!.config! as ConfigInner; + this.currentDeck = data.currentDeck!; + this.defaults = data.defaults!.config!; this.configs = data.allConfig.map((config) => { - const configInner = config.config as DeckConfig.DeckConfig; + const configInner = config.config!; + return { config: configInner, useCount: config.useCount!, @@ -136,7 +135,7 @@ export class DeckOptionsState { const config = DeckConfig.DeckConfig.create({ id: 0, name: uniqueName, - config: cloneDeep(source), + config: DeckConfig.DeckConfig.Config.create(cloneDeep(source)), }); const configWithCount = { config, useCount: 0 }; this.configs.push(configWithCount); @@ -195,10 +194,14 @@ export class DeckOptionsState { } async save(applyToChildren: boolean): Promise { - await deckConfig.updateDeckConfigs(this.dataForSaving(applyToChildren)); + await deckConfig.updateDeckConfigs( + DeckConfig.UpdateDeckConfigsRequest.create( + this.dataForSaving(applyToChildren), + ), + ); } - private onCurrentConfigChanged(config: ConfigInner): void { + private onCurrentConfigChanged(config: DeckConfig.DeckConfig.Config): void { const configOuter = this.configs[this.selectedIdx].config; if (!isEqual(config, configOuter.config)) { configOuter.config = config; @@ -240,13 +243,13 @@ export class DeckOptionsState { } /// Returns a copy of the currently selected config. - private getCurrentConfig(): ConfigInner { - return cloneDeep(this.configs[this.selectedIdx].config.config as ConfigInner); + private getCurrentConfig(): DeckConfig.DeckConfig.Config { + return cloneDeep(this.configs[this.selectedIdx].config.config!); } /// Extra data associated with current config (for add-ons) private getCurrentAuxData(): Record { - const conf = this.configs[this.selectedIdx].config.config as ConfigInner; + const conf = this.configs[this.selectedIdx].config.config!; return bytesToObject(conf.other); } @@ -287,20 +290,21 @@ export class DeckOptionsState { function bytesToObject(bytes: Uint8Array): Record { if (!bytes.length) { - return {} as Record; + return {}; } let obj: Record; + try { - obj = JSON.parse(new TextDecoder().decode(bytes)) as Record; + obj = JSON.parse(new TextDecoder().decode(bytes)); } catch (err) { console.log(`invalid json in deck config`); - return {} as Record; + return {}; } if (obj.constructor !== Object) { console.log(`invalid object in deck config`); - return {} as Record; + return {}; } return obj; diff --git a/ts/editor/tag-editor/TagEditor.svelte b/ts/editor/tag-editor/TagEditor.svelte index 26cea9470..9db34e7a1 100644 --- a/ts/editor/tag-editor/TagEditor.svelte +++ b/ts/editor/tag-editor/TagEditor.svelte @@ -8,7 +8,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import ButtonToolbar from "../../components/ButtonToolbar.svelte"; import StickyContainer from "../../components/StickyContainer.svelte"; - import { tags as tagsService } from "../../lib/proto"; + import { Tags, tags as tagsService } from "../../lib/proto"; import { execCommand } from "../helpers"; import Tag from "./Tag.svelte"; import TagEditMode from "./TagEditMode.svelte"; @@ -56,7 +56,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html let autocompleteDisabled: boolean = false; async function fetchSuggestions(input: string): Promise { - const { tags } = await tagsService.completeTag({ input, matchLimit: 500 }); + const { tags } = await tagsService.completeTag( + Tags.CompleteTagRequest.create({ input, matchLimit: 500 }), + ); return tags; } diff --git a/ts/graphs/WithGraphData.svelte b/ts/graphs/WithGraphData.svelte index e2fefff08..d63b7c491 100644 --- a/ts/graphs/WithGraphData.svelte +++ b/ts/graphs/WithGraphData.svelte @@ -5,8 +5,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html