PresentationConsoleMessageHelper.js 4.4 KB

1234567891011121314151617181920212223242526272829
  1. export class PresentationConsoleMessageManager{constructor(){SDK.targetManager.observeModels(SDK.DebuggerModel,this);SDK.consoleModel.addEventListener(SDK.ConsoleModel.Events.ConsoleCleared,this._consoleCleared,this);SDK.consoleModel.addEventListener(SDK.ConsoleModel.Events.MessageAdded,event=>this._consoleMessageAdded((event.data)));SDK.consoleModel.messages().forEach(this._consoleMessageAdded,this);}
  2. modelAdded(debuggerModel){debuggerModel[PresentationConsoleMessageManager._symbol]=new PresentationConsoleMessageHelper(debuggerModel);}
  3. modelRemoved(debuggerModel){debuggerModel[PresentationConsoleMessageManager._symbol]._consoleCleared();}
  4. _consoleMessageAdded(message){if(!message.isErrorOrWarning()||!message.runtimeModel()||message.source===SDK.ConsoleMessage.MessageSource.Violation){return;}
  5. const debuggerModel=message.runtimeModel().debuggerModel();debuggerModel[PresentationConsoleMessageManager._symbol]._consoleMessageAdded(message);}
  6. _consoleCleared(){for(const debuggerModel of SDK.targetManager.models(SDK.DebuggerModel)){debuggerModel[PresentationConsoleMessageManager._symbol]._consoleCleared();}}}
  7. PresentationConsoleMessageManager._symbol=Symbol('PresentationConsoleMessageHelper');export default class PresentationConsoleMessageHelper{constructor(debuggerModel){this._debuggerModel=debuggerModel;this._pendingConsoleMessages={};this._presentationConsoleMessages=[];debuggerModel.addEventListener(SDK.DebuggerModel.Events.ParsedScriptSource,event=>setImmediate(this._parsedScriptSource.bind(this,event)));debuggerModel.addEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared,this._debuggerReset,this);this._locationPool=new Bindings.LiveLocationPool();}
  8. _consoleMessageAdded(message){const rawLocation=this._rawLocation(message);if(rawLocation){this._addConsoleMessageToScript(message,rawLocation);}else{this._addPendingConsoleMessage(message);}}
  9. _rawLocation(message){if(message.scriptId){return this._debuggerModel.createRawLocationByScriptId(message.scriptId,message.line,message.column);}
  10. const callFrame=message.stackTrace&&message.stackTrace.callFrames?message.stackTrace.callFrames[0]:null;if(callFrame){return this._debuggerModel.createRawLocationByScriptId(callFrame.scriptId,callFrame.lineNumber,callFrame.columnNumber);}
  11. if(message.url){return this._debuggerModel.createRawLocationByURL(message.url,message.line,message.column);}
  12. return null;}
  13. _addConsoleMessageToScript(message,rawLocation){this._presentationConsoleMessages.push(new PresentationConsoleMessage(message,rawLocation,this._locationPool));}
  14. _addPendingConsoleMessage(message){if(!message.url){return;}
  15. if(!this._pendingConsoleMessages[message.url]){this._pendingConsoleMessages[message.url]=[];}
  16. this._pendingConsoleMessages[message.url].push(message);}
  17. _parsedScriptSource(event){const script=(event.data);const messages=this._pendingConsoleMessages[script.sourceURL];if(!messages){return;}
  18. const pendingMessages=[];for(let i=0;i<messages.length;i++){const message=messages[i];const rawLocation=this._rawLocation(message);if(!rawLocation){continue;}
  19. if(script.scriptId===rawLocation.scriptId){this._addConsoleMessageToScript(message,rawLocation);}else{pendingMessages.push(message);}}
  20. if(pendingMessages.length){this._pendingConsoleMessages[script.sourceURL]=pendingMessages;}else{delete this._pendingConsoleMessages[script.sourceURL];}}
  21. _consoleCleared(){this._pendingConsoleMessages={};this._debuggerReset();}
  22. _debuggerReset(){for(const message of this._presentationConsoleMessages){message.dispose();}
  23. this._presentationConsoleMessages=[];this._locationPool.disposeAll();}}
  24. export class PresentationConsoleMessage{constructor(message,rawLocation,locationPool){this._text=message.messageText;this._level=message.level===SDK.ConsoleMessage.MessageLevel.Error?Workspace.UISourceCode.Message.Level.Error:Workspace.UISourceCode.Message.Level.Warning;Bindings.debuggerWorkspaceBinding.createLiveLocation(rawLocation,this._updateLocation.bind(this),locationPool);}
  25. _updateLocation(liveLocation){if(this._uiMessage){this._uiMessage.remove();}
  26. const uiLocation=liveLocation.uiLocation();if(!uiLocation){return;}
  27. this._uiMessage=uiLocation.uiSourceCode.addLineMessage(this._level,this._text,uiLocation.lineNumber,uiLocation.columnNumber);}
  28. dispose(){if(this._uiMessage){this._uiMessage.remove();}}}
  29. self.Bindings=self.Bindings||{};Bindings=Bindings||{};Bindings.PresentationConsoleMessageManager=PresentationConsoleMessageManager;Bindings.PresentationConsoleMessage=PresentationConsoleMessage;