Skip to content

Commit

Permalink
Added getters in FluvioError and made example js use it (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
simlay authored Jun 11, 2021
1 parent 8af870d commit d37aeb6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ build-dev:
test: install-wasm-pack
wasm-pack test --firefox --headless

fluvio-websocket-proxy:
cargo run -- ./fluvio-websocket-proxy/Cargo.toml
run-fluvio-websocket-proxy:
cargo run --manifest-path ./fluvio-websocket-proxy/Cargo.toml

webpack-dev:
npm install
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This project is currently setup to be used via rollup.
Setup fluvio with a `--local` cluster installation locally then run the
following:

* `make fluvio-websocket-proxy`
* `make run-fluvio-websocket-proxy`

### Hot Reloading
To use hotreloading for the contents of
Expand Down
59 changes: 45 additions & 14 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
import("../pkg").then(async fluvioWasm => {
var Fluvio = fluvioWasm.Fluvio;
var Offset = fluvioWasm.Offset;
const fluvio = await Fluvio.connect("ws://localhost:3000")
const producer = await fluvio.topicProducer("foobar");
await producer.send("", `count`);
while (true) {
try {
const fluvio = await Fluvio.connect("ws://localhost:3000")
const producer = await fluvio.topicProducer("foobar");
await producer.send("", `count`);

const consumer = await fluvio.partitionConsumer("foobar", 0);
let stream = await consumer.stream(Offset.fromEnd(10))
const consumer = await fluvio.partitionConsumer("foobar", 0);
let stream = await consumer.stream(Offset.fromEnd(1))
const userAgent = navigator.userAgent;

let count = 0;
let before = new Date();
while (count < 10) {
let count = 0;
let before = new Date();
while (count < 10000) {

await producer.send("", `count-${count}`);
let next = await stream.next();
console.log(`${next.keyString()} - ${next.valueString()}`);
count++;
try {
await producer.send("", `${count}-${userAgent}`);
let next = await stream.next();
let text = `${next.valueString()}`;
console.log(text);
document.body.innerHTML =
`<div>${text}</div>` +
document.body.innerHTML;
count++;
} catch (e) {
console.error(e);
console.error(e.message);
console.error(e.stack);
let text = `${e} - ${userAgent}`;
document.body.innerHTML =
`<div>${text}</div>` +
document.body.innerHTML;
break;
}
await sleep(50);
}
let after = new Date();
console.log(`The recieved ${count} in took ${after - before} ms`);
} catch (e) {
console.error(e);
console.error(e.message);
console.error(e.stack);
break;
}
await sleep(5000);
}
let after = new Date();
console.log(`The recieved ${count} in took ${after - before} ms`);
});

const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
28 changes: 28 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ pub struct FluvioError {
inner: NativeFluvioError,
}

// This is to get the stack for `FluvioError`
#[wasm_bindgen]
extern "C" {
type Error;

#[wasm_bindgen(constructor)]
fn new() -> Error;

#[wasm_bindgen(structural, method, getter)]
fn stack(error: &Error) -> String;
}
#[wasm_bindgen]
impl FluvioError {
#[wasm_bindgen(getter)]
pub fn message(&self) -> String {
format!("{:?}", self.inner)
}
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
format!("FluvioError")
}
#[wasm_bindgen(getter)]
pub fn stack(&self) -> String {
let e = Error::new();
e.stack()
}
}

impl From<NativeFluvioError> for FluvioError {
fn from(inner: NativeFluvioError) -> Self {
Self { inner }
Expand Down

0 comments on commit d37aeb6

Please sign in to comment.