|
| 1 | +import 'package:launchdarkly_dart_common/launchdarkly_dart_common.dart' |
| 2 | + show ApplicationInfo; |
| 3 | + |
| 4 | +import '../config/defaults/credential_type.dart'; |
| 5 | +import '../hooks/hook.dart' show Hook; |
| 6 | + |
| 7 | +/// Metadata about a plugin implementation. |
| 8 | +/// |
| 9 | +/// May be used in logs and analytics to identify the plugin. |
| 10 | +final class PluginMetadata { |
| 11 | + /// The name of the plugin. |
| 12 | + final String name; |
| 13 | + |
| 14 | + const PluginMetadata({required this.name}); |
| 15 | + |
| 16 | + @override |
| 17 | + String toString() { |
| 18 | + return 'PluginMetadata{name: $name}'; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// Metadata about the SDK that is running the plugin. |
| 23 | +final class PluginSdkMetadata { |
| 24 | + /// The name of the SDK. |
| 25 | + final String name; |
| 26 | + |
| 27 | + /// The version of the SDK. |
| 28 | + final String version; |
| 29 | + |
| 30 | + /// If this is a wrapper SDK, then this is the name of the wrapper. |
| 31 | + final String? wrapperName; |
| 32 | + |
| 33 | + /// If this is a wrapper SDK, then this is the version of the wrapper. |
| 34 | + final String? wrapperVersion; |
| 35 | + |
| 36 | + const PluginSdkMetadata( |
| 37 | + {required this.name, |
| 38 | + required this.version, |
| 39 | + this.wrapperName, |
| 40 | + this.wrapperVersion}); |
| 41 | + |
| 42 | + @override |
| 43 | + String toString() { |
| 44 | + return 'PluginSdkMetadata{name: $name, version: $version,' |
| 45 | + ' wrapperName: $wrapperName, wrapperVersion: $wrapperVersion}'; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Information about the credential used to initialize the SDK. |
| 50 | +final class PluginCredentialInfo { |
| 51 | + /// The type of credential. |
| 52 | + final CredentialType type; |
| 53 | + |
| 54 | + /// The value of the credential. |
| 55 | + final String value; |
| 56 | + |
| 57 | + const PluginCredentialInfo({required this.type, required this.value}); |
| 58 | + |
| 59 | + @override |
| 60 | + String toString() { |
| 61 | + return 'PluginCredentialInfo{type: $type, value: $value}'; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Metadata about the environment where the plugin is running. |
| 66 | +final class PluginEnvironmentMetadata { |
| 67 | + /// Metadata about the SDK that is running the plugin. |
| 68 | + final PluginSdkMetadata sdk; |
| 69 | + |
| 70 | + /// Metadata about the application where the LaunchDarkly SDK is running. |
| 71 | + /// |
| 72 | + /// Plugins only have access to application info collected during |
| 73 | + /// configuration. Application information collected by environment reporting |
| 74 | + /// is not available. |
| 75 | + /// |
| 76 | + /// If access to the environment reporting information is required, then it |
| 77 | + /// is available via the [LDContext] by using hooks. |
| 78 | + /// |
| 79 | + /// Only present if any application information is available. |
| 80 | + final ApplicationInfo? application; |
| 81 | + |
| 82 | + /// Information about the credential used to initialize the SDK. |
| 83 | + final PluginCredentialInfo credential; |
| 84 | + |
| 85 | + PluginEnvironmentMetadata( |
| 86 | + {required this.sdk, required this.credential, this.application}); |
| 87 | + |
| 88 | + @override |
| 89 | + String toString() { |
| 90 | + return 'PluginEnvironmentMetadata{sdk: $sdk, credential: $credential,' |
| 91 | + ' application: $application}'; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Base class from which all plugins must derive. |
| 96 | +/// |
| 97 | +/// Implementation note: SDK packages must export a specialized version of this |
| 98 | +/// for their specific TClient type. This class cannot provide a type, because |
| 99 | +/// it would limit the API to methods available in the base client. |
| 100 | +abstract base class PluginBase<TClient> { |
| 101 | + /// Metadata associated with this plugin. |
| 102 | + /// |
| 103 | + /// Plugin implementations must implement this property. |
| 104 | + /// ```dart |
| 105 | + /// final _metadata = PluginMetadata(name: 'MyPluginName'); |
| 106 | + /// |
| 107 | + /// @override |
| 108 | + /// PluginMetadata get metadata => _metadata; |
| 109 | + /// ``` |
| 110 | + PluginMetadata get metadata; |
| 111 | + |
| 112 | + /// Registers the plugin with the SDK. Called once during SDK initialization. |
| 113 | + /// |
| 114 | + /// The SDK initialization will typically not have been completed at this |
| 115 | + /// point, so the plugin should take appropriate actions to ensure the SDK is |
| 116 | + /// ready before sending track events or evaluating flags. For example the |
| 117 | + /// plugin could wait for the [Hook.afterIdentify] stage to indicate success |
| 118 | + /// before tracking any events. |
| 119 | + /// |
| 120 | + /// The [client] the plugin is registered with. |
| 121 | + void register( |
| 122 | + TClient client, PluginEnvironmentMetadata environmentMetadata) {} |
| 123 | + |
| 124 | + /// Hooks which are bundled with this plugin. |
| 125 | + /// |
| 126 | + /// Implementations should override this method to return their bundled |
| 127 | + /// hooks. |
| 128 | + /// ```dart |
| 129 | + /// @override |
| 130 | + /// List<Hook> get hooks => [MyBundledHook()]; |
| 131 | + /// ``` |
| 132 | + List<Hook> get hooks => []; |
| 133 | + |
| 134 | + PluginBase(); |
| 135 | +} |
0 commit comments