dalyIsaac / onenote-markdown

https://onenote-markdown.azurewebsites.net/
MIT License
43 stars 2 forks source link

Object references aren't updated after a function call. #15

Closed dalyIsaac closed 5 years ago

dalyIsaac commented 5 years ago

In the deleteNode function in src/page/tree/delete.ts, yIndex is defined as a number relating to the index of y in page.nodes, and and y is defined as a node in page.nodes, as seen in https://github.com/dalyIsaac/onenote-markdown-prototype/blob/515b7fd7bd11e6677fa48c1d4c5707307be8f0de/src/page/tree/delete.ts#L211-L237

Later, in the same function, https://github.com/dalyIsaac/onenote-markdown-prototype/blob/515b7fd7bd11e6677fa48c1d4c5707307be8f0de/src/page/tree/delete.ts#L280-L285

If page.nodes[yIndex] is updated to point to a new object, the reference for y will point to an old object.

Thus, in order to avoid variables not being updated, from now on do not use variables like y: Node, and rely on page.nodes[yIndex].

The downsides are that scenarios like https://github.com/dalyIsaac/onenote-markdown-prototype/blob/515b7fd7bd11e6677fa48c1d4c5707307be8f0de/src/page/tree/delete.ts#L324

become

if (page.nodes[page.nodes[x].parent].left === x) {

where xIndex: number is replaced by x: number, and x: Node is no longer used.

This is in a similar vein to #5.

Related files: