Closed graph theorem: How do domain and codomain affect continuity? I had to examine the closed graph theorem under the following circumstances:

$X, Y$ metric spaces with $Y$ compact. Does the theorem also hold if Y is not compact?
(Assuming compactness in the context of metric spaces means complete and bounded. → link)

I have an idea of how to proceed (draft below), but I cannot visualize—and hence incorporate into the proof—why $Y$ would need to be compact, but not $X$.
For example, $f:\mathbb R \to \mathbb R, x \mapsto x$, is continuous, while $\mathbb R$ is not compact. What am I missing?

$(\Rightarrow)$ by contradiction:
Assume $f$ is continuous, but $G(f)$ is not closed. Then  $$\exists (x_n,y_n)_n\in G(f): \lim_{n \to \infty} (x_n, y_n)\not\in G(f).$$
But since the graph only consists of tuples where $y_n = f(x_n)$ and $f$ is continuous that leads to a contradiction.
$(\Leftarrow)$ worked out in a similar fashion: If there is a sequence $x_n \in X$ with limit $c \in X$ such that the limit of $f(x_n)\not = f(c)$, then the graph would have at least one point—$(c,f(c))$—afflickting the graph's closedness.
 A: In fact, this theorem holds if both $X,Y$ are F-spaces, or metric spaces with a complete invariant metric.
The reason why $Y$ is assumed compact is because this is a necessary condition for every projection $\pi_1:X\times Y\rightarrow X$ to be a closed map. This is discussed in the post here. However, this claim can be omitted using the fact that $f$ is a linear function.
To prove this using only the fact $X,Y$ are F-spaces and $f:X\rightarrow Y$ linear, and $G(f)$ closed:
Since $f$ is linear, $G(f)$ is a subspace of $X\times Y$ under the metric $d((x_1,y_1),(x_2,y_2))=d_X(x_1,x_2) + d_Y(y_1,y_2)$. Since $G(f)$ is assumed closed, it is also an F-space. Take the mappings
$\pi_1:G\rightarrow X$ and $\pi_2:X\times Y\rightarrow Y$ by $\pi_1(x,f(x))=x$ and $\pi_2(x,y)=y$.
The map $\pi_1$ is an injective continuous linear mapping of $G$ onto $X$. So it has an inverse $\pi_1^{-1}$ which is continuous by the open mapping theorem.
Also, $f=\pi_2\circ \pi_1^{-1}$ and $\pi_2$ is continuous. So $f$ is continuous.
