Skip to content

Commit

Permalink
Big ABI refactor (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb authored May 3, 2024
1 parent 6e865d1 commit 59e8371
Show file tree
Hide file tree
Showing 22 changed files with 423 additions and 286 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions examples/binary-echo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions examples/binary-echo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use stateroom_wasm::{
stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService,
stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService, MessagePayload
};

#[stateroom_wasm]
#[derive(Default)]
struct BinaryEcho;

impl StateroomService for BinaryEcho {
fn message(&mut self, _: ClientId, message: &str, ctx: &impl StateroomContext) {
ctx.send_binary(MessageRecipient::Broadcast, message.as_bytes());
}

fn binary(&mut self, _: ClientId, message: &[u8], ctx: &impl StateroomContext) {
ctx.send_message(
MessageRecipient::Broadcast,
&format!("Received binary data: {:?}", &message),
);
fn message(&mut self, _: ClientId, message: MessagePayload, ctx: &impl StateroomContext) {
let message = match message {
MessagePayload::Text(s) => MessagePayload::Bytes(s.as_bytes().to_vec()),
MessagePayload::Bytes(b) => MessagePayload::Text(format!("{:?}", b)),
};
ctx.send_message(MessageRecipient::Broadcast, message);
}
}
33 changes: 33 additions & 0 deletions examples/clock/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/clock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl StateroomService for ClockServer {
}

fn timer(&mut self, ctx: &impl StateroomContext) {
ctx.send_message(MessageRecipient::Broadcast, &format!("Timer @ {}", self.0));
ctx.send_message(MessageRecipient::Broadcast, format!("Timer @ {}", self.0));
self.0 += 1;
ctx.set_timer(4000);
}
Expand Down
33 changes: 33 additions & 0 deletions examples/counter-service/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions examples/counter-service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use stateroom_wasm::{
stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService,
stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService, MessagePayload
};

#[stateroom_wasm]
#[derive(Default)]
struct SharedCounterServer(i32);

impl StateroomService for SharedCounterServer {
fn message(&mut self, _: ClientId, message: &str, ctx: &impl StateroomContext) {
match message {
fn message(&mut self, _: ClientId, message: MessagePayload, ctx: &impl StateroomContext) {
let message = match message {
MessagePayload::Text(s) => s,
MessagePayload::Bytes(_) => return,
};

match &message[..] {
"increment" => self.0 += 1,
"decrement" => self.0 -= 1,
_ => (),
}

ctx.send_message(
MessageRecipient::Broadcast,
&format!("new value: {}", self.0),
format!("new value: {}", self.0),
);
}
}
33 changes: 33 additions & 0 deletions examples/cpu-hog/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/cpu-hog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn get_time() -> u64 {

impl StateroomService for CpuHog {
fn connect(&mut self, _: ClientId, ctx: &impl StateroomContext) {
ctx.send_message(MessageRecipient::Broadcast, &format!("Connected."));
ctx.send_message(MessageRecipient::Broadcast, format!("Connected."));

let init_time = get_time();
loop {
Expand All @@ -25,6 +25,6 @@ impl StateroomService for CpuHog {
}
}

ctx.send_message(MessageRecipient::Broadcast, &format!("Finished."));
ctx.send_message(MessageRecipient::Broadcast, format!("Finished."));
}
}
33 changes: 33 additions & 0 deletions examples/echo-server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified examples/echo-server/dist/server.wasm
Binary file not shown.
Loading

0 comments on commit 59e8371

Please sign in to comment.