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

Generic WeakDom using AsRef<Instance> #488

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

krakow10
Copy link
Contributor

@krakow10 krakow10 commented Feb 8, 2025

Hey I had this idea for allowing WeakDom to be usable with a generic type parameter. The problem I am trying to solve is that additional metadata about instances (such as RBXScriptSignal) cannot be directly owned by the WeakDom. Each additional piece of metadata has to live in a HashMap<Ref,_> somewhere else and be managed every time the dom is updated, so this essentially becomes a garbage collection problem. Let me know your thoughts!

Here's a code example of how I imagine this being used:
use my_lib::RBXScriptSignal;
use rbx_dom_weak::{GenericWeakDom, Instance, InstanceBuilder};

struct MyInstance {
    instance: Instance,
    render_stepped: RBXScriptSignal,
}
impl AsRef<Instance> for MyInstance {
    fn as_ref(&self) -> &Instance {
        &self.instance
    }
}
impl AsMut<Instance> for MyInstance {
    fn as_mut(&mut self) -> &mut Instance {
        &mut self.instance
    }
}
impl From<Instance> for MyInstance {
    fn from(instance: Instance) -> Self {
        Self {
            instance,
            render_stepped: RBXScriptSignal::new(),
        }
    }
}
impl Into<Instance> for MyInstance {
    fn into(self) -> Instance {
        self.instance
    }
}

fn main() {
    let builder = InstanceBuilder::new("RunService");

    let cool_dom = GenericWeakDom::<MyInstance>::new(builder);

    dbg!(cool_dom.root().render_stepped);
}

Of course MyInstance would have some sort of collection of RBXScriptSignal instead of every instance having a render_stepped property, but this is just an example.

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.

1 participant