Skip to content

Commit

Permalink
Add PreLoadedPlugin mixin for pre-initialized plugins (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanpodila committed Sep 18, 2024
2 parents 06150c7 + 0b8d6d4 commit 3ffa6da
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 183 deletions.
10 changes: 5 additions & 5 deletions packages/sanity/sanity_client/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ final class SanityConfig {
bool? useCdn,
Perspective? perspective,
bool? explainQuery,
}) : this.useCdn = useCdn ?? true,
this.apiVersion = apiVersion ?? defaultApiVersion,
this.perspective = perspective ?? Perspective.raw,
this.explainQuery = explainQuery ?? false {
assert(this.token.trim().isNotEmpty, '''
}) : useCdn = useCdn ?? true,
apiVersion = apiVersion ?? defaultApiVersion,
perspective = perspective ?? Perspective.raw,
explainQuery = explainQuery ?? false {
assert(token.trim().isNotEmpty, '''
Invalid Token provided.
Setup an API token, with Viewer access, in the Sanity Management Console.
Without a valid token you will not be able to fetch data from Sanity.''');
Expand Down
12 changes: 6 additions & 6 deletions packages/sanity/sanity_client/test/url_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import 'package:sanity_client/sanity_client.dart';
import 'util.dart';

void main() {
[
for (var ref in [
'invalid-ref',
'imgInvalid-invalid-400x300-jpg',
].forEach((ref) {
]) {
test('Invalid image-reference ($ref) throws exception', () {
expect(
() {
Expand All @@ -16,7 +16,7 @@ void main() {
throwsA(const TypeMatcher<InvalidReferenceException>()),
);
});
});
}

test('Valid image-reference generates proper image-url', () {
final url = getClient()
Expand All @@ -26,10 +26,10 @@ void main() {
expect(url, contains('cdn.sanity.io/images/$project/$dataset/'));
});

[
for (var ref in [
'invalid-ref',
'fileInvalid-invalid-jpg',
].forEach((ref) {
]) {
test('Invalid file-reference ($ref) throws exception', () {
expect(
() {
Expand All @@ -38,7 +38,7 @@ void main() {
throwsA(const TypeMatcher<AssertionError>()),
);
});
});
}

test('Valid file-reference generates proper file-url', () {
final url = getClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import 'package:flutter/widgets.dart';
import 'package:vyuh_core/vyuh_core.dart';

/// The default implementation for an Analytics Plugin.
final class AnalyticsPlugin extends Plugin implements AnalyticsProvider {
final class AnalyticsPlugin extends Plugin
with PreloadedPlugin
implements AnalyticsProvider {
/// The list of providers for the plugin.
final List<AnalyticsProvider> providers;

Expand All @@ -19,7 +21,6 @@ final class AnalyticsPlugin extends Plugin implements AnalyticsProvider {
: super(
name: 'vyuh.plugin.analytics',
title: 'Analytics Plugin',
pluginType: PluginType.analytics,
);

@override
Expand Down
2 changes: 1 addition & 1 deletion packages/system/vyuh_core/lib/plugin/auth/auth_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class AuthPlugin<TUser extends User> extends Plugin {

/// Creates an instance of [AuthPlugin].
AuthPlugin({required super.name, required super.title})
: super(pluginType: PluginType.auth);
: super();

/// The current user that is signed in.
TUser get currentUser => throw UnimplementedError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class ContentPlugin extends Plugin {
required this.provider,
required super.name,
required super.title,
}) : super(pluginType: PluginType.content);
}) : super();

/// The type registry that maps types to their descriptors
Map<Type, Map<String, TypeDescriptor>> get typeRegistry;
Expand Down
5 changes: 2 additions & 3 deletions packages/system/vyuh_core/lib/plugin/di/di_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import 'package:vyuh_core/vyuh_core.dart';
/// It is associated with the Dependency Injection (DI) Plugin type and
/// includes methods for registering, unregistering, retrieving, and
/// checking the existence of instances in the DI container.
abstract class DIPlugin extends Plugin {
abstract class DIPlugin extends Plugin with PreloadedPlugin {
/// The `DIPlugin` constructor accepts two required parameters: `name` and `title`.
DIPlugin({required super.name, required super.title})
: super(pluginType: PluginType.di);
DIPlugin({required super.name, required super.title}) : super();

/// Registers an instance of any Object type with the DI container.
void register<T extends Object>(T instance, {String? name});
Expand Down
2 changes: 1 addition & 1 deletion packages/system/vyuh_core/lib/plugin/feature_flag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class FeatureFlagPlugin<TSettings, TContext> extends Plugin {
required this.settings,
required super.name,
required super.title,
}) : super(pluginType: PluginType.featureFlag);
}) : super();

/// Returns the value of the feature flag as a boolean.
Future<bool> getBool(String featureName, {bool defaultValue = false});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vyuh_core/vyuh_core.dart';

abstract class LoggerPlugin extends Plugin {
LoggerPlugin({required super.name, required super.title})
: super(pluginType: PluginType.logger);
: super();

/// trace
void t(dynamic message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class NavigationPlugin extends Plugin {
);

NavigationPlugin({required super.name, required super.title})
: super(pluginType: PluginType.navigation);
: super();

void initRouter({
String? initialLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import 'dart:convert';
import 'package:http/http.dart';
import 'package:vyuh_core/vyuh_core.dart';

abstract class NetworkPlugin extends Plugin implements Client {
NetworkPlugin({required super.name, required super.title})
: super(pluginType: PluginType.network);
abstract class NetworkPlugin extends Plugin
with PreloadedPlugin
implements Client {
NetworkPlugin({required super.name, required super.title}) : super();

@override
Future<Response> get(Uri url, {Map<String, String>? headers});
Expand Down
23 changes: 7 additions & 16 deletions packages/system/vyuh_core/lib/plugin/plugin.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
enum PluginType {
content,
di,
analytics,
featureFlag,
navigation,
logger,
storage,
secureStorage,
network,
auth,
notifications,
ads,
}
/// A mixin to mark any plugin to be loaded before the Platform.
///
/// This mixin should be applied to plugins that need to be initialized
/// before the main platform initialization. It ensures that the
/// plugin is loaded at the correct time in the initialization sequence.
mixin PreloadedPlugin on Plugin {}

abstract class Plugin {
final String name;
final String title;
final PluginType pluginType;

Plugin({required this.pluginType, required this.name, required this.title});
Plugin({required this.name, required this.title});

Future<void> init();
Future<void> dispose();
Expand Down
42 changes: 42 additions & 0 deletions packages/system/vyuh_core/lib/plugin/plugin_descriptor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:vyuh_core/plugin/auth/anonymous_auth_plugin.dart';
import 'package:vyuh_core/vyuh_core.dart';

final class PluginDescriptor {
final Set<Plugin> _plugins = {};

List<Plugin> get plugins => List.unmodifiable(_plugins);

PluginDescriptor({
final DIPlugin? di,
final ContentPlugin? content,
final AnalyticsPlugin? analytics,
final NetworkPlugin? network,
final AuthPlugin? auth,
final NavigationPlugin? navigation,
final List<Plugin>? others,
}) {
_plugins.addAll([
di ?? defaultPlugins.get<DIPlugin>(),
content ?? defaultPlugins.get<ContentPlugin>(),
analytics ?? defaultPlugins.get<AnalyticsPlugin>(),
network ?? defaultPlugins.get<NetworkPlugin>(),
auth ?? defaultPlugins.get<AuthPlugin>(),
navigation ?? defaultPlugins.get<NavigationPlugin>(),
]);

_plugins.addAll(others ?? []);
}

static final defaultPlugins = PluginDescriptor(
di: GetItDIPlugin(),
content: NoOpContentPlugin(),
analytics: AnalyticsPlugin(providers: [NoOpAnalyticsProvider()]),
network: HttpNetworkPlugin(),
auth: UnknownAuthPlugin(),
navigation: DefaultNavigationPlugin(),
);

Plugin get<T>() {
return _plugins.firstWhere((element) => element is T);
}
}
4 changes: 2 additions & 2 deletions packages/system/vyuh_core/lib/plugin/storage_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:vyuh_core/vyuh_core.dart';

abstract class StoragePlugin extends Plugin {
StoragePlugin({required super.name, required super.title})
: super(pluginType: PluginType.storage);
: super();

Future<dynamic> read(String key);
Future<dynamic> write(String key, dynamic value);
Expand All @@ -12,7 +12,7 @@ abstract class StoragePlugin extends Plugin {

abstract class SecureStoragePlugin extends Plugin {
SecureStoragePlugin({required super.name, required super.title})
: super(pluginType: PluginType.secureStorage);
: super();

Future<dynamic> read(String key);
Future<dynamic> write(String key, dynamic value);
Expand Down
Loading

0 comments on commit 3ffa6da

Please sign in to comment.