Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker v2 #200

Merged
merged 34 commits into from
Jun 23, 2022
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
435f9d6
Move messages to a different file.
futursolo Mar 17, 2022
3845128
Remove WorkerLink.
futursolo Mar 17, 2022
ea8e5ec
Move WorkerExt to a different file.
futursolo Mar 17, 2022
7abcaf2
Bridge, Spawner and Registrar.
futursolo Mar 17, 2022
fb8cf16
Implement Spawner.
futursolo Mar 18, 2022
ff774ff
Remove existing implementation.
futursolo Mar 18, 2022
defbbba
Remove more unused code.
futursolo Mar 18, 2022
950060e
Remove more unused code.
futursolo Mar 18, 2022
e3aebf5
Better naming.
futursolo Mar 18, 2022
2845afa
worker_ext -> native_worker.
futursolo Mar 19, 2022
a3eb5b9
Registers the first callback as well.
futursolo Mar 19, 2022
74d5d1a
Fix exports.
futursolo Mar 19, 2022
a2ba75f
Less worker prefixes.
futursolo Mar 19, 2022
d26e873
Restore Worker Prefix, PartialEq on WorkerBridge.
futursolo Mar 20, 2022
19974b7
Introduce a method to delay worker shutdown.
futursolo Mar 22, 2022
f292201
Fix message handling.
futursolo Jun 12, 2022
3130340
Merge branch 'master' into worker-v2
futursolo Jun 12, 2022
64a08e0
Fix URL handling.
futursolo Jun 12, 2022
3af881f
Name WorkerScope scope.
futursolo Jun 12, 2022
8ee5efa
Add an example.
futursolo Jun 13, 2022
891ced8
Add test.
futursolo Jun 13, 2022
1d16320
Cargo fmt.
futursolo Jun 13, 2022
c5a0bd6
Swappable Encoding.
futursolo Jun 14, 2022
a3dbd0e
Adds an encoding setter for Registrar as well.
futursolo Jun 15, 2022
1bbeff4
Adjust the worker signature so it matches Component v2.
futursolo Jun 15, 2022
6843d99
Workers should continue to receive to messages.
futursolo Jun 15, 2022
3952ebb
More reliable destroy behaviour.
futursolo Jun 15, 2022
75402be
Adjust documentation & Destroy handle.
futursolo Jun 16, 2022
ae2435b
Export worker registrar.
futursolo Jun 16, 2022
fa79c2c
Avoid panicking after worker is destroyed.
futursolo Jun 16, 2022
cc6968f
Adjust documentation.
futursolo Jun 16, 2022
23a1e27
Make Registrable and Spawnable type configurable.
futursolo Jun 16, 2022
a2101e1
Merge branch 'master' into worker-v2
futursolo Jun 17, 2022
9a7b422
Instructions.
futursolo Jun 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More reliable destroy behaviour.
futursolo committed Jun 15, 2022
commit 3952ebb0bed6a38cce8cf63efa056b99ced5fee8
30 changes: 21 additions & 9 deletions crates/worker/src/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use wasm_bindgen::prelude::*;

use crate::messages::ToWorker;
use crate::native_worker::{DedicatedWorker, WorkerSelf};
use crate::scope::WorkerScope;
use crate::traits::Worker;
use crate::Shared;
@@ -10,8 +11,7 @@ where
W: Worker,
{
worker: Option<(W, WorkerScope<W>)>,
// TODO: Use worker field to control create message this flag
destroyed: bool,
to_destroy: bool,
}

impl<W> WorkerState<W>
@@ -21,7 +21,7 @@ where
pub fn new() -> Self {
WorkerState {
worker: None,
destroyed: false,
to_destroy: false,
}
}
}
@@ -36,6 +36,9 @@ pub(crate) enum WorkerLifecycleEvent<W: Worker> {

/// External Messages from bridges
Remote(ToWorker<W>),

/// Destroy the Worker
Destroy,
}

pub(crate) struct WorkerRunnable<W: Worker> {
@@ -53,7 +56,7 @@ where
// We should block all event other than message after a worker is destroyed.
match self.event {
WorkerLifecycleEvent::Create(scope) => {
if state.destroyed {
if state.to_destroy {
return;
}
state.worker = Some((W::create(&scope), scope));
@@ -67,7 +70,7 @@ where
worker.update(scope, msg);
}
WorkerLifecycleEvent::Remote(ToWorker::Connected(id)) => {
if state.destroyed {
if state.to_destroy {
return;
}

@@ -79,7 +82,7 @@ where
worker.connected(scope, id);
}
WorkerLifecycleEvent::Remote(ToWorker::ProcessInput(id, inp)) => {
if state.destroyed {
if state.to_destroy {
return;
}

@@ -91,7 +94,7 @@ where
worker.received(scope, inp, id);
}
WorkerLifecycleEvent::Remote(ToWorker::Disconnected(id)) => {
if state.destroyed {
if state.to_destroy {
return;
}

@@ -103,7 +106,7 @@ where
worker.disconnected(scope, id);
}
WorkerLifecycleEvent::Remote(ToWorker::Destroy) => {
if state.destroyed {
if state.to_destroy {
return;
}

@@ -118,7 +121,16 @@ where
if should_terminate_now {
scope.close();
}
state.destroyed = true;
state.to_destroy = true;
}

WorkerLifecycleEvent::Destroy => {
state
.worker
.take()
.expect_throw("worker is not initialised");

DedicatedWorker::worker_self().close();
}
}
}
6 changes: 3 additions & 3 deletions crates/worker/src/scope.rs
Original file line number Diff line number Diff line change
@@ -63,8 +63,8 @@ where
pub(crate) fn send(&self, event: WorkerLifecycleEvent<W>) {
let state = self.state.clone();

// We can implement a scheduler outself,
// but it's easier to just borrow the one from wasm-bindgen-futures.
// We can implement a custom scheduler,
// but it's easier to borrow the one from wasm-bindgen-futures.
spawn_local(async move {
WorkerRunnable { state, event }.run();
});
@@ -116,7 +116,7 @@ where
"a worker can only be closed after its destroy method is notified."
);

DedicatedWorker::worker_self().close();
self.send(WorkerLifecycleEvent::Destroy);
}

/// This method creates a callback which returns a Future which
13 changes: 10 additions & 3 deletions crates/worker/src/traits.rs
Original file line number Diff line number Diff line change
@@ -20,21 +20,28 @@ pub trait Worker: Sized + 'static {
fn update(&mut self, scope: &WorkerScope<Self>, msg: Self::Message);

/// This method called on when a new bridge created.
fn connected(&mut self, _scope: &WorkerScope<Self>, _id: HandlerId) {}
fn connected(&mut self, scope: &WorkerScope<Self>, id: HandlerId) {
let _scope = scope;
let _id = id;
}

/// Receives an input.
fn received(&mut self, scope: &WorkerScope<Self>, msg: Self::Input, id: HandlerId);

/// This method called on when a new bridge destroyed.
fn disconnected(&mut self, _scope: &WorkerScope<Self>, _id: HandlerId) {}
fn disconnected(&mut self, scope: &WorkerScope<Self>, id: HandlerId) {
let _scope = scope;
let _id = id;
}

/// This method called when the worker is destroyed.
///
/// Returns a boolean indicating whether a worker is going to close itself afterwards.
/// When the value is `true`, it means that it can be closed immediately.
/// When the value is `false`, the worker itself is responsible to close it with
/// [`WorkerScope::close`].
fn destroy(&mut self, _scope: &WorkerScope<Self>) -> bool {
fn destroy(&mut self, scope: &WorkerScope<Self>) -> bool {
let _scope = scope;
true
}
}