Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/front_end/workspace_diff/WorkspaceDiff.js |
| diff --git a/third_party/WebKit/Source/devtools/front_end/workspace_diff/WorkspaceDiff.js b/third_party/WebKit/Source/devtools/front_end/workspace_diff/WorkspaceDiff.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..94d0b90fd95b3441be2892515ca6bc22f2c30606 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/front_end/workspace_diff/WorkspaceDiff.js |
| @@ -0,0 +1,111 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| + |
| +WorkspaceDiff.WorkspaceDiff = class { |
| + constructor() { |
| + /** @type {!WeakMap<!Workspace.UISourceCode, !WorkspaceDiff._UISourceCodeDiff>} */ |
| + this._uiSourceCodeDiffs = new WeakMap(); |
| + } |
| + |
| + /** |
| + * @param {!Workspace.UISourceCode} uiSourceCode |
| + * @return {!Promise<!Diff.Diff.DiffArray>} |
| + */ |
| + requestDiff(uiSourceCode) { |
| + return this._uiSourceCodeDiff(uiSourceCode).requestDiff(); |
| + } |
| + |
| + /** |
| + * @param {!Workspace.UISourceCode} uiSourceCode |
| + * @param {function(!WorkspaceDiff.DiffChangedEvent)} callback |
| + * @param {!Object=} thisObj |
| + */ |
| + subscribeToDiffChange(uiSourceCode, callback, thisObj) { |
| + this._uiSourceCodeDiff(uiSourceCode).on(WorkspaceDiff.DiffChangedEvent, callback, thisObj); |
| + } |
| + |
| + /** |
| + * @param {!Workspace.UISourceCode} uiSourceCode |
| + * @param {function(!WorkspaceDiff.DiffChangedEvent)} callback |
| + * @param {!Object=} thisObj |
| + */ |
| + unsubscribeToDiffChange(uiSourceCode, callback, thisObj) { |
| + this._uiSourceCodeDiff(uiSourceCode).off(WorkspaceDiff.DiffChangedEvent, callback, thisObj); |
| + } |
| + |
| + /** |
| + * @param {!Workspace.UISourceCode} uiSourceCode |
| + * @return {!WorkspaceDiff._UISourceCodeDiff} |
| + */ |
| + _uiSourceCodeDiff(uiSourceCode) { |
| + if (!this._uiSourceCodeDiffs.has(uiSourceCode)) |
| + this._uiSourceCodeDiffs.set(uiSourceCode, new WorkspaceDiff._UISourceCodeDiff(uiSourceCode)); |
| + return this._uiSourceCodeDiffs.get(uiSourceCode); |
|
lushnikov
2017/03/10 01:17:04
how does this compile without a cast?
einbinder
2017/03/13 17:47:57
Closure trusts you on getting non-nullable values
|
| + } |
| +}; |
| + |
| +WorkspaceDiff._UISourceCodeDiff = class extends Common.Object { |
|
lushnikov
2017/03/10 01:17:04
WorkspaceDiff.WorkspaceDiff.UISourceCodeDiff
einbinder
2017/03/13 17:47:57
WorkspaceDiff.WorkspaceDiff._UISourceCodeDiff
|
| + /** |
| + * @param {!Workspace.UISourceCode} uiSourceCode |
| + */ |
| + constructor(uiSourceCode) { |
| + super(); |
| + this._uiSourceCode = uiSourceCode; |
| + uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyChanged, this._uiSourceCodeChanged, this); |
| + uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyCommitted, this._uiSourceCodeChanged, this); |
| + this._requestDiffPromise = null; |
| + this._pendingChanges = null; |
| + } |
| + |
| + _uiSourceCodeChanged() { |
| + if (this._pendingChanges) { |
| + clearTimeout(this._pendingChanges); |
|
lushnikov
2017/03/10 01:17:04
do you really want this logic? In case of continio
einbinder
2017/03/13 17:47:57
This is the current behavior. If the user is typin
|
| + this._pendingChanges = null; |
| + } |
| + this._requestDiffPromise = null; |
| + |
| + var content = this._uiSourceCode.content(); |
| + var delay = (!content || content.length < 65536) ? 0 : WorkspaceDiff.WorkspaceDiff.UpdateTimeout; |
| + this._pendingChanges = setTimeout(emitDiffChanged.bind(this), delay); |
| + |
| + /** |
| + * @this {WorkspaceDiff._UISourceCodeDiff} |
| + */ |
| + function emitDiffChanged() { |
| + this.emit(new WorkspaceDiff.DiffChangedEvent()); |
| + this._pendingChanges = null; |
| + } |
| + } |
| + |
| + /** |
| + * @return {!Promise<!Diff.Diff.DiffArray>} |
| + */ |
| + requestDiff() { |
| + if (!this._requestDiffPromise) { |
| + this._requestDiffPromise = this._uiSourceCode.requestOriginalContent().then(baseline => { |
| + var current = this._uiSourceCode.workingCopy(); |
| + var diff = Diff.Diff.lineDiff(baseline.split('\n'), current.split('\n')); |
| + return diff || []; |
| + }); |
| + } |
| + return this._requestDiffPromise; |
| + } |
| +}; |
| + |
| +/** |
| + * @implements {Common.Emittable} |
| + */ |
| +WorkspaceDiff.DiffChangedEvent = class {}; |
| + |
| +/** |
| + * @return {!WorkspaceDiff.WorkspaceDiff} |
| + */ |
| +WorkspaceDiff.workspaceDiff = function() { |
| + if (!WorkspaceDiff.WorkspaceDiff._instance) |
| + WorkspaceDiff.WorkspaceDiff._instance = new WorkspaceDiff.WorkspaceDiff(); |
| + return WorkspaceDiff.WorkspaceDiff._instance; |
| +}; |
| + |
| +WorkspaceDiff.WorkspaceDiff.UpdateTimeout = 200; |