OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 this._name = parsedURL.lastPathComponent; | 49 this._name = parsedURL.lastPathComponent; |
50 if (parsedURL.queryParams) | 50 if (parsedURL.queryParams) |
51 this._name += '?' + parsedURL.queryParams; | 51 this._name += '?' + parsedURL.queryParams; |
52 } else { | 52 } else { |
53 this._origin = ''; | 53 this._origin = ''; |
54 this._parentURL = ''; | 54 this._parentURL = ''; |
55 this._name = url; | 55 this._name = url; |
56 } | 56 } |
57 | 57 |
58 this._contentType = contentType; | 58 this._contentType = contentType; |
59 /** @type {?function(?string)} */ | |
60 this._requestContentCallback = null; | |
61 /** @type {?Promise<?string>} */ | 59 /** @type {?Promise<?string>} */ |
62 this._requestContentPromise = null; | 60 this._requestContentPromise = null; |
63 /** @type {!Multimap<string, !Workspace.UISourceCode.LineMarker>} */ | 61 /** @type {!Multimap<string, !Workspace.UISourceCode.LineMarker>} */ |
64 this._decorations = new Multimap(); | 62 this._decorations = new Multimap(); |
65 | 63 |
66 /** @type {!Array.<!Workspace.Revision>} */ | 64 /** @type {!Array.<!Workspace.Revision>} */ |
67 this.history = []; | 65 this.history = []; |
68 | 66 |
69 /** @type {!Array<!Workspace.UISourceCode.Message>} */ | 67 /** @type {!Array<!Workspace.UISourceCode.Message>} */ |
70 this._messages = []; | 68 this._messages = []; |
69 | |
70 this._contentLoaded = false; | |
71 /** @type {?string} */ | |
72 this._content = null; | |
73 this._forceLoadOnCheckContent = false; | |
74 this._checkingContent = false; | |
75 /** @type {?string} */ | |
76 this._lastAcceptedContent = null; | |
77 /** @type {?string} */ | |
78 this._workingCopy = null; | |
79 /** @type {?function() : string} */ | |
80 this._workingCopyGetter = null; | |
71 } | 81 } |
72 | 82 |
73 /** | 83 /** |
74 * @return {!Promise<?Workspace.UISourceCodeMetadata>} | 84 * @return {!Promise<?Workspace.UISourceCodeMetadata>} |
75 */ | 85 */ |
76 requestMetadata() { | 86 requestMetadata() { |
77 return this._project.requestMetadata(this); | 87 return this._project.requestMetadata(this); |
78 } | 88 } |
79 | 89 |
80 /** | 90 /** |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 */ | 213 */ |
204 project() { | 214 project() { |
205 return this._project; | 215 return this._project; |
206 } | 216 } |
207 | 217 |
208 /** | 218 /** |
209 * @override | 219 * @override |
210 * @return {!Promise<?string>} | 220 * @return {!Promise<?string>} |
211 */ | 221 */ |
212 requestContent() { | 222 requestContent() { |
213 if (this._content || this._contentLoaded) | 223 if (!this._requestContentPromise) { |
lushnikov
2016/12/19 23:51:58
let's fast-return!
einbinder
2016/12/20 00:28:45
Done.
| |
214 return Promise.resolve(this._content); | 224 if (this._contentLoaded) { |
215 var promise = this._requestContentPromise; | 225 this._requestContentPromise = Promise.resolve(this._content); |
216 if (!promise) { | 226 } else { |
217 promise = new Promise(fulfill => this._requestContentCallback = fulfill); | 227 var fulfill; |
218 this._requestContentPromise = promise; | 228 this._requestContentPromise = new Promise(x => fulfill = x); |
219 this._project.requestFileContent(this, this._fireContentAvailable.bind(thi s)); | 229 this._project.requestFileContent(this, content => { |
230 this._contentLoaded = true; | |
231 this._content = content; | |
232 fulfill(content); | |
233 }); | |
234 } | |
220 } | 235 } |
221 return promise; | 236 return this._requestContentPromise; |
222 } | 237 } |
223 | 238 |
224 checkContentUpdated() { | 239 checkContentUpdated() { |
225 if (!this._contentLoaded && !this._forceLoadOnCheckContent) | 240 if (!this._contentLoaded && !this._forceLoadOnCheckContent) |
226 return; | 241 return; |
227 | 242 |
228 if (!this._project.canSetFileContent() || this._checkingContent) | 243 if (!this._project.canSetFileContent() || this._checkingContent) |
229 return; | 244 return; |
230 | 245 |
231 this._checkingContent = true; | 246 this._checkingContent = true; |
232 this._project.requestFileContent(this, contentLoaded.bind(this)); | 247 this._project.requestFileContent(this, contentLoaded.bind(this)); |
233 | 248 |
234 /** | 249 /** |
235 * @param {?string} updatedContent | 250 * @param {?string} updatedContent |
236 * @this {Workspace.UISourceCode} | 251 * @this {Workspace.UISourceCode} |
237 */ | 252 */ |
238 function contentLoaded(updatedContent) { | 253 function contentLoaded(updatedContent) { |
239 this._checkingContent = false; | 254 this._checkingContent = false; |
240 if (updatedContent === null) { | 255 if (updatedContent === null) { |
241 var workingCopy = this.workingCopy(); | 256 var workingCopy = this.workingCopy(); |
242 this._contentCommitted('', false); | 257 this._contentCommitted('', false); |
243 this.setWorkingCopy(workingCopy); | 258 this.setWorkingCopy(workingCopy); |
244 return; | 259 return; |
245 } | 260 } |
246 if (typeof this._lastAcceptedContent === 'string' && this._lastAcceptedCon tent === updatedContent) | 261 if (this._lastAcceptedContent === updatedContent) |
247 return; | 262 return; |
248 | 263 |
249 if (this._content === updatedContent) { | 264 if (this._content === updatedContent) { |
250 delete this._lastAcceptedContent; | 265 this._lastAcceptedContent = null; |
251 return; | 266 return; |
252 } | 267 } |
253 | 268 |
254 if (!this.isDirty() || this._workingCopy === updatedContent) { | 269 if (!this.isDirty() || this._workingCopy === updatedContent) { |
255 this._contentCommitted(updatedContent, false); | 270 this._contentCommitted(updatedContent, false); |
256 return; | 271 return; |
257 } | 272 } |
258 | 273 |
259 var shouldUpdate = | 274 var shouldUpdate = |
260 window.confirm(Common.UIString('This file was changed externally. Woul d you like to reload it?')); | 275 window.confirm(Common.UIString('This file was changed externally. Woul d you like to reload it?')); |
(...skipping 29 matching lines...) Expand all Loading... | |
290 Workspace.fileManager.close(this._url); | 305 Workspace.fileManager.close(this._url); |
291 } | 306 } |
292 this._contentCommitted(content, true); | 307 this._contentCommitted(content, true); |
293 } | 308 } |
294 | 309 |
295 /** | 310 /** |
296 * @param {string} content | 311 * @param {string} content |
297 * @param {boolean} committedByUser | 312 * @param {boolean} committedByUser |
298 */ | 313 */ |
299 _contentCommitted(content, committedByUser) { | 314 _contentCommitted(content, committedByUser) { |
300 delete this._lastAcceptedContent; | 315 this._lastAcceptedContent = null; |
301 this._content = content; | 316 this._content = content; |
302 this._contentLoaded = true; | 317 this._contentLoaded = true; |
318 this._requestContentPromise = null; | |
303 | 319 |
304 var lastRevision = this.history.length ? this.history[this.history.length - 1] : null; | 320 var lastRevision = this.history.length ? this.history[this.history.length - 1] : null; |
305 if (!lastRevision || lastRevision._content !== this._content) { | 321 if (!lastRevision || lastRevision._content !== this._content) { |
306 var revision = new Workspace.Revision(this, this._content, new Date()); | 322 var revision = new Workspace.Revision(this, this._content, new Date()); |
307 this.history.push(revision); | 323 this.history.push(revision); |
308 } | 324 } |
309 | 325 |
310 this._innerResetWorkingCopy(); | 326 this._innerResetWorkingCopy(); |
311 this.dispatchEventToListeners( | 327 this.dispatchEventToListeners( |
312 Workspace.UISourceCode.Events.WorkingCopyCommitted, {uiSourceCode: this, content: content}); | 328 Workspace.UISourceCode.Events.WorkingCopyCommitted, {uiSourceCode: this, content: content}); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 Host.userMetrics.actionTaken(Host.UserMetrics.Action.RevisionApplied); | 394 Host.userMetrics.actionTaken(Host.UserMetrics.Action.RevisionApplied); |
379 this.requestOriginalContent().then(revert.bind(this)); | 395 this.requestOriginalContent().then(revert.bind(this)); |
380 } | 396 } |
381 | 397 |
382 /** | 398 /** |
383 * @return {string} | 399 * @return {string} |
384 */ | 400 */ |
385 workingCopy() { | 401 workingCopy() { |
386 if (this._workingCopyGetter) { | 402 if (this._workingCopyGetter) { |
387 this._workingCopy = this._workingCopyGetter(); | 403 this._workingCopy = this._workingCopyGetter(); |
388 delete this._workingCopyGetter; | 404 this._workingCopyGetter = null; |
389 } | 405 } |
390 if (this.isDirty()) | 406 if (this.isDirty()) |
391 return this._workingCopy; | 407 return /** @type {string} */ (this._workingCopy); |
392 return this._content; | 408 return this._content || ''; |
393 } | 409 } |
394 | 410 |
395 resetWorkingCopy() { | 411 resetWorkingCopy() { |
396 this._innerResetWorkingCopy(); | 412 this._innerResetWorkingCopy(); |
397 this._workingCopyChanged(); | 413 this._workingCopyChanged(); |
398 } | 414 } |
399 | 415 |
400 _innerResetWorkingCopy() { | 416 _innerResetWorkingCopy() { |
401 delete this._workingCopy; | 417 this._workingCopy = null; |
402 delete this._workingCopyGetter; | 418 this._workingCopyGetter = null; |
403 } | 419 } |
404 | 420 |
405 /** | 421 /** |
406 * @param {string} newWorkingCopy | 422 * @param {string} newWorkingCopy |
407 */ | 423 */ |
408 setWorkingCopy(newWorkingCopy) { | 424 setWorkingCopy(newWorkingCopy) { |
409 this._workingCopy = newWorkingCopy; | 425 this._workingCopy = newWorkingCopy; |
410 delete this._workingCopyGetter; | 426 this._workingCopyGetter = null; |
411 this._workingCopyChanged(); | 427 this._workingCopyChanged(); |
412 } | 428 } |
413 | 429 |
430 /** | |
431 * @param {function(): string } workingCopyGetter | |
432 */ | |
414 setWorkingCopyGetter(workingCopyGetter) { | 433 setWorkingCopyGetter(workingCopyGetter) { |
415 this._workingCopyGetter = workingCopyGetter; | 434 this._workingCopyGetter = workingCopyGetter; |
416 this._workingCopyChanged(); | 435 this._workingCopyChanged(); |
417 } | 436 } |
418 | 437 |
419 _workingCopyChanged() { | 438 _workingCopyChanged() { |
420 this._removeAllMessages(); | 439 this._removeAllMessages(); |
421 this.dispatchEventToListeners(Workspace.UISourceCode.Events.WorkingCopyChang ed, this); | 440 this.dispatchEventToListeners(Workspace.UISourceCode.Events.WorkingCopyChang ed, this); |
422 this._project.workspace().dispatchEventToListeners( | 441 this._project.workspace().dispatchEventToListeners( |
423 Workspace.Workspace.Events.WorkingCopyChanged, {uiSourceCode: this}); | 442 Workspace.Workspace.Events.WorkingCopyChanged, {uiSourceCode: this}); |
424 } | 443 } |
425 | 444 |
426 removeWorkingCopyGetter() { | 445 removeWorkingCopyGetter() { |
427 if (!this._workingCopyGetter) | 446 if (!this._workingCopyGetter) |
428 return; | 447 return; |
429 this._workingCopy = this._workingCopyGetter(); | 448 this._workingCopy = this._workingCopyGetter(); |
430 delete this._workingCopyGetter; | 449 this._workingCopyGetter = null; |
431 } | 450 } |
432 | 451 |
433 commitWorkingCopy() { | 452 commitWorkingCopy() { |
434 if (this.isDirty()) | 453 if (this.isDirty()) |
435 this._commitContent(this.workingCopy()); | 454 this._commitContent(this.workingCopy()); |
436 } | 455 } |
437 | 456 |
438 /** | 457 /** |
439 * @return {boolean} | 458 * @return {boolean} |
440 */ | 459 */ |
441 isDirty() { | 460 isDirty() { |
442 return typeof this._workingCopy !== 'undefined' || typeof this._workingCopyG etter !== 'undefined'; | 461 return this._workingCopy !== null || this._workingCopyGetter !== null; |
443 } | 462 } |
444 | 463 |
445 /** | 464 /** |
446 * @return {string} | 465 * @return {string} |
447 */ | 466 */ |
448 extension() { | 467 extension() { |
449 return Common.ParsedURL.extractExtension(this._name); | 468 return Common.ParsedURL.extractExtension(this._name); |
450 } | 469 } |
451 | 470 |
452 /** | 471 /** |
(...skipping 22 matching lines...) Expand all Loading... | |
475 | 494 |
476 /** | 495 /** |
477 * @param {string} content | 496 * @param {string} content |
478 */ | 497 */ |
479 function doSearch(content) { | 498 function doSearch(content) { |
480 callback(Common.ContentProvider.performSearchInContent(content, query, cas eSensitive, isRegex)); | 499 callback(Common.ContentProvider.performSearchInContent(content, query, cas eSensitive, isRegex)); |
481 } | 500 } |
482 } | 501 } |
483 | 502 |
484 /** | 503 /** |
485 * @param {?string} content | |
486 */ | |
487 _fireContentAvailable(content) { | |
488 this._contentLoaded = true; | |
489 this._content = content; | |
490 | |
491 var callback = this._requestContentCallback; | |
492 this._requestContentCallback = null; | |
493 this._requestContentPromise = null; | |
494 | |
495 callback.call(null, content); | |
496 } | |
497 | |
498 /** | |
499 * @return {boolean} | 504 * @return {boolean} |
500 */ | 505 */ |
501 contentLoaded() { | 506 contentLoaded() { |
502 return this._contentLoaded; | 507 return this._contentLoaded; |
503 } | 508 } |
504 | 509 |
505 /** | 510 /** |
506 * @param {number} lineNumber | 511 * @param {number} lineNumber |
507 * @param {number=} columnNumber | 512 * @param {number=} columnNumber |
508 * @return {!Workspace.UILocation} | 513 * @return {!Workspace.UILocation} |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
901 Workspace.UISourceCodeMetadata = class { | 906 Workspace.UISourceCodeMetadata = class { |
902 /** | 907 /** |
903 * @param {?Date} modificationTime | 908 * @param {?Date} modificationTime |
904 * @param {?number} contentSize | 909 * @param {?number} contentSize |
905 */ | 910 */ |
906 constructor(modificationTime, contentSize) { | 911 constructor(modificationTime, contentSize) { |
907 this.modificationTime = modificationTime; | 912 this.modificationTime = modificationTime; |
908 this.contentSize = contentSize; | 913 this.contentSize = contentSize; |
909 } | 914 } |
910 }; | 915 }; |
OLD | NEW |