Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add createOnReadyTask. Fix broken growls triggered before init. #8271

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}