1.0.9 • Published 1 year ago

mani-signal v1.0.9

Weekly downloads
1
License
MIT
Repository
github
Last release
1 year ago

mani-signal

a simple and very fast signal class

Build

$ npm install
$ npm run build

Test

$ npm run test

Usage

$ npm install mani-signal --save

without parameter

import {Signal} from 'mani-signal';

const signal = new Signal();

signal.add(()=> {
    console.log('signal callback');
});

signal.dispatch();

with parameter

import {Signal} from 'mani-signal';

const signal = new Signal<string>();

signal.add(param => {
    console.log(`signal callback with ${param}`);
});

signal.dispatch('hello world');

listen once

import {Signal} from 'mani-signal';

const signal = new Signal();

signal.addOnce(() => {
    console.log(`will be called once`);
});

signal.dispatch();
signal.dispatch();

detach listener

import {Signal} from 'mani-signal';

const signal = new Signal();

const binding = signal.add(() => {
    console.log(`won't be called`);
});

binding.detach();

signal.dispatch();

detach all

import {Signal} from 'mani-signal';

const signal = new Signal();

signal.add(() => {
    console.log(`won't be called`);
});
signal.add(() => {
    console.log(`won't be called`);
});

signal.detachAll();

signal.dispatch();

with context

import {Signal} from 'mani-signal';

class Foo {
    value = 'Bar';

    constructor() {
        const signal = new Signal();

        // pass the context as second parameter
        signal.add(this.handler, this);

        signal.dispatch();
    }

    handler() {
        // accessing 'this' within a callback
        console.log(this.value);
    }

}

deactivate/activate binding

import {Signal} from 'mani-signal';

const signal = new Signal();

const binding = signal.add(() => {});

signal.dispatch(); // handler will be called

binding.setActive(false);

signal.dispatch(); // handler will not be called
1.0.9

1 year ago

1.0.8

1 year ago

1.0.8-0

1 year ago

1.0.7

1 year ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago