Dynamically updating a menu in Catalyst

For the macOS version of Rigelian I wanted to move the list of players into the main menu. Because those are detected on the fly (and players can come and go) this requires that the menu can be updated dynamically.

Turns out this is possible through builder.system.setNeedsRebuild(). The following code snippet from my AppDelegate shows an example with RxSwift, where a rebuild is triggered every time the players observable publishes a next event.

override func buildMenu(with builder: UIMenuBuilder) {
    super.buildMenu(with: builder
    guard builder.system == .main else { return }

    builder.insertSibling(playerMenu(), afterMenu: .edit)

    PlayerManager.shared.players
        .subscribe(onNext: { (menu) in
            builder.system.setNeedsRebuild()
        })
        .disposed(by: bag)
}