Yuta Saito
どこで欲しくなるんだろう
WebMock
を実装してて困った。 public class WebEventTarget: JSNativeObject { public init() { } private var listeners: [String: [JSFunction]] = [:] public func addEventListener(_ type: String, _ listener: JSFunction) { var lns = self.listeners[type] ?? [] if lns.contains(listener) { return } lns.append(listener) self.listeners[type] = lns } public func removeEventListener(_ type: String, _ listener: JSFunction) { guard var lns = self.listeners[type] else { return } lns.removeAll { $0 == listener } self.listeners[type] = lns } public func dispatchEvent(_ event: WebEvent) -> Bool { guard let lns = self.listeners[event.type] else { return true } for ln in lns { ln(event) } return true }