BastiaanJansen/Toast-Swift

View on GitHub
Sources/Toast/MulticastDelegate.swift

Summary

Maintainability
A
0 mins
Test Coverage
//
//  File.swift
//  
//
//  Created by Bas Jansen on 16/09/2023.
//

import Foundation

class MulticastDelegate<T> {

    private let delegates: NSHashTable<AnyObject> = NSHashTable.init()

    func add(_ delegate: T) {
        delegates.add(delegate as AnyObject)
    }

    func remove(_ delegateToRemove: T) {
        for delegate in delegates.allObjects.reversed() {
            if delegate === delegateToRemove as AnyObject {
                delegates.remove(delegate)
            }
        }
    }

    func invoke(_ invocation: (T) -> Void) {
        for delegate in delegates.allObjects.reversed() {
            invocation(delegate as! T)
        }
    }
}