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 02:25:21 +00:00
|
|
|
final LinkedHashMap<PluginType, PluginBuilder> _pluginMap = LinkedHashMap();
|
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-02-28 14:38:53 +00:00
|
|
|
final index = _pluginMap.keys.toList().indexWhere((ty) => ty == pluginType);
|
|
|
|
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) {
|
|
|
|
final plugin = _pluginMap[pluginType]!.build(data);
|
2022-02-28 14:38:53 +00:00
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2022-03-01 02:25:21 +00:00
|
|
|
void registerPlugin(PluginType pluginType, PluginBuilder builder) {
|
2022-02-28 14:38:53 +00:00
|
|
|
if (_pluginMap.containsKey(pluginType)) {
|
|
|
|
throw PlatformException(code: '-1', message: "$pluginType was registered before");
|
|
|
|
}
|
|
|
|
_pluginMap[pluginType] = builder;
|
|
|
|
}
|
|
|
|
|
2022-03-01 02:25:21 +00:00
|
|
|
List<int> get supportPluginTypes => _pluginMap.keys.toList();
|
2022-02-28 14:38:53 +00:00
|
|
|
|
|
|
|
List<PluginBuilder> get builders => _pluginMap.values.toList();
|
|
|
|
}
|