| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 WorkspaceDiff.WorkspaceDiff = class extends Common.Object { | 6 WorkspaceDiff.WorkspaceDiff = class extends Common.Object { |
| 7 /** | 7 /** |
| 8 * @param {!Workspace.Workspace} workspace | 8 * @param {!Workspace.Workspace} workspace |
| 9 */ | 9 */ |
| 10 constructor(workspace) { | 10 constructor(workspace) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 _uiSourceCodeAdded(event) { | 82 _uiSourceCodeAdded(event) { |
| 83 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data); | 83 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data); |
| 84 this._updateModifiedState(uiSourceCode); | 84 this._updateModifiedState(uiSourceCode); |
| 85 } | 85 } |
| 86 | 86 |
| 87 /** | 87 /** |
| 88 * @param {!Common.Event} event | 88 * @param {!Common.Event} event |
| 89 */ | 89 */ |
| 90 _uiSourceCodeRemoved(event) { | 90 _uiSourceCodeRemoved(event) { |
| 91 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data); | 91 var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data); |
| 92 this._loadingUISourceCodes.delete(uiSourceCode); | 92 this._removeUISourceCode(uiSourceCode); |
| 93 this._markAsUnmodified(uiSourceCode); | |
| 94 } | 93 } |
| 95 | 94 |
| 96 /** | 95 /** |
| 97 * @param {!Common.Event} event | 96 * @param {!Common.Event} event |
| 98 */ | 97 */ |
| 99 _projectRemoved(event) { | 98 _projectRemoved(event) { |
| 100 var project = /** @type {!Workspace.Project} */ (event.data); | 99 var project = /** @type {!Workspace.Project} */ (event.data); |
| 101 for (var uiSourceCode of project.uiSourceCodes()) { | 100 for (var uiSourceCode of project.uiSourceCodes()) |
| 102 this._loadingUISourceCodes.delete(uiSourceCode); | 101 this._removeUISourceCode(uiSourceCode); |
| 103 this._markAsUnmodified(uiSourceCode); | |
| 104 } | |
| 105 } | 102 } |
| 106 | 103 |
| 107 /** | 104 /** |
| 105 * @param {!Workspace.UISourceCode} uiSourceCode |
| 106 */ |
| 107 _removeUISourceCode(uiSourceCode) { |
| 108 this._loadingUISourceCodes.delete(uiSourceCode); |
| 109 var uiSourceCodeDiff = this._uiSourceCodeDiffs.get(uiSourceCode); |
| 110 if (uiSourceCodeDiff) |
| 111 uiSourceCodeDiff._dispose = true; |
| 112 this._markAsUnmodified(uiSourceCode); |
| 113 } |
| 114 |
| 115 /** |
| 108 * @param {!Workspace.UISourceCode} uiSourceCode | 116 * @param {!Workspace.UISourceCode} uiSourceCode |
| 109 */ | 117 */ |
| 110 _markAsUnmodified(uiSourceCode) { | 118 _markAsUnmodified(uiSourceCode) { |
| 111 this._uiSourceCodeProcessedForTest(); | 119 this._uiSourceCodeProcessedForTest(); |
| 112 if (this._modifiedUISourceCodes.delete(uiSourceCode)) | 120 if (this._modifiedUISourceCodes.delete(uiSourceCode)) |
| 113 this.dispatchEventToListeners(WorkspaceDiff.Events.ModifiedStatusChanged,
{uiSourceCode, isModified: false}); | 121 this.dispatchEventToListeners(WorkspaceDiff.Events.ModifiedStatusChanged,
{uiSourceCode, isModified: false}); |
| 114 } | 122 } |
| 115 | 123 |
| 116 /** | 124 /** |
| 117 * @param {!Workspace.UISourceCode} uiSourceCode | 125 * @param {!Workspace.UISourceCode} uiSourceCode |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 /** | 172 /** |
| 165 * @param {!Workspace.UISourceCode} uiSourceCode | 173 * @param {!Workspace.UISourceCode} uiSourceCode |
| 166 */ | 174 */ |
| 167 constructor(uiSourceCode) { | 175 constructor(uiSourceCode) { |
| 168 super(); | 176 super(); |
| 169 this._uiSourceCode = uiSourceCode; | 177 this._uiSourceCode = uiSourceCode; |
| 170 uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyChang
ed, this._uiSourceCodeChanged, this); | 178 uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyChang
ed, this._uiSourceCodeChanged, this); |
| 171 uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyCommi
tted, this._uiSourceCodeChanged, this); | 179 uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyCommi
tted, this._uiSourceCodeChanged, this); |
| 172 this._requestDiffPromise = null; | 180 this._requestDiffPromise = null; |
| 173 this._pendingChanges = null; | 181 this._pendingChanges = null; |
| 182 this._dispose = false; |
| 174 } | 183 } |
| 175 | 184 |
| 176 _uiSourceCodeChanged() { | 185 _uiSourceCodeChanged() { |
| 177 if (this._pendingChanges) { | 186 if (this._pendingChanges) { |
| 178 clearTimeout(this._pendingChanges); | 187 clearTimeout(this._pendingChanges); |
| 179 this._pendingChanges = null; | 188 this._pendingChanges = null; |
| 180 } | 189 } |
| 181 this._requestDiffPromise = null; | 190 this._requestDiffPromise = null; |
| 182 | 191 |
| 183 var content = this._uiSourceCode.content(); | 192 var content = this._uiSourceCode.content(); |
| 184 var delay = (!content || content.length < 65536) ? 0 : WorkspaceDiff.Workspa
ceDiff.UpdateTimeout; | 193 var delay = (!content || content.length < 65536) ? 0 : WorkspaceDiff.Workspa
ceDiff.UpdateTimeout; |
| 185 this._pendingChanges = setTimeout(emitDiffChanged.bind(this), delay); | 194 this._pendingChanges = setTimeout(emitDiffChanged.bind(this), delay); |
| 186 | 195 |
| 187 /** | 196 /** |
| 188 * @this {WorkspaceDiff.WorkspaceDiff.UISourceCodeDiff} | 197 * @this {WorkspaceDiff.WorkspaceDiff.UISourceCodeDiff} |
| 189 */ | 198 */ |
| 190 function emitDiffChanged() { | 199 function emitDiffChanged() { |
| 200 if (this._dispose) |
| 201 return; |
| 191 this.dispatchEventToListeners(WorkspaceDiff.Events.DiffChanged); | 202 this.dispatchEventToListeners(WorkspaceDiff.Events.DiffChanged); |
| 192 this._pendingChanges = null; | 203 this._pendingChanges = null; |
| 193 } | 204 } |
| 194 } | 205 } |
| 195 | 206 |
| 196 /** | 207 /** |
| 197 * @return {!Promise<?Diff.Diff.DiffArray>} | 208 * @return {!Promise<?Diff.Diff.DiffArray>} |
| 198 */ | 209 */ |
| 199 requestDiff() { | 210 requestDiff() { |
| 200 if (!this._requestDiffPromise) | 211 if (!this._requestDiffPromise) |
| 201 this._requestDiffPromise = this._innerRequestDiff(); | 212 this._requestDiffPromise = this._innerRequestDiff(); |
| 202 return this._requestDiffPromise; | 213 return this._requestDiffPromise; |
| 203 } | 214 } |
| 204 | 215 |
| 205 /** | 216 /** |
| 206 * @return {!Promise<?Diff.Diff.DiffArray>} | 217 * @return {!Promise<?Diff.Diff.DiffArray>} |
| 207 */ | 218 */ |
| 208 async _innerRequestDiff() { | 219 async _innerRequestDiff() { |
| 220 if (this._dispose) |
| 221 return null; |
| 222 |
| 209 var current = this._uiSourceCode.workingCopy(); | 223 var current = this._uiSourceCode.workingCopy(); |
| 210 if (!current && !this._uiSourceCode.contentLoaded()) | 224 if (!current && !this._uiSourceCode.contentLoaded()) |
| 211 current = await this._uiSourceCode.requestContent(); | 225 current = await this._uiSourceCode.requestContent(); |
| 226 // ------------ ASYNC ------------ |
| 227 if (this._dispose) |
| 228 return null; |
| 229 |
| 212 var baseline = await this._uiSourceCode.requestOriginalContent(); | 230 var baseline = await this._uiSourceCode.requestOriginalContent(); |
| 231 // ------------ ASYNC ------------ |
| 232 if (this._dispose) |
| 233 return null; |
| 234 |
| 213 if (current === null || baseline === null) | 235 if (current === null || baseline === null) |
| 214 return null; | 236 return null; |
| 215 return Diff.Diff.lineDiff(baseline.split('\n'), current.split('\n')); | 237 return Diff.Diff.lineDiff(baseline.split('\n'), current.split('\n')); |
| 216 } | 238 } |
| 217 }; | 239 }; |
| 218 | 240 |
| 219 /** | 241 /** |
| 220 * @enum {symbol} | 242 * @enum {symbol} |
| 221 */ | 243 */ |
| 222 WorkspaceDiff.Events = { | 244 WorkspaceDiff.Events = { |
| 223 DiffChanged: Symbol('DiffChanged'), | 245 DiffChanged: Symbol('DiffChanged'), |
| 224 ModifiedStatusChanged: Symbol('ModifiedStatusChanged') | 246 ModifiedStatusChanged: Symbol('ModifiedStatusChanged') |
| 225 }; | 247 }; |
| 226 | 248 |
| 227 /** | 249 /** |
| 228 * @return {!WorkspaceDiff.WorkspaceDiff} | 250 * @return {!WorkspaceDiff.WorkspaceDiff} |
| 229 */ | 251 */ |
| 230 WorkspaceDiff.workspaceDiff = function() { | 252 WorkspaceDiff.workspaceDiff = function() { |
| 231 if (!WorkspaceDiff.WorkspaceDiff._instance) | 253 if (!WorkspaceDiff.WorkspaceDiff._instance) |
| 232 WorkspaceDiff.WorkspaceDiff._instance = new WorkspaceDiff.WorkspaceDiff(Work
space.workspace); | 254 WorkspaceDiff.WorkspaceDiff._instance = new WorkspaceDiff.WorkspaceDiff(Work
space.workspace); |
| 233 return WorkspaceDiff.WorkspaceDiff._instance; | 255 return WorkspaceDiff.WorkspaceDiff._instance; |
| 234 }; | 256 }; |
| 235 | 257 |
| 236 WorkspaceDiff.WorkspaceDiff.UpdateTimeout = 200; | 258 WorkspaceDiff.WorkspaceDiff.UpdateTimeout = 200; |
| OLD | NEW |