Internal Working - AkashBabu/supervised-emitter GitHub Wiki
Internal Working in each scenario
General Case (Subscribe first, then publish)
Supervised-Emitter (SE) is initialized with a bunch of middlewares and certain other options
A few subscriptions will occur. For this sequence lets assume the following events have been subscribed:
/foo/bar/baz
/foo/*/baz
/foo/**
/foo/boo
/hello/world
Lets say a publish was made on event /foo/bar/baz
The is data is added to the context and the middleware pipeline is invoked
Now the decision point will check if the published event (pubEvent) is already present in LFU cache (why LFU ? is explain below)
If present, then the result is returned
If NOT present, then the decision point will run the match of pubEvent against all the wildcard subscription events and curate a list of matching subEvents and add it to the cache. In this case the following events are matched:
/foo/bar/baz
/foo/*/baz
/foo/**
Then every matching subscription pipeline is run with the modified context (modified by middlewares)
When a new subscription is made
Every time a new subscription is made, we check if there exists a subEvent with the exact string, if so, then we add this pipeline to the existing DLL (why DLL is explained here)
Cache is updated if necessary, i.e. if a pubEvent with the same string as subEvent exists.
When a new wildcard subscription is made
In this scenario, the new subscription might match any of the cached pubEvent and hence the wildcard match is checked on all the cached pubEvent, if any match is found, then the corresponding matching subEvents are updated with the new subscription event
Also this new subscription event is added to wildcard subEvents array. This is done, so that next time if a new pubEvent (not in cache) is received, it shall be checked on all the wildcard events.
When an event handler is unsubscribed
We just remove the handler pipeline from DLL.
Interesting thing to note here is that, this event is not removed from LFU cache, but it will be done later during publish cycle, if NO handlers are present for the cached event, then the same is removed from the cache and hence will not be available on the next publish cycle.