Skip to content

Commit faa6038

Browse files
committed
Defer ux lookup until first call
1 parent 6005821 commit faa6038

File tree

1 file changed

+69
-77
lines changed

1 file changed

+69
-77
lines changed

src/utils/alert/rules.ts

+69-77
Original file line numberDiff line numberDiff line change
@@ -184,89 +184,81 @@ let _uxLookup: AlertUxLookup | undefined
184184
export async function uxLookup(
185185
settings: AlertUxLookupSettings
186186
): Promise<AlertUxLookupResult> {
187-
while (_uxLookup === undefined) {
188-
// eslint-disable-next-line no-await-in-loop
189-
await wait(1, { signal: abortSignal })
190-
}
191-
return _uxLookup(settings)
192-
}
193-
194-
// Start initializing the AlertUxLookupResult immediately.
195-
void (async () => {
196-
const { orgs, settings } = await (async () => {
197-
try {
198-
const sockSdk = await setupSdk(getPublicToken())
199-
const orgResult = await sockSdk.getOrganizations()
200-
if (!orgResult.success) {
201-
throw new Error(
202-
`Failed to fetch Socket organization info: ${orgResult.error.message}`
203-
)
204-
}
205-
const orgs: Array<
206-
Exclude<(typeof orgResult.data.organizations)[string], undefined>
207-
> = []
208-
for (const org of Object.values(orgResult.data.organizations)) {
209-
if (org) {
210-
orgs.push(org)
187+
if (_uxLookup === undefined) {
188+
const { orgs, settings } = await (async () => {
189+
try {
190+
const sockSdk = await setupSdk(getPublicToken())
191+
const orgResult = await sockSdk.getOrganizations()
192+
if (!orgResult.success) {
193+
throw new Error(
194+
`Failed to fetch Socket organization info: ${orgResult.error.message}`
195+
)
211196
}
212-
}
213-
const result = await sockSdk.postSettings(
214-
orgs.map(org => ({ organization: org.id }))
215-
)
216-
if (!result.success) {
217-
throw new Error(
218-
`Failed to fetch API key settings: ${result.error.message}`
219-
)
220-
}
221-
return {
222-
orgs,
223-
settings: result.data
224-
}
225-
} catch (e) {
226-
const cause = isObject(e) && 'cause' in e ? e['cause'] : undefined
227-
if (
228-
isErrnoException(cause) &&
229-
(cause.code === 'ENOTFOUND' || cause.code === 'ECONNREFUSED')
230-
) {
231-
throw new Error(
232-
'Unable to connect to socket.dev, ensure internet connectivity before retrying',
233-
{
234-
cause: e
197+
const orgs: Array<
198+
Exclude<(typeof orgResult.data.organizations)[string], undefined>
199+
> = []
200+
for (const org of Object.values(orgResult.data.organizations)) {
201+
if (org) {
202+
orgs.push(org)
235203
}
204+
}
205+
const result = await sockSdk.postSettings(
206+
orgs.map(org => ({ organization: org.id }))
236207
)
208+
if (!result.success) {
209+
throw new Error(
210+
`Failed to fetch API key settings: ${result.error.message}`
211+
)
212+
}
213+
return {
214+
orgs,
215+
settings: result.data
216+
}
217+
} catch (e) {
218+
const cause = isObject(e) && 'cause' in e ? e['cause'] : undefined
219+
if (
220+
isErrnoException(cause) &&
221+
(cause.code === 'ENOTFOUND' || cause.code === 'ECONNREFUSED')
222+
) {
223+
throw new Error(
224+
'Unable to connect to socket.dev, ensure internet connectivity before retrying',
225+
{
226+
cause: e
227+
}
228+
)
229+
}
230+
throw e
231+
}
232+
})()
233+
// Remove any organizations not being enforced.
234+
const enforcedOrgs = getSetting('enforcedOrgs') ?? []
235+
for (const { 0: i, 1: org } of orgs.entries()) {
236+
if (!enforcedOrgs.includes(org.id)) {
237+
settings.entries.splice(i, 1)
237238
}
238-
throw e
239-
}
240-
})()
241-
242-
// Remove any organizations not being enforced.
243-
const enforcedOrgs = getSetting('enforcedOrgs') ?? []
244-
for (const { 0: i, 1: org } of orgs.entries()) {
245-
if (!enforcedOrgs.includes(org.id)) {
246-
settings.entries.splice(i, 1)
247239
}
248-
}
249-
250-
const socketYml = findSocketYmlSync()
251-
if (socketYml) {
252-
settings.entries.push({
253-
start: socketYml.path,
254-
settings: {
255-
[socketYml.path]: {
256-
deferTo: null,
257-
// TODO: TypeScript complains about the type not matching. We should
258-
// figure out why are providing
259-
// issueRules: { [issueName: string]: boolean }
260-
// but expecting
261-
// issueRules: { [issueName: string]: { action: 'defer' | 'error' | 'ignore' | 'monitor' | 'warn' } }
262-
issueRules: socketYml.parsed.issueRules as unknown as {
263-
[key: string]: {
264-
action: 'defer' | 'error' | 'ignore' | 'monitor' | 'warn'
240+
const socketYml = findSocketYmlSync()
241+
if (socketYml) {
242+
settings.entries.push({
243+
start: socketYml.path,
244+
settings: {
245+
[socketYml.path]: {
246+
deferTo: null,
247+
// TODO: TypeScript complains about the type not matching. We should
248+
// figure out why are providing
249+
// issueRules: { [issueName: string]: boolean }
250+
// but expecting
251+
// issueRules: { [issueName: string]: { action: 'defer' | 'error' | 'ignore' | 'monitor' | 'warn' } }
252+
issueRules: socketYml.parsed.issueRules as unknown as {
253+
[key: string]: {
254+
action: 'defer' | 'error' | 'ignore' | 'monitor' | 'warn'
255+
}
265256
}
266257
}
267258
}
268-
}
269-
})
259+
})
260+
}
261+
_uxLookup = createAlertUXLookup(settings)
270262
}
271-
_uxLookup = createAlertUXLookup(settings)
272-
})()
263+
return _uxLookup(settings)
264+
}

0 commit comments

Comments
 (0)