forked from javascript-tutorial/en.javascript.info
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request javascript-tutorial#3609 from WOLFRIEND/master
Add WeakRef and FinalizationRegistry article
- Loading branch information
Showing
22 changed files
with
1,787 additions
and
0 deletions.
There are no files selected for viewing
483 changes: 483 additions & 0 deletions
483
1-js/99-js-misc/07-weakref-finalizationregistry/article.md
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+55.1 KB
1-js/99-js-misc/07-weakref-finalizationregistry/google-chrome-developer-tools.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions
49
1-js/99-js-misc/07-weakref-finalizationregistry/weakref-dom.view/index.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
.app { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 16px; | ||
} | ||
|
||
.start-messages { | ||
width: fit-content; | ||
} | ||
|
||
.window { | ||
width: 100%; | ||
border: 2px solid #464154; | ||
overflow: hidden; | ||
} | ||
|
||
.window__header { | ||
position: sticky; | ||
padding: 8px; | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
background-color: #736e7e; | ||
} | ||
|
||
.window__title { | ||
margin: 0; | ||
font-size: 24px; | ||
font-weight: 700; | ||
color: white; | ||
letter-spacing: 1px; | ||
} | ||
|
||
.window__button { | ||
padding: 4px; | ||
background: #4f495c; | ||
outline: none; | ||
border: 2px solid #464154; | ||
color: white; | ||
font-size: 16px; | ||
cursor: pointer; | ||
} | ||
|
||
.window__body { | ||
height: 250px; | ||
padding: 16px; | ||
overflow: scroll; | ||
background-color: #736e7e33; | ||
} |
28 changes: 28 additions & 0 deletions
28
1-js/99-js-misc/07-weakref-finalizationregistry/weakref-dom.view/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!DOCTYPE HTML> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<link rel="stylesheet" href="index.css"> | ||
<title>WeakRef DOM Logger</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<div class="app"> | ||
<button class="start-messages">Start sending messages</button> | ||
<div class="window"> | ||
<div class="window__header"> | ||
<p class="window__title">Messages:</p> | ||
<button class="window__button">Close</button> | ||
</div> | ||
<div class="window__body"> | ||
No messages. | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
<script type="module" src="index.js"></script> | ||
</body> | ||
</html> |
24 changes: 24 additions & 0 deletions
24
1-js/99-js-misc/07-weakref-finalizationregistry/weakref-dom.view/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const startMessagesBtn = document.querySelector('.start-messages'); // (1) | ||
const closeWindowBtn = document.querySelector('.window__button'); // (2) | ||
const windowElementRef = new WeakRef(document.querySelector(".window__body")); // (3) | ||
|
||
startMessagesBtn.addEventListener('click', () => { // (4) | ||
startMessages(windowElementRef); | ||
startMessagesBtn.disabled = true; | ||
}); | ||
|
||
closeWindowBtn.addEventListener('click', () => document.querySelector(".window__body").remove()); // (5) | ||
|
||
|
||
const startMessages = (element) => { | ||
const timerId = setInterval(() => { // (6) | ||
if (element.deref()) { // (7) | ||
const payload = document.createElement("p"); | ||
payload.textContent = `Message: System status OK: ${new Date().toLocaleTimeString()}`; | ||
element.deref().append(payload); | ||
} else { // (8) | ||
alert("The element has been deleted."); // (9) | ||
clearInterval(timerId); | ||
} | ||
}, 1000); | ||
}; |
32 changes: 32 additions & 0 deletions
32
.../99-js-misc/07-weakref-finalizationregistry/weakref-finalizationregistry-01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions
33
.../99-js-misc/07-weakref-finalizationregistry/weakref-finalizationregistry-02.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.