Giter Site home page Giter Site logo

notificar's Introduction

Notificar - A Cooler Event Library

Features include:

  • Unlike on, off, addListener, removeListener etc. Notificar event subscriptions return a Disposable object with dispose() method to unsubscribe from event.
  • Above feature allows you to group event subscriptions using DisposablesComposite and dispose them all with one call, i.e DisposableComposite#dispose()
  • Listeners voice matters!! You can now dispatch an event using Notificar#dispatchWithReturn where each listener's return value can be accessed from returned array.

The Last feature can come in handy like in example below:

// Here some listeners are subscribed to `onWillCommit` event, but we can allow listeners to disagree if they deem so
commitChange(change) {
    // ...

    // here we'll notify all `will-commit` listeners, but if one of them returns false, we'll abort the `commit` operation
    if (this.notificar.dispatchWithReturn('will-commit', this.currentValue).some((canCommit) => canCommit === false)) {
        return
    }

    // Proceed with commit opertaion

    // ...
}

Usage

npm i notificar
// Textbuffer.ts (I highly recommend TypeScript as you can get a huge performance boost by using `enum`s instead of strings as event identifiers)

import { Notificar } from 'notificar'
// ...

enum TextBufferEvent {
    WillSave,
    WillPaste,
}

class TextBuffer implements IDisposable {
    private notificar = new Notificar<TextBufferEvent>()
    // ...

    public onWillPaste(callback: (textFragment: TextFragment, pos: Position) => void): IDiposable {
        return this.notificar.add(TextBufferEvent.Will, callback)
    }

    public onWillSave(callback: () => void): IDiposable {
        return this.notificar.add(TextBufferEvent.WillSave, callback)
    }

    // ...

    private _pasteTextFragment(textFragment: TextFragment, pos: Position): boolean {
        // ...
        this.notificar.dispatch(TextBufferEvent.WillPaste, textFragment, pos) // dispatch with args
        // ...
    }

    private _trySave(): boolean {
        // ...
        this.notificar.dispatch(TextBufferEvent.WillSave) // dispatch without args
        // ...
    }
}

Full API can be found in index.d.ts

License

MIT © 2018 Neek Sandhu

notificar's People

Contributors

zikaari avatar

Watchers

James Cloos 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.