File tree Expand file tree Collapse file tree
01-local-initialization-check
03-async-init-queues-state Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,12 +2,18 @@ import { setTimeout } from 'node:timers/promises'
22
33class 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
Original file line number Diff line number Diff line change @@ -2,15 +2,24 @@ import { setTimeout } from 'node:timers/promises'
22
33class 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
Original file line number Diff line number Diff line change @@ -3,6 +3,10 @@ import { setTimeout } from 'node:timers/promises'
33const deactivate = Symbol ( 'deactivate' )
44
55class 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
3741class 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
You can’t perform that action at this time.
0 commit comments