-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathffi-client.ts
More file actions
36 lines (30 loc) · 1.12 KB
/
ffi-client.ts
File metadata and controls
36 lines (30 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
import type * as ffiTypes from "@eserstack/ajan/ffi";
// Lazy FFI singleton — loaded once per process, reused for every codebase call.
// If the native library is unavailable the promise resolves without setting _lib,
// and callers fall back to the pure TypeScript implementation.
let _lib: ffiTypes.FFILibrary | null = null;
let _libPromise: Promise<void> | null = null;
export const ensureLib = (): Promise<void> => {
if (_libPromise === null) {
_libPromise = import("@eserstack/ajan/ffi")
.then((ffi) => ffi.loadEserAjan())
.then((lib) => {
_lib = lib;
})
.catch(() => {
// Native library unavailable — callers use TS fallback.
});
}
return _libPromise;
};
export const getLib = (): ffiTypes.FFILibrary | null => _lib;
export const requireLib = async (): Promise<ffiTypes.FFILibrary> => {
await ensureLib();
if (_lib === null) {
throw new Error(
"native FFI library unavailable; ensure @eserstack/ajan is built and accessible",
);
}
return _lib;
};