Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
updated utils imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sdadn committed May 7, 2024
1 parent 5fdd017 commit d69cec6
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ApplicationStatusOverlayProvider } from '../application-status-overlay'
import { NavigationPromptCheckpoint } from '../navigation-prompt';
import getBrowserLocale from './private/getBrowserLocale';
import useTestOverrides from './private/useTestOverrides';
import Logger from '../utils/logger';
import { Logger } from '../utils';

import './private/initializeInert';
import './baseStyles';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './Base.scss';
import Logger from '../utils/logger';
import { Logger } from '../utils';

// Checks to run when not in production
if (process.env.NODE_ENV !== 'production') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hasIntlData from 'intl-locales-supported';
import loadLocaleData from './loadLocaleData';
import logger from '../../utils/logger';
import { Logger } from '../../utils';

const supportedIntlConstructors = (polyfill) => {
/**
Expand Down Expand Up @@ -52,7 +52,7 @@ const loadIntl = (locale, polyfill) => {
if (!hasIntlData([locale], supportedIntlConstructors(polyfill))) {
return loadLocaleData(locale, polyfill).catch((error) => {
if (fallbackLocale) {
logger.warn(`${error.message} Using ${fallbackLocale} data as the fallback locale data.`);
Logger.warn(`${error.message} Using ${fallbackLocale} data as the fallback locale data.`);
if (!hasIntlData([fallbackLocale], supportedIntlConstructors(polyfill))) {
return loadLocaleData(fallbackLocale, polyfill);
}
Expand All @@ -62,7 +62,7 @@ const loadIntl = (locale, polyfill) => {

return Promise.resolve();
}).catch((error) => {
logger.warn(`${error.message} Using en data as the fallback locale data.`);
Logger.warn(`${error.message} Using en data as the fallback locale data.`);

if (!hasIntlData(['en'], supportedIntlConstructors(polyfill))) {
return loadLocaleData('en', polyfill);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-unresolved */
import logger from '../../utils/logger';
import { Logger } from '../../utils/';

const loadTranslationsFile = (locale) => {
switch (locale) {
Expand Down Expand Up @@ -44,15 +44,15 @@ const loadTranslations = (locale) => {
const fallbackLocale = locale.split('-').length > 1 ? locale.split('-')[0] : false;

return loadTranslationsFile(locale).catch((error) => {
logger.warn(`${error.message} Using ${fallbackLocale} data as the fallback locale.`);
Logger.warn(`${error.message} Using ${fallbackLocale} data as the fallback locale.`);

if (fallbackLocale) {
return loadTranslationsFile(fallbackLocale);
}

return Promise.reject(error);
}).catch((error) => {
logger.warn(`${error.message} Using en as the fallback locale.`);
Logger.warn(`${error.message} Using en as the fallback locale.`);

return loadTranslationsFile('en');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import StatusView from 'terra-status-view';
import { injectIntl } from 'react-intl';
import logger from '../utils/logger';
import { Logger } from '../utils';

const propTypes = {
/**
Expand Down Expand Up @@ -79,7 +79,7 @@ class ApplicationErrorBoundary extends React.Component {
* the ApplicationErrorBoundary to update again to ensure that the StatusView remains presented until the
* next update occurs.
*/
logger.error(error);
Logger.error(error);
this.errorRef.current = error;
this.setState({ error: undefined });
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react';
import EventEmitter from 'terra-application/utils/event-emitter';
import {EventEmitter} from 'terra-application/utils';

const EventEmitterExample = () => {
const [counter, setCounter] = useState(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,87 @@
EventEmitter is a singleton that is shared throughout your application. It is important to unsubscribe by removing all listeners to all events when your application is unmounted.

```js
import EventEmitter from 'terra-application/utils/event-emitter';
import { EventEmitter } from 'terra-application/utils';
```

## API

|method|syntax|Description|
|---|---|---|
|`once`|`eventEmitter.once('event-name', listener)`|Adds a one-time listener function for the event named `event-name`. The next time `event-name` is triggered, this listener is removed and then invoked.|
|`on`|`eventEmitter.on('event-name', listener)`|Adds the listener function to the end of the listeners array for the event named `event-name`. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of `event-name` and listener will result in the listener being added, and called, multiple times.|
|`addListener`|`eventEmitter.addListener('event-name', listener)`|Alias to `on`|
|`off`|`eventEmitter.off('event-name', listener)`|Removes all specified listeners from the listener array for the event named `event-name`.|
|`removeListener`|`eventEmitter.removeListener('event-name', listener)`|Alias to `off`|
|`removeAllListeners`|`eventEmitter.removeAllListeners('event-name')`|Removes all listeners if not event names are specified, or those of the specified 'event-name'.|
|`emit`|`eventEmitter.emit('event-name', arg1, arg2);`|Synchronously calls each of the listeners registered for the event named 'event-name', in the order they were registered, passing the supplied arguments to each.|
|`eventNames`|`eventEmitter.eventNames()`|Returns an array listing the event names for which the emitter has registered listeners. The values in the array will be strings.|
|`listenerCount`|`eventEmitter.listenerCount('event-name')`|Returns the number of listeners listening to the event named 'event-name'.|
|`listeners`|`eventEmitter.listeners('event-name')`|Returns a copy of the array of listeners for the event named 'event-name'.|
|`once`|`EventEmitter.once('event-name', listener)`|Adds a one-time listener function for the event named `event-name`. The next time `event-name` is triggered, this listener is removed and then invoked.|
|`on`|`EventEmitter.on('event-name', listener)`|Adds the listener function to the end of the listeners array for the event named `event-name`. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of `event-name` and listener will result in the listener being added, and called, multiple times.|
|`addListener`|`EventEmitter.addListener('event-name', listener)`|Alias to `on`|
|`off`|`EventEmitter.off('event-name', listener)`|Removes all specified listeners from the listener array for the event named `event-name`.|
|`removeListener`|`EventEmitter.removeListener('event-name', listener)`|Alias to `off`|
|`removeAllListeners`|`EventEmitter.removeAllListeners('event-name')`|Removes all listeners if not event names are specified, or those of the specified 'event-name'.|
|`emit`|`EventEmitter.emit('event-name', arg1, arg2);`|Synchronously calls each of the listeners registered for the event named 'event-name', in the order they were registered, passing the supplied arguments to each.|
|`eventNames`|`EventEmitter.eventNames()`|Returns an array listing the event names for which the emitter has registered listeners. The values in the array will be strings.|
|`listenerCount`|`EventEmitter.listenerCount('event-name')`|Returns the number of listeners listening to the event named 'event-name'.|
|`listeners`|`EventEmitter.listeners('event-name')`|Returns a copy of the array of listeners for the event named 'event-name'.|

## Examples


### `once`

```js
import eventEmitter from 'terra-application/utils/event-emitter';
import EventEmitter from 'terra-application/utils';

let listenCount = 0;
const listener = () => {listenCount += 1;}

eventEmitter.once('event-name', listener);
eventEmitter.emit('event-name'); // listenCount == 1
eventEmitter.emit('event-name'); // Ignored: listenCount == 1
EventEmitter.once('event-name', listener);
EventEmitter.emit('event-name'); // listenCount == 1
EventEmitter.emit('event-name'); // Ignored: listenCount == 1
```

### `on`

```js
import eventEmitter from 'terra-application/utils/event-emitter';
import EventEmitter from 'terra-application/utils';

let listenCount = 0;
const listener = () => {listenCount += 1;}

eventEmitter.on('event-name', listener);
eventEmitter.emit('event-name'); // listenCount == 1
eventEmitter.emit('event-name'); // listenCount == 2
EventEmitter.on('event-name', listener);
EventEmitter.emit('event-name'); // listenCount == 1
EventEmitter.emit('event-name'); // listenCount == 2
```

### `off`

```js
import eventEmitter from 'terra-application/utils/event-emitter';
import EventEmitter from 'terra-application/utils';

let listenCount = 0;
const listener = () => {listenCount += 1;}

eventEmitter.on('event-name', listener);
eventEmitter.off('event-name', listener);
eventEmitter.emit('event-name'); // Ignored: listenCount == 0
EventEmitter.on('event-name', listener);
EventEmitter.off('event-name', listener);
EventEmitter.emit('event-name'); // Ignored: listenCount == 0
```

### `removeAllListeners`

```js
import eventEmitter from 'terra-application/utils/event-emitter';
import EventEmitter from 'terra-application/utils';

let listenCount = 0;
const listener = () => {listenCount += 1;}

eventEmitter.on('event-name1', listener);
eventEmitter.on('event-name2', listener);
eventEmitter.on('event-name3', listener);
eventEmitter.removeAllListeners();
eventEmitter.emit('event-name1'); // Ignored: listenCount == 0
eventEmitter.emit('event-name2'); // Ignored: listenCount == 0
eventEmitter.emit('event-name3'); // Ignored: listenCount == 0
EventEmitter.on('event-name1', listener);
EventEmitter.on('event-name2', listener);
EventEmitter.on('event-name3', listener);
EventEmitter.removeAllListeners();
EventEmitter.emit('event-name1'); // Ignored: listenCount == 0
EventEmitter.emit('event-name2'); // Ignored: listenCount == 0
EventEmitter.emit('event-name3'); // Ignored: listenCount == 0
```

### `emit`

```js
import eventEmitter from 'terra-application/utils/event-emitter';
import EventEmitter from 'terra-application/utils';

let listenCount = 0;
const listener = (count) => {
Expand All @@ -97,52 +97,52 @@ const listener = (count) => {
listenCount += 1;
}

eventEmitter.on('event-name', listener);
eventEmitter.emit('event-name'); // listenCount == 1
eventEmitter.emit('event-name', 50); // listenCount == 51
EventEmitter.on('event-name', listener);
EventEmitter.emit('event-name'); // listenCount == 1
EventEmitter.emit('event-name', 50); // listenCount == 51
```
### `eventNames`
```js
import eventEmitter from 'terra-application/utils/event-emitter';
import EventEmitter from 'terra-application/utils';

const eventNames = ['event-name1', 'event-name2', 'event-name3']

eventEmitter.on(eventNames[0], () => {});
eventEmitter.on(eventNames[1], () => {});
eventEmitter.on(eventNames[2], () => {});
EventEmitter.on(eventNames[0], () => {});
EventEmitter.on(eventNames[1], () => {});
EventEmitter.on(eventNames[2], () => {});

eventEmitter.eventNames(); // ['event-name1', 'event-name2', 'event-name3']
EventEmitter.eventNames(); // ['event-name1', 'event-name2', 'event-name3']
```
### `listenerCount`
```js
import eventEmitter from 'terra-application/utils/event-emitter';
import EventEmitter from 'terra-application/utils';

eventEmitter.on('event-name1', () => {});
eventEmitter.on('event-name2', () => {});
eventEmitter.on('event-name2', () => {});
EventEmitter.on('event-name1', () => {});
EventEmitter.on('event-name2', () => {});
EventEmitter.on('event-name2', () => {});

eventEmitter.listenerCount('event-name1'); // 1
eventEmitter.listenerCount('event-name2'); // 2
EventEmitter.listenerCount('event-name1'); // 1
EventEmitter.listenerCount('event-name2'); // 2
```
### `listeners`
```js
import eventEmitter from 'terra-application/utils/event-emitter';
import EventEmitter from 'terra-application/utils';

const listener1 = () => {};
const listener2 = () => {};

eventEmitter.on('event-name', listener1);
eventEmitter.on('event-name', listener2);
EventEmitter.on('event-name', listener1);
EventEmitter.on('event-name', listener2);

eventEmitter.listeners('event-name').length; // 2
eventEmitter.listeners('event-name')[0]; // listener1
eventEmitter.listeners('event-name')[1]; // listener2
EventEmitter.listeners('event-name').length; // 2
EventEmitter.listeners('event-name')[0]; // listener1
EventEmitter.listeners('event-name')[1]; // listener2
```
Follow the [official Node.js documentation](https://nodejs.org/docs/latest-v10.x/api/events.html) for more examples and other less common APIs.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
The logger works like the `console` for the `warn`, `error`, and `info` methods. By default the logger will send the messages to the corresponding console methods. In production, info and warning messages will be suppressed.

```js
import logger from 'terra-application/utils/logger';
import { Logger } from 'terra-application/utils';

logger.error('error');
logger.warn('warning');
logger.info('info');
Logger.error('error');
Logger.warn('warning');
Logger.info('info');
```

## Overriding
Expand Down
8 changes: 8 additions & 0 deletions packages/terra-application/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Logger, {initializeLogger} from './logger';
import EventEmitter from './event-emitter';

export {
Logger,
EventEmitter,
initializeLogger,
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import hasIntlData from 'intl-locales-supported';
import * as loadLocaleData from '../../../../src/application-base/private/loadLocaleData';
import loadIntl from '../../../../src/application-base/private/intlLoaders';
import logger from '../../../../src/utils/logger';
import { Logger } from '../../../../src/utils';

jest.mock('intl-locales-supported');
jest.mock('../../../../src/application-base/private/loadLocaleData');
jest.mock('../../../../src/utils/logger', () => ({
jest.mock('../../../../src/utils/Logger', () => ({
warn: jest.fn(),
}));

Expand All @@ -26,7 +26,7 @@ describe('intlLoaders', () => {

describe('dev environment', () => {
beforeEach(() => {
logger.warn.mockClear();
Logger.warn.mockClear();
});

it('logs a warning when the regional locale is not provided and locale fallback is used', () => {
Expand All @@ -38,7 +38,7 @@ describe('intlLoaders', () => {
});
expect.assertions(4);
return loadIntl('es-US', 'intl').then(() => {
expect(logger.warn).toBeCalledWith('Locale data was not supplied for the es-US locale. Using es data as the fallback locale data.');
expect(Logger.warn).toBeCalledWith('Locale data was not supplied for the es-US locale. Using es data as the fallback locale data.');
expect(loadLocaleData.default).toHaveBeenCalledTimes(2);
expect(loadLocaleData.default).toHaveBeenNthCalledWith(2, 'es', 'intl');
expect(loadLocaleData.default).not.toHaveBeenCalledWith('en', 'intl');
Expand All @@ -54,7 +54,7 @@ describe('intlLoaders', () => {
});
expect.assertions(3);
return loadIntl('es', 'intl').then(() => {
expect(logger.warn).toBeCalledWith('Locale data was not supplied for the es locale. Using en data as the fallback locale data.');
expect(Logger.warn).toBeCalledWith('Locale data was not supplied for the es locale. Using en data as the fallback locale data.');
expect(loadLocaleData.default).toHaveBeenCalledTimes(2);
expect(loadLocaleData.default).toHaveBeenNthCalledWith(2, 'en', 'intl');
});
Expand All @@ -72,8 +72,8 @@ describe('intlLoaders', () => {
});
expect.assertions(4);
return loadIntl('es-US', 'intl').then(() => {
expect(logger.warn).toBeCalledWith('Locale data was not supplied for the es-US locale. Using es data as the fallback locale data.');
expect(logger.warn).toBeCalledWith('Locale data was not supplied for the es locale. Using en data as the fallback locale data.');
expect(Logger.warn).toBeCalledWith('Locale data was not supplied for the es-US locale. Using es data as the fallback locale data.');
expect(Logger.warn).toBeCalledWith('Locale data was not supplied for the es locale. Using en data as the fallback locale data.');
expect(loadLocaleData.default).toHaveBeenCalledTimes(3);
expect(loadLocaleData.default).toHaveBeenNthCalledWith(3, 'en', 'intl');
});
Expand All @@ -93,7 +93,7 @@ describe('intlLoaders', () => {
describe('production environment', () => {
beforeEach(() => {
process.env.NODE_ENV = 'production';
logger.warn.mockClear();
Logger.warn.mockClear();
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import loadTranslations from '../../../../src/application-base/private/translationsLoaders';
import Logger from '../../../../src/utils/logger';
import { Logger } from '../../../../src/utils';

describe('translationsLoaders', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import ApplicationErrorBoundary from '../../../src/application-error-boundary/ApplicationErrorBoundary';
import Logger from '../../../src/utils/logger';
import { Logger } from '../../../src/utils';

describe('ApplicationErrorBoundary', () => {
describe('Snapshots', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EventEmitter from '../../../../src/utils/event-emitter/EventEmitter'; // eslint-disable-line import/no-duplicates
import EventEmitter2 from '../../../../src/utils/event-emitter/EventEmitter'; // eslint-disable-line import/no-duplicates
import {EventEmitter} from '../../../../src/utils'; // eslint-disable-line import/no-duplicates
import {EventEmitter as EventEmitter2} from '../../../../src/utils'; // eslint-disable-line import/no-duplicates

describe('EventEmitter', () => {
let listenCount = 0;
Expand Down
Loading

0 comments on commit d69cec6

Please sign in to comment.