register method
注册策略
可在 start 前或 start 后动态注册。若已 start,新注册的策略会立即启动。
Registers a strategy. If already started, the new strategy will be started immediately.
Implementation
void register(PlayerStrategy strategy) {
// 检查是否已注册同名策略
final existing = _strategies.indexWhere((s) => s.name == strategy.name);
if (existing != -1) {
logw(
'Strategy "${strategy.name}" already registered, replacing with new instance.',
tag: _tag,
);
// 停止旧策略
_safeStop(_strategies[existing]);
_strategies[existing] = strategy;
} else {
_strategies.add(strategy);
}
// 若已启动,立即启动新策略
if (_started && _controller != null) {
_safeStart(strategy, _controller!);
}
}