import 'dart:collection';
import 'dart:ui';

///
/// Created by @Haoyi on 2021/7/18
///

class FrameAction {
  final double delayed;
  final VoidCallback callback;
  double _elapsedInMillis = 0;

  FrameAction(this.callback, {int delayedInMillis = 0}) : delayed = delayedInMillis / 1000.0;

  bool _tick(double t) {
    _elapsedInMillis += t;
    return _elapsedInMillis >= delayed;
  }

  void process() {
    callback.call();
  }
}

class FrameActionQueue {
  final DoubleLinkedQueue<FrameAction> actions = DoubleLinkedQueue();

  void dispatchFrameAction(double t) {
    while (actions.isNotEmpty) {
      final entry = actions.firstEntry();
      if (entry != null && entry.element._tick(t)) {
        entry.element.process();
        entry.remove();
      }
    }
  }

  void deliverFrameAction(FrameAction action) {
    actions.addLast(action);
  }
}
