Git Product home page Git Product logo

signals's Introduction

Build Status

Signals for Dart

Signals are a light-weight, strongly-typed way to implement the observer pattern. They allow you to explicitly define the messages your objects provide, without relying on metadata or documentation.

The Signal object at a basic level is a list of methods to be invoked when the Signal is dispatched.

To get started, first add blix_signals to your pubspec.yaml

dependencies: blix_signals: any

Usage Example:

 import 'package:blix_signals/signals.dart';

 class MilkEvent {
 	num amount;
 	num duration;
 	MilkEvent(this.amount, this.duration);
 }

 abstract class IMilkable {
 	ISignal<MilkEvent> get milked;
 }

 class Cow implements IMilkable {
 	Signal<MilkEvent> _milked = new Signal<MilkEvent>();

 	ISignal<MilkEvent> get milked => _milked;

 	void milk() {
 		if (_milked.isNotEmpty) {
 			_milked.dispatch(new MilkEvent(1, 1));
 		}
 	}
 }

 class DeliveryPerson {
 	void watchMilkable(IMilkable milkable) {
 		milkable.milked.add((MilkEvent e) => fillBottle(e.amount));
 	}

 	void fillBottle(int amount) {
 		print("Added ${amount} milk");
 	}
 }

 void main() {
 	var cow = new Cow();
 	var deliveryPerson = new DeliveryPerson();
 	deliveryPerson.watchMilkable(cow);
 	cow.milk(); // added 1 milk
 }

Three conventions should be pointed out:

  1. The signal is described in past tense.

  2. The dispatch is surrounded by an isNotEmpty check. This is only important for performance if the message requires work to be done. That is, if the message requires a new object to be created, it is best to optimize for the case if there are no handlers.

  3. The exposed interface is using ISignal instead of Signal. The ISignal interface does not expose the dispatch(T) function. This is because it's generally a bad practice to broadcast a message on behalf of another object.

signals's People

Contributors

nbilyk avatar

Watchers

 avatar  avatar

Forkers

pangor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.