Skip to content

Commit 21580d1

Browse files
committed
loadModule() and remove isReady()
1 parent d1f40da commit 21580d1

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

README.md

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

186-
### `isReady(): boolean`
186+
### Initialization
187187

188-
Checks if the WebAssembly module is initialized and ready for synchronous operations. This is only needed when using the synchronous methods (`parseQuerySync`, `deparseSync`, etc.).
188+
The library provides both async and sync methods. Async methods handle initialization automatically, while sync methods require explicit initialization.
189+
190+
#### Async Methods (Recommended)
191+
192+
Async methods handle initialization automatically and are always safe to use:
189193

190194
```typescript
191-
import { isReady, parseQuerySync } 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-
// Module needs initialization
198-
console.warn('WASM module not initialized. Use async methods first to initialize.');
199-
}
195+
import { parseQuery, deparse } from 'libpg-query';
200196

201-
// Recommended pattern for sync methods
202-
if (!isReady()) {
203-
throw new Error('WASM module not initialized. Use async methods first to initialize.');
204-
}
197+
// These handle initialization automatically
198+
const result = await parseQuery('SELECT * FROM users');
199+
const sql = await deparse(result[0]);
200+
```
201+
202+
#### Sync Methods
203+
204+
Sync methods require explicit initialization using `loadModule()`:
205+
206+
```typescript
207+
import { loadModule, parseQuerySync } from 'libpg-query';
208+
209+
// Initialize first
210+
await loadModule();
211+
212+
// Now safe to use sync methods
213+
const result = parseQuerySync('SELECT * FROM users');
214+
```
215+
216+
### `loadModule(): Promise<void>`
217+
218+
Explicitly initializes the WASM module. Required before using any sync methods.
219+
220+
```typescript
221+
import { loadModule, parseQuerySync } from 'libpg-query';
222+
223+
// Initialize before using sync methods
224+
await loadModule();
205225
const result = parseQuerySync('SELECT * FROM users');
206226
```
207227

208-
Note: The async methods (`parseQuery`, `deparse`, `parsePlPgSQL`, etc.) handle initialization automatically and are always safe to use. The `isReady()` check is only needed for the synchronous versions of these methods.
228+
Note: We recommend using async methods as they handle initialization automatically. Use sync methods only when necessary, and always call `loadModule()` first.
209229

210230
### Type Definitions
211231

wasm/index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ const initPromise = PgQueryModule().then((module) => {
66
wasmModule = module;
77
});
88

9+
export async function loadModule() {
10+
if (!wasmModule) {
11+
await initPromise;
12+
}
13+
return wasmModule;
14+
}
15+
916
function awaitInit(fn) {
1017
return async (...args) => {
1118
await initPromise;
@@ -164,8 +171,6 @@ export const normalize = awaitInit(async (query) => {
164171
}
165172
});
166173

167-
168-
169174
export const parseQueryDetailed = awaitInit(async (query) => {
170175
const queryPtr = stringToPtr(query);
171176
let resultPtr;
@@ -207,7 +212,7 @@ export const parseQueryDetailed = awaitInit(async (query) => {
207212

208213
export function parseQuerySync(query) {
209214
if (!wasmModule) {
210-
throw new Error('WASM module not initialized. Call an async method first to initialize.');
215+
throw new Error('WASM module not initialized. Call loadModule() first.');
211216
}
212217
const queryPtr = stringToPtr(query);
213218
let resultPtr;
@@ -252,7 +257,7 @@ export function parseQuerySync(query) {
252257

253258
export function deparseSync(parseTree) {
254259
if (!wasmModule) {
255-
throw new Error('WASM module not initialized. Call an async method first to initialize.');
260+
throw new Error('WASM module not initialized. Call loadModule() first.');
256261
}
257262
const protobufData = protobufCache.get(parseTree);
258263

@@ -283,7 +288,7 @@ export function deparseSync(parseTree) {
283288

284289
export function parsePlPgSQLSync(query) {
285290
if (!wasmModule) {
286-
throw new Error('WASM module not initialized. Call an async method first to initialize.');
291+
throw new Error('WASM module not initialized. Call loadModule() first.');
287292
}
288293
const queryPtr = stringToPtr(query);
289294
let resultPtr;
@@ -307,7 +312,7 @@ export function parsePlPgSQLSync(query) {
307312

308313
export function fingerprintSync(query) {
309314
if (!wasmModule) {
310-
throw new Error('WASM module not initialized. Call an async method first to initialize.');
315+
throw new Error('WASM module not initialized. Call loadModule() first.');
311316
}
312317
const queryPtr = stringToPtr(query);
313318
let resultPtr;
@@ -331,7 +336,7 @@ export function fingerprintSync(query) {
331336

332337
export function normalizeSync(query) {
333338
if (!wasmModule) {
334-
throw new Error('WASM module not initialized. Call an async method first to initialize.');
339+
throw new Error('WASM module not initialized. Call loadModule() first.');
335340
}
336341
const queryPtr = stringToPtr(query);
337342
let resultPtr;
@@ -355,7 +360,7 @@ export function normalizeSync(query) {
355360

356361
export function parseQueryDetailedSync(query) {
357362
if (!wasmModule) {
358-
throw new Error('WASM module not initialized. Call an async method first to initialize.');
363+
throw new Error('WASM module not initialized. Call loadModule() first.');
359364
}
360365
const queryPtr = stringToPtr(query);
361366
let resultPtr;
@@ -394,7 +399,3 @@ export function parseQueryDetailedSync(query) {
394399
wasmModule._free(queryPtr);
395400
}
396401
}
397-
398-
export function isReady() {
399-
return !!wasmModule;
400-
}

0 commit comments

Comments
 (0)