Restrict element self connections

Could you suggest what is a proper way to prohibit (disable) connections from element to self?
Is this even possible and if yes what is the way to achieve it: should I add new rule via RuleProvider?

I managed to catch ‘connection.added’ event from eventBus and looking for a method or callback to delete this connection if event.element.businessObject.sourceRef and event.element.businessObject.targetRef elements are same.

Will appreciate any help with this!

This can be achieved using custom rules:

class MyRuleProvider extends RuleProvider {
  constructor(bpmnRules, eventBus) {

    this._bpmnRules = bpmnRules;

  init() {
    this.addRule("connection.create", 2000, ({ source, target }) => {
      if (source === target) {
        // Disallow loops
        return false;

      return this._bpmnRules.canConnect(source, target);

CodeSandbox: lucid-cherry-zg7o1 - CodeSandbox