Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
e3ffff5
Update article.md
astropsy999 May 24, 2022
ceb3946
Update article.md
astropsy999 May 24, 2022
f6aafd1
Update article.md
astropsy999 May 25, 2022
0f3f2a9
Update article.md
astropsy999 May 25, 2022
ba4c2b9
Update article.md
astropsy999 May 25, 2022
9a855d9
Update article.md
astropsy999 May 26, 2022
ca0b538
Update article.md
astropsy999 May 26, 2022
52e5ab9
Update article.md
astropsy999 May 26, 2022
7d621a6
Update article.md
astropsy999 May 26, 2022
b310810
Update index.html
astropsy999 May 26, 2022
46d37a3
Update article.md
astropsy999 Jun 4, 2022
d5d59f6
Update article.md
astropsy999 Jun 4, 2022
3fe04de
Update article.md
astropsy999 Jun 5, 2022
d161dd1
Update index.html
astropsy999 Jun 5, 2022
52bf3af
Update article.md
astropsy999 Jun 5, 2022
8f3e3fe
Update article.md
astropsy999 Jun 6, 2022
ac9996b
Update article.md
astropsy999 Jun 6, 2022
3bd140e
Update article.md
astropsy999 Jun 7, 2022
18a38a3
Update iframe.html
astropsy999 Jun 7, 2022
75d1f7c
Update index.html
astropsy999 Jun 7, 2022
d9660ab
Update iframe.html
astropsy999 Jun 7, 2022
fb08989
Update facebook.html
astropsy999 Jun 7, 2022
0ef68a9
Update index.html
astropsy999 Jun 7, 2022
6e53413
Update facebook.html
astropsy999 Jun 7, 2022
c80ed0f
Update index.html
astropsy999 Jun 7, 2022
469e2d3
Update article.md
astropsy999 Jun 8, 2022
f84fa7d
Merge branch 'master' into master
astropsy999 Jun 8, 2022
d4c6a64
Update article.md
astropsy999 Jun 8, 2022
935199a
Update article.md
astropsy999 Jun 9, 2022
5f35c46
Update article.md
astropsy999 Jun 10, 2022
999ab9d
Update task.md
astropsy999 Jun 12, 2022
fafe785
Update task.md
astropsy999 Jun 12, 2022
76007bc
Update index.html
astropsy999 Jun 12, 2022
84362d4
Update index.html
astropsy999 Jun 12, 2022
22fdad6
Update index.html
astropsy999 Jun 12, 2022
d051d1e
Update index.html
astropsy999 Jun 12, 2022
f0f4d93
Update index.html
astropsy999 Jun 12, 2022
58e0228
Update index.html
astropsy999 Jun 12, 2022
d3cdbac
Update index.html
astropsy999 Jun 12, 2022
410a0e1
Update index.html
astropsy999 Jun 12, 2022
18f9da2
Update 3-frames-and-windows/06-clickjacking/article.md
astropsy999 Jul 3, 2022
a6806c0
Update 3-frames-and-windows/06-clickjacking/article.md
astropsy999 Jul 3, 2022
82cd9d5
Update 3-frames-and-windows/06-clickjacking/article.md
astropsy999 Jul 3, 2022
6b1b439
Update 3-frames-and-windows/06-clickjacking/article.md
astropsy999 Jul 3, 2022
64ccba1
Update 3-frames-and-windows/06-clickjacking/article.md
astropsy999 Jul 3, 2022
e2a2219
Update 3-frames-and-windows/06-clickjacking/article.md
astropsy999 Jul 3, 2022
ab9ef8d
Update 3-frames-and-windows/06-clickjacking/article.md
astropsy999 Jul 3, 2022
af25da9
Update 3-frames-and-windows/06-clickjacking/clickjacking-visible.view…
astropsy999 Jul 3, 2022
3a10141
Update 3-frames-and-windows/06-clickjacking/clickjacking-visible.view…
astropsy999 Jul 3, 2022
08ed6b1
Update 3-frames-and-windows/06-clickjacking/clickjacking.view/index.html
astropsy999 Jul 3, 2022
74de077
Apply suggestions from code review
dolgachio Jul 12, 2022
494b88d
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 21, 2022
f3db6f5
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 21, 2022
afeb15b
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 21, 2022
10e610c
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 21, 2022
5d29d2d
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 21, 2022
118c6cf
Update 7-animation/2-css-animations/article.md
astropsy999 Jul 21, 2022
c0b84c4
Update 7-animation/2-css-animations/article.md
astropsy999 Jul 21, 2022
ab8be27
Update 7-animation/2-css-animations/article.md
astropsy999 Jul 21, 2022
5ae6e12
Update 7-animation/2-css-animations/article.md
astropsy999 Jul 21, 2022
d2973b3
Update 7-animation/2-css-animations/article.md
astropsy999 Jul 21, 2022
1da1d54
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 28, 2022
2078a03
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 28, 2022
9a56401
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 28, 2022
400fbf6
Update 5-network/12-server-sent-events/article.md
astropsy999 Jul 28, 2022
51a327f
Update 6-data-storage/03-indexeddb/article.md
astropsy999 Jul 28, 2022
80213a9
Update 6-data-storage/03-indexeddb/article.md
astropsy999 Jul 28, 2022
51fc021
Update 6-data-storage/03-indexeddb/article.md
astropsy999 Jul 28, 2022
6529a81
Update 6-data-storage/03-indexeddb/article.md
astropsy999 Jul 28, 2022
fd843a1
Update 7-animation/2-css-animations/article.md
astropsy999 Jul 28, 2022
4e193b5
Apply suggestions from code review
dolgachio Aug 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update article.md
95%
  • Loading branch information
astropsy999 authored May 26, 2022
commit 52e5ab92ca5a7ff457eacb4f11f0d6052eb0f523
44 changes: 22 additions & 22 deletions 6-data-storage/03-indexeddb/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,18 +719,18 @@ request.onsuccess = function() {
};
```

## Promise wrapper
## Обгортка промісів

Adding `onsuccess/onerror` to every request is quite a cumbersome task. Sometimes we can make our life easier by using event delegation, e.g. set handlers on the whole transactions, but `async/await` is much more convenient.
Додавання `onsuccess/onerror` до кожного запиту є досить громіздким завданням. Іноді ми можемо полегшити життя, використовуючи делегування подій, напр. встановити обробники для всіх транзакцій, але `async/await` набагато зручніше.

Let's use a thin promise wrapper <https://github.com/jakearchibald/idb> further in this chapter. It creates a global `idb` object with [promisified](info:promisify) IndexedDB methods.
Давайте використаємо тонку обгортку промісів <https://github.com/jakearchibald/idb> далі в цьому розділі. Вона створює глобальний об’єкт `idb` з методами IndexedDB [promisified](info:promisify).

Then, instead of `onsuccess/onerror` we can write like this:
Тоді замість `onsuccess/onerror` ми можемо написати так:

```js
let db = await idb.openDB('store', 1, db => {
if (db.oldVersion == 0) {
// perform the initialization
// виконати ініціалізацію
db.createObjectStore('books', {keyPath: 'id'});
}
});
Expand All @@ -751,33 +751,33 @@ try {

```

So we have all the sweet "plain async code" and "try..catch" stuff.
Тож маємо все солоденьке «простий асинхронний код» та "try..catch".

### Error handling
### Обробка помилок

If we don't catch an error, then it falls through, till the closest outer `try..catch`.
Якщо ми не ловимо помилку, вона провалюється до найближчого зовнішнього `try..catch`.

An uncaught error becomes an "unhandled promise rejection" event on `window` object.
Невиявлена помилка стає подією "необробленого відхилення промісу" на об’єкті `window`.

We can handle such errors like this:
Ми можемо обробляти такі помилки таким чином:

```js
window.addEventListener('unhandledrejection', event => {
let request = event.target; // IndexedDB native request object
let error = event.reason; // Unhandled error object, same as request.error
...report about the error...
let request = event.target; // Власний об’єкт запиту IndexedDB
let error = event.reason; // Необроблений об’єкт помилки, те саме, що request.error
...повідомити про помилку...
});
```

### "Inactive transaction" pitfall
### Підводний камінь «Неактивна транзакція».


As we already know, a transaction auto-commits as soon as the browser is done with the current code and microtasks. So if we put a *macrotask* like `fetch` in the middle of a transaction, then the transaction won't wait for it to finish. It just auto-commits. So the next request in it would fail.
Як ми вже знаємо, транзакція автоматично завершується, як тільки браузер закінчить роботу з поточним кодом і мікрозавданнями. Отже, якщо ми помістимо *макрозавдання* на кшталт `fetch` в середині транзакції, то транзакція не чекатиме завершення. Вона просто автоматично завершається. Отже, наступний запит буде невдалим.


For a promise wrapper and `async/await` the situation is the same.
Для обгортки промісів і `async/await` ситуація однакова.

Here's an example of `fetch` in the middle of the transaction:
Ось приклад `fetch` у середині транзакції:

```js
let transaction = db.transaction("inventory", "readwrite");
Expand All @@ -787,14 +787,14 @@ await inventory.add({ id: 'js', price: 10, created: new Date() });

await fetch(...); // (*)

await inventory.add({ id: 'js', price: 10, created: new Date() }); // Error
await inventory.add({ id: 'js', price: 10, created: new Date() }); // Помилка
```

The next `inventory.add` after `fetch` `(*)` fails with an "inactive transaction" error, because the transaction is already committed and closed at that time.
Наступний `inventory.add` після `fetch` `(*)` не вдається з помилкою "неактивна транзакція", оскільки на той момент транзакція вже завершена та закрита.

The workaround is the same as when working with native IndexedDB: either make a new transaction or just split things apart.
1. Prepare the data and fetch all that's needed first.
2. Then save in the database.
Обхідний шлях такий же, як і під час роботи з рідною IndexedDB: або зробіть нову транзакцію, або просто розділіть речі.
1. Підготуйте дані та спершу отримайте все необхідне.
2. Потім збережіть у базі даних.

### Getting native objects

Expand Down