Skip to content

Commit

Permalink
feat: add support for the toJSON() method when encoding (#113)
Browse files Browse the repository at this point in the history
Example with BigInt values:

```js
BigInt.prototype.toJSON = function () {
  return String(this);
};

emitter.emit("foo", 42n);
```

Important note! There is a non backward-compatible change regarding
Date objects, which means that the adapter may not be able to properly
decode them.

Reference: https://github.com/darrachequesne/notepack/releases/tag/3.0.0
Diff: darrachequesne/notepack@2.3.0...3.0.1
  • Loading branch information
papandreou authored and darrachequesne committed Sep 7, 2022
1 parent d62af2c commit 3a6d94d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
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 @@ -20,7 +20,7 @@
},
"dependencies": {
"debug": "~4.3.1",
"notepack.io": "~2.1.0",
"notepack.io": "~3.0.1",
"socket.io-parser": "~4.0.4"
},
"devDependencies": {
Expand Down
28 changes: 28 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ describe("emitter", () => {
});
});

it("should support the toJSON() method", (done) => {
// @ts-ignore
BigInt.prototype.toJSON = function () {
return String(this);
};

// @ts-ignore
Set.prototype.toJSON = function () {
return [...this];
};

class MyClass {
toJSON() {
return 4;
}
}

// @ts-ignore
emitter.emit("payload", 1n, new Set(["2", 3]), new MyClass());

clientSockets[0].on("payload", (a, b, c) => {
expect(a).to.eql("1");
expect(b).to.eql(["2", 3]);
expect(c).to.eql(4);
done();
});
});

it("should support all broadcast modifiers", () => {
emitter.in(["room1", "room2"]).emit("test");
emitter.except(["room4", "room5"]).emit("test");
Expand Down

0 comments on commit 3a6d94d

Please sign in to comment.