Skip to content

Commit

Permalink
Merge pull request #12 from cristiammercado/develop
Browse files Browse the repository at this point in the history
Reconstruction of the project
  • Loading branch information
cristiammercado authored Nov 21, 2020
2 parents 02bf94d + dca709d commit 87cb6e9
Show file tree
Hide file tree
Showing 61 changed files with 15,041 additions and 2,046 deletions.
15 changes: 10 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Editor configuration, see https://editorconfig.org
root = true

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

[*.yml]
indent_size = 2
[*.ts]
quote_type = single

[*.md]
max_line_length = off
trim_trailing_whitespace = false
49 changes: 45 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
/node_modules/
/.idea/
/.nyc_output/
/coverage/
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/tmp
/out-tsc
# Only exists if Bazel was run
/bazel-out

# dependencies
/node_modules

# profiling files
chrome-profiler-events*.json
speed-measure-plugin*.json

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings

# System Files
.DS_Store
Thumbs.db
4 changes: 0 additions & 4 deletions .npmignore

This file was deleted.

12 changes: 0 additions & 12 deletions .nycrc.json

This file was deleted.

26 changes: 26 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# angular gitignore
# see https://github.com/prettier/prettier/issues/2294

## compiled output
/dist
/tmp
/out-tsc

## dependencies
/node_modules

## IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
.vscode/*

# auto-generated files
/angular.json
/.angular-cli.json
/package-lock.json
/yarn.lock
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"printWidth": 120,
"trailingComma": "es5"
}
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
language: node_js
dist: bionic
dist: focal
arch: amd64
cache: npm
addons:
chrome: stable
os:
- linux
- osx
- windows
sudo: false
node_js:
- 15
- 14
- 13
- 12
- 11
- 10
install:
- npm install
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Cristiam Mercado
Copyright (c) 2020 Cristiam Mercado

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
115 changes: 75 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Ng-Event-Bus
# 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. You can check [npm page](https://www.npmjs.com/package/ng-event-bus).

Expand All @@ -15,63 +15,98 @@ First, import it:

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

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

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

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

Or create an instance manually:

`let eventBus = new NgEventBus();`
`constructor(private eventBus: NgEventBus) { ... }`

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

* `eventBus.cast(key, data)` - send message to event bus.
* `this.eventBus.cast(key, data)` - send a message to event bus.

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

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

- `key` must be a string and must not be empty, otherwise it will throw an exception.
- `data` is optional and can be any type of object.
- `pattern` must be a string and must not be empty.

### Patterns

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!');
this.eventBus.cast('app:start', 'started');
this.eventBus.cast('message:greet', 'Hi!');
this.eventBus.cast('message:bye', 'Bye!');
this.eventBus.on('app:start').subscribe((meta: MetaData) => {
console.log(meta.data); // will receive 'started' only
});
eventBus.on('app:start').subscribe((message)=>{
console.log(message); //will receive 'started' only
this.eventBus.on('message:greet').subscribe((meta: MetaData) => {
console.log(meta.data); // will receive 'Hi!'
});
eventBus.on('message:greet').subscribe((message)=>{
console.log(message); //will receive 'Hi!'
this.eventBus.on('message:bye').subscribe((meta: MetaData) => {
console.log(meta.data); // will receive 'Bye!'
});
eventBus.on('message:bye').subscribe((message)=>{
console.log(message); //will receive 'Bye!'
this.eventBus.on('message:*').subscribe((meta: MetaData) => {
console.log(meta.data); // will receive both 'Hi!' and 'Bye!'
});
eventBus.on('message:*').subscribe((message)=>{
console.log(message); //will receive both 'Hi!' and 'Bye!'
this.eventBus.on('**').subscribe((meta: MetaData) => {
console.log(meta.data); // will receive all messages: 'started', 'Hi!' and 'Bye!'
});
eventBus.on('**').subscribe((message)=>{
console.log(message); //will receive all messages: 'started', 'Hi!' and 'Bye!'
```

### MetaData (Breaking change in v2.x.x)

When you subscribe to the observable, you can optionally get an instance of `MetaData` class. This instance contains information related to the emission of the event through the bus. The properties of this instance are:

- `id`: A unique identifier of the message sent through the events bus.
- `key`: Original key associated to the message.
- `data`: Data associated to message. It's optional.
- `timestamp`: Time in milliseconds in which the message was generated.

```
this.eventBus.cast('app:start', 'started');
this.eventBus.on('app:start').subscribe((meta: MetaData) => {
console.log(meta.id); // will print "d9c31eb0-b3f3-4764-a96d-6a703112a696"
console.log(meta.key); // will print "app:start"
console.log(meta.data); // will print "started"
console.log(meta.timestamp); // will print 1605934473553
});
```

```
this.eventBus.cast('message:bye', {message: 'bye'});
this.eventBus.on('**').subscribe((meta: MetaData) => {
console.log(meta.id); // will print "4945f28c-d2a5-4738-b7d1-f3df8f08422c"
console.log(meta.key); // will print "message:bye"
console.log(meta.data); // will print {message: 'bye'}
console.log(meta.timestamp); // will print 1605934709116
});
```
Expand All @@ -80,35 +115,35 @@ eventBus.on('**').subscribe((message)=>{

These strings will match:

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

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

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

* `on('a:b:c' , callback)` can subscribe to `cast('a:b:c', ...)`
- `on('a:b:c' ).suscribe` 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:**' ).suscribe` 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:*:*' ).suscribe` 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:*' ).suscribe` 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('a:b:**').suscribe` can subscribe to `cast('a:b:c',. ..)`

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

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

### Need to unsubscribe (observable)?

Yes, in the normal scenario usage it's necessary you unsubscribe from the observable to avoid memory leaks. That happens because the library exposes an infinite observable. That's no exactly related to the library, but in the way in which the observables work in RxJS.

However, there're ways that you don't need to unsubscribe, for example if you use `.first()` in the pipe of the observable because using this method would turn it into a finite observable (It would apply in your case if you're just waiting for a one-time event).
However, there are ways that you don't need to unsubscribe, for example if you use `.first()` in the pipe of the observable because using this method would turn it into a finite observable (It would apply in your case if you're just waiting for a one-time event).

I recommend you to read this [stackoverflow answer](https://stackoverflow.com/questions/50629357/rxjs-angular-unsubscribe-from-subjects/50633482#50633482) about a similar question!
I recommend you to read this [stackoverflow answer](https://stackoverflow.com/questions/50629357/rxjs-angular-unsubscribe-from-subjects/50633482#50633482) about a similar question.

## Release History & Changelog
## Release history & changelog

See the [Releases](https://github.com/cristiammercado/ng-event-bus/releases) page for a list of all releases, including changes.

Expand Down
48 changes: 48 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"ng-event-bus": {
"projectType": "library",
"root": "./",
"sourceRoot": "./src",
"prefix": "lib",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:ng-packagr",
"options": {
"tsConfig": "./tsconfig.lib.json",
"project": "./ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "./tsconfig.lib.json"
}
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "./src/test.ts",
"tsConfig": "./tsconfig.spec.json",
"karmaConfig": "./karma.conf.js"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"./tsconfig.lib.json",
"./tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "ng-event-bus"
}
Loading

0 comments on commit 87cb6e9

Please sign in to comment.