OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library plugin; |
| 6 |
| 7 /** |
| 8 * A function used to register the given [extension] to the extension point with |
| 9 * the given unique [identifier]. |
| 10 * |
| 11 * An [ExtensionError] will be thrown if the [extension] is not appropriate |
| 12 * for the extension point, such as an [extension] that does not implement the |
| 13 * required interface. |
| 14 */ |
| 15 typedef void RegisterExtension(String identifier, Object extension); |
| 16 |
| 17 /** |
| 18 * A function used to register an extension point with the given simple |
| 19 * [identifier]. If given, the [validator] will be used to validate extensions |
| 20 * to the extension point. |
| 21 * |
| 22 * An [ExtensionError] will be thrown if the extension point cannot be |
| 23 * registered, such as when a plugin attempts to define two extension points |
| 24 * with the same simple identifier. |
| 25 */ |
| 26 typedef ExtensionPoint RegisterExtensionPoint(String identifier, |
| 27 [ValidateExtension validateExtension]); |
| 28 |
| 29 /** |
| 30 * A function used by a plugin to validate an [extension] to a extension point. |
| 31 * |
| 32 * An [ExtensionError] should be thrown if the [extension] is not valid for the |
| 33 * extension point, such as an [extension] that does not implement the required |
| 34 * interface. |
| 35 */ |
| 36 typedef void ValidateExtension(Object extension); |
| 37 |
| 38 /** |
| 39 * An exception indicating that an error occurred while attempting to register |
| 40 * either an extension or an extension point. |
| 41 * |
| 42 * Clients are not expected to subtype this class. |
| 43 */ |
| 44 class ExtensionError implements Exception { |
| 45 /** |
| 46 * The message describing the error that occurred. |
| 47 */ |
| 48 final String message; |
| 49 |
| 50 /** |
| 51 * Initialize a newly created error to have the given message. |
| 52 */ |
| 53 ExtensionError(this.message); |
| 54 } |
| 55 |
| 56 /** |
| 57 * A representation of an extension point. |
| 58 * |
| 59 * Clients are not expected to subtype this class. |
| 60 */ |
| 61 abstract class ExtensionPoint { |
| 62 /** |
| 63 * Return an immutable list containing all of the extensions that were |
| 64 * registered for this extension point. |
| 65 */ |
| 66 List<Object> get extensions; |
| 67 |
| 68 /** |
| 69 * Return the plugin that defined this extension point. |
| 70 */ |
| 71 Plugin get plugin; |
| 72 |
| 73 /** |
| 74 * Return the identifier used to uniquely identify this extension point within |
| 75 * the defining plugin. |
| 76 */ |
| 77 String get simpleIdentifier; |
| 78 |
| 79 /** |
| 80 * Return the identifier used to uniquely identify this extension point. The |
| 81 * unique identifier is the identifier for the plugin, followed by a period |
| 82 * (`.`), followed by the [simpleIdentifier] for the extension point. |
| 83 */ |
| 84 String get uniqueIdentifier; |
| 85 } |
| 86 |
| 87 /** |
| 88 * A contribution to the host application that can extend the behavior of the |
| 89 * application while also allowing other plugins to extend it's behavior. |
| 90 * |
| 91 * Clients are expected to subtype this class when implementing plugins. |
| 92 */ |
| 93 abstract class Plugin { |
| 94 /** |
| 95 * Return the identifier used to uniquely identify this plugin. |
| 96 */ |
| 97 String get uniqueIdentifier; |
| 98 |
| 99 /** |
| 100 * Use the [register] function to register all of the extension points |
| 101 * contributed by this plugin. |
| 102 * |
| 103 * Clients should not invoke the [register] function after this method has |
| 104 * returned. |
| 105 */ |
| 106 void registerExtensionPoints(RegisterExtensionPoint register); |
| 107 |
| 108 /** |
| 109 * Use the [register] function to register all of the extensions contributed |
| 110 * by this plugin. |
| 111 * |
| 112 * Clients should not invoke the [register] function after this method has |
| 113 * returned. |
| 114 */ |
| 115 void registerExtensions(RegisterExtension register); |
| 116 |
| 117 /** |
| 118 * Return a unique identifier created from the unique identifier from the |
| 119 * [plugin] and the [simpleIdentifier]. |
| 120 */ |
| 121 static String buildUniqueIdentifier(Plugin plugin, String simpleIdentifier) => |
| 122 join(plugin.uniqueIdentifier, simpleIdentifier); |
| 123 |
| 124 /** |
| 125 * Return an identifier created by joining the [pluginIdentifier] and the |
| 126 * [simpleIdentifier]. |
| 127 */ |
| 128 static String join(String pluginIdentifier, String simpleIdentifier) => |
| 129 '$pluginIdentifier.$simpleIdentifier'; |
| 130 } |
OLD | NEW |