Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(675)

Unified Diff: third_party/WebKit/Source/devtools/front_end/workspace_diff/UISourceCodeDiff.js

Issue 2729783002: DevTools: Diff subsystem (Closed)
Patch Set: this Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/front_end/workspace_diff/UISourceCodeDiff.js
diff --git a/third_party/WebKit/Source/devtools/front_end/workspace_diff/UISourceCodeDiff.js b/third_party/WebKit/Source/devtools/front_end/workspace_diff/UISourceCodeDiff.js
new file mode 100644
index 0000000000000000000000000000000000000000..d3324c379a2f3f24ec435cb26fbe4330ec4af9a7
--- /dev/null
+++ b/third_party/WebKit/Source/devtools/front_end/workspace_diff/UISourceCodeDiff.js
@@ -0,0 +1,98 @@
+// 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.
+
+/**
+ * @param {!Workspace.UISourceCode} uiSourceCode
+ * @return {!Promise<!Diff.Diff.DiffArray>}
+ */
+WorkspaceDiff.requestDiff = function(uiSourceCode) {
+ return WorkspaceDiff._uiSourceCodeDiff(uiSourceCode).requestDiff();
+};
+
+/**
+ * @param {!Workspace.UISourceCode} uiSourceCode
+ * @param {function(!WorkspaceDiff.DiffChangedEvent)} callback
+ * @param {!Object=} thisObj
+ */
+WorkspaceDiff.addThrottledDiffListener = function(uiSourceCode, callback, thisObj) {
lushnikov 2017/03/08 17:55:16 .subscribeToDiffChange(..)
einbinder 2017/03/08 23:12:47 Done.
+ WorkspaceDiff._uiSourceCodeDiff(uiSourceCode).on(WorkspaceDiff.DiffChangedEvent, callback, thisObj);
+};
+
+/**
+ * @param {!Workspace.UISourceCode} uiSourceCode
+ * @param {function(!WorkspaceDiff.DiffChangedEvent)} callback
+ * @param {!Object=} thisObj
+ */
+WorkspaceDiff.removeThrottledDiffListener = function(uiSourceCode, callback, thisObj) {
+ WorkspaceDiff._uiSourceCodeDiff(uiSourceCode).off(WorkspaceDiff.DiffChangedEvent, callback, thisObj);
+};
+
+/**
+ * @param {!Workspace.UISourceCode} uiSourceCode
+ * @return {!WorkspaceDiff._UISourceCodeDiff}
+ */
+WorkspaceDiff._uiSourceCodeDiff = function(uiSourceCode) {
+ if (!uiSourceCode[WorkspaceDiff._uiSourceCodeDiff.Symbol])
+ uiSourceCode[WorkspaceDiff._UISourceCodeDiff.Symbol] = new WorkspaceDiff._UISourceCodeDiff(uiSourceCode);
+ return uiSourceCode[WorkspaceDiff._UISourceCodeDiff.Symbol];
+};
+
+WorkspaceDiff._UISourceCodeDiff = class extends Common.Object {
lushnikov 2017/03/08 17:55:16 Let's do the following: 1. introduce class Worksp
einbinder 2017/03/08 23:12:47 This will pull the Diff module into autostart.
+ /**
+ * @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);
+ this._pendingChanges = null;
+ }
+ this._requestDiffPromise = null;
+ if (!this.hasEventListeners(WorkspaceDiff.DiffChangedEvent))
lushnikov 2017/03/08 17:55:16 we can omit this check - there's already one in th
einbinder 2017/03/08 23:12:47 Removed.
+ return;
+
+ var content = this._uiSourceCode.content();
+ if (content && content.length < 65536)
lushnikov 2017/03/08 17:55:16 typeof content === 'string'
+ emitDiffChanged.call(this);
lushnikov 2017/03/08 17:55:16 let's always call the throttled event through time
einbinder 2017/03/08 23:12:47 Done.
+ else
+ this._pendingChanges = setTimeout(emitDiffChanged.bind(this), 200);
+
+ /**
+ * @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 {};
+
+WorkspaceDiff._UISourceCodeDiff.Symbol = Symbol('WorkspaceDiff._UISourceCodeDiff.Symbol');

Powered by Google App Engine
This is Rietveld 408576698