Files
react-native-devtools-frontend/front_end/common/Console.js
T
Tim van der Lippe 7f2002c598 [globals] self.runtime
Migrate all usages of self.runtime to Root.Runtime.Runtime.instance().

This also requires various type updates, as now Closure/TypeScript
can better understand the code and discover issues.

R=jacktfranklin@chromium.org

Bug: 1011811
Change-Id: Ice07aa0ca959e61a6acdf4618b9a272d8c1096b8
Fixed: 1081614
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2434339
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
2020-09-29 10:52:39 +00:00

121 lines
2.2 KiB
JavaScript

// Copyright 2014 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.
import {ObjectWrapper} from './Object.js';
import {reveal} from './Revealer.js';
/**
* @type {!Console}
*/
let consoleInstance;
/**
* @unrestricted
*/
export class Console extends ObjectWrapper {
/**
* Instantiable via the instance() factory below.
*
* @private
*/
constructor() {
super();
/** @type {!Array.<!Message>} */
this._messages = [];
}
/**
* @param {{forceNew: boolean}} opts
*/
static instance({forceNew} = {forceNew: false}) {
if (!consoleInstance || forceNew) {
consoleInstance = new Console();
}
return consoleInstance;
}
/**
* @param {string} text
* @param {!MessageLevel} level
* @param {boolean=} show
*/
addMessage(text, level, show) {
const message = new Message(text, level || MessageLevel.Info, Date.now(), show || false);
this._messages.push(message);
this.dispatchEventToListeners(Events.MessageAdded, message);
}
/**
* @param {string} text
*/
log(text) {
this.addMessage(text, MessageLevel.Info);
}
/**
* @param {string} text
*/
warn(text) {
this.addMessage(text, MessageLevel.Warning);
}
/**
* @param {string} text
*/
error(text) {
this.addMessage(text, MessageLevel.Error, true);
}
/**
* @return {!Array.<!Message>}
*/
messages() {
return this._messages;
}
show() {
this.showPromise();
}
/**
* @return {!Promise.<void>}
*/
showPromise() {
return reveal(this);
}
}
/** @enum {symbol} */
export const Events = {
MessageAdded: Symbol('messageAdded')
};
/**
* @enum {string}
*/
export const MessageLevel = {
Info: 'info',
Warning: 'warning',
Error: 'error'
};
/**
* @unrestricted
*/
export class Message {
/**
* @param {string} text
* @param {!MessageLevel} level
* @param {number} timestamp
* @param {boolean} show
*/
constructor(text, level, timestamp, show) {
this.text = text;
this.level = level;
this.timestamp = (typeof timestamp === 'number') ? timestamp : Date.now();
this.show = show;
}
}