mirror of
https://github.com/ivabus/gui
synced 2025-04-23 14:07:14 +03:00
Co-authored-by: neil molina <neil@neils-MacBook-Pro.local>
This commit is contained in:
parent
84339660c9
commit
447f84db30
2 changed files with 23 additions and 30 deletions
|
@ -8,6 +8,7 @@ import axios from "axios";
|
||||||
import get from "./v1-client";
|
import get from "./v1-client";
|
||||||
import { DeviceAuth } from "../../src/libs/types";
|
import { DeviceAuth } from "../../src/libs/types";
|
||||||
import { notifyMainWindow } from "../electron";
|
import { notifyMainWindow } from "../electron";
|
||||||
|
import { InitWatcher } from "./initialize";
|
||||||
|
|
||||||
const sessionFilePath = path.join(getTeaPath(), "tea.xyz/gui/tmp.dat");
|
const sessionFilePath = path.join(getTeaPath(), "tea.xyz/gui/tmp.dat");
|
||||||
const sessionFolder = path.join(getTeaPath(), "tea.xyz/gui");
|
const sessionFolder = path.join(getTeaPath(), "tea.xyz/gui");
|
||||||
|
@ -20,17 +21,6 @@ export interface Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
let sessionMemory: Session = { device_id: "", locale: "en" };
|
let sessionMemory: Session = { device_id: "", locale: "en" };
|
||||||
const initialized: Promise<Session> = new Promise((resolve, reject) => {
|
|
||||||
try {
|
|
||||||
log.info("initializing GUI session folder");
|
|
||||||
createInitialSessionFile().then((newSession) => {
|
|
||||||
resolve(newSession);
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
log.error(error);
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
async function addEmptySessionFile(): Promise<Session> {
|
async function addEmptySessionFile(): Promise<Session> {
|
||||||
const locale = app.getLocale();
|
const locale = app.getLocale();
|
||||||
|
@ -44,12 +34,8 @@ async function addEmptySessionFile(): Promise<Session> {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createInitialSessionFile(): Promise<Session> {
|
async function createInitialSessionFile(): Promise<Session> {
|
||||||
// TODO: this looks nasty, refactor this
|
let session: Session = {};
|
||||||
// the app is too dependent that this function succeeds
|
|
||||||
let session = {
|
|
||||||
...sessionMemory
|
|
||||||
};
|
|
||||||
const locale = app.getLocale();
|
const locale = app.getLocale();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -70,19 +56,17 @@ export async function createInitialSessionFile(): Promise<Session> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!session?.device_id) {
|
if (!session?.device_id) {
|
||||||
try {
|
const newSession = await addEmptySessionFile();
|
||||||
const newSession = await addEmptySessionFile();
|
if (newSession) {
|
||||||
if (newSession) {
|
session = newSession;
|
||||||
session = newSession;
|
session.locale = locale;
|
||||||
session.locale = locale;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
log.error(error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionMemory = session;
|
sessionMemory = session;
|
||||||
|
|
||||||
|
if (!session.device_id) throw new Error("device_id is empty!");
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +90,12 @@ async function getDeviceId() {
|
||||||
|
|
||||||
export async function readSessionData(): Promise<Session> {
|
export async function readSessionData(): Promise<Session> {
|
||||||
log.info("read session data.");
|
log.info("read session data.");
|
||||||
const data = await initialized;
|
|
||||||
|
if (authFileState.getState() === "INITIALIZED" && !fs.existsSync(sessionFilePath)) {
|
||||||
|
authFileState.reset();
|
||||||
|
}
|
||||||
|
const data = await authFileState.initialize();
|
||||||
|
|
||||||
log.info(
|
log.info(
|
||||||
"initialized session device_id:",
|
"initialized session device_id:",
|
||||||
data?.device_id,
|
data?.device_id,
|
||||||
|
@ -129,7 +118,8 @@ export async function readSessionData(): Promise<Session> {
|
||||||
sessionMemory = session;
|
sessionMemory = session;
|
||||||
log.info("re-read session data done");
|
log.info("re-read session data done");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
sessionMemory = await createInitialSessionFile();
|
authFileState.reset();
|
||||||
|
sessionMemory = await authFileState.initialize();
|
||||||
log.error(error);
|
log.error(error);
|
||||||
}
|
}
|
||||||
return sessionMemory;
|
return sessionMemory;
|
||||||
|
@ -196,3 +186,6 @@ export async function pollAuth() {
|
||||||
}, 2000);
|
}, 2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const authFileState = new InitWatcher<Session>(createInitialSessionFile);
|
||||||
|
authFileState.initialize();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { getTeaPath } from "./tea-dir";
|
import { getTeaPath } from "./tea-dir";
|
||||||
import { createInitialSessionFile } from "./auth";
|
import { authFileState } from "./auth";
|
||||||
import * as https from "https";
|
import * as https from "https";
|
||||||
import { spawn } from "child_process";
|
import { spawn } from "child_process";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
@ -8,7 +8,7 @@ import { parse as semverParse } from "@tea/libtea";
|
||||||
|
|
||||||
type InitState = "NOT_INITIALIZED" | "PENDING" | "INITIALIZED";
|
type InitState = "NOT_INITIALIZED" | "PENDING" | "INITIALIZED";
|
||||||
|
|
||||||
class InitWatcher<T> {
|
export class InitWatcher<T> {
|
||||||
private initState: InitState;
|
private initState: InitState;
|
||||||
private initFunction: () => Promise<T>;
|
private initFunction: () => Promise<T>;
|
||||||
private initializationPromise: Promise<T> | undefined;
|
private initializationPromise: Promise<T> | undefined;
|
||||||
|
@ -156,6 +156,6 @@ async function installTeaCli() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function initialize(): Promise<string> {
|
export default async function initialize(): Promise<string> {
|
||||||
const [version] = await Promise.all([initializeTeaCli(), createInitialSessionFile()]);
|
const [version] = await Promise.all([initializeTeaCli(), authFileState.observe()]);
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue