@@ -24,8 +24,12 @@ const $allDisabledButtons = document.querySelectorAll('button:disabled')
24
24
const $allDisabledInputs = document . querySelectorAll ( 'input:disabled' )
25
25
const $allDisabledElements = document . querySelectorAll ( '.disabled' )
26
26
27
- const FILES = [ ]
28
- const workspace = location . hash
27
+ // Workspace inputs
28
+ const $workspaceInput = document . querySelector ( '#workspace-input' )
29
+ const $workspaceBtn = document . querySelector ( '#workspace-btn' )
30
+
31
+ let FILES = [ ]
32
+ let workspace = location . hash
29
33
30
34
let fileSize = 0
31
35
@@ -65,6 +69,8 @@ function start () {
65
69
. catch ( ( error ) => onError ( error ) )
66
70
67
71
subscribeToWorkpsace ( )
72
+
73
+ window . addEventListener ( 'hashchange' , workspaceUpdated )
68
74
} )
69
75
}
70
76
}
@@ -87,9 +93,25 @@ const messageHandler = (message) => {
87
93
88
94
const subscribeToWorkpsace = ( ) => {
89
95
node . pubsub . subscribe ( workspace , messageHandler )
96
+ . then ( ( ) => {
97
+ const msg = `Subscribed to workspace ${ workspace } `
98
+ $logs . innerHTML = msg
99
+ } )
90
100
. catch ( ( ) => onError ( 'An error occurred when subscribing to the workspace.' ) )
91
101
}
92
102
103
+ // unsubscribe from old workspace and re-subscribe to new one
104
+ const workspaceUpdated = ( ) => {
105
+ node . pubsub . unsubscribe ( workspace ) . then ( ( ) => {
106
+ // clear files from old workspace
107
+ FILES = [ ]
108
+ $fileHistory . innerHTML = ''
109
+
110
+ workspace = location . hash
111
+ subscribeToWorkpsace ( )
112
+ } )
113
+ }
114
+
93
115
const publishHash = ( hash ) => {
94
116
const data = Buffer . from ( hash )
95
117
@@ -327,6 +349,9 @@ const startApplication = () => {
327
349
$dragContainer . addEventListener ( 'dragleave' , onDragLeave )
328
350
$fetchButton . addEventListener ( 'click' , getFile )
329
351
$connectButton . addEventListener ( 'click' , connectToPeer )
352
+ $workspaceBtn . addEventListener ( 'click' , ( ) => {
353
+ window . location . hash = $workspaceInput . value
354
+ } )
330
355
331
356
start ( )
332
357
}
0 commit comments