Skip to content

Commit

Permalink
Fix revealPrivate() for async functions
Browse files Browse the repository at this point in the history
But not powerful enough to model conditional async...

Signed-off-by: William So <polyipseity@gmail.com>
  • Loading branch information
polyipseity committed Apr 30, 2023
1 parent 17fc431 commit 6bea3e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
28 changes: 25 additions & 3 deletions sources/utils/obsidian-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ export function revealPrivate<R, const As extends readonly unknown[]>(
args: true extends {
readonly [A in keyof As]: IsNever<RevealPrivate<As[A]>>
}[number] ? never : Sized<As>,
func: (...args: { readonly [A in keyof As]: RevealPrivate<As[A]> }) => R,
fallback: (error: unknown, ...args: As) => R,
): R {
func: (...args: { readonly [A in keyof As]: RevealPrivate<As[A]> }) =>
R extends PromiseLike<unknown> ? never : R,
fallback: (error: unknown, ...args: As) =>
R extends PromiseLike<unknown> ? never : R,
): R extends PromiseLike<unknown> ? never : R {
try {
return func(...args as { readonly [A in keyof As]: RevealPrivate<As[A]> })
} catch (error) {
Expand All @@ -43,6 +45,26 @@ export function revealPrivate<R, const As extends readonly unknown[]>(
return fallback(error, ...args)
}
}
export async function revealPrivateAsync<R extends PromiseLike<unknown>,
const As extends readonly unknown[]>(
plugin: TerminalPlugin,
args: true extends {
readonly [A in keyof As]: IsNever<RevealPrivate<As[A]>>
}[number] ? never : Sized<As>,
func: (...args: { readonly [A in keyof As]: RevealPrivate<As[A]> }) => R,
fallback: (error: unknown, ...args: As) => R,
): Promise<Awaited<R>> {
try {
return await func(...args as
{ readonly [A in keyof As]: RevealPrivate<As[A]> })
} catch (error) {
self.console.warn(
plugin.language.i18n.t("errors.private-API-changed"),
error,
)
return await fallback(error, ...args)
}
}
export type RevealPrivate<T> =
T extends DataAdapter ? $DataAdapter :
T extends ViewStateResult ? $ViewStateResult :
Expand Down
4 changes: 2 additions & 2 deletions sources/utils/obsidian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import {
typedStructuredClone,
} from "./util"
import { constant, isUndefined } from "lodash-es"
import { revealPrivate, revealPrivateAsync } from "./obsidian-private"
import { DEFAULT_LANGUAGE } from "assets/locales"
import { Platform } from "./platforms"
import type { TerminalPlugin } from "sources/main"
import { around } from "monkey-around"
import { revealPrivate } from "./obsidian-private"
import { saveAs } from "file-saver"

export class UpdatableUI {
Expand Down Expand Up @@ -390,7 +390,7 @@ export async function saveFileAs(
data: File,
): Promise<void> {
if (inSet(Platform.MOBILE, Platform.CURRENT)) {
await revealPrivate(plugin, [adapter] as const, async ({ fs }) => {
await revealPrivateAsync(plugin, [adapter] as const, async ({ fs }) => {
await fs.open<typeof Platform.CURRENT>(
(await Filesystem.writeFile({
data: await data.text(),
Expand Down

0 comments on commit 6bea3e7

Please sign in to comment.