Skip to content

Commit 8717c44

Browse files
committed
docs(readme): add examples for async* options
[ci skip]
1 parent ddde7d4 commit 8717c44

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

README.md

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
</div>
99

1010
[![npm][npm]][npm-url]
11-
[![npm][npm-download]][npm-url]
1211
[![node][node]][node-url]
12+
[![size][size]][size-url]
13+
[![npm][npm-download]][npm-url]
1314
[![deps][deps]][deps-url]
1415
[![tests][tests]][tests-url]
15-
[![size][size]][size-url]
16+
[![cover][cover]][cover-url]
17+
[![stencil][stencil]][stencil-url]
1618

1719
# rollup-plugin-rust
1820
<sup><sup>tl;dr -- see [examples](#examples)</sup></sup>
@@ -84,7 +86,9 @@ And run `rollup` via your preferred method.
8486
- `buffer` will export wasm code as [Buffer][]
8587
- `module` will export wasm code as [WebAssembly.Module][]
8688
- `instance` will export wasm code as [WebAssembly.Instance][]
87-
- `promise` will [instantiate][WebAssembly.instantiate] wasm code asynchronously
89+
- `async` will [instantiate][WebAssembly.instantiate] wasm code asynchronously, return promise of both [WebAssembly.Module][] and [WebAssembly.Instance][]
90+
- `async-module` will [compile][WebAssembly.compile] wasm code asynchronously, return promise of [WebAssembly.Module][]
91+
- `async-instance` will [instantiate][WebAssembly.instantiate] wasm code asynchronously, return promise of [WebAssembly.Instance][]
8892

8993
How wasm code would be exported. (see [examples](#examples))
9094

@@ -228,13 +232,48 @@ import wasm from './lib.rs'
228232
console(wasm.exports.add(1, 2)); // 3
229233
```
230234
---
231-
#### `{export: 'promise'}`
235+
#### `{export: 'async'}`
236+
```rust
237+
extern {
238+
fn hook(c: i32);
239+
}
240+
241+
#[no_mangle]
242+
pub fn add(a: i32, b: i32) -> i32 {
243+
hook(a + b)
244+
}
245+
```
246+
232247
```js
233248
import wasmInstantiate from './lib.rs'
234249

235-
wasmInstantiate.then(({ instance, module }) => {
250+
wasmInstantiate(importObject | undefined).then(({ instance, module }) => {
236251
console(instance.exports.add(1, 2)); // 3
237-
// create different instance
252+
253+
// create different instance, extra will be called in different environment
254+
const differentInstance = new WebAssembly.Instance(module, {
255+
env: {
256+
hook: result => result * 2
257+
}
258+
});
259+
console(differentInstance.exports.add(1, 2)); // 6
260+
})
261+
```
262+
---
263+
#### `{export: 'async-instance'}`
264+
```js
265+
import wasmInstantiate from './lib.rs'
266+
267+
wasmInstantiate(importObject | undefined).then(instance => {
268+
console(instance.exports.add(1, 2)); // 3
269+
})
270+
```
271+
---
272+
#### `{export: 'async-module'}`
273+
```js
274+
import wasmInstantiate from './lib.rs'
275+
276+
wasmCompile(importObject | undefined).then(module => {
238277
const differentInstance = new WebAssembly.Instance(module);
239278
console(differentInstance.exports.add(1, 2)); // 3
240279
})
@@ -263,6 +302,7 @@ wasmInstantiate.then(({ instance, module }) => {
263302
[WebAssembly.Module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module
264303
[WebAssembly.Instance]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance
265304
[WebAssembly.instantiate]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate
305+
[WebAssembly.compile]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/compile
266306

267307
[npm]: https://img.shields.io/npm/v/rollup-plugin-rust.svg
268308
[npm-url]: https://npmjs.com/package/rollup-plugin-rust
@@ -277,6 +317,9 @@ wasmInstantiate.then(({ instance, module }) => {
277317
[tests]: https://img.shields.io/circleci/project/github/DrSensor/rollup-plugin-rust.svg
278318
[tests-url]: https://circleci.com/gh/DrSensor/rollup-plugin-rust
279319

320+
[stencil]: https://img.shields.io/travis/DrSensor/rollup-plugin-rust.svg?label=smoke%20stencil
321+
[stencil-url]: https://travis-ci.org/DrSensor/rollup-plugin-rust
322+
280323
[cover]: https://codecov.io/gh/DrSensor/rollup-plugin-rust/branch/master/graph/badge.svg
281324
[cover-url]: https://codecov.io/gh/DrSensor/rollup-plugin-rust
282325

0 commit comments

Comments
 (0)