order_book_entry.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /// 订单簿单条(不可变)
  2. /// 实现 == / hashCode 让 .select() 能正确跳过无变化的重建。
  3. class OrderBookEntry {
  4. final double price;
  5. final double amount;
  6. final double total; // 累计量
  7. const OrderBookEntry({
  8. required this.price,
  9. required this.amount,
  10. required this.total,
  11. });
  12. @override
  13. bool operator ==(Object other) =>
  14. identical(this, other) ||
  15. other is OrderBookEntry &&
  16. price == other.price &&
  17. amount == other.amount &&
  18. total == other.total;
  19. @override
  20. int get hashCode => Object.hash(price, amount, total);
  21. }
  22. /// 订单簿(买盘 + 卖盘)
  23. /// 实现 == / hashCode,让 .select((s) => s.orderBook) 能正确跳过无变化的重建。
  24. /// 依赖 OrderBookEntry 已实现的 == 做逐元素深比较。
  25. class OrderBook {
  26. final List<OrderBookEntry> asks; // 卖盘(从低到高)
  27. final List<OrderBookEntry> bids; // 买盘(从高到低)
  28. const OrderBook({required this.asks, required this.bids});
  29. @override
  30. bool operator ==(Object other) {
  31. if (identical(this, other)) return true;
  32. if (other is! OrderBook) return false;
  33. if (asks.length != other.asks.length || bids.length != other.bids.length) {
  34. return false;
  35. }
  36. for (var i = 0; i < asks.length; i++) {
  37. if (asks[i] != other.asks[i]) return false;
  38. }
  39. for (var i = 0; i < bids.length; i++) {
  40. if (bids[i] != other.bids[i]) return false;
  41. }
  42. return true;
  43. }
  44. @override
  45. int get hashCode => Object.hash(
  46. Object.hashAll(asks), Object.hashAll(bids));
  47. }