UNPKG

haste-resolver

Version:
92 lines (74 loc) 2.27 kB
/** * Copyright (c) 2015-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ 'use strict'; var chalk = require('chalk'); var events = require('events'); var _eventStarts = Object.create(null); var _eventEmitter = new events.EventEmitter(); var _uuid = 1; var _enabled = true; function endEvent(eventId) { var eventEndTime = Date.now(); if (!_eventStarts[eventId]) { throw new Error('event(' + eventId + ') either ended or never started'); } _writeAction({ action: 'endEvent', eventId: eventId, tstamp: eventEndTime }); } function startEvent(eventName, data) { var eventStartTime = Date.now(); if (eventName == null) { throw new Error('No event name specified'); } if (data == null) { data = null; } var eventId = _uuid++; var action = { action: 'startEvent', data: data, eventId: eventId, eventName: eventName, tstamp: eventStartTime }; _eventStarts[eventId] = action; _writeAction(action); return eventId; } function disable() { _enabled = false; } function _writeAction(action) { _eventEmitter.emit(action.action, action); if (!_enabled) { return; } var data = action.data ? ': ' + JSON.stringify(action.data) : ''; var fmtTime = new Date(action.tstamp).toLocaleTimeString(); switch (action.action) { case 'startEvent': console.log(chalk.dim('[' + fmtTime + '] ' + '<START> ' + action.eventName + data)); break; case 'endEvent': var startAction = _eventStarts[action.eventId]; var startData = startAction.data ? ': ' + JSON.stringify(startAction.data) : ''; console.log(chalk.dim('[' + fmtTime + '] ' + '<END> ' + startAction.eventName + ' (' + (action.tstamp - startAction.tstamp) + 'ms)' + startData)); delete _eventStarts[action.eventId]; break; default: throw new Error('Unexpected scheduled action type: ' + action.action); } } exports.endEvent = endEvent; exports.startEvent = startEvent; exports.disable = disable; exports.eventEmitter = _eventEmitter;