Skip to content
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Let's store read messages in `WeakSet`:
بیایید پیام‌های خوانده شده را در `WeakSet` ذخیره کنیم:

```js run
let messages = [
Expand All @@ -9,35 +9,35 @@ let messages = [

let readMessages = new WeakSet();

// two messages have been read
// دو پیام خوانده شد
readMessages.add(messages[0]);
readMessages.add(messages[1]);
// readMessages has 2 elements
// دو المان دارد readMessages

// ...let's read the first message again!
// !بیایید اولین پیام را دوباره بخوانیم...
readMessages.add(messages[0]);
// readMessages still has 2 unique elements
// همچنان دو المان یکتا دارد readMessages

// answer: was the message[0] read?
// خوانده شده است؟ message جواب: آیا
alert("Read message 0: " + readMessages.has(messages[0])); // true

messages.shift();
// now readMessages has 1 element (technically memory may be cleaned later)
// یک المان دارد (از لحاظ فنی، حافظه ممکن است بعدا از آن المان تمیز شود) readMessages حالا
```

The `WeakSet` allows to store a set of messages and easily check for the existence of a message in it.
ساختار `WeakSet` به ما این امکان را می‌دهد که یک دسته از پیام‌ها را ذخیره کنیم و به راحتی بررسی کنیم که پیامی درون آن هست یا نه.

It cleans up itself automatically. The tradeoff is that we can't iterate over it, can't get "all read messages" from it directly. But we can do it by iterating over all messages and filtering those that are in the set.
این ساختار به طور خودکار محتوای دورنش را پاک می‌کند. اما بدی آن این است که ما نمی‌توانیم درون آن حلقه بزنیم، نمی‌توانیم به طور مستقیم «تمام پیام‌های خوانده شده» را از آن بگیریم. اما می‌توانیم این کار را با حلقه‌زدن درون تمام پیام‌ها و جداسازی آن‌هایی که درون set هستند، انجام دهیم.

Another, different solution could be to add a property like `message.isRead=true` to a message after it's read. As messages objects are managed by another code, that's generally discouraged, but we can use a symbolic property to avoid conflicts.
یک راه حل متفاوت دیگر می‌تواند اضافه کردن ویژگی `message.isRead=true` به پیام، بعد از اینکه خوانده شد باشد. به دلیل اینکه شیءهای پیام‌ها توسط کد دیگری انجام می‌شود، این کار توصیه نمی‌شود اما می‌توانیم از ویژگی سمبلی برای جلوگیری از تناقضات استفاده کنیم.

Like this:
مثلا اینگونه:
```js
// the symbolic property is only known to our code
// ویژگی سمبلی تنها در کد ما شناخته شده است
let isRead = Symbol("isRead");
messages[0][isRead] = true;
```

Now third-party code probably won't see our extra property.
حالا کد شخص ثالث احتمالا ویژگی اضافی ما را نخواهد دید.

Although symbols allow to lower the probability of problems, using `WeakSet` is better from the architectural point of view.
اگرچه سمبل‌ها به ما این امکان را می‌دهند که از احتمال بروز مشکل را کم کنیم، استفاده از `WeakSet` از نظر معماری بهتر است.
12 changes: 6 additions & 6 deletions 1-js/05-data-types/08-weakmap-weakset/01-recipients-read/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Store "unread" flags
# پرچم‌های «خوانده نشده» را ذخیره کنید

There's an array of messages:
یک آرایه از پیام‌هایی داریم:

```js
let messages = [
Expand All @@ -14,10 +14,10 @@ let messages = [
];
```

Your code can access it, but the messages are managed by someone else's code. New messages are added, old ones are removed regularly by that code, and you don't know the exact moments when it happens.
کد شما می‌تواند به آن دسترسی پیدا کند اما پیام‌ها توسط کد شخص دیگری مدیریت می‌شود. پیام‌های جدید اضافه می‌شوند و قدیمی‌ها توسط آن کد به طور منظم حذف می‌شوند و شما نمی‌دانید دقیقا کی اتفاق می‌افتد.

Now, which data structure could you use to store information about whether the message "has been read"? The structure must be well-suited to give the answer "was it read?" for the given message object.
حالا شما کدام ساختار داده را استفاده می‌کنید تا اطلاعاتی درباره اینکه پیام «خوانده شده یا نه» را دخیره کنید؟ ساختار باید برای جواب دادن به این سوال که «آیا خوانده شد؟» برای شیء داده شده به خوبی پاسخ دهد.

P.S. When a message is removed from `messages`, it should disappear from your structure as well.
پی‌نوشت: زمانی که یک پیام از `messages` حذف شود، باید از ساختار شما هم حذف شود.

P.P.S. We shouldn't modify message objects, add our properties to them. As they are managed by someone else's code, that may lead to bad consequences.
پی‌نوشت دوم: ما نباید شیء‌های پیام را تغییر دهیم یا ویژگی‌های خودمان را به آنها اضافه کنیم. به دلیل اینکه آنها توسط کد شخص دیگری کنترل می‌شوند، این کار ممکن است نتایج بدی داشته باشد.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

To store a date, we can use `WeakMap`:
برای ذخیره یک تاریخ، می‌توانیم از `WeakMap` استفاده کنیم:

```js
let messages = [
Expand All @@ -11,5 +11,5 @@ let messages = [
let readMap = new WeakMap();

readMap.set(messages[0], new Date(2017, 1, 1));
// Date object we'll study later
// را بعدا می‌آموزیم Date شیء
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Store read dates
# تاریخ خواندن را ذخیره کنید

There's an array of messages as in the [previous task](info:task/recipients-read). The situation is similar.
یک آرایه از پیام‌ها مانند [تکلیف قبلی](info:task/recipients-read) داریم. وضعیت هم مشابه است.

```js
let messages = [
Expand All @@ -14,8 +14,8 @@ let messages = [
];
```

The question now is: which data structure you'd suggest to store the information: "when the message was read?".
حالا سوال این است: کدام ساختار داده را برای ذخیره اطلاعات «در چه تاریخی پیام خوانده شد؟» استفاده می‌کنید.

In the previous task we only needed to store the "yes/no" fact. Now we need to store the date, and it should only remain in memory until the message is garbage collected.
در تکلیف قبلی ما فقط نیاز داشتیم که «بله/خیر» را ذخیره کنیم. حالا نیاز داریم که تاریخ را ذخیره کنیم و باید تا زمانی که پیام زباله‌روبی شود باقی بماند.

P.S. Dates can be stored as objects of built-in `Date` class, that we'll cover later.
پی‌نوشت: تاریخ‌ها می‌توانند به عنوان شیء از کلاس درون ساخت `Date` ذخیره شوند که بعدا آن را پوشش می‌دهیم.
Loading