-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cca3200
commit e2cd7b4
Showing
5 changed files
with
90 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,85 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:meta/meta.dart'; | ||
import 'package:nyxx/src/api_options.dart'; | ||
import 'package:nyxx/src/client.dart'; | ||
import 'package:nyxx/src/client_options.dart'; | ||
|
||
/// Provides access to the connection and closing process for implementing plugins. | ||
abstract class NyxxPlugin { | ||
abstract class NyxxPlugin<ClientType extends Nyxx> { | ||
/// The name of this plugin. | ||
String get name; | ||
|
||
late final Expando<NyxxPluginState<ClientType, NyxxPlugin<ClientType>>> _states = Expando('$name plugin states'); | ||
|
||
/// Perform the connection operation. | ||
/// | ||
/// The function passed as an argument should be called to obtain the underlying client. | ||
Future<ClientType> connect<ClientType extends Nyxx>(ApiOptions apiOptions, ClientOptions clientOptions, Future<ClientType> Function() connect) => connect(); | ||
/// People overriding this method should call it to obtain the client instance. | ||
@mustCallSuper | ||
Future<ClientType> doConnect(ApiOptions apiOptions, ClientOptions clientOptions, Future<ClientType> Function() connect) async { | ||
final state = await createState(); | ||
await state.beforeConnect(apiOptions, clientOptions); | ||
|
||
final client = await connect(); | ||
_states[client] = state; | ||
|
||
await state.afterConnect(client); | ||
return client; | ||
} | ||
|
||
/// Perform the close operation. | ||
/// | ||
/// The function passed as an argument should be called to close the underlying client. | ||
Future<void> close(Nyxx client, Future<void> Function() close) => close(); | ||
/// People overriding this method should call it to obtain the client instance. | ||
Future<void> doClose(ClientType client, Future<void> Function() close) async { | ||
final state = _states[client]; | ||
await state?.beforeClose(client); | ||
|
||
await close(); | ||
_states[client] = null; | ||
|
||
await state?.afterClose(); | ||
} | ||
|
||
/// Called to create the state for this plugin. | ||
/// | ||
/// Each plugin creates a state for each client is attached to. States can contain mutable fields that can be updated at any time without affecting other | ||
/// instances of the plugin attached to other clients. | ||
FutureOr<NyxxPluginState<ClientType, NyxxPlugin<ClientType>>> createState() => NyxxPluginState(this); | ||
|
||
/// Called before each client this plugin is added to connects. | ||
FutureOr<void> beforeConnect(ApiOptions apiOptions, ClientOptions clientOptions) {} | ||
|
||
/// Called after each client this plugin is added to connects. | ||
FutureOr<void> afterConnect(ClientType client) {} | ||
|
||
/// Called before each client this plugin is added to closes. | ||
FutureOr<void> beforeClose(ClientType client) {} | ||
|
||
/// Called after each client this plugin is added to closes. | ||
FutureOr<void> afterClose() {} | ||
} | ||
|
||
/// Holds the state of a plugin added to a client. | ||
class NyxxPluginState<ClientType extends Nyxx, PluginType extends NyxxPlugin<ClientType>> { | ||
/// The plugin this state belongs to. | ||
final PluginType plugin; | ||
|
||
/// Create a new plugin state. | ||
NyxxPluginState(this.plugin); | ||
|
||
/// Called before each client this plugin is added to connects. | ||
@mustCallSuper | ||
FutureOr<void> beforeConnect(ApiOptions apiOptions, ClientOptions clientOptions) => plugin.beforeConnect(apiOptions, clientOptions); | ||
|
||
/// Called after each client this plugin is added to connects. | ||
@mustCallSuper | ||
FutureOr<void> afterConnect(ClientType client) => plugin.afterConnect(client); | ||
|
||
/// Called before each client this plugin is added to closes. | ||
@mustCallSuper | ||
FutureOr<void> beforeClose(ClientType client) => plugin.beforeClose(client); | ||
|
||
/// Called after each client this plugin is added to closes. | ||
@mustCallSuper | ||
FutureOr<void> afterClose() => plugin.afterClose(); | ||
} |