-
Notifications
You must be signed in to change notification settings - Fork 113
/
FeatureMessageReceiver.swift
67 lines (59 loc) · 2.96 KB
/
FeatureMessageReceiver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-Present Datadog, Inc.
*/
import Foundation
/// The `FeatureMessageReceiver` defines an interface for a Feature to receive messages
/// from a bus that is shared between Features registered to same instance of the core.
///
/// The message is composed of a key and a dictionary of attributes. The message format is a loose
/// agreement between Features - all messages supported by a Feature should be properly documented.
public protocol FeatureMessageReceiver {
/// Receives messages from the message bus.
///
/// The message can be used to build an event or execute custom routine in the Feature.
///
/// This method is always called on the same thread managed by core. If the implementation
/// of `FeatureMessageReceiver` needs to manage a state it can consider its mutations started
/// from `receive(message:from:)` to be thread-safe. The implementation should be mindful of
/// not blocking the caller thread to not delay processing of other messages in the system.
///
/// - Parameters:
/// - message: The message.
/// - core: An instance of the core from which the message is transmitted.
/// - Returns: `true` if the message was processed by the receiver;`false` if it was ignored.
@discardableResult
func receive(message: FeatureMessage, from core: DatadogCoreProtocol) -> Bool
// ^ TODO: RUM-3717
// Remove `core:` parameter from this API once all features are migrated to depend on `FeatureScope` interface
// instead of depending on directly on `core`.
}
public struct NOPFeatureMessageReceiver: FeatureMessageReceiver {
public init() { }
/// no-op: returns `false`
public func receive(message: FeatureMessage, from core: DatadogCoreProtocol) -> Bool {
return false
}
}
public struct CombinedFeatureMessageReceiver: FeatureMessageReceiver {
let receivers: [FeatureMessageReceiver]
/// Creates an instance initialized with the given receivers.
public init(_ receivers: FeatureMessageReceiver...) {
self.receivers = Array(receivers)
}
/// Creates an instance initialized with the given receivers.
public init(_ receivers: [FeatureMessageReceiver]) {
self.receivers = receivers
}
/// Receiving a message will loop though receivers and stop on the first that is able to
/// consume the given message.
///
/// - Parameters:
/// - message: The message.
/// - core: An instance of the core from which the message is transmitted.
/// - Returns: `true` if the message was processed by one of the receiver; `false` if it was ignored.
public func receive(message: FeatureMessage, from core: DatadogCoreProtocol) -> Bool {
receivers.contains(where: { $0.receive(message: message, from: core) })
}
}