christianalfoni / impact

Reactive React
https://impact-react.dev
MIT License
113 stars 6 forks source link

Evaluating store to store communication #330

Closed christianalfoni closed 3 months ago

christianalfoni commented 3 months ago

One thing I discovered is that nested stores can only communicate using the public API they expose to components. This is not ideal as you might use one nested store to update some internals of a parent store.

In Angular JS they used events for this controller parent/child based communication. I think an RPC solution would be better. It would be something like:


import { receiver, emitter } from 'impact-react'

// You just define a type for this internal store-store communication. The events
// are defined as methods (RPC)
export type MyStoreEvents = { foo(): string }

function MyStore() {
    // The handler of events
    receiver<MyStoreEvents>({
        foo() {
            return 'foo'
        }
    })

    return {}
}

function MyNestedStore() {
    // The emitter
    const emit = emitter<MyStoreEvents>()

    const foo = emit.foo()

    return {}
}

The naming is a bit difficult. RPC is very technical. emitter and receiver make a lot of sense I think.

It is a relatively simple implementation. We already have the StoreContainer, so it just needs to convert the method call to a method key and params to an array with emitter. Then it walks up the parent containers and tries to find a relevant RPC registered for that key and then just calls it. So receiver will just add the methods on its StoreContainer.

christianalfoni commented 3 months ago

So implemented this as Messages