instrumentisto / medea

[Closed] Medea media server
Mozilla Public License 2.0
23 stars 3 forks source link

Add #[watchable] macro to 'medea-macro' crate (#27, #47) #169

Closed evdokimovs closed 3 years ago

evdokimovs commented 3 years ago

Part of #27, #47

Required for #164

Synopsis

For #164 we need to implement macro which will generate method for spawning listeners for observable field changes.

Also in this PR Component structure will be added, because #[watchable] macro works only with this concrete struct.

Solution

Add macro which will generate from code like this:

struct FooState {
    muted: ObservableCell<bool>,
}

struct FooCtx;

type FooComponent = Component<FooState, FooCtx>;

#[watchers]
impl FooComponent {
    #[watch(self.state().muted.subscribe())]
    async fn muted_change_watcher(
        ctx: Rc<FooCtx>,
        state: Rc<FooState>,
        new_muted_val: bool
    ) -> Result<(), ()> {
        Ok(())
    }
}

code like this:

impl FooComponent {
    fn spawn(&self) {
        self.spawn_watcher(
            self.state().muted.subscribe(),
            Self::muted_change_watcher,
        );
    }

    async fn muted_change_watcher(
        ctx: Rc<FooCtx>,
        state: Rc<FooState>,
        new_muted_val: bool
    ) -> Result<(), ()> {
        Ok(())
    }
}

Checklist

evdokimovs commented 3 years ago

FCM

Add #[watchable] macro to 'medea-macro' crate (#169, #27, #47)

- impl reactivity Component structure and its helper abstractions in 'medea-jason' crate