Skip to content

Commit

Permalink
support the using operator
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Jul 22, 2024
1 parent 80a6db4 commit 079a4af
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#54]: Added option to use raw dmx values
- [#53]: Allow sending data to a specific IP instead of the whole LAN
- [#57]: Export `Sender` props
- Support the `using` operator
- Minor performance improvements
- Minor improvements to type defintions and integration tests

Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"jest": "^27.5.1",
"ts-jest": "^27.1.4",
"ts-node": "^10.7.0",
"typescript": "^4.6.2"
"typescript": "^5.5.3"
},
"eslintConfig": {
"extends": "kyle",
Expand Down
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ main(); // wrapped in a main() function so that we can `await` the promise
| `minRefreshRate` | `number` | Optional. How often the data should be re-sent (*in Hertz/Hz*), even if it hasn't changed. By default data will only be sent once (equivilant of setting `refreshRate: 0`). To re-send data 5 times per second (`5Hz`), set `refreshRate: 5`. This is equivilant to `200ms`. | `0`
| `useUnicastDestination`| `string` | Optional. Setting this attribute to an IPv4 address will cause data to be sent directly to that device, instead of broadcasting to the whole LAN. |

## `using` operator

This library supports [the `using` operator](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html). This means you don't have to explicitly call `.close()`.

```ts
async function main() {
using sender = new Sender({ /* ... */ });

await sender.send({ /* ... */ });

// you don't need to call sender.close(), it's automatically
// closed when the block ends, thanks to the `using` keyword
}

```

# Contribute

```bash
Expand Down
2 changes: 2 additions & 0 deletions src/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,6 @@ export class Receiver extends EventEmitter {
this.socket.close(callback);
return this;
}

[Symbol.dispose] = this.close;
}
2 changes: 2 additions & 0 deletions src/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ export class Sender {
this.socket.close();
return this;
}

[Symbol.dispose] = this.close;
}
30 changes: 30 additions & 0 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import assert from 'assert';
import { networkInterfaces } from 'os';
import { Receiver, Sender, Packet } from '../src';

// @ts-expect-error -- polyfill
Symbol.dispose ||= Symbol("dispose");

// TODO: switch to `node:timers/promises` once we drop support for node14
const sleep = (ms: number) => new Promise((cb) => setTimeout(cb, ms));

function collectErrors(Rx: Receiver, errors: Error[]) {
Expand Down Expand Up @@ -42,6 +46,32 @@ describe('Receiver & Sender (integration test)', () => {
}
});

it("supports Symbol.dipose", async () => {
using Tx1 = new Sender({ universe: 1, reuseAddr: true });
using Tx2 = new Sender({ universe: 2, reuseAddr: true });
using Rx = new Receiver({
universes: [1, 3], // not listening to universe 2
reuseAddr: true,
});

const received: Packet[] = [];
const errors: Error[] = [];
Rx.on('packet', (packet) => received.push(packet));
collectErrors(Rx, errors);

// stuff takes time
await sleep(3500);
await Tx1.send({ payload: { 1: 100 } });
await Tx1.send({ payload: { 4: 25.1, 5: 0 } });
await Tx2.send({ payload: { 512: 100 } });
await sleep(3500);

assert.strictEqual(errors.length, 0);
assert.strictEqual(received.length, 2);
assert.deepStrictEqual(received[0]!.payload, { 1: 100 });
assert.deepStrictEqual(received[1]!.payload, { 4: 25.1 });
});

it('re-sends the packet data if minRefreshRate is supplied', async () => {
const Tx = new Sender({ universe: 1, minRefreshRate: 1, reuseAddr: true });
const Rx = new Receiver({ universes: [1], reuseAddr: true });
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"outDir": "./dist",
"module": "commonjs",
"target": "es2015",
"lib": ["esnext"],
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
Expand Down

0 comments on commit 079a4af

Please sign in to comment.