Skip to content

Commit 5ab1ce2

Browse files
authoredNov 15, 2023
Merge pull request #3609 from WOLFRIEND/master
Add WeakRef and FinalizationRegistry article
2 parents 285083f + 8ab6b39 commit 5ab1ce2

22 files changed

+1787
-0
lines changed
 

‎1-js/99-js-misc/07-weakref-finalizationregistry/article.md

+483
Large diffs are not rendered by default.
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.app {
2+
display: flex;
3+
flex-direction: column;
4+
gap: 16px;
5+
}
6+
7+
.start-messages {
8+
width: fit-content;
9+
}
10+
11+
.window {
12+
width: 100%;
13+
border: 2px solid #464154;
14+
overflow: hidden;
15+
}
16+
17+
.window__header {
18+
position: sticky;
19+
padding: 8px;
20+
display: flex;
21+
justify-content: space-between;
22+
align-items: center;
23+
background-color: #736e7e;
24+
}
25+
26+
.window__title {
27+
margin: 0;
28+
font-size: 24px;
29+
font-weight: 700;
30+
color: white;
31+
letter-spacing: 1px;
32+
}
33+
34+
.window__button {
35+
padding: 4px;
36+
background: #4f495c;
37+
outline: none;
38+
border: 2px solid #464154;
39+
color: white;
40+
font-size: 16px;
41+
cursor: pointer;
42+
}
43+
44+
.window__body {
45+
height: 250px;
46+
padding: 16px;
47+
overflow: scroll;
48+
background-color: #736e7e33;
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE HTML>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<link rel="stylesheet" href="index.css">
7+
<title>WeakRef DOM Logger</title>
8+
</head>
9+
10+
<body>
11+
12+
<div class="app">
13+
<button class="start-messages">Start sending messages</button>
14+
<div class="window">
15+
<div class="window__header">
16+
<p class="window__title">Messages:</p>
17+
<button class="window__button">Close</button>
18+
</div>
19+
<div class="window__body">
20+
No messages.
21+
</div>
22+
</div>
23+
</div>
24+
25+
26+
<script type="module" src="index.js"></script>
27+
</body>
28+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const startMessagesBtn = document.querySelector('.start-messages'); // (1)
2+
const closeWindowBtn = document.querySelector('.window__button'); // (2)
3+
const windowElementRef = new WeakRef(document.querySelector(".window__body")); // (3)
4+
5+
startMessagesBtn.addEventListener('click', () => { // (4)
6+
startMessages(windowElementRef);
7+
startMessagesBtn.disabled = true;
8+
});
9+
10+
closeWindowBtn.addEventListener('click', () => document.querySelector(".window__body").remove()); // (5)
11+
12+
13+
const startMessages = (element) => {
14+
const timerId = setInterval(() => { // (6)
15+
if (element.deref()) { // (7)
16+
const payload = document.createElement("p");
17+
payload.textContent = `Message: System status OK: ${new Date().toLocaleTimeString()}`;
18+
element.deref().append(payload);
19+
} else { // (8)
20+
alert("The element has been deleted."); // (9)
21+
clearInterval(timerId);
22+
}
23+
}, 1000);
24+
};
Loading
Loading

0 commit comments

Comments
 (0)
Failed to load comments.