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

套件詳情v2.0.1

event_bus

一個使用 Dart Streams 的簡單事件總線,用於解耦應用程式

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
35.6萬
喜歡數
749
Pub 分數
150

安裝套件

flutter pub add event_bus

套件介紹

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

事件總線是一個輕量級的 Dart 套件,使用 Streams 來實現應用程式中元件之間的事件驅動通訊。它透過允許物件發布事件並訂閱事件來促進鬆散耦合,而不需直接依賴。非常適合協調應用程式不同部分的操作,例如 UI 更新、狀態變更或跨模組互動。該套件容易整合,並可在 Flutter 支援的所有主要平台無縫運行。

適用情境

  • 解耦UI組件
  • 處理全域狀態變更
  • 廣播應用範圍的通知
  • 簡化模組間通訊
  • 管理非同步事件
  • 減少狀態管理中的樣板程式碼

主要特色

  • 基於流的事件傳遞
  • 簡單的發佈/訂閱模式
  • 跨平台相容性
  • 最小 API 表面
  • 支援類型化事件
  • 無外部依賴

適合對象

  • 需要鬆耦合的應用程式
  • 開發人員避免使用複雜的狀態管理器
  • 頻繁進行組件間通訊的項目
  • 快速事件流原型設計
  • 傾向於簡單性而非設定的團隊

使用建議

  • 不適合大規模狀態管理
  • 有限的內建錯誤處理
  • 需要仔細命名事件以避免衝突
  • 不會在應用重新啟動後保留事件
  • 最適合搭配其他狀態管理解決方案,用於複雜應用

原始 README

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

Event Bus

A simple Event Bus using Dart Streams for decoupling applications.

Star this Repo Pub Package

GitHub | Pub | Demos and Examples

Event Bus Pattern

An Event Bus follows the publish/subscribe pattern. It allows listeners to subscribe for events and publishers to fire events. This enables objects to interact without requiring to explicitly define listeners and keeping track of them.

Event Bus and MVC

The Event Bus pattern is especially helpful for decoupling MVC (or MVP) applications.

One group of MVC is not a problem.

Model-View-Controller

But as soon as there are multiple groups of MVCs, those groups will have to talk to each other. This creates a tight coupling between the controllers.

Multi Model-View-Controllers

By communication through an Event Bus, the coupling is reduced.

Event Bus

Usage

1. Create an Event Bus

Create an instance of EventBus and make it available to other classes.

Usually there is just one Event Bus per application, but more than one may be set up to group a specific set of events.

import 'package:event_bus/event_bus.dart';

EventBus eventBus = EventBus();

Note: The default constructor will create an asynchronous event bus. To create a synchronous bus you must provide the optional sync: true attribute.

2. Define Events

Any Dart class can be used as an event.

class UserLoggedInEvent {
  User user;

  UserLoggedInEvent(this.user);
}

class NewOrderEvent {
  Order order;

  NewOrderEvent(this.order);
}
3. Register Listeners

Register listeners for specific events:

eventBus.on<UserLoggedInEvent>().listen((event) {
  // All events are of type UserLoggedInEvent (or subtypes of it).
  print(event.user);
});

Register listeners for all events:

eventBus.on().listen((event) {
  // Print the runtime type. Such a set up could be used for logging.
  print(event.runtimeType);
});
About Dart Streams

EventBus uses Dart Streams as its underlying mechanism to keep track of listeners. You may use all functionality available by the Stream API. One example is the use of StreamSubscriptions to later unsubscribe from the events.

StreamSubscription loginSubscription = eventBus.on<UserLoggedInEvent>().listen((event) {
  print(event.user);
});

loginSubscription.cancel();
4. Fire Events

Finally, we need to fire an event.

User myUser = User('Mickey');
eventBus.fire(UserLoggedInEvent(myUser));

Using Custom Stream Controllers

Instead of using the default StreamController you can use the following constructor to provide your own.

An example would be to use an RxDart Subject as the controller.

import 'package:rxdart/rxdart.dart';

EventBus behaviorBus = EventBus.customController(BehaviorSubject());

Running / Building / Testing

  • Run from the terminal: webdev serve
  • Build from the terminal: webdev build
  • Testing: pub run build_runner test -- -p chrome

License

The MIT License (MIT)

所屬分類

原始碼儲存庫

使用此套件的專案