# @zapatran/subscriber-collection

> tiny package to subscribe for events

Latest version **1.0.0** (published 2021-02-10) · ISC license · 0 weekly downloads

## Install

```sh
npm install @zapatran/subscriber-collection
pnpm add @zapatran/subscriber-collection
yarn add @zapatran/subscriber-collection
bun add @zapatran/subscriber-collection
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-02-10 |
| First published | 2021-02-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Thor Jubera Albo |
| Maintainers | zapatran |
| Keywords | event, subscription |

## Links

- npm: https://www.npmjs.com/package/@zapatran/subscriber-collection
- Repository: https://github.com/zapatran/subscriber
- Issues: https://github.com/zapatran/subscriber/issues
- npm.io page: https://npm.io/package/@zapatran/subscriber-collection

## Dependencies (1)

- [lodash.remove](https://npm.io/package/lodash.remove.md) ^4.7.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2021-02-10

## README

# @zapatran/subscriber-collection

This is a tiny package to subscribe for events

## Instalation

```bash
npm install @zapatran/subscriber-collection
```

## Usage

This package export a class `SubscriberCollection` and has two methods;

- `subscribe` where you can add a subscriber following the interface `Subscriber`

- `notify` where will call all the subcribers

## Example

using in React

** Counter.ts **

```typescript
import { Subscriber, SubscriberCollection, Subscription } from '@zapatran/subscriber-collection';

export class Counter {
  value: number;
  private subscribers = new SubscriberCollection<Counter>();

  constructor() {
    this.value = 0;
  }

  subscribeToUpdates(subscriber: Subscriber<Counter>): Subscription {
    return this.subscribers.subscribe(subscriber);
  }

  private notifySubscribers(): void {
    this.subscribers.notify(this);
  }

  increment() {
    this.value++;
    this.notifySubscribers();
  }

  decrement() {
    this.value--;
    this.notifySubscribers();
  }
}
```

** Store.ts **

```ts
import React, { useContext } from 'react';
import { Counter } from './Counter';

class Store {
  counter: Counter;

  constructor() {
    this.counter = new Counter();
  }
}

const defaultStore = new Store();

export const StoreContext = React.createContext(defaultStore);

export function useCounterStore() {
  const { counter } = useContext(StoreContext);
  return counter;
}
```

** useCounter.tsx **

```ts
import { useEffect, useState } from 'react';
import { Counter } from './Counter';
import { useCounterStore } from './store';

export function useCounter(): Counter {
  const counter = useCounterStore();
  const [state, setState] = useState<{ counter: Counter }>({ counter });

  useEffect(() => {
    return counter.subscribeToUpdates((updatedCounter: Counter): void => {
      setState({ counter: updatedCounter });
    });
  }, [counter]);

  return state.counter;
}
```

** Component **

```ts
import { useCounter } from './useCounter';

function Component() {
  const counter = useCounter();

  const handleIncrement = () => {
    counter.increment();
  };

  return (
    <div>
      <header>
        <p>{counter.value}</p>
        <button onClick={handleIncrement}>increment</button>
      </header>
    </div>
  );
}

export default Component;
```

---
_Source: https://npm.io/package/@zapatran/subscriber-collection · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
