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

Make Durable Objects support DECENT #675

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kanarus
Copy link

@kanarus kanarus commented Nov 28, 2024

Problem

Currently, this crate's support for Durable Objects works, but its developer experience is incredibly bad:

use worker::{durable_object, DurableObject};
// 1. We MUST introduce them into the scope for this code to compile.
use worker::{async_trait, wasm_bindgen};

#[durable_object]
struct MyObject {
    state: worker::State,
}

// 2. This attribute to impl-block prevents editor's completions of trait methods.
// 3. And this uses `async_trait` which is unnecessary, bothering layer now.
#[durable_object]
// 4. Due to current implementation of the attribute, hover and code jump on
// this ↓ `DurableObject` doesn't work.
impl DurableObject for MyObject {
    //...
}

// 5. Defining multiple Durable Objects causes compile error due to the wrong design of Durable Objects support
#[durable_object]
struct AnotherObject;

#[durable_object]
impl DurableObject for AnotherObject { /* ... */ }
Details with Pictures

Screenshot from 2024-11-28 16-19-53

  • 3 leads to very unfriendly error messages for unexpected usage like:

Screenshot from 2024-11-28 16-07-45

  • 2, 4: completion or hover doesn't work:

Screenshot from 2024-11-28 16-25-45

Solution

This PR resolves all of them by re-designing Durable Objects support. See worker-macros/src/durable_object.rs for how new #[DurableObject] works internally.

usage:

// 1. We only have to import `DurableObject`.
use worker::DurableObject;

#[DurableObject]
struct MyObject {
    state: worker::State,
}

// 2,3,4. Now we don't need annoying attribute on impl-block.
// Of course, hover and code jump and completion works perfectly.
impl DurableObject for MyObject {
    //...
}

// 5. This compiles. No problem.
#[DurableObject]
pub struct AnotherObject;

impl DurableObject for AnotherObject {
    //...
}
Details with Pictures
  • ( 1, 2, 3, 4 ) Of course, completion, hover and code jump work perfectly

Screenshot from 2024-11-28 16-02-37

Screenshot from 2024-11-28 16-12-55

Screenshot from 2024-11-28 16-39-13

Screenshot from 2024-11-28 16-11-47

  • Very clear error message for unexpected usage:

if attribute is missing:
Screenshot from 2024-11-28 16-01-16

if trait impl is missing:
Screenshot from 2024-11-28 16-01-58

Additionally, even if we merge this PR immediately, current codes still compile and work, just with "deprecated" warnings, due to some effort for backward compatibility:

Screenshot from 2024-11-28 16-44-14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using #[durable_object] more than once causes a compile error
1 participant