Giter Site home page Giter Site logo

mediamonks / seng-signals Goto Github PK

View Code? Open in Web Editor NEW
1.0 6.0 0.0 225 KB

Singel event messaging system, ideal for hi-performance event system.

License: MIT License

JavaScript 40.20% HTML 1.08% TypeScript 58.73%
seng npm-package event-emitter signal

seng-signals's Introduction

Travis Code Climate Coveralls npm npm

seng-signals

Signal is a Observer Pattern that works on the basis that every event has its own controller, its more clear what a controller dispatches and its hi-performance.

Installation

npm

npm install --save seng-signals

other

We also have browser, amd, commonjs, umd, systemjs and es6 versions of this module available attached to the Github Releases.

manual

Check the build section below to see your you can build for all the targets yourself.

Usage

import {Signal, Signal1, Signal2} from 'seng-signals';

var signal = new Signal();
var connection = signal.connect(function(){
	console.log('emitted');
});

signal.emit(); // console.log('emitted');
signal.emit(); // console.log('emitted');
signal.emit(); // console.log('emitted');

// -----

connection.dispose();

var signal = new Signal();
var connection = signal.connect(function(){
	console.log('emitted');
});

connection.once();

signal.emit(); // console.log('emitted');
signal.emit(); // nothing
signal.emit(); // nothing

// -----

var signal = new Signal1();
var connection = signal.connect(function(value){
	console.log('emitted:', value);
});

signal.emit(1); // console.log('emitted:', 1);
signal.emit(2); // console.log('emitted:', 2);
signal.emit(3); // console.log('emitted:', 3);

// -----

var signal = new Signal2();
var connection = signal.connect(function(a, b){
	console.log('emitted:', a, b);
});

signal.emit('apple', 1); // console.log('emitted:', 'apple', 1);
signal.emit('pinapple', 2); // console.log('emitted:', 'apple', 1);

connection.dispose();
signal.disconnectAll();

signal.emit('sugar', 3); // nothing

// -----

var signal = new Signal();
signal.connect(function(a, b){
	console.log('emitted A');
}).once();

// put this listener on top of all others
signal.connect(function(){
	console.log('emitted B');
}, true).once();

signal.emit();

// -----

Usage with Class

This is all hypothetical fetch streaming code, this just gives a example of how to use signals in different setting.

class Loader {
  constructor(path){
    this.path = path;
    this.progress = new Signal1();
    this.complete = new Signal();
  }
  
  load(){
    var _this = this;
    fetch(this.path).then(response => {
      var reader = response.body.getReader();
      var bytesReceived = 0;
      var totalBytes = 1000000;
      
    
      reader.read().then(function processResult(result) {

        if (result.done) {
          _this.complete.emit();
          return;
        }
    
        bytesReceived += result.value.length;
        
        _this.progress.emit(bytesReceived/totalBytes);
        return reader.read().then(processResult);
      });
    });
  }
}

Documentation

View the generated documentation.

Building

In order to build signals, ensure that you have Git and Node.js installed.

Clone a copy of the repo:

git clone https://github.com/mediamonks/seng-signals.git

Change to the signals directory:

cd seng-signals

Install dev dependencies:

npm install

Use one of the following main scripts:

npm run build           # build this project
npm run dev             # run dev-watch mode, seving example/index.html in the browser
npm run generate        # generate all artifacts (compiles ts, webpack, docs and coverage)
npm run test-unit       # run the unit tests
npm run validate        # runs validation scripts, including test, lint and coverage check
npm run lint            # run tslint on this project
npm run doc             # generate typedoc documentation
npm run typescript-npm  # just compile the typescript output used in the npm module

When installing this module, it adds a pre-push hook, that runs the validate script before committing, so you can be sure that everything checks out.

If you want to create the distribution files yourself, you can run the build-dist script, and the following files will get generated in the dist folder:

  • /dist/seng-signals.js: bundled with webpack, can be loaded from a script tag, available as window.SengSignals
  • /dist/seng-signals.min.js: same as above, but minified
  • /dist/seng-signals-amd.js: bundled with webpack, can be used with e.g. requirejs
  • /dist/seng-signals-commonjs.js: bundled with webpack, can be used in systems that support commonjs, but you should just use npm
  • /dist/seng-signals-umd.js: bundled with webpack, works in the browser, with requirejs, and in a commonjs system
  • /dist/seng-signals-umd.min.js: same as above, but minified
  • /dist/seng-signals-system.js: bundled with typescript, can be used in systems that support systemjs
  • /dist/seng-signals-es6.zip: transpiled with typescript, only types are removed from the source files

Contribute

View CONTRIBUTING.md

Changelog

View CHANGELOG.md

Authors

View AUTHORS.md

LICENSE

MIT

seng-signals's People

Contributors

mientjan avatar thanarie avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

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.