Skip to content

Commit

Permalink
Merge pull request #8271 from Expensify/marcaaron-onReadyTask
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham committed Mar 23, 2022
2 parents 9beba91 + dcf0276 commit b613330
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/components/GrowlNotification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as Expensicons from '../Icon/Expensicons';
import styles from '../../styles/styles';
import GrowlNotificationContainer from './GrowlNotificationContainer';
import CONST from '../../CONST';
import * as Growl from '../../libs/Growl';

const types = {
[CONST.GROWL.SUCCESS]: {
Expand All @@ -31,8 +32,8 @@ const types = {
const INACTIVE_POSITION_Y = -255;

class GrowlNotification extends Component {
constructor() {
super();
constructor(props) {
super(props);

this.state = {
bodyText: '',
Expand All @@ -44,6 +45,10 @@ class GrowlNotification extends Component {
this.fling = this.fling.bind(this);
}

componentDidMount() {
Growl.setIsReady();
}

/**
* Show the growl notification
*
Expand Down
12 changes: 3 additions & 9 deletions src/libs/ActiveClientManager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import Onyx from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import ONYXKEYS from '../../ONYXKEYS';
import * as ActiveClients from '../actions/ActiveClients';
import createOnReadyTask from '../createOnReadyTask';

const clientID = Str.guid();
const maxClients = 20;

let activeClients;
let isInitialized;

// Keeps track of the ActiveClientManager's readiness in one place
// so that multiple calls of isReady resolve the same promise
const isInitializedPromise = new Promise((resolve) => {
isInitialized = resolve;
});
const [isReady, setIsReady] = createOnReadyTask();

Onyx.connect({
key: ONYXKEYS.ACTIVE_CLIENTS,
Expand All @@ -32,11 +30,7 @@ Onyx.connect({
*/
function init() {
ActiveClients.addClient(clientID)
.then(isInitialized);
}

function isReady() {
return isInitializedPromise;
.then(setIsReady);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/libs/Growl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import CONST from '../CONST';
import createOnReadyTask from './createOnReadyTask';

const growlRef = React.createRef();
const [isGrowlReady, setIsReady] = createOnReadyTask();

/**
* Show the growl notification
Expand All @@ -11,7 +13,7 @@ const growlRef = React.createRef();
* @param {Number} [duration]
*/
function show(bodyText, type, duration = CONST.GROWL.DURATION) {
growlRef.current.show(bodyText, type, duration);
isGrowlReady().then(() => growlRef.current.show(bodyText, type, duration));
}

/**
Expand Down Expand Up @@ -42,4 +44,5 @@ export default {

export {
growlRef,
setIsReady,
};
18 changes: 18 additions & 0 deletions src/libs/createOnReadyTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Helper method to create a task to track the "readiness" of something and defer any actions until after something is "ready".
*
* @example
*
* const [isSomethingReady, setIsReady] = createOnReadyTask();
* isSomethingReady().then(() => doIt());
* setIsReady(); // -> doIt() will now execute
*
* @returns {Array<Promise, Function>}
*/
export default function createOnReadyTask() {
let resolveIsReadyPromise;
const isReadyPromise = (new Promise((resolve) => {
resolveIsReadyPromise = resolve;
}));
return [() => isReadyPromise, resolveIsReadyPromise];
}

0 comments on commit b613330

Please sign in to comment.