Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

nativeApi.ts

26 lines595 Bytes

1import type { NativeApi } from "@t3tools/contracts";
2​
3import { createWsNativeApi } from "./wsNativeApi";
4​
5let cachedApi: NativeApi | undefined;
6​
7export function readNativeApi(): NativeApi | undefined {
8 if (typeof window === "undefined") return undefined;
9 if (cachedApi) return cachedApi;
10​
11 if (window.nativeApi) {
12 cachedApi = window.nativeApi;
13 return cachedApi;
14 }
15​
16 cachedApi = createWsNativeApi();
17 return cachedApi;
19​
20export function ensureNativeApi(): NativeApi {
21 const api = readNativeApi();
22 if (!api) {
23 throw new Error("Native API not found");
24 }
25 return api;