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

Side by Side Diff: Source/devtools/front_end/sources/ScriptFormatter.js

Issue 685203003: DevTools: Get rid of synchronous XHRs in the frontend code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 * @param {string} mimeType 98 * @param {string} mimeType
99 * @param {string} content 99 * @param {string} content
100 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback 100 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback
101 */ 101 */
102 formatContent: function(mimeType, content, callback) 102 formatContent: function(mimeType, content, callback)
103 { 103 {
104 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uF EFF/, ''); 104 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uF EFF/, '');
105 const method = "format"; 105 const method = "format";
106 var parameters = { mimeType: mimeType, content: content, indentString: W ebInspector.settings.textEditorIndent.get() }; 106 var parameters = { mimeType: mimeType, content: content, indentString: W ebInspector.settings.textEditorIndent.get() };
107 this._tasks.push({ data: parameters, callback: callback }); 107 this._tasks.push({ data: parameters, callback: callback });
108 this._worker.postMessage({ method: method, params: parameters }); 108 this._postMessage({ method: method, params: parameters });
109 }, 109 },
110 110
111 _didFormatContent: function(event) 111 _didFormatContent: function(event)
112 { 112 {
113 var task = this._tasks.shift(); 113 var task = this._tasks.shift();
114 var originalContent = task.data.content; 114 var originalContent = task.data.content;
115 var formattedContent = event.data.content; 115 var formattedContent = event.data.content;
116 var mapping = event.data["mapping"]; 116 var mapping = event.data["mapping"];
117 var sourceMapping = new WebInspector.FormatterSourceMappingImpl(original Content.lineEndings(), formattedContent.lineEndings(), mapping); 117 var sourceMapping = new WebInspector.FormatterSourceMappingImpl(original Content.lineEndings(), formattedContent.lineEndings(), mapping);
118 task.callback(formattedContent, sourceMapping); 118 task.callback(formattedContent, sourceMapping);
119 }, 119 },
120 120
121 /** 121 /**
122 * @return {!Worker} 122 * @param {!Object} message
123 */ 123 */
124 get _worker() 124 _postMessage: function(message)
125 { 125 {
126 if (!this._cachedWorker) { 126 this._workerPromise().then(postMessage.bind(this));
127 this._cachedWorker = Runtime.startWorker("script_formatter_worker"); 127
128 /**
129 * @this {WebInspector.ScriptFormatter}
130 */
131 function postMessage()
132 {
133 this._cachedWorker.postMessage(message);
134 }
135 },
136
137 /**
138 * @return {!Promise.<undefined>}
139 */
140 _workerPromise: function()
141 {
142 if (this._cachedWorker)
143 return Promise.resolve(this._cachedWorker);
144
145 return Runtime.startWorker("script_formatter_worker").then(started.bind( this));
pfeldman 2014/11/07 14:51:17 You want to store this resulting promise and retur
apavlov 2014/11/10 07:44:12 Done.
146
147 /**
148 * @param {!Worker} worker
149 * @this {WebInspector.ScriptFormatter}
150 */
151 function started(worker)
152 {
153 this._cachedWorker = worker;
128 this._cachedWorker.onmessage = /** @type {function(this:Worker)} */ (this._didFormatContent.bind(this)); 154 this._cachedWorker.onmessage = /** @type {function(this:Worker)} */ (this._didFormatContent.bind(this));
129 } 155 }
130 return this._cachedWorker;
131 } 156 }
132 } 157 }
133 158
134 /** 159 /**
135 * @constructor 160 * @constructor
136 * @implements {WebInspector.Formatter} 161 * @implements {WebInspector.Formatter}
137 */ 162 */
138 WebInspector.IdentityFormatter = function() 163 WebInspector.IdentityFormatter = function()
139 { 164 {
140 this._tasks = []; 165 this._tasks = [];
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 */ 286 */
262 _convertPosition: function(positions1, positions2, position) 287 _convertPosition: function(positions1, positions2, position)
263 { 288 {
264 var index = positions1.upperBound(position) - 1; 289 var index = positions1.upperBound(position) - 1;
265 var convertedPosition = positions2[index] + position - positions1[index] ; 290 var convertedPosition = positions2[index] + position - positions1[index] ;
266 if (index < positions2.length - 1 && convertedPosition > positions2[inde x + 1]) 291 if (index < positions2.length - 1 && convertedPosition > positions2[inde x + 1])
267 convertedPosition = positions2[index + 1]; 292 convertedPosition = positions2[index + 1];
268 return convertedPosition; 293 return convertedPosition;
269 } 294 }
270 } 295 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698