Skip to content

Add pub/sub messaging system

Martin Hanzel requested to merge mh/pubsub into master

Do we not have something like this already?

If so, I haven't seen it and didn't find it while grepping.

What does this MR do?

Adds a very simple, asynchronous publish/subscribe messaging system.

API:

  • subscribe(topic, handler): Subscribes a handler function to a topic, returning an unsubscribe function that removes the handler when called.
    • Tip: namespace topics with a colon or slash or something
    • Tip: In a Vue component, subscribe in mounted() and unsubscribe in beforeDestroy().
  • publish(topic, payload): Calls all handlers listening on topic with the specified payload. Handlers are queued to run asynchronously, on the next tick.

Why?

While refactoring things to Vue, it is a problem to get legacy jQuery controls and Vue components to share state and pass messages to each other. AFAIK we don't have a solid mechanism like this in place yet.

I've seen places in the code where VueX was being used for this purpose, but VueX comes with great overhead, needs many files to do a single thing, and requires the state to be in the global scope. I've also seen mediators being used, but I feel like it's overkill to have a different mediator for every interaction, and mediators aren't always observable.

Pub/sub also allows for very easy unit testing of interactions between different components - just publish() a message to the pubsub. No mocking necessary.

With pub/sub, I got the issuables list (new Vue component) working with the bulk edit sidebar (jQuery) in less than 10 lines of code, and no new files.

@winh, @pslaughter, @fatihacet, @sstern, what think you?

Does this MR meet the acceptance criteria?

Conformity

Performance and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Martin Hanzel

Merge request reports