Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / ECMAScript 6
Tip/Trick

eyepatch - A Library of Notification Based Objects for TypeScript/JavaScript

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
8 Nov 2016CPOL1 min read 14.1K   5   2
A library of notification based objects for TypeScript/JavaScript

Introduction

I was a C# developer for a few years and then I moved to programming web apps in TypeScript.

One of the things I was amazed to find out was the absence of the events. It is such a useful thing and for some reason they don't exist normally in the JavaScript or the TypeScript languages.

Writing a code polling for changes is ugly and most of the times if there was an event, I'd use it.

So I wrote the library that has some basic event implementations and some more complex ones, moreover I went further more and added collections that have the event system built into them.

Features

  • Event - Simple or parametrized
  • ConditionalEvent - Simple or parametrized
  • GlobalEvent - Communicating globally with events
  • ObservableCollection - Notifies you when something changes
  • ObservableDictionary - Works with keys as an object and not only strings and numbers. Notifies on changes. Works simply adding non enumerable property as the key id. This doesn't affect the JSON.stringify method or the for in loop

Using the Code

Installation

npm install eyepatch --save

The code examples are written in TypeScript, but you can simply use them in ES6 or convert to ES5.

Using Parametrized Event

JavaScript
var event = new EventT<number>();

event.on((_num: number) => console.log('The number ' + _num + ' was raised'));

event.raise(15);

Using Conditional Event

JavaScript
var conditionalEvent = new ConditionalEventT<number>();

conditionalEvent.on(
  (_num) => console.log('The number ' + _num + ' is positive'),
  (_num) => _num > 0
  );

conditionalEvent.raise(-1); // Will not be logged
conditionalEvent.raise(12); // Will be logged

Using Global Event

const globalEvent1 = new GlobalEvent();

globalEvent1.on('some event name', (_num: number) => console.log('The number ' + _num + ' was raised'));

// In some other place
const globalEvent2 = new GlobalEvent();
globalEvent2.raise('some event name', 123); // Will log: "The number 123 was raised"

Raising Safely In Case One of the Registrations Throws An Error

JavaScript
var event = new Event();

event.on(() => throw 'some error');
event.on(() => console.log('I still want to log'));

event.raiseSafe();

Using ObservableCollection

JavaScript
var collection = new ObservableCollection<number>();

var items: number[] = collection.items;

collection.itemsChanged.on(
  (_args: IItemsChangedEventArgs<number>) => {
    console.log('Added items: ' + _args.added);
    console.log('Removed items: ' + _args.removed);
  });

collection.add(2);
collection.addRange([1,2,3,4,5,6,7,8,9]);

collection.removeMatching(2);
collection.removeMatchingRange([5,6]);
collection.removeAtIndex(0);
collection.removeAtIndices([0,1]);

var size: number = collection.size;

var has7: boolean = collection.contains(7);

collection.clear();

Using ObservableDictionary

JavaScript
var dictionary = new ObservableDictionary<Obejct, Object>();

dictionary.itemsChanged.on(
  _args => {
    console.log('Added key value pairs: ' + _args.added);
    console.log('Removed key value pairs: ' + _args.removed);
  });

var key = {};
var value = {};
dictionary.add(key, value);

var containsKey: boolean = dictionary.containsKey(key);
var containsValue: boolean = dictionary.containsValue(value);

var valueByKey: Object = dictionary.getValueByKey(key);

var size: number = dictionary.size;

var keys: Object[] = dictionary.keys;
var values: Object[] = dictionary.values;

dictionary.remove(key);

dictionary.clear();

What's Next?

Have fun and feel free to use, comment, request, and contribute.
I'll keep adding stuff from time to time...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHey Pin
Sacha Barber8-Nov-16 11:23
Sacha Barber8-Nov-16 11:23 
AnswerRe: Hey Pin
slavik578-Nov-16 18:13
slavik578-Nov-16 18:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.