Skip to content
Permalink
Browse files

feat: initial commit

  • Loading branch information...
paulobmarcos committed Feb 27, 2019
1 parent 530f35a commit 0dd0b269accdd814f7b73e44040da0ca308fd04f
Showing with 9,037 additions and 1 deletion.
  1. +3 −0 .babelrc
  2. +16 −0 .editorconfig
  3. +11 −0 .eslintrc.json
  4. +5 −0 .gitignore
  5. +19 −0 LICENSE
  6. +99 −1 README.md
  7. +25 −0 index.js
  8. +8,721 −0 package-lock.json
  9. +72 −0 package.json
  10. +66 −0 tests/index.test.js
@@ -0,0 +1,3 @@
{
"presets": ["babel-preset-moxy/lib"]
}
@@ -0,0 +1,16 @@
# editorconfig.org
root = true

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

[package.json]
indent_size = 2

[{*.md,*.snap}]
trim_trailing_whitespace = false
@@ -0,0 +1,11 @@
{
"root": true,
"extends": [
"eslint-config-moxy/es9",
"eslint-config-moxy/addons/jest",
"eslint-config-moxy/addons/babel-parser",
"eslint-config-moxy/addons/es-modules",
"eslint-config-moxy/addons/browser",
"eslint-config-moxy/addons/node"
]
}
@@ -0,0 +1,5 @@
node_modules
npm-debug.log*
coverage
lib/
es/
19 LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2019 Made With MOXY Lda <hello@moxy.studio>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
100 README.md
@@ -1,2 +1,100 @@
# pico-signals
A very simple signal library inspired by the 'signals' package

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]

[npm-url]:https://npmjs.org/package/pico-signals
[downloads-image]:https://img.shields.io/npm/dm/pico-signals.svg
[npm-image]:https://img.shields.io/npm/v/pico-signals.svg
[travis-url]:https://travis-ci.org/moxystudio/js-pico-signals
[travis-image]:http://img.shields.io/travis/moxystudio/js-pico-signals/master.svg
[codecov-url]:https://codecov.io/gh/moxystudio/js-pico-signals
[codecov-image]:https://img.shields.io/codecov/c/github/moxystudio/js-pico-signals/master.svg
[david-dm-url]:https://david-dm.org/moxystudio/js-pico-signals
[david-dm-image]:https://img.shields.io/david/moxystudio/js-pico-signals.svg
[david-dm-dev-url]:https://david-dm.org/moxystudio/js-pico-signals?type=dev
[david-dm-dev-image]:https://img.shields.io/david/dev/moxystudio/js-pico-signals.svg

A very simple signal library inspired by the [signals](https://github.com/millermedeiros/js-signals) package.


## Installation

```sh
$ npm install pico-signals
```

This library is written in modern JavaScript and is published with both CommonJS and ES module transpiled variants.

If you target older browsers please make sure to transpile accordingly.


## Usage

```js
import picoSignals from 'pico-signals';
const listener1 = () => console.log('Listener 1');
const listener2 = () => console.log('Listener 2');
const ps = picoSignals();
const removeListener1 = ps.add(listener1);
const removeListener2 = ps.add(listener2);
ps.dispatch('foo', 'bar');
//=> Both listeners will be called and both logs produced.
//=> Every listener will receive the same arguments provided in the dispatch method.
removeListener2();
//=> Deletes `listener2` from the listeners list;
ps.dispatch();
//=> Only `listener1` will be called since its currently the only listener on the list.
ps.clear();
//=> Clears all listeners.
```

## API

### add(listener)

Adds a new listener to the list.

Returns a method to remove the listener.

#### listener
Type: `Function`

A listener to be called on dispatch.

### delete(listener)

Deletes a listener from the list.

#### listener
Type: `Function`

An existing listener in the list.

### clear()

Deletes all listeners from the list.

### dispatch(...args)

Calls every listener with the specified arguments.


## Tests

```sh
$ npm test
$ npm test -- --watch # during development
```


## License

Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).

@@ -0,0 +1,25 @@
class PicoSignals {
listeners = new Set();

add(listener) {
this.listeners.add(listener);

return () => this.delete(listener);
}

delete(listener) {
return this.listeners.delete(listener);
}

clear() {
this.listeners.clear();
}

dispatch(...args) {
this.listeners.forEach((listener) => listener(...args));
}
}

const createPicoSignals = () => new PicoSignals();

export default createPicoSignals;
Oops, something went wrong.

0 comments on commit 0dd0b26

Please sign in to comment.
You can’t perform that action at this time.