morganstanley / message-broker

The MessageBroker is a typescript library for providing asynchronous communication throughout your app
http://opensource.morganstanley.com/message-broker/
Apache License 2.0
12 stars 4 forks source link

Preliminary Scopes Implementation #102

Open aidanm3341 opened 2 months ago

aidanm3341 commented 2 months ago

An initial attempt at implementing scopes to get some ideas and discussions going. I'm wondering if there is a better way to do the instantiation using Needle...

Davidhanson90 commented 2 months ago

I think we need some additional docs for this change which details about how messages propagate up and down scopes.

Davidhanson90 commented 2 months ago

What are we doing about scope disposal. I think it needs to be part of this process.

aidanm3341 commented 2 months ago

What are we doing about scope disposal. I think it needs to be part of this process.

That's a good point, there should be a mechanism for disposing of a scope similar to what exists for channels. This also made me realise another point - given that messages on a channel trickle down to the same channel in child scopes, the existing disposal logic should dispose of the same channel in the children.

aidanm3341 commented 1 week ago

Some minor suggestions. Where do we stand on destroy or scopes?

Yes sorry I had forgotten about the scope destroying my bad. My interpretation of this would be to have the ability to disconnect the child of an instance that you have, e.g.

const broker1 = messagebroker<Contract>();
const broker2 = broker1.createScope('my-scope');
broker1.destroyScope('my-scope');

broker2.get('channel').subscribe(_ => console.log('hello!'));
broker1.create('channel').publish('test'); // nothing happens

so it's essentially orphaning the child broker, stopping any message propagation down to it. What do you think, is this the kind of thing you had in mind or am I misunderstanding?