Skip to content

Commit

Permalink
Added files to project.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiammercado committed Apr 8, 2019
1 parent 4d20214 commit eba6a40
Show file tree
Hide file tree
Showing 11 changed files with 620 additions and 63 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_size = 2
63 changes: 2 additions & 61 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,61 +1,2 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
/node_modules/
/.idea/
107 changes: 105 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,105 @@
# ng-event-bus
RxJS-based message bus service for Angular.
# Ng-Event-Bus

RxJS-based message/event bus service for Angular apps inspired by [NgRadio](https://github.com/govorov/ng-radio). Inject it in your application module.

## Installation

`npm install --save ng-event-bus`

[NPM page](https://www.npmjs.com/package/ng-event-bus)

## Usage

First, import it:

`import { NgEventBus } from 'ng-event-bus';`

Then, if using Angular, inject it as a service (do not forget about providers):


```
......
import { NgEventBus } from 'ng-event-bus';
......
@NgModule({
imports:[
......
],
providers: [
.......
NgEventBus,
.......
],
```

`constructor(private eventBus: NgEventBus){...}`

Or create an instance manually:

`let radio = new NgEventBus();`

Since you have `NgEventBus` instance in your app, you can use these methods for passing messages:

* `eventBus.cast(key, data)` - send message to radio.

* `eventBus.on(pattern)` - returns observable you can subscribe to listen events.

Patterns may contain multiple segments split by `:`. Use this feature to create namespaces for messages you cast. You can use `*` in `pattern` to subscribe to any matching segment, or use `**` to subscribe to all segments, starting from particular position.

For example, you can use `on('error:*')` and subscribe to all errors, including something like `error:http` or `error:internal` and so on:

```
eventBus.cast('app:start', 'started');
eventBus.cast('message:greet', 'Hi!');
eventBus.cast('message:bye', 'Bye!');
eventBus.on('app:start').subscribe((message)=>{
console.log(message); //will receive 'started' only
});
eventBus.on('message:greet').subscribe((message)=>{
console.log(message); //will receive 'Hi!'
});
eventBus.on('message:bye').subscribe((message)=>{
console.log(message); //will receive 'Bye!'
});
eventBus.on('message:*').subscribe((message)=>{
console.log(message); //will receive both 'Hi!' and 'Bye!'
});
eventBus.on('**').subscribe((message)=>{
console.log(message); //will receive all messages: 'started', 'Hi!' and 'Bye!'
});
```

### Examples

These strings will match:

* `on('**' , callback)` can subscribe to any message with any segments count

* `on('a' , callback)` can subscribe to `cast('a', ...)`

* `on('a:b' , callback)` can subscribe to `cast('a:b', ...)`

* `on('a:b:c' , callback)` can subscribe to `cast('a:b:c', ...)`

* `on('a:**' , callback)` can subscribe to `cast('a:b:c', ...)`, `cast('a:b:c:d:e:f', ...)`

* `on('a:*:*' , callback)` can subscribe to `cast('a:b:c', ...)`, `cast('a:f:g', ...)`, `cast('a:n:m', ...)`

* `on('a:b:*' , callback)` can subscribe to `cast('a:b:c', ...)`, `cast('a:b:d', ...)`, but not `cast('a:b', ...)`

* `on('a:b:**', callback)` can subscribe to `cast('a:b:c',. ..)`

* `on('*:b:*' , callback)` can subscribe to `cast('a:b:c', ...)`

* `on('a:*:*' , callback)` can subscribe to `cast('a:b:c', ...)`

## License

[MIT](https://github.com/cristiammercado/ng-event-bus/blob/master/LICENSE)
35 changes: 35 additions & 0 deletions dist/ng_event_bus.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Observable } from 'rxjs';
/**
* Main library class.
*
* @since 1.0.0
* @version 1.0.0
*/
export declare class NgEventBus {
private _eventBus;
private separator;
/**
* Constructor for this class: Initializes event bus.
*/
constructor();
/**
* Validates key matching.
*
* @param {string} key Key to identify the message/event.
* @param {string} wildcard Wilcard received from on method.
*/
private keyMatch;
/**
* Publish a message/event to event bus.
*
* @param {string} key Key to identify the message/event.
* @param {any} [data] Optional: Additional data sent with the message/event.
*/
cast(key: string, data?: any): void;
/**
* Returns an observable you can subscribe to listen messages/events.
*
* @param {string} key Key to identify the message/event.
*/
on<T>(key: string): Observable<T>;
}
74 changes: 74 additions & 0 deletions dist/ng_event_bus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"use strict";
exports.__esModule = true;
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
/**
* Main library class.
*
* @since 1.0.0
* @version 1.0.0
*/
var NgEventBus = /** @class */ (function () {
/**
* Constructor for this class: Initializes event bus.
*/
function NgEventBus() {
this.separator = ':';
this._eventBus = new rxjs_1.Subject();
}
/**
* Validates key matching.
*
* @param {string} key Key to identify the message/event.
* @param {string} wildcard Wilcard received from on method.
*/
NgEventBus.prototype.keyMatch = function (key, wildcard) {
var w = '*';
var ww = '**';
var partMatch = function (wl, k) {
return (wl === w) || (wl === k);
};
var sep = this.separator;
var kArr = key.split(sep);
var wArr = wildcard.split(sep);
var kLen = kArr.length;
var wLen = wArr.length;
var max = Math.max(kLen, wLen);
for (var i = 0; i < max; i++) {
var cK = kArr[i];
var cW = wArr[i];
if (cW == ww && (typeof cK !== 'undefined')) {
return true;
}
if (!partMatch(cW, cK)) {
return false;
}
}
return true;
};
/**
* Publish a message/event to event bus.
*
* @param {string} key Key to identify the message/event.
* @param {any} [data] Optional: Additional data sent with the message/event.
*/
NgEventBus.prototype.cast = function (key, data) {
if (typeof key !== 'string' || !key.length) {
throw 'key must be a string and mustn\'t be empty.';
}
this._eventBus.next({ key: key, data: data });
};
/**
* Returns an observable you can subscribe to listen messages/events.
*
* @param {string} key Key to identify the message/event.
*/
NgEventBus.prototype.on = function (key) {
var _this = this;
return this._eventBus.asObservable().pipe(operators_1.filter(function (event) {
return _this.keyMatch(event.key, key);
}), operators_1.map(function (event) { return event.data; }));
};
return NgEventBus;
}());
exports.NgEventBus = NgEventBus;
8 changes: 8 additions & 0 deletions jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"spec_dir": "test",
"spec_files": [
"ng-event-bus.spec.ts"
],
"stopSpecOnExpectationFailure": false,
"random": true
}
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "ng-event-bus",
"version": "0.0.1",
"description": "RxJS-based message/event bus service for Angular.",
"homepage": "https://github.com/cristiammercado/ng-event-bus",
"bugs": {
"email": "contact@cristiammercado.com",
"url": "https://github.com/govorov/ng-radio/issues"
},
"main": "dist/ng_event_bus.js",
"types": "dist/ng_event_bus.d.ts",
"scripts": {
"build": "./node_modules/.bin/tsc -d ./src/ng_event_bus.ts --lib es6 --outDir ./dist",
"test": "./node_modules/.bin/jasmine-ts --config=jasmine.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cristiammercado/ng-event-bus.git"
},
"keywords": [
"angular",
"message-bus",
"event-bus"
],
"author": "Cristiam Mercado <contact@cristiammercado.com>",
"license": "MIT",
"dependencies": {
"rxjs": "6.4.0"
},
"devDependencies": {
"@types/jasmine": "3.3.12",
"jasmine": "3.4.0",
"jasmine-core": "3.4.0",
"jasmine-ts": "0.3.0",
"ts-node": "8.0.3",
"typescript": "3.4.2",
"uuid": "3.3.2"
}
}
Loading

0 comments on commit eba6a40

Please sign in to comment.