Skip to content

Commit e395ab4

Browse files
committed
chore: improvements after review
1 parent 2640c0d commit e395ab4

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

  • 11-advanced-recipes
    • 01-local-initialization-check
    • 02-async-init-queue
    • 03-async-init-queues-state

11-advanced-recipes/01-local-initialization-check/db.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ import { setTimeout } from 'node:timers/promises'
22

33
class Database {
44
connected = false
5+
#pendingConnection = null
56

67
async connect() {
78
if (!this.connected) {
9+
if (this.#pendingConnection) {
10+
return this.#pendingConnection
11+
}
812
// simulate the delay of the connection
9-
await setTimeout(500)
13+
this.#pendingConnection = setTimeout(500)
14+
await this.#pendingConnection
1015
this.connected = true
16+
this.#pendingConnection = null
1117
}
1218
}
1319

11-advanced-recipes/02-async-init-queue/db.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@ import { setTimeout } from 'node:timers/promises'
22

33
class Database {
44
connected = false
5+
#pendingConnection = null
56
commandsQueue = []
67

78
async connect() {
8-
// simulate the delay of the connection
9-
await setTimeout(500)
10-
this.connected = true
11-
while (this.commandsQueue.length > 0) {
12-
const command = this.commandsQueue.shift()
13-
command()
9+
if (!this.connected) {
10+
if (this.#pendingConnection) {
11+
return this.#pendingConnection
12+
}
13+
// simulate the delay of the connection
14+
this.#pendingConnection = setTimeout(500)
15+
await this.#pendingConnection
16+
this.connected = true
17+
this.#pendingConnection = null
18+
// once connected executes all the queued commands
19+
while (this.commandsQueue.length > 0) {
20+
const command = this.commandsQueue.shift()
21+
command()
22+
}
1423
}
1524
}
1625

11-advanced-recipes/03-async-init-queues-state/db.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { setTimeout } from 'node:timers/promises'
33
const deactivate = Symbol('deactivate')
44

55
class InitializedState {
6+
constructor(db) {
7+
this.db = db
8+
}
9+
610
async query(queryString) {
711
// simulate the delay of the query execution
812
await setTimeout(100)
@@ -35,6 +39,9 @@ class QueuingState {
3539
}
3640

3741
class Database {
42+
connected = false
43+
#pendingConnection = null
44+
3845
constructor() {
3946
this.state = new QueuingState(this)
4047
}
@@ -45,12 +52,20 @@ class Database {
4552
}
4653

4754
async connect() {
48-
// simulate the delay of the connection
49-
await setTimeout(500)
50-
this.connected = true
51-
const oldState = this.state
52-
this.state = new InitializedState(this)
53-
oldState[deactivate]?.()
55+
if (!this.connected) {
56+
if (this.#pendingConnection) {
57+
return this.#pendingConnection
58+
}
59+
// simulate the delay of the connection
60+
this.#pendingConnection = setTimeout(500)
61+
await this.#pendingConnection
62+
this.connected = true
63+
this.#pendingConnection = null
64+
// once connected update the state
65+
const oldState = this.state
66+
this.state = new InitializedState(this)
67+
oldState[deactivate]?.()
68+
}
5469
}
5570
}
5671

0 commit comments

Comments
 (0)