@@ -22,6 +22,9 @@ const $allDisabledButtons = document.querySelectorAll('button:disabled')
22
22
const $allDisabledInputs = document . querySelectorAll ( 'input:disabled' )
23
23
const $allDisabledElements = document . querySelectorAll ( '.disabled' )
24
24
25
+ const FILES = [ ]
26
+ const workspace = location . hash
27
+
25
28
let node
26
29
let info
27
30
let Buffer
@@ -33,6 +36,9 @@ let Buffer
33
36
function start ( ) {
34
37
if ( ! node ) {
35
38
const options = {
39
+ EXPERIMENTAL : {
40
+ pubsub : true
41
+ } ,
36
42
repo : 'ipfs-' + Math . random ( ) ,
37
43
config : {
38
44
Addresses : {
@@ -52,16 +58,51 @@ function start () {
52
58
updateView ( 'ready' , node )
53
59
onSuccess ( 'Node is ready.' )
54
60
setInterval ( refreshPeerList , 1000 )
61
+ setInterval ( sendFileList , 10000 )
55
62
} )
56
63
. catch ( ( error ) => onError ( err ) )
64
+
65
+ subscribeToWorkpsace ( )
57
66
} )
58
67
}
59
68
}
60
69
70
+ /* ===========================================================================
71
+ Pubsub
72
+ =========================================================================== */
73
+
74
+ const messageHandler = ( message ) => {
75
+ const myNode = info . id
76
+ const hash = message . data . toString ( )
77
+ const messageSender = message . from
78
+
79
+ // append new files when someone uploads them
80
+ if ( myNode !== messageSender && ! isFileInList ( hash ) ) {
81
+ $multihashInput . value = hash
82
+ getFile ( )
83
+ }
84
+ }
85
+
86
+ const subscribeToWorkpsace = ( ) => {
87
+ node . pubsub . subscribe ( workspace , messageHandler )
88
+ . catch ( ( ) => onError ( 'An error occurred when subscribing to the workspace.' ) )
89
+ }
90
+
91
+ const publishHash = ( hash ) => {
92
+ const data = Buffer . from ( hash )
93
+
94
+ node . pubsub . publish ( workspace , data )
95
+ . catch ( ( error ) => onError ( 'An error occurred when publishing the message.' ) )
96
+ }
97
+
61
98
/* ===========================================================================
62
99
Files handling
63
100
=========================================================================== */
64
101
102
+ const isFileInList = ( hash ) => FILES . indexOf ( hash ) !== - 1
103
+
104
+ const sendFileList = ( ) => FILES . forEach ( ( hash ) => publishHash ( hash ) )
105
+
65
106
function appendFile ( name , hash , size , data ) {
66
107
const file = new window . Blob ( [ data ] , { type : 'application/octet-binary' } )
67
108
const url = window . URL . createObjectURL ( file )
@@ -89,22 +130,28 @@ function appendFile (name, hash, size, data) {
89
130
row . appendChild ( downloadCell )
90
131
91
132
$fileHistory . insertBefore ( row , $fileHistory . firstChild )
133
+
134
+ publishHash ( hash )
92
135
}
93
136
94
137
function getFile ( ) {
95
- const cid = $multihashInput . value
138
+ const hash = $multihashInput . value
96
139
97
140
$multihashInput . value = ''
98
141
99
- if ( ! cid ) {
142
+ if ( ! hash ) {
100
143
return onError ( 'No multihash was inserted.' )
144
+ } else if ( isFileInList ( hash ) ) {
145
+ return onSuccess ( 'The file is already in the current workspace.' )
101
146
}
102
147
103
- node . files . get ( cid )
148
+ FILES . push ( hash )
149
+
150
+ node . files . get ( hash )
104
151
. then ( ( files ) => {
105
152
files . forEach ( ( file ) => {
106
153
if ( file . content ) {
107
- appendFile ( file . name , cid , file . size , file . content )
154
+ appendFile ( file . name , hash , file . size , file . content )
108
155
onSuccess ( `The ${ file . name } file was added.` )
109
156
$emptyRow . style . display = 'none'
110
157
}
@@ -260,12 +307,13 @@ function updateView (state, ipfs) {
260
307
const startApplication = ( ) => {
261
308
// Setup event listeners
262
309
$dragContainer . addEventListener ( 'dragenter' , onDragEnter )
310
+ $dragContainer . addEventListener ( 'dragover' , onDragEnter )
263
311
$dragContainer . addEventListener ( 'drop' , onDrop )
264
312
$dragContainer . addEventListener ( 'dragleave' , onDragLeave )
265
313
$fetchButton . addEventListener ( 'click' , getFile )
266
314
$connectButton . addEventListener ( 'click' , connectToPeer )
267
315
268
- start ( ) ;
316
+ start ( )
269
317
}
270
318
271
319
startApplication ( )
0 commit comments