Skip to content

Commit 3e165e9

Browse files
committed
Cache response from update server
1 parent 1e07960 commit 3e165e9

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

packages/compass-intercom/src/setup-intercom.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { SinonStub } from 'sinon';
33
import sinon from 'sinon';
44

5-
import { setupIntercom } from './setup-intercom';
5+
import { setupIntercom, resetIntercomAllowedCache } from './setup-intercom';
66
import { expect } from 'chai';
77
import type { IntercomScript } from './intercom-script';
88
import type { PreferencesAccess } from 'compass-preferences-model';
@@ -100,6 +100,7 @@ describe('setupIntercom', function () {
100100
process.env.HADRON_APP_VERSION = backupEnv.HADRON_APP_VERSION as any;
101101
process.env.NODE_ENV = backupEnv.NODE_ENV;
102102
fetchMock.restore();
103+
resetIntercomAllowedCache();
103104
});
104105

105106
describe('when it can be enabled', function () {

packages/compass-intercom/src/setup-intercom.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,26 @@ export async function setupIntercom(
8989
);
9090
}
9191

92+
let isIntercomAllowedPromise: Promise<boolean> | null = null;
93+
9294
function isIntercomAllowed(): Promise<boolean> {
93-
return fetchIntegrations().then(
94-
({ intercom }) => intercom,
95-
(error) => {
96-
debug(
97-
'Failed to fetch intercom integration status, defaulting to false',
98-
{ error }
99-
);
100-
return false;
101-
}
102-
);
95+
if (!isIntercomAllowedPromise) {
96+
isIntercomAllowedPromise = fetchIntegrations().then(
97+
({ intercom }) => intercom,
98+
(error) => {
99+
debug(
100+
'Failed to fetch intercom integration status, defaulting to false',
101+
{ error }
102+
);
103+
return false;
104+
}
105+
);
106+
}
107+
return isIntercomAllowedPromise;
108+
}
109+
110+
export function resetIntercomAllowedCache(): void {
111+
isIntercomAllowedPromise = null;
103112
}
104113

105114
/**

0 commit comments

Comments
 (0)