This commit is contained in:
Manav Rathi
2024-11-18 18:37:30 +05:30
parent ebd550505f
commit ebbca2b609
4 changed files with 11 additions and 22 deletions

View File

@@ -38,7 +38,7 @@ import {
import { detectFileTypeInfoFromChunk } from "@/new/photos/utils/detect-type";
import { readStream } from "@/new/photos/utils/native-stream";
import { mergeUint8Arrays } from "@/utils/array";
import { ensure, ensureInteger, ensureNumber } from "@/utils/ensure";
import { ensureInteger, ensureNumber } from "@/utils/ensure";
import { CustomError, handleUploadError } from "@ente/shared/error";
import { addToCollection } from "services/collectionService";
import {
@@ -857,7 +857,7 @@ const readImageOrVideoDetails = async (uploadItem: UploadItem) => {
const fileTypeInfo = await detectFileTypeInfoFromChunk(async () => {
const reader = stream.getReader();
const chunk = ensure((await reader.read()).value);
const chunk = (await reader.read())!.value;
await reader.cancel();
return chunk;
}, uploadItemFileName(uploadItem));

View File

@@ -168,7 +168,7 @@ export const handleSelectCreator =
collectionID: 0,
context: {
mode: selected.context?.mode,
personID: ensure(activePersonID),
personID: activePersonID!,
},
};
}
@@ -183,7 +183,7 @@ export const handleSelectCreator =
collectionID: 0,
context: {
mode: selected.context?.mode,
collectionID: ensure(activeCollectionID),
collectionID: activeCollectionID!,
},
};
}
@@ -194,8 +194,8 @@ export const handleSelectCreator =
const newContext: SelectionContext | undefined = !mode
? undefined
: mode == "people"
? { mode, personID: ensure(activePersonID) }
: { mode, collectionID: ensure(activeCollectionID) };
? { mode, personID: activePersonID! }
: { mode, collectionID: activeCollectionID! };
const handleCounterChange = (count: number) => {
if (selected[id] === checked) {

View File

@@ -1,6 +1,5 @@
// TODO: This file belongs to the accounts package
import { ensure } from "@/utils/ensure";
import { z } from "zod";
import { getKVS } from "./kv";
@@ -57,4 +56,8 @@ export const ensureLocalUser = (): LocalUser => {
* The underlying data is stored in IndexedDB, and can be accessed from web
* workers.
*/
export const ensureAuthToken = async () => ensure(await getKVS("token"));
export const ensureAuthToken = async () => {
const token = await getKVS("token");
if (!token) throw new Error("Not logged in");
return token;
};

View File

@@ -1,17 +1,3 @@
/**
* Throw an exception if the given value is `null` or `undefined`.
*
* This is different from TypeScript's built in null assertion operator `!` in
* that `ensure` involves a runtime check, and will throw if the given value is
* null-ish. On the other hand the TypeScript null assertion is only an
* indication to the type system and does not involve any runtime checks.
*/
export const ensure = <T>(v: T | null | undefined): T => {
if (v === null) throw new Error("Required value was null");
if (v === undefined) throw new Error("Required value was undefined");
return v;
};
/**
* Throw an exception if the given value is not a string.
*/