Skip to content

Commit

Permalink
Use function expression for statusReplacer and statusReviver in o…
Browse files Browse the repository at this point in the history
…rder to encode the raw `NodeId` before `toJSON` method is called on `IdInternal`
  • Loading branch information
CMCDragonkai committed Feb 7, 2022
1 parent 134ad24 commit cf54df9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/status/Status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,23 @@ class Status {
return statusInfo;
}

protected statusReplacer = (key, value) => {
/**
* Replacer used during encoding to JSON
* This is a function expression and not an arrow function expression
* because it needs to access the `this` inside the JSON.stringify
* in order to encode the `NodeId` before the `toJSON` of IdInternal is called
*/
protected statusReplacer = function (key: string, value: any): any {
if (key === 'nodeId') {
return nodesUtils.encodeNodeId(value.data);
return nodesUtils.encodeNodeId(this[key]);
}
return value;
};

protected statusReviver = (key, value) => {
/**
* Reviver used during decoding from JSON
*/
protected statusReviver = function (key: string, value: any): any {
if (key === 'nodeId') {
value = nodesUtils.decodeNodeId(value);
if (value == null) {
Expand Down

0 comments on commit cf54df9

Please sign in to comment.