Giter Site home page Giter Site logo

signals's Introduction

Build Status License: MIT License

signals

Current version: 0.0.5

The Signals crate is an asynchronous functional-reactive-like api.

Installation

Add this to cargo.toml

[dependencies]
signals = "*"

Add this to your crate

extern crate signals;

Simple Example

use signals::{Signal, Emitter};

fn main() {
    // create a signal that will assert when emitted
    let signal = Signal::new_arc_mutex( |x: &u32| Ok(*x) );
    let listener = Signal::new_arc_mutex( |x: &u32| { assert_ne!(*x, 5); Ok(()) } ); //fail!

    // when signal is emitted, listener should execute.
    signal.lock().register_listener(&listener);

    // emit signal
    signal.lock().emit(&5);
}

Complex Example

use signals::{Signal, Emitter};

fn main() {
    // create a bunch of signals. At the last signal, we should fail.
    // If we do not fail, the signals did not work.
    let root = Signal::new_arc_mutex( |x: &u32| Ok(x.to_string()) ); //convert x to string.
    let peek = Signal::new_arc_mutex( |x: &String| { println!("Peek: {}", x); Ok(()) } );
    let to_i32 = Signal::new_arc_mutex( |x: &String| Ok(x.parse::<i32>()?) ); //convert to integer
    let inc = Signal::new_arc_mutex( |x: &i32| Ok(x+1) ); //increment value
    let fail = Signal::new_arc_mutex( |x: &i32| { assert_ne!(*x, 8); Ok(()) } ); //fail!

    //connect all signals together.
    root.lock().register_listener(&peek); // snoop on the value! - because we can!
    root.lock().register_listener(&to_i32); // parse the string to an integer
    to_i32.lock().register_listener(&inc); //increment the value
    inc.lock().register_listener(&fail); //then finally fail if the value is 8!

    root.lock().emit(&7); //7 will increment to 8 and fail!
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

signals's People

Contributors

zutils avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

signals's Issues

Add Examples

How can signals be used to generate a stream when done.
How can signals be used to talk across threads.
How can signals be used to do async io.

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.