# @element-ts/chlorine

> An abstracted, async, easy to use, type-safe function invocation event handler framework.

Latest version **0.1.0** (published 2020-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @element-ts/chlorine
pnpm add @element-ts/chlorine
yarn add @element-ts/chlorine
bun add @element-ts/chlorine
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2020-08-09 |
| First published | 2020-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 37.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Elijah Cobb |
| Maintainers | ejc |
| Keywords | event, handler, typescript, function, implement, invoke |

## Links

- npm: https://www.npmjs.com/package/@element-ts/chlorine
- Repository: https://github.com/element-ts/chlorine
- Homepage: https://github.com/element-ts/chlorine#readme
- Issues: https://github.com/element-ts/chlorine/issues
- npm.io page: https://npm.io/package/@element-ts/chlorine

## Dependencies (4)

- [@element-ts/neon](https://npm.io/package/@element-ts/neon.md) latest
- [@element-ts/oxygen](https://npm.io/package/@element-ts/oxygen.md) latest
- [@elijahjcobb/prom-type](https://npm.io/package/@elijahjcobb/prom-type.md) latest
- [@elijahjcobb/better-json](https://npm.io/package/@elijahjcobb/better-json.md) latest

## 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

- 0.1.0 (latest) — 2020-08-09

## README

# @element-ts/chlorine
An abstracted, async, easy to use, type-safe function invocation event handler framework.

## Example
The below example is actually from a test for this package. You can see how to create a commanded object and in the test example a simple handler is provided.
```typescript
interface SideACommands extends CLRegistryStructure<SideACommands> {
	increment: {
		param: number;
		return: number;
	};
}

interface SideBCommands extends CLRegistryStructure<SideBCommands> {
	decrement: {
		param: number;
		return: number;
	};
}

class SideA extends ClCommander<SideACommands, SideBCommands, number> {

	public handler: ((packet: string) => Promise<void>) | undefined;

	public constructor() {

		super(1337);

	}

	protected async send(packet: string): Promise<void> {

		if (this.handler) await this.handler(packet);

	}

}

class SideB extends ClCommander<SideBCommands, SideACommands, number> {

	public handler: ((packet: string) => Promise<void>) | undefined;

	public constructor() {

		super(1337);

	}

	protected async send(packet: string): Promise<void> {

		if (this.handler) await this.handler(packet);

	}

}

test("General", async () => {

	const a = new SideA();
	const b = new SideB();

	a.handler = (packet => b.receive(packet));
	b.handler = (packet => a.receive(packet));

	a.implement("increment", async num => num + 1);
	b.implement("decrement", async num => num - 1);

	const testSize = 100;

	let numA = 0;
	let numB = testSize;

	for (let i: number = 0; i < testSize; i++) {
		numA = await b.invoke("increment", numA);
		numB = await a.invoke("decrement", numB);
	}

	expect(numA).toEqual(testSize);
	expect(numB).toEqual(0);

});

```

## Basics
### `ClCommander`
The `ClCommander` is the main thing you will interface with. It is an `abstract` class, again this is all written in
TypeScript. Extend this class and you will only have to do two things. Supply a `referencer` to the `super()` call and
implement the abstract `send(packet: string): Promise<void>` method. You can attach anything you need to your class and
just handle sending data out. When you get data in, call the protected method `receive(packet: string): Promise<void>`.
The commander will handle everything else for you.

There are three generic types. `LC` stands for _"local commands"_, `RC` stands for _"remote commands"_, and `R` stands
for _"referencer"_. A commander implements its own local commands, invokes its remote commands and passes the referencer
to itself when implementing commands. This can be useful to pass the socket or id so it is always available when
implementing commands.

### `ClMessageManager`
The message manager is a class used to store messages and generate unique ids for them. Once a command is invoked and
the response is received, the handler in the message manager is given back.

### `ClRegistry`
The registry handles what commands are implemented on one side.

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