Skip to content

Commit f95b0d0

Browse files
Add isReady function to check WASM module initialization
- Add isReady() function to wasm/index.js and wasm/index.cjs - Export isReady from main index.js with TypeScript definition - Document isReady in README with clear usage examples - Helps users avoid 'WASM module not initialized' errors with sync methods - All 32 tests passing, no breaking changes Co-Authored-By: Dan Lynch <[email protected]>
1 parent 8f40283 commit f95b0d0

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,30 @@ const result = parseQueryDetailedSync('SELECT * FROM users WHERE active = true')
183183
// Returns: DetailedParseResult with enhanced error information if parsing fails
184184
```
185185

186+
### `isReady(): boolean`
187+
188+
Checks if the WebAssembly module is initialized and ready for synchronous operations. This is useful when using sync methods to avoid "WASM module not initialized" errors.
189+
190+
```typescript
191+
import { isReady, parseQuerySync, parseQuery } from 'libpg-query';
192+
193+
// Check if module is ready before using sync methods
194+
if (isReady()) {
195+
const result = parseQuerySync('SELECT * FROM users');
196+
} else {
197+
// Initialize by calling any async method first
198+
await parseQuery('SELECT 1');
199+
// Now sync methods will work
200+
const result = parseQuerySync('SELECT * FROM users');
201+
}
202+
203+
// Alternative pattern - always safe
204+
if (!isReady()) {
205+
await parseQuery('SELECT 1'); // Initialize module
206+
}
207+
const result = parseQuerySync('SELECT * FROM users');
208+
```
209+
186210
### Type Definitions
187211

188212
```typescript

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export function normalize(sql: string): Promise<string>;
1212
export function normalizeSync(sql: string): string;
1313
export function parseQueryDetailed(sql: string): Promise<ParseResult>;
1414
export function parseQueryDetailedSync(sql: string): ParseResult;
15+
export function isReady(): boolean;
1516
export * from '@pgsql/types';

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function parseQueryDetailedSync(query) {
2525
return wasmModule.parseQueryDetailedSync(query);
2626
}
2727

28+
function isReady() {
29+
return wasmModule.isReady();
30+
}
31+
2832
module.exports = {
2933
parseQuery: wasmModule.parseQuery,
3034
deparse: wasmModule.deparse,
@@ -38,5 +42,6 @@ module.exports = {
3842
parsePlPgSQLSync,
3943
fingerprintSync,
4044
normalizeSync,
41-
parseQueryDetailedSync
45+
parseQueryDetailedSync,
46+
isReady
4247
};

wasm/index.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ function parseQueryDetailedSync(query) {
398398

399399

400400

401+
function isReady() {
402+
return !!wasmModule;
403+
}
404+
401405
module.exports = {
402406
parseQuery,
403407
deparse,
@@ -411,5 +415,6 @@ module.exports = {
411415
fingerprintSync,
412416
normalizeSync,
413417
parseQueryDetailedSync,
418+
isReady,
414419
initPromise
415420
};

wasm/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,7 @@ export function parseQueryDetailedSync(query) {
394394
wasmModule._free(queryPtr);
395395
}
396396
}
397+
398+
export function isReady() {
399+
return !!wasmModule;
400+
}

0 commit comments

Comments
 (0)