-
Notifications
You must be signed in to change notification settings - Fork 5
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
Introducing Scopes #102
base: main
Are you sure you want to change the base?
Introducing Scopes #102
Conversation
I think we need some additional docs for this change which details about how messages propagate up and down scopes. |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor suggestions. Where do we stand on destroy or scopes?
main/contracts/contracts.ts
Outdated
/** | ||
* A reference to the parent scope if this is not the root node in the tree of scopes. If this is the root, it's undefined. | ||
*/ | ||
readonly parent?: IMessageBroker<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the parent may have different generic params. We are assuming all the tree is uniform. Its fine but something to considerr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm understand right, do you mean supporting something like this?
const broker1 = messagebroker<Contract1>();
const broker2 = broker1.createScope<Contract2>('my-scope');
in this case, would Contract2 be required to extend Contract 1? Otherwise a message sent to broker1 could not necessarily be passed down to broker2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah so I think this is governed by the propergation logic you plan to implement. For example say create scope and that scope extends the parents channels type, then it would mean my scope would essentially be a superset of the parent and messages subsribed or publishes essentially propergate upwards.
interface Contract1{
"foo": boolean;
}
const broker1 = messagebroker<Contract1>();
const broker2 = broker1.createScope<Contract2 extends Contract1>('my-scope');
broker2.publish('foo') //Works
However if you do not extend the parents type then you could it this way
const broker1 = messagebroker<Contract1>();
const broker2 = broker1.createScope<Contract2 extends Contract1>('my-scope');
broker2.publish('foo') //Compile error
broker2.parent.publish('foo') //Works
I prefer the extended types option as it makes more sense that channels delegate upwards
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? |
main/contracts/contracts.ts
Outdated
/** | ||
* A unique identifier for this instance of the MessageBroker. This is useful for identifying an instance within a tree of scopes. | ||
*/ | ||
readonly name: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've introduced the name property on the messagebroker instance itself to keep track. The root broker has the name 'root'
I have added the concept of Scopes to the MessageBroker. This is a mechanism for building a tree of message brokers in which messages will propagate up the tree until either the root is reacher, or a broker is found which has a subscriber to handle the message.
One outstanding question on this implementation is if the semantics for disposing of a scope is complete. Currently you can
.destroy()
a broker, which will unlink it from its parent meaning messages will no longer propagate up. Should this functionality do anything else? E.g. should it unlink itself from any children scopes as well?