2022-02-28 14:38:53 +00:00
|
|
|
import 'dart:collection';
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
|
|
import '../plugin.dart';
|
|
|
|
import 'runner.dart';
|
|
|
|
|
|
|
|
class PluginSandbox {
|
2022-03-01 03:22:39 +00:00
|
|
|
final LinkedHashMap<PluginType, PluginBuilder> _pluginBuilders = LinkedHashMap();
|
|
|
|
final Map<PluginType, PluginConfig> _pluginConfigs = <PluginType, PluginConfig>{};
|
2022-02-28 14:38:53 +00:00
|
|
|
late PluginRunner pluginRunner;
|
|
|
|
|
|
|
|
PluginSandbox() {
|
|
|
|
pluginRunner = PluginRunner();
|
|
|
|
}
|
|
|
|
|
2022-03-01 02:25:21 +00:00
|
|
|
int indexOf(PluginType pluginType) {
|
2022-03-01 03:22:39 +00:00
|
|
|
final index = _pluginBuilders.keys.toList().indexWhere((ty) => ty == pluginType);
|
2022-02-28 14:38:53 +00:00
|
|
|
if (index == -1) {
|
|
|
|
throw PlatformException(code: '-1', message: "Can't find the flowy plugin type: $pluginType");
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2022-03-01 02:25:21 +00:00
|
|
|
Plugin buildPlugin(PluginType pluginType, dynamic data) {
|
2022-03-01 03:22:39 +00:00
|
|
|
final plugin = _pluginBuilders[pluginType]!.build(data);
|
2022-02-28 14:38:53 +00:00
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2022-03-01 03:22:39 +00:00
|
|
|
void registerPlugin(PluginType pluginType, PluginBuilder builder, {PluginConfig? config}) {
|
|
|
|
if (_pluginBuilders.containsKey(pluginType)) {
|
2022-02-28 14:38:53 +00:00
|
|
|
throw PlatformException(code: '-1', message: "$pluginType was registered before");
|
|
|
|
}
|
2022-03-01 03:22:39 +00:00
|
|
|
_pluginBuilders[pluginType] = builder;
|
|
|
|
|
|
|
|
if (config != null) {
|
|
|
|
_pluginConfigs[pluginType] = config;
|
|
|
|
}
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 03:22:39 +00:00
|
|
|
List<int> get supportPluginTypes => _pluginBuilders.keys.toList();
|
|
|
|
|
|
|
|
List<PluginBuilder> get builders => _pluginBuilders.values.toList();
|
2022-02-28 14:38:53 +00:00
|
|
|
|
2022-03-01 03:22:39 +00:00
|
|
|
Map<PluginType, PluginConfig> get pluginConfigs => _pluginConfigs;
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|