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 { DeviceAuth } from "../../src/libs/types";
|
||||
import { notifyMainWindow } from "../electron";
|
||||
import { InitWatcher } from "./initialize";
|
||||
|
||||
const sessionFilePath = path.join(getTeaPath(), "tea.xyz/gui/tmp.dat");
|
||||
const sessionFolder = path.join(getTeaPath(), "tea.xyz/gui");
|
||||
|
@ -20,17 +21,6 @@ export interface Session {
|
|||
}
|
||||
|
||||
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> {
|
||||
const locale = app.getLocale();
|
||||
|
@ -44,12 +34,8 @@ async function addEmptySessionFile(): Promise<Session> {
|
|||
return data;
|
||||
}
|
||||
|
||||
export async function createInitialSessionFile(): Promise<Session> {
|
||||
// TODO: this looks nasty, refactor this
|
||||
// the app is too dependent that this function succeeds
|
||||
let session = {
|
||||
...sessionMemory
|
||||
};
|
||||
async function createInitialSessionFile(): Promise<Session> {
|
||||
let session: Session = {};
|
||||
const locale = app.getLocale();
|
||||
|
||||
try {
|
||||
|
@ -70,19 +56,17 @@ export async function createInitialSessionFile(): Promise<Session> {
|
|||
}
|
||||
|
||||
if (!session?.device_id) {
|
||||
try {
|
||||
const newSession = await addEmptySessionFile();
|
||||
if (newSession) {
|
||||
session = newSession;
|
||||
session.locale = locale;
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
const newSession = await addEmptySessionFile();
|
||||
if (newSession) {
|
||||
session = newSession;
|
||||
session.locale = locale;
|
||||
}
|
||||
}
|
||||
|
||||
sessionMemory = session;
|
||||
|
||||
if (!session.device_id) throw new Error("device_id is empty!");
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
|
@ -106,7 +90,12 @@ async function getDeviceId() {
|
|||
|
||||
export async function readSessionData(): Promise<Session> {
|
||||
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(
|
||||
"initialized session device_id:",
|
||||
data?.device_id,
|
||||
|
@ -129,7 +118,8 @@ export async function readSessionData(): Promise<Session> {
|
|||
sessionMemory = session;
|
||||
log.info("re-read session data done");
|
||||
} catch (error) {
|
||||
sessionMemory = await createInitialSessionFile();
|
||||
authFileState.reset();
|
||||
sessionMemory = await authFileState.initialize();
|
||||
log.error(error);
|
||||
}
|
||||
return sessionMemory;
|
||||
|
@ -196,3 +186,6 @@ export async function pollAuth() {
|
|||
}, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
export const authFileState = new InitWatcher<Session>(createInitialSessionFile);
|
||||
authFileState.initialize();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import fs from "fs";
|
||||
import { getTeaPath } from "./tea-dir";
|
||||
import { createInitialSessionFile } from "./auth";
|
||||
import { authFileState } from "./auth";
|
||||
import * as https from "https";
|
||||
import { spawn } from "child_process";
|
||||
import path from "path";
|
||||
|
@ -8,7 +8,7 @@ import { parse as semverParse } from "@tea/libtea";
|
|||
|
||||
type InitState = "NOT_INITIALIZED" | "PENDING" | "INITIALIZED";
|
||||
|
||||
class InitWatcher<T> {
|
||||
export class InitWatcher<T> {
|
||||
private initState: InitState;
|
||||
private initFunction: () => Promise<T>;
|
||||
private initializationPromise: Promise<T> | undefined;
|
||||
|
@ -156,6 +156,6 @@ async function installTeaCli() {
|
|||
}
|
||||
|
||||
export default async function initialize(): Promise<string> {
|
||||
const [version] = await Promise.all([initializeTeaCli(), createInitialSessionFile()]);
|
||||
const [version] = await Promise.all([initializeTeaCli(), authFileState.observe()]);
|
||||
return version;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue