@@ -2,8 +2,9 @@ const {CompositeDisposable, Emitter, TextEditor, TextBuffer} = require('atom')
2
2
const { Errors, FollowState} = require ( '@atom/teletype-client' )
3
3
const BufferBinding = require ( './buffer-binding' )
4
4
const EditorBinding = require ( './editor-binding' )
5
- const EmptyPortalPaneItem = require ( './empty-portal-pane-item' )
6
5
const SitePositionsController = require ( './site-positions-controller' )
6
+ const getPathWithNativeSeparators = require ( './get-path-with-native-separators' )
7
+ const getEditorURI = require ( './get-editor-uri' )
7
8
const NOOP = ( ) => { }
8
9
9
10
module . exports =
@@ -15,14 +16,14 @@ class GuestPortalBinding {
15
16
this . notificationManager = notificationManager
16
17
this . emitDidDispose = didDispose || NOOP
17
18
this . lastActivePaneItem = null
18
- this . editorBindingsByEditorProxy = new Map ( )
19
- this . bufferBindingsByBufferProxy = new Map ( )
19
+ this . editorBindingsByEditorProxyId = new Map ( )
20
+ this . bufferBindingsByBufferProxyId = new Map ( )
20
21
this . editorProxiesByEditor = new WeakMap ( )
22
+ this . editorProxiesMetadataById = new Map ( )
21
23
this . emitter = new Emitter ( )
22
24
this . subscriptions = new CompositeDisposable ( )
23
25
this . lastEditorProxyChangePromise = Promise . resolve ( )
24
26
this . shouldRelayActiveEditorChanges = true
25
- this . lastDestroyedEditor = null
26
27
}
27
28
28
29
async initialize ( ) {
@@ -32,10 +33,8 @@ class GuestPortalBinding {
32
33
33
34
this . sitePositionsController = new SitePositionsController ( { portal : this . portal , workspace : this . workspace } )
34
35
this . subscriptions . add ( this . workspace . onDidChangeActivePaneItem ( this . didChangeActivePaneItem . bind ( this ) ) )
35
- this . subscriptions . add ( this . workspace . onDidDestroyPaneItem ( this . didDestroyPaneItem . bind ( this ) ) )
36
36
37
37
await this . portal . setDelegate ( this )
38
- await this . toggleEmptyPortalPaneItem ( )
39
38
40
39
return true
41
40
} catch ( error ) {
@@ -47,7 +46,6 @@ class GuestPortalBinding {
47
46
dispose ( ) {
48
47
this . subscriptions . dispose ( )
49
48
this . sitePositionsController . destroy ( )
50
- if ( this . emptyPortalItem ) this . emptyPortalItem . destroy ( )
51
49
52
50
this . emitDidDispose ( )
53
51
}
@@ -66,28 +64,36 @@ class GuestPortalBinding {
66
64
this . emitter . emit ( 'did-change' )
67
65
}
68
66
69
- addEditorProxy ( editorProxy ) {
70
- // TODO Implement in order to allow guests to open any editor that's in the host's workspace
71
- }
67
+ didChangeEditorProxies ( ) { }
72
68
73
- removeEditorProxy ( editorProxy ) {
74
- this . lastEditorProxyChangePromise = this . lastEditorProxyChangePromise . then ( async ( ) => {
75
- const editorBinding = this . editorBindingsByEditorProxy . get ( editorProxy )
76
- if ( editorBinding ) {
77
- const isRetracted = this . portal . resolveFollowState ( ) === FollowState . RETRACTED
78
- this . shouldRelayActiveEditorChanges = ! isRetracted
79
- editorBinding . dispose ( )
80
- this . shouldRelayActiveEditorChanges = true
69
+ getRemoteEditors ( ) {
70
+ const hostIdentity = this . portal . getSiteIdentity ( 1 )
71
+ const bufferProxyIds = new Set ( )
72
+ const remoteEditors = [ ]
73
+ const editorProxiesMetadata = this . portal . getEditorProxiesMetadata ( )
81
74
82
- if ( this . editorBindingsByEditorProxy . size === 0 ) {
83
- this . portal . follow ( 1 )
84
- }
75
+ for ( let i = 0 ; i < editorProxiesMetadata . length ; i ++ ) {
76
+ const { id , bufferProxyId , bufferProxyURI } = editorProxiesMetadata [ i ]
77
+ if ( bufferProxyIds . has ( bufferProxyId ) ) continue
85
78
86
- await this . toggleEmptyPortalPaneItem ( )
87
- }
88
- } )
79
+ remoteEditors . push ( {
80
+ hostGitHubUsername : hostIdentity . login ,
81
+ uri : getEditorURI ( this . portal . id , id ) ,
82
+ path : getPathWithNativeSeparators ( bufferProxyURI )
83
+ } )
84
+ bufferProxyIds . add ( bufferProxyId )
85
+ }
89
86
90
- return this . lastEditorProxyChangePromise
87
+ return remoteEditors
88
+ }
89
+
90
+ async getRemoteEditor ( editorProxyId ) {
91
+ const editorProxy = await this . portal . findOrFetchEditorProxy ( editorProxyId )
92
+ if ( editorProxy ) {
93
+ return this . findOrCreateEditorForEditorProxy ( editorProxy )
94
+ } else {
95
+ return null
96
+ }
91
97
}
92
98
93
99
updateActivePositions ( positionsBySiteId ) {
@@ -111,12 +117,11 @@ class GuestPortalBinding {
111
117
this . shouldRelayActiveEditorChanges = false
112
118
await this . openPaneItem ( editor )
113
119
this . shouldRelayActiveEditorChanges = true
114
- await this . toggleEmptyPortalPaneItem ( )
115
120
} else {
116
- this . editorBindingsByEditorProxy . forEach ( ( b ) => b . updateTether ( followState ) )
121
+ this . editorBindingsByEditorProxyId . forEach ( ( b ) => b . updateTether ( followState ) )
117
122
}
118
123
119
- const editorBinding = this . editorBindingsByEditorProxy . get ( editorProxy )
124
+ const editorBinding = this . editorBindingsByEditorProxyId . get ( editorProxy . id )
120
125
if ( editorBinding && position ) {
121
126
editorBinding . updateTether ( followState , position )
122
127
}
@@ -125,7 +130,7 @@ class GuestPortalBinding {
125
130
// Private
126
131
findOrCreateEditorForEditorProxy ( editorProxy ) {
127
132
let editor
128
- let editorBinding = this . editorBindingsByEditorProxy . get ( editorProxy )
133
+ let editorBinding = this . editorBindingsByEditorProxyId . get ( editorProxy . id )
129
134
if ( editorBinding ) {
130
135
editor = editorBinding . editor
131
136
} else {
@@ -140,12 +145,20 @@ class GuestPortalBinding {
140
145
editorBinding . setEditorProxy ( editorProxy )
141
146
editorProxy . setDelegate ( editorBinding )
142
147
143
- this . editorBindingsByEditorProxy . set ( editorProxy , editorBinding )
148
+ this . editorBindingsByEditorProxyId . set ( editorProxy . id , editorBinding )
144
149
this . editorProxiesByEditor . set ( editor , editorProxy )
150
+
151
+ const didDestroyEditorSubscription = editor . onDidDestroy ( ( ) => editorBinding . dispose ( ) )
145
152
editorBinding . onDidDispose ( ( ) => {
146
- this . lastDestroyedEditor = editor
153
+ didDestroyEditorSubscription . dispose ( )
154
+
155
+ const isRetracted = this . portal . resolveFollowState ( ) === FollowState . RETRACTED
156
+ this . shouldRelayActiveEditorChanges = ! isRetracted
157
+ editor . destroy ( )
158
+ this . shouldRelayActiveEditorChanges = true
159
+
147
160
this . editorProxiesByEditor . delete ( editor )
148
- this . editorBindingsByEditorProxy . delete ( editorProxy )
161
+ this . editorBindingsByEditorProxyId . delete ( editorProxy . id )
149
162
} )
150
163
151
164
this . sitePositionsController . addEditorBinding ( editorBinding )
@@ -156,34 +169,23 @@ class GuestPortalBinding {
156
169
// Private
157
170
findOrCreateBufferForBufferProxy ( bufferProxy ) {
158
171
let buffer
159
- let bufferBinding = this . bufferBindingsByBufferProxy . get ( bufferProxy )
172
+ let bufferBinding = this . bufferBindingsByBufferProxyId . get ( bufferProxy . id )
160
173
if ( bufferBinding ) {
161
174
buffer = bufferBinding . buffer
162
175
} else {
163
176
buffer = new TextBuffer ( )
164
177
bufferBinding = new BufferBinding ( {
165
178
buffer,
166
179
isHost : false ,
167
- didDispose : ( ) => this . bufferBindingsByBufferProxy . delete ( bufferProxy )
180
+ didDispose : ( ) => this . bufferBindingsByBufferProxyId . delete ( bufferProxy . id )
168
181
} )
169
182
bufferBinding . setBufferProxy ( bufferProxy )
170
183
bufferProxy . setDelegate ( bufferBinding )
171
- this . bufferBindingsByBufferProxy . set ( bufferProxy , bufferBinding )
184
+ this . bufferBindingsByBufferProxyId . set ( bufferProxy . id , bufferBinding )
172
185
}
173
186
return buffer
174
187
}
175
188
176
- // Private
177
- async toggleEmptyPortalPaneItem ( ) {
178
- const emptyPortalItem = this . getEmptyPortalPaneItem ( )
179
- const pane = this . workspace . paneForItem ( emptyPortalItem )
180
- if ( this . editorBindingsByEditorProxy . size === 0 ) {
181
- if ( ! pane ) await this . openPaneItem ( emptyPortalItem )
182
- } else {
183
- if ( pane ) emptyPortalItem . destroy ( )
184
- }
185
- }
186
-
187
189
activate ( ) {
188
190
const paneItem = this . lastActivePaneItem
189
191
const pane = this . workspace . paneForItem ( paneItem )
@@ -241,48 +243,25 @@ class GuestPortalBinding {
241
243
didChangeActivePaneItem ( paneItem ) {
242
244
const editorProxy = this . editorProxiesByEditor . get ( paneItem )
243
245
244
- if ( editorProxy || paneItem === this . getEmptyPortalPaneItem ( ) ) {
246
+ if ( editorProxy ) {
245
247
this . sitePositionsController . show ( paneItem . element )
246
248
} else {
247
249
this . sitePositionsController . hide ( )
248
250
}
249
251
250
- if ( this . shouldRelayActiveEditorChanges && paneItem !== this . getEmptyPortalPaneItem ( ) ) {
252
+ if ( this . shouldRelayActiveEditorChanges ) {
251
253
this . portal . activateEditorProxy ( editorProxy )
252
254
}
253
255
}
254
256
255
- didDestroyPaneItem ( { item} ) {
256
- const emptyPortalItem = this . getEmptyPortalPaneItem ( )
257
- const hasNoPortalPaneItem = this . workspace . getPaneItems ( ) . every ( ( item ) => (
258
- item !== emptyPortalItem && ! this . editorProxiesByEditor . has ( item )
259
- ) )
260
- const lastDestroyedEditorWasClosedManually = this . lastDestroyedEditor !== item
261
- if ( hasNoPortalPaneItem && lastDestroyedEditorWasClosedManually ) {
262
- this . leave ( )
263
- }
264
- }
265
-
266
257
hasPaneItem ( paneItem ) {
267
- return (
268
- paneItem === this . getEmptyPortalPaneItem ( ) ||
269
- this . editorProxiesByEditor . has ( paneItem )
270
- )
258
+ return this . editorProxiesByEditor . has ( paneItem )
271
259
}
272
260
273
261
getActivePaneItem ( ) {
274
262
return this . newActivePaneItem || this . workspace . getActivePaneItem ( )
275
263
}
276
264
277
- getEmptyPortalPaneItem ( ) {
278
- if ( this . emptyPortalItem == null ) {
279
- this . emptyPortalItem = new EmptyPortalPaneItem ( {
280
- hostIdentity : this . portal . getSiteIdentity ( 1 )
281
- } )
282
- }
283
- return this . emptyPortalItem
284
- }
285
-
286
265
onDidChange ( callback ) {
287
266
return this . emitter . on ( 'did-change' , callback )
288
267
}
0 commit comments