Skip to content

Commit

Permalink
Synchronous WASM init
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvictoor committed Jul 21, 2020
1 parent a509561 commit 3ef01db
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,14 @@ The code fragment below shows how to instantiate a resizable 32 bits WebAssembly
```
import * as hdr from "hdr-histogram-js"
// load HdrHistogramJS WASM module
// If you are on the browser side, you need to
// load asynchronously HdrHistogramJS WASM module
await hdr.initWebAssembly();
// If you are on the server side, you can
// load synchronously HdrHistogramJS WASM module
hdr.initWebAssemblySync();
const histogram = hdr.build({ useWebAssembly: true });
// you can now use your histogram the same way you would do
Expand Down
4 changes: 2 additions & 2 deletions src/Histogram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { build } from ".";
import JsHistogram from "./JsHistogram";
import { NO_TAG } from "./Histogram";
import Int32Histogram from "./Int32Histogram";
import { initWebAssembly, WasmHistogram } from "./wasm";
import { initWebAssembly, WasmHistogram, initWebAssemblySync } from "./wasm";
import Int8Histogram from "./Int8Histogram";
import Histogram from "./Histogram";

Expand Down Expand Up @@ -309,7 +309,7 @@ describe("WASM Histogram not initialized", () => {
});

describe("WASM Histogram not happy path", () => {
beforeEach(initWebAssembly);
beforeEach(initWebAssemblySync);
it("should throw a clear error message when used after destroy", () => {
const destroyedHistogram = build({ useWebAssembly: true });
destroyedHistogram.destroy();
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import Int8Histogram from "./Int8Histogram";
import JsHistogram from "./JsHistogram";
import PackedHistogram from "./PackedHistogram";
import Recorder from "./Recorder";
import { initWebAssembly, WasmHistogram } from "./wasm";
import { initWebAssembly, initWebAssemblySync, WasmHistogram } from "./wasm";
import { build } from "./HistogramBuilder";

export {
initWebAssembly,
initWebAssemblySync,
Int8Histogram,
Int16Histogram,
Int32Histogram,
Expand Down
5 changes: 5 additions & 0 deletions src/wasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export const initWebAssembly = async (): Promise<void> => {
.then((w: any) => (wasm = w.exports || w));
};

export const initWebAssemblySync = () => {
const w = loader.instantiateSync(pako.inflate(base64.toByteArray(BINARY)));
wasm = w.exports || w;
};

export const webAssemblyReady = () => !!wasm;

const defaultRequest: BuildRequest = {
Expand Down

0 comments on commit 3ef01db

Please sign in to comment.