搜尋套件、儲存庫與 APP 分類

套件詳情v3.4.0

queue

從多個來源排隊非同步操作,並在程式碼中的任何位置等待其回傳。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
9.6萬
喜歡數
117
Pub 分數
160

安裝套件

flutter pub add queue

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

隊列套件讓開發者能以彈性、集中化的方式管理並等候來自不同來源的多個非同步操作(未來物件)。它允許在程式碼的一個點排隊未來物件,並在其他地方等待其結果,進而提升對非同步執行流程的控制。適用於需要協調非同步任務的場景,例如批次處理或順序資料擷取。此套件輕量且相容所有主要平台,適合跨平台應用。透過將工作建立與結果處理分離,提升程式碼可讀性。

適用情境

  • 非同步操作的批次處理
  • 協調多個 API 呼叫
  • 異步任務的延遲執行
  • 在UI元件中管理非同步工作流程
  • 跨模組同步資料加載

主要特色

  • 集中式未來佇列
  • 從任何位置等待未來
  • 支援多個非同步來源
  • 跨平台相容性
  • 最小化樣板程式碼
  • 執行緒安全作業

適合對象

  • 開發人員管理複雜的異步流程
  • 需要有序異步執行的應用程式
  • 使用響應式或狀態管理模式的團隊
  • 針對多個平台的專案
  • 具有高異步複雜性的程式碼庫

使用建議

  • 不適用於即時串流
  • 需要仔細管理佇列生命週期
  • 使用大隊列時可能會增加記憶體使用量
  • 最適合有限且可預測的異步序列
  • 不處理重試邏輯或內部錯誤恢復

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

queue

Build Status

Easily queue futures and await their values.

This library allows you to send a future to central queue. The queue will execute the futures in the order they are queued and once the future is complete it will return its result.

My use case was to rate limit calls to bluetooth devices. There were multiple bluetooth devices connected that may have different commands being sent from lots of areas in the app. The devices were tripping over themselves and not responding. A stream wasn't the appropriate tool as I needed to get the result back. Hence a library was born.

Alternative use cases could be spidering a website, downloading a number of files, or rate limiting calls to an API.

Usage

The most simple example:

import 'package:queue/queue.dart';

main() async {
  final queue = Queue();

  //Queue up a future and await its result
  final result = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

  //Thats it!
}

A proof of concept:

import 'package:queue/queue.dart';

main() async {
  //Create the queue container
  final Queue queue = Queue(delay: Duration(milliseconds: 10));

  //Add items to the queue asyncroniously
  queue.add(()=>Future.delayed(Duration(milliseconds: 100)));
  queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

  //Get a result from the future in line with await
  final result = await queue.add(() async {
    await Future.delayed(Duration(milliseconds: 1));
    return "Future Complete";
  });

  //100, 10, 1 will reslove in that order.
  result == "Future Complete"; //true
}
Parallel processing

This doesn't work in batches and will fire the next item as soon as as there is space in the queue Use [Queue(delayed: ...)] to specify a delay before firing the next item

import 'package:queue/queue.dart';

main() async {
  final queue = Queue(parallel: 2);

  //Queue up a future and await its result
  final result1 = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
  final result2 = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

  //Thats it!
}
On complete
import 'package:queue/queue.dart';

main() async {
  final queue = Queue(parallel: 2);

  //Queue up a couple of futures
  queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
  queue.add(()=>Future.delayed(Duration(milliseconds: 10)));


  // Will only resolve when all the queue items have resolved.
  await queue.onComplete;
}
Rate limiting

You can specify a delay before the next item is fired as per the following example:

import 'package:queue/queue.dart';

main() async {
  final queue = Queue(delay: Duration(milliseconds: 500)); // Set the delay here

  //Queue up a future and await its result
  final result1 = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
  final result2 = await queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

  //Thats it!
}
LIFO (Last In First Out)

You can configure the queue to process items in LIFO order (last in, first out) by setting the lifo parameter:

import 'package:queue/queue.dart';

main() async {
  // Create a LIFO queue
  final queue = Queue(lifo: true);

  // Add items to the queue
  queue.add(() => Future.delayed(Duration(milliseconds: 100)));  // Will be processed last
  queue.add(() => Future.delayed(Duration(milliseconds: 50)));   // Will be processed second
  queue.add(() => Future.delayed(Duration(milliseconds: 10)));   // Will be processed first
}
Adding items to the front of the queue

You can add items to the front of the queue to process them sooner using the addToFront parameter:

import 'package:queue/queue.dart';

main() async {
  final queue = Queue();

  // Add a regular item to the queue
  queue.add(() => Future.delayed(Duration(milliseconds: 100)));
  
  // Add an urgent item that should be processed next
  queue.add(
    () => Future.delayed(Duration(milliseconds: 50)),
    addToFront: true
  );
}
Cancel

If you need to stop a queue from processing call Queue.cancel();

This will cancel the remaining items in the queue by throwing a QueueCancelledException. A cancelled queue is "dead" and should be recreated. If you try adding items to the queue after you call cancel, it will throw a QueueCancelledException.

If you have no reason to listen to the results of the items, simply call dispose.

If you want to wait until all the items which are inflight complete, call Queue.onComplete first.

Disposing

If you need to dispose of the queue object (best practice in flutter any any time the queue object will close) simply call queue.dispose();

This is necessary to close the Queue.remainingItems controller.

Reporting

If you want to query how many items are outstanding in the queue once, you can use the getter remainingItemCount.

import 'package:queue/queue.dart';
final queue = Queue();

final count = queue.remainingItemCount;

If you want to get notified when the remainingItemCount changes, listen to the Queue.remainingItems stream.

import 'package:queue/queue.dart';

final queue = Queue();
final remainingItemsStream = queue.remainingItems.listen((numberOfItems)=>print(numberOfItems));

//Queue up a couple of futures
queue.add(()=>Future.delayed(Duration(milliseconds: 10)));
queue.add(()=>Future.delayed(Duration(milliseconds: 10)));

// Will only resolve when all the queue items have resolved.
await queue.onComplete;
remainingItemsStream.close();
queue.dispose(); // Will clean up any resources in the queue if you are done with it.

Contributing

Pull requests are welcome. There is a shell script ci_checks.sh that will run the checks to get past CI and also format the code before committing. If that all passes your PR will likely be accepted.

Please write tests to cover your new feature.

所屬分類

原始碼儲存庫

使用此套件的專案

所在榜單