You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.startSession = startSession;
exports.stopSession = stopSession;
function _os() {
const data = _interopRequireDefault(require("os"));
_os = function () {
return data;
};
return data;
}
function _url() {
const data = require("url");
_url = function () {
return data;
};
return data;
}
function _internal() {
const data = require("./internal");
_internal = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const UPDATE_FREQUENCY_SECS = 20;
let keepUpdating = true;
// TODO notify www when a project is started, and every N seconds afterwards
async function startSession(projectRoot, exp, platform, forceUpdate = false) {
if (forceUpdate) {
keepUpdating = true;
}
if (!_internal().ConnectionStatus.isOffline() && keepUpdating) {
const authSession = await _internal().UserManager.getSessionAsync();
const {
devices
} = await _internal().ProjectSettings.getDevicesInfoAsync(projectRoot);
if (!authSession && !(devices !== null && devices !== void 0 && devices.length)) {
// 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 or with a device ID');
return;
}
try {
let url;
if (platform === 'native') {
url = await _internal().UrlUtils.constructDeepLinkAsync(projectRoot);
} else if (platform === 'web') {
url = await _internal().UrlUtils.constructWebAppUrlAsync(projectRoot);
} else {
throw new Error(`Unsupported platform: ${platform}`);
}
let queryString = '';
if (devices) {
const searchParams = new (_url().URLSearchParams)();
devices.forEach(device => {
searchParams.append('deviceId', device.installationId);
});
queryString = `?${searchParams.toString()}`;
}
const apiClient = _internal().ApiV2.clientForUser(authSession);
await apiClient.postAsync(`development-sessions/notify-alive${queryString}`, {
data: {
session: {
description: `${exp.name} on ${_os().default.hostname()}`,
hostname: _os().default.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) {
_internal().Logger.global.debug(e, `Error updating dev session: ${e}`);
}
setTimeout(() => startSession(projectRoot, exp, platform), UPDATE_FREQUENCY_SECS * 1000);
}
}
function stopSession() {
keepUpdating = false;
}
//# sourceMappingURL=DevSession.js.map