sig3.js v0.0.1
sig3.js
a micro-library javascript to transmit instructions to a satellite(s) through a signal.
Problem❓
Are you wanted to implemented custom events in your application? in a scenario in which you need to linked components between them. You may need a specific canal to process the payload provided by a local station before to use it.
Solution❓
this micro-library can be made for you. it lets you the possibility to deploy satellites around your application. each satellite can be register to a signal then when the local station transmit a signal attached to a specific satellite. it gets this signal and then processes it. It will helps your components to be informed when a station has update his data.
⚠️ Disclaimer
i am not a developer, i am just a regular guy whos appreciate programming and want to learn more about design patterns. this project try to follow the Observer pattern but i custom it a little bit so in a future some changes will be expected.
📦 Install dependencies
npm i sig3
OR
yarn add sig3
🚀 Start project
import * as SIG3 from 'sig3';
OR
import { Station, Satellite } from 'sig3';
💻 Demo
const operations = [
state => {
state.counter = state.counter + 5;
console.log( '1. a => counter => ', state.counter );
return state;
},
state => {
state.counter = state.counter * 5;
console.log( '2. a => counter => ', state.counter );
return state;
}
];
const state = { counter: 0 };
const station = new SIG3.Station();
const satellite = new SIG3.Satellite( ...operations );
station.onsignal( 'event:onready', satellite );
station.transmit( 'event:onready', state );
/* output:
1. satellite => counter => 5
2. satellite => counter => 25
*/
📖 API
Station()
it creates a local station to manage signal(s) you want to transmit.
params
no params
example
const station = new Station(); // output: {onsignal: ƒ, offsignal: ƒ, transmit: ƒ}
.onsignal( name, satellite )
it subscribes a signal emission to a satellite.
params
name
{ String }: name of the signal you wish to subscribe.satellite
{ Function } callback.returns
{ Boolean }:true
/false
, if the subscription was successful.example
station.onsignal( 'event:onready', new SIG3.Satellite() ); // outuput: true
.offsignal( name )
it unsubscribes a signal emission to a satellite.
params
name
{ String }: name of the signal you wish to unsubscribe.returns
{ Boolean }:true
/false
, if the unsubscription was successful.example
station.offsignal( 'event:onready' ); // outuput: true
.transmit( name, payload )
it transmits instructions to satellite through a signal.
params
name
{ String }: name of the signal you want transmit.payload
{ Object }: collection.returns
{ Boolean }:true
/false
, if the transmit has been made.example
station.transmit( 'event:onready', { counter: 0 } ); // output: true
Satellite( ...fs )
it creates a satellite that will be able to receive instructions transmit by a local station.
params
fs
{ Array<Function>
}: array of callback(s).returns
{ Object }: method(s).example
const increment = state => { state.counter++; return state; }; const satellite = new Satellite( increment ); // output: {fs: Array(1)}
🚨 Tests
Running
npm run test:default
OR
yarn test:default
Coverage
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---|---|---|---|---|---|
All files | 92.31 | 90 | 80 | 100 | |
sig3.satellite.js | 100 | 100 | 100 | 100 | |
sig3.station.js | 91.3 | 90 | 75 | 100 | 16 |
📝 Todo
- async feature
- add examples
- improve test cases
🔗 Source
©️ License
Copyright ©️ 2019 monsieurbadia
Released under the MIT license
🙏 Supports
Logo with 🖤 by @mllemartins
Built with 🖤 by @monsieurbadia
⭐️ this repository if this project helped you!
5 years ago