hnswlib-wasm
Advanced tools
Comparing version 0.1.1-20 to 0.1.1-22
@@ -337,2 +337,3 @@ /***************** GENERATED FILE ********************/ | ||
static isInitialized(): boolean; | ||
static isSynced(): boolean; | ||
static setDebugLogs(enable: boolean): void; | ||
@@ -345,3 +346,3 @@ static checkFileExists(filename: string): boolean; | ||
*/ | ||
static syncFs(read: boolean, callback: () => void): Promise<boolean>; | ||
static syncFS(read: boolean, callback: () => void): Promise<boolean>; | ||
} | ||
@@ -348,0 +349,0 @@ |
@@ -1,7 +0,22 @@ | ||
let IDBFS_STORE_NAME, addItemsWithPtrsHelper, defaultParams, hnswParamsForAda, loadHnswlib, waitForFileSystemReady; | ||
let IDBFS_STORE_NAME, addItemsWithPtrsHelper, defaultParams, hnswParamsForAda, loadHnswlib, syncFileSystem, waitForFileSystemInitalized, waitForFileSystemSynced; | ||
let __tla = (async () => { | ||
let library; | ||
waitForFileSystemReady = () => { | ||
syncFileSystem = (action) => { | ||
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager; | ||
const syncAction = action === "read" ? true : action === "write" ? false : void 0; | ||
if (syncAction === void 0) | ||
throw new Error("Invalid action type"); | ||
return new Promise((resolve, reject) => { | ||
try { | ||
EmscriptenFileSystemManager.syncFS(syncAction, () => { | ||
resolve(); | ||
}); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
}; | ||
waitForFileSystemInitalized = () => { | ||
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager; | ||
return new Promise((resolve, reject) => { | ||
let totalWaitTime = 0; | ||
@@ -23,2 +38,21 @@ const checkInterval = 100; | ||
}; | ||
waitForFileSystemSynced = () => { | ||
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager; | ||
return new Promise((resolve, reject) => { | ||
let totalWaitTime = 0; | ||
const checkInterval = 100; | ||
const maxWaitTime = 4e3; | ||
const checkInitialization = () => { | ||
if (EmscriptenFileSystemManager.isSynced()) { | ||
resolve(); | ||
} else if (totalWaitTime >= maxWaitTime) { | ||
reject(new Error("Failed to initialize filesystem")); | ||
} else { | ||
totalWaitTime += checkInterval; | ||
setTimeout(checkInitialization, checkInterval); | ||
} | ||
}; | ||
setTimeout(checkInitialization, checkInterval); | ||
}); | ||
}; | ||
const initializeFileSystemAsync = async (inputFsType) => { | ||
@@ -31,3 +65,3 @@ const fsType = inputFsType == null ? "IDBFS" : inputFsType; | ||
EmscriptenFileSystemManager.initializeFileSystem(fsType); | ||
return await waitForFileSystemReady(); | ||
return await waitForFileSystemInitalized(); | ||
}; | ||
@@ -42,7 +76,6 @@ loadHnswlib = async (inputFsType) => { | ||
if (!library) { | ||
const temp = await import("./hnswlib-22cdeb44.js"); | ||
const factory = temp.default; | ||
library = await factory(); | ||
const temp = await import("./hnswlib-a294e600.js"); | ||
const factoryFunc = temp.default; | ||
library = await factoryFunc(); | ||
await initializeFileSystemAsync(inputFsType); | ||
console.log("IDBFS Filesystem initialized"); | ||
return library; | ||
@@ -107,3 +140,5 @@ } | ||
loadHnswlib, | ||
waitForFileSystemReady | ||
syncFileSystem, | ||
waitForFileSystemInitalized, | ||
waitForFileSystemSynced | ||
}; |
/***************** GENERATED FILE ********************/ | ||
import { HnswlibModule } from '.'; | ||
type InputFsType = 'IDBFS' | undefined; | ||
export declare const waitForFileSystemReady: () => Promise<void>; | ||
export declare const syncFileSystem: (action: 'read' | 'write') => Promise<void>; | ||
export declare const waitForFileSystemInitalized: () => Promise<void>; | ||
export declare const waitForFileSystemSynced: () => Promise<void>; | ||
/** | ||
@@ -6,0 +8,0 @@ * Load the HNSW library in node or browser |
@@ -96,2 +96,7 @@ /***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
export const defaultParams = { | ||
@@ -303,1 +308,11 @@ /** | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ | ||
/***************** GENERATED FILE ********************/ |
@@ -337,2 +337,3 @@ /***************** GENERATED FILE ********************/ | ||
static isInitialized(): boolean; | ||
static isSynced(): boolean; | ||
static setDebugLogs(enable: boolean): void; | ||
@@ -345,3 +346,3 @@ static checkFileExists(filename: string): boolean; | ||
*/ | ||
static syncFs(read: boolean, callback: () => void): Promise<boolean>; | ||
static syncFS(read: boolean, callback: () => void): Promise<boolean>; | ||
} | ||
@@ -348,0 +349,0 @@ |
/***************** GENERATED FILE ********************/ | ||
import './hnswlib.mjs'; | ||
import { type HnswModuleFactory, HnswlibModule } from '.'; | ||
import { read } from 'fs'; | ||
let library: Awaited<ReturnType<HnswModuleFactory>>; | ||
let library: Awaited<HnswlibModule>; | ||
type InputFsType = 'IDBFS' | undefined; | ||
export const waitForFileSystemReady = (): Promise<void> => { | ||
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager; | ||
export const syncFileSystem = (action: 'read' | 'write'): Promise<void> => { | ||
const EmscriptenFileSystemManager: HnswlibModule['EmscriptenFileSystemManager'] = library.EmscriptenFileSystemManager; | ||
const syncAction = action === 'read' ? true : action === 'write' ? false : undefined; | ||
if (syncAction === undefined) throw new Error('Invalid action type'); | ||
return new Promise((resolve, reject) => { | ||
try { | ||
EmscriptenFileSystemManager.syncFS(syncAction, () => { | ||
resolve(); | ||
}); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
}; | ||
export const waitForFileSystemInitalized = (): Promise<void> => { | ||
const EmscriptenFileSystemManager: HnswlibModule['EmscriptenFileSystemManager'] = library.EmscriptenFileSystemManager; | ||
return new Promise((resolve, reject) => { | ||
let totalWaitTime = 0; | ||
@@ -30,2 +48,24 @@ const checkInterval = 100; // Check every 100ms | ||
export const waitForFileSystemSynced = (): Promise<void> => { | ||
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager; | ||
return new Promise((resolve, reject) => { | ||
let totalWaitTime = 0; | ||
const checkInterval = 100; // Check every 100ms | ||
const maxWaitTime = 4000; // Maximum wait time of 4 seconds | ||
const checkInitialization = () => { | ||
if (EmscriptenFileSystemManager.isSynced()) { | ||
resolve(); | ||
} else if (totalWaitTime >= maxWaitTime) { | ||
reject(new Error('Failed to initialize filesystem')); | ||
} else { | ||
totalWaitTime += checkInterval; | ||
setTimeout(checkInitialization, checkInterval); | ||
} | ||
}; | ||
setTimeout(checkInitialization, checkInterval); | ||
}); | ||
}; | ||
/** | ||
@@ -45,3 +85,3 @@ * Initializes the file system for the HNSW library using the specified file system type. | ||
EmscriptenFileSystemManager.initializeFileSystem(fsType); | ||
return await waitForFileSystemReady(); | ||
return await waitForFileSystemInitalized(); | ||
}; | ||
@@ -65,8 +105,6 @@ | ||
const temp = await import('./hnswlib.mjs'); | ||
const factory = temp.default; | ||
const factoryFunc = temp.default; | ||
library = await factory(); | ||
// console.log('Library initialized'); | ||
library = await factoryFunc(); | ||
await initializeFileSystemAsync(inputFsType); | ||
console.log('IDBFS Filesystem initialized'); | ||
return library; // Add this line | ||
@@ -73,0 +111,0 @@ } |
{ | ||
"name": "hnswlib-wasm", | ||
"version": "0.1.1-20", | ||
"version": "0.1.1-22", | ||
"description": "typescript and wasm bindings for Hnswlib", | ||
@@ -5,0 +5,0 @@ "module": "./dist/hnswlib.js", |
@@ -88,4 +88,4 @@ # hnswlib-wasm | ||
```ts | ||
await lib.EmscriptenFileSystemManager.syncFs(true); // Read data from the persistent source | ||
await lib.EmscriptenFileSystemManager.syncFs(false); // Save data to the persistent source | ||
await lib.EmscriptenFileSystemManager.syncFS(true, emscripten::val::undefined()); // Read data from the persistent source | ||
await lib.EmscriptenFileSystemManager.syncFS(false, emscripten::val::undefined()); // Save data to the persistent source | ||
``` | ||
@@ -92,0 +92,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
7368245
19936