mirror of https://github.com/status-im/metro.git
[react-packager] Modernize Activity to ES6
This commit is contained in:
parent
f77f48c61c
commit
02b8f264ef
|
@ -9,5 +9,5 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
dim: function(s) { return s; },
|
dim: s => s,
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,64 +10,63 @@
|
||||||
|
|
||||||
jest.autoMockOff();
|
jest.autoMockOff();
|
||||||
|
|
||||||
describe('Activity', function() {
|
describe('Activity', () => {
|
||||||
var Activity;
|
const origConsoleLog = console.log;
|
||||||
|
let Activity;
|
||||||
|
|
||||||
var origConsoleLog = console.log;
|
beforeEach(() => {
|
||||||
|
|
||||||
beforeEach(function() {
|
|
||||||
console.log = jest.genMockFn();
|
console.log = jest.genMockFn();
|
||||||
Activity = require('../');
|
Activity = require('../');
|
||||||
jest.runOnlyPendingTimers();
|
jest.runOnlyPendingTimers();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(() => {
|
||||||
console.log = origConsoleLog;
|
console.log = origConsoleLog;
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('startEvent', function() {
|
describe('startEvent', () => {
|
||||||
it('writes a START event out to the console', function() {
|
it('writes a START event out to the console', () => {
|
||||||
var EVENT_NAME = 'EVENT_NAME';
|
const EVENT_NAME = 'EVENT_NAME';
|
||||||
var DATA = {someData: 42};
|
const DATA = {someData: 42};
|
||||||
|
|
||||||
Activity.startEvent(EVENT_NAME, DATA);
|
Activity.startEvent(EVENT_NAME, DATA);
|
||||||
jest.runOnlyPendingTimers();
|
jest.runOnlyPendingTimers();
|
||||||
|
|
||||||
expect(console.log.mock.calls.length).toBe(1);
|
expect(console.log.mock.calls.length).toBe(1);
|
||||||
var consoleMsg = console.log.mock.calls[0][0];
|
const consoleMsg = console.log.mock.calls[0][0];
|
||||||
expect(consoleMsg).toContain('START');
|
expect(consoleMsg).toContain('START');
|
||||||
expect(consoleMsg).toContain(EVENT_NAME);
|
expect(consoleMsg).toContain(EVENT_NAME);
|
||||||
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('endEvent', function() {
|
describe('endEvent', () => {
|
||||||
it('writes an END event out to the console', function() {
|
it('writes an END event out to the console', () => {
|
||||||
var EVENT_NAME = 'EVENT_NAME';
|
const EVENT_NAME = 'EVENT_NAME';
|
||||||
var DATA = {someData: 42};
|
const DATA = {someData: 42};
|
||||||
|
|
||||||
var eventID = Activity.startEvent(EVENT_NAME, DATA);
|
const eventID = Activity.startEvent(EVENT_NAME, DATA);
|
||||||
Activity.endEvent(eventID);
|
Activity.endEvent(eventID);
|
||||||
jest.runOnlyPendingTimers();
|
jest.runOnlyPendingTimers();
|
||||||
|
|
||||||
expect(console.log.mock.calls.length).toBe(2);
|
expect(console.log.mock.calls.length).toBe(2);
|
||||||
var consoleMsg = console.log.mock.calls[1][0];
|
const consoleMsg = console.log.mock.calls[1][0];
|
||||||
expect(consoleMsg).toContain('END');
|
expect(consoleMsg).toContain('END');
|
||||||
expect(consoleMsg).toContain(EVENT_NAME);
|
expect(consoleMsg).toContain(EVENT_NAME);
|
||||||
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws when called with an invalid eventId', function() {
|
it('throws when called with an invalid eventId', () => {
|
||||||
expect(function() {
|
expect(() => Activity.endEvent(42)).toThrow(
|
||||||
Activity.endEvent(42);
|
'event(42) is not a valid event id!',
|
||||||
}).toThrow('event(42) is not a valid event id!');
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws when called with an expired eventId', function() {
|
it('throws when called with an expired eventId', () => {
|
||||||
var eid = Activity.startEvent('', '');
|
const eid = Activity.startEvent('', '');
|
||||||
Activity.endEvent(eid);
|
Activity.endEvent(eid);
|
||||||
|
|
||||||
expect(function() {
|
expect(() => {
|
||||||
Activity.endEvent(eid);
|
Activity.endEvent(eid);
|
||||||
}).toThrow('event(3) has already ended!');
|
}).toThrow('event(3) has already ended!');
|
||||||
|
|
||||||
|
@ -75,17 +74,16 @@ describe('Activity', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('signal', function() {
|
describe('signal', () => {
|
||||||
it('writes a SIGNAL event out to the console', function() {
|
it('writes a SIGNAL event out to the console', () => {
|
||||||
|
const EVENT_NAME = 'EVENT_NAME';
|
||||||
var EVENT_NAME = 'EVENT_NAME';
|
const DATA = {someData: 42};
|
||||||
var DATA = {someData: 42};
|
|
||||||
|
|
||||||
Activity.signal(EVENT_NAME, DATA);
|
Activity.signal(EVENT_NAME, DATA);
|
||||||
jest.runOnlyPendingTimers();
|
jest.runOnlyPendingTimers();
|
||||||
|
|
||||||
expect(console.log.mock.calls.length).toBe(1);
|
expect(console.log.mock.calls.length).toBe(1);
|
||||||
var consoleMsg = console.log.mock.calls[0][0];
|
const consoleMsg = console.log.mock.calls[0][0];
|
||||||
expect(consoleMsg).toContain(EVENT_NAME);
|
expect(consoleMsg).toContain(EVENT_NAME);
|
||||||
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
expect(consoleMsg).toContain(JSON.stringify(DATA));
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,21 +8,22 @@
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
var events = require('events');
|
const events = require('events');
|
||||||
|
|
||||||
var COLLECTION_PERIOD = 1000;
|
const COLLECTION_PERIOD = 1000;
|
||||||
|
|
||||||
var _endedEvents = Object.create(null);
|
const _endedEvents = Object.create(null);
|
||||||
var _eventStarts = Object.create(null);
|
const _eventStarts = Object.create(null);
|
||||||
var _queuedActions = [];
|
const _queuedActions = [];
|
||||||
var _scheduledCollectionTimer = null;
|
const _eventEmitter = new events.EventEmitter();
|
||||||
var _uuid = 1;
|
|
||||||
var _enabled = true;
|
let _scheduledCollectionTimer = null;
|
||||||
var _eventEmitter = new events.EventEmitter();
|
let _uuid = 1;
|
||||||
|
let _enabled = true;
|
||||||
|
|
||||||
function endEvent(eventId) {
|
function endEvent(eventId) {
|
||||||
var eventEndTime = Date.now();
|
const eventEndTime = Date.now();
|
||||||
|
|
||||||
if (!_eventStarts[eventId]) {
|
if (!_eventStarts[eventId]) {
|
||||||
_throw('event(' + eventId + ') is not a valid event id!');
|
_throw('event(' + eventId + ') is not a valid event id!');
|
||||||
|
@ -41,7 +42,7 @@ function endEvent(eventId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function signal(eventName, data) {
|
function signal(eventName, data) {
|
||||||
var signalTime = Date.now();
|
const signalTime = Date.now();
|
||||||
|
|
||||||
if (eventName == null) {
|
if (eventName == null) {
|
||||||
_throw('No event name specified');
|
_throw('No event name specified');
|
||||||
|
@ -60,7 +61,7 @@ function signal(eventName, data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function startEvent(eventName, data) {
|
function startEvent(eventName, data) {
|
||||||
var eventStartTime = Date.now();
|
const eventStartTime = Date.now();
|
||||||
|
|
||||||
if (eventName == null) {
|
if (eventName == null) {
|
||||||
_throw('No event name specified');
|
_throw('No event name specified');
|
||||||
|
@ -70,8 +71,8 @@ function startEvent(eventName, data) {
|
||||||
data = null;
|
data = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var eventId = _uuid++;
|
const eventId = _uuid++;
|
||||||
var action = {
|
const action = {
|
||||||
action: 'startEvent',
|
action: 'startEvent',
|
||||||
data: data,
|
data: data,
|
||||||
eventId: eventId,
|
eventId: eventId,
|
||||||
|
@ -90,7 +91,7 @@ function disable() {
|
||||||
|
|
||||||
function _runCollection() {
|
function _runCollection() {
|
||||||
/* jshint -W084 */
|
/* jshint -W084 */
|
||||||
var action;
|
let action;
|
||||||
while ((action = _queuedActions.shift())) {
|
while ((action = _queuedActions.shift())) {
|
||||||
_writeAction(action);
|
_writeAction(action);
|
||||||
}
|
}
|
||||||
|
@ -117,10 +118,10 @@ function _scheduleAction(action) {
|
||||||
* won't be adding such a non-trivial optimization anytime soon)
|
* won't be adding such a non-trivial optimization anytime soon)
|
||||||
*/
|
*/
|
||||||
function _throw(msg) {
|
function _throw(msg) {
|
||||||
var err = new Error(msg);
|
const err = new Error(msg);
|
||||||
|
|
||||||
// Strip off the call to _throw()
|
// Strip off the call to _throw()
|
||||||
var stack = err.stack.split('\n');
|
const stack = err.stack.split('\n');
|
||||||
stack.splice(1, 1);
|
stack.splice(1, 1);
|
||||||
err.stack = stack.join('\n');
|
err.stack = stack.join('\n');
|
||||||
|
|
||||||
|
@ -132,8 +133,8 @@ function _writeAction(action) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = action.data ? ': ' + JSON.stringify(action.data) : '';
|
const data = action.data ? ': ' + JSON.stringify(action.data) : '';
|
||||||
var fmtTime = new Date(action.tstamp).toLocaleTimeString();
|
const fmtTime = new Date(action.tstamp).toLocaleTimeString();
|
||||||
|
|
||||||
switch (action.action) {
|
switch (action.action) {
|
||||||
case 'startEvent':
|
case 'startEvent':
|
||||||
|
@ -145,8 +146,8 @@ function _writeAction(action) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'endEvent':
|
case 'endEvent':
|
||||||
var startAction = _eventStarts[action.eventId];
|
const startAction = _eventStarts[action.eventId];
|
||||||
var startData = startAction.data ? ': ' + JSON.stringify(startAction.data) : '';
|
const startData = startAction.data ? ': ' + JSON.stringify(startAction.data) : '';
|
||||||
console.log(chalk.dim(
|
console.log(chalk.dim(
|
||||||
'[' + fmtTime + '] ' +
|
'[' + fmtTime + '] ' +
|
||||||
'<END> ' + startAction.eventName +
|
'<END> ' + startAction.eventName +
|
||||||
|
|
Loading…
Reference in New Issue