Tracing What Keeps a Match Alive
In this lesson, you trace what keeps a match alive in JavaScript. The match-log processor’s
scores are right, but it still holds a whole day of match records. Your Python teammate’s fix:
set each match to None when the handler’s done with it.
In Python, the handler builds match M1 as a small dataclass and appends it to history, the
replay list. weakref.finalize() prints a line the moment CPython frees M1. Then the script sets match to None and clears the history:
import weakref
from dataclasses import dataclass
@dataclass
class Match:
id: str
points: int
match = Match("M1", 3)
weakref.finalize(match, print, "M1 freed")
history = []
history.append(match)
match = None
print("match is None")
history.clear()
print("history cleared")
Node’s REPL runs the JavaScript port line by line and echoes each result. A let holds the
match, since the port reassigns it, and a const holds the history. Declarations echo
undefined, since they have no value. .push() echoes the new length, 1, and match = null
echoes null:
> let match = { id: "M1", points: 3 };
undefined
> const history = [];
undefined
> history.push(match);
1
> match = null;
null
> history
[ { id: 'M1', points: 3 } ]