This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 477
/
DevSession.ts
76 lines (66 loc) · 2.17 KB
/
DevSession.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { ExpoConfig } from '@expo/config-types';
import os from 'os';
import {
ApiV2 as ApiV2Client,
ConnectionStatus,
Logger as logger,
UrlUtils,
UserManager,
} from './internal';
const UPDATE_FREQUENCY_SECS = 20;
let keepUpdating = true;
// TODO notify www when a project is started, and every N seconds afterwards
export async function startSession(
projectRoot: string,
exp: Pick<ExpoConfig, 'name' | 'description' | 'slug' | 'primaryColor'>,
platform: 'native' | 'web',
forceUpdate: boolean = false
): Promise<void> {
if (forceUpdate) {
keepUpdating = true;
}
if (!ConnectionStatus.isOffline() && keepUpdating) {
// TODO(anp) if the user has configured device ids, then notify for those too
const authSession = await UserManager.getSessionAsync();
if (!authSession) {
// NOTE(brentvatne) let's just bail out in this case for now
// throw new Error('development sessions can only be initiated for logged in users');
return;
}
try {
let url;
if (platform === 'native') {
url = await UrlUtils.constructDeepLinkAsync(projectRoot);
} else if (platform === 'web') {
url = await UrlUtils.constructWebAppUrlAsync(projectRoot);
} else {
throw new Error(`Unsupported platform: ${platform}`);
}
const apiClient = ApiV2Client.clientForUser(authSession);
await apiClient.postAsync('development-sessions/notify-alive', {
data: {
session: {
description: `${exp.name} on ${os.hostname()}`,
hostname: os.hostname(),
platform,
config: {
// TODO: if icons are specified, upload a url for them too so people can distinguish
description: exp.description,
name: exp.name,
slug: exp.slug,
primaryColor: exp.primaryColor,
},
url,
source: 'desktop',
},
},
});
} catch (e) {
logger.global.debug(e, `Error updating dev session: ${e}`);
}
setTimeout(() => startSession(projectRoot, exp, platform), UPDATE_FREQUENCY_SECS * 1000);
}
}
export function stopSession() {
keepUpdating = false;
}