11import { writable } from "svelte/store" ;
22
3- import { getDeviceAuth } from "@native" ;
3+ import { listenToChannel } from "@native" ;
44import type { Developer } from "@tea/ui/types" ;
55import type { Session } from "$libs/types" ;
66import { getSession as electronGetSession , updateSession as electronUpdateSession } from "@native" ;
77import { initSentry } from "../sentry" ;
8+ import log from "$libs/logger" ;
89
910export let session : Session | null = null ;
1011export const getSession = async ( ) : Promise < Session | null > => {
@@ -15,7 +16,6 @@ export const getSession = async (): Promise<Session | null> => {
1516export default function initAuthStore ( ) {
1617 const user = writable < Developer | undefined > ( ) ;
1718 const sessionStore = writable < Session > ( { } ) ;
18- let pollLoop = 0 ;
1919
2020 const deviceIdStore = writable < string > ( "" ) ;
2121 let deviceId = "" ;
@@ -31,7 +31,16 @@ export default function initAuthStore() {
3131 }
3232 } ) ;
3333
34- let timer : NodeJS . Timer | null ;
34+ listenToChannel ( "session-update" , ( data : Session ) => {
35+ log . info ( "session update renderer" , data ) ;
36+ sessionStore . update ( ( val ) => ( {
37+ ...val ,
38+ ...data
39+ } ) ) ;
40+ if ( data . user ) {
41+ user . set ( data . user ) ;
42+ }
43+ } ) ;
3544
3645 async function updateSession ( data : Session ) {
3746 sessionStore . update ( ( val ) => ( {
@@ -43,34 +52,6 @@ export default function initAuthStore() {
4352 await electronUpdateSession ( data ) ;
4453 }
4554
46- async function pollSession ( ) {
47- if ( ! timer ) {
48- timer = setInterval ( async ( ) => {
49- pollLoop ++ ;
50- try {
51- const data = await getDeviceAuth ( deviceId ) ;
52- if ( data . status === "SUCCESS" ) {
53- updateSession ( {
54- key : data . key ,
55- user : data . user
56- } ) ;
57- user . set ( data . user ! ) ;
58- timer && clearInterval ( timer ) ;
59- timer = null ;
60- }
61- } catch ( error ) {
62- console . error ( error ) ;
63- }
64-
65- if ( pollLoop > 20 && timer ) {
66- clearInterval ( timer ) ;
67- pollLoop = 0 ;
68- timer = null ;
69- }
70- } , 2000 ) ;
71- }
72- }
73-
7455 function clearSession ( ) {
7556 updateSession ( { key : undefined , user : undefined } ) ;
7657 user . set ( undefined ) ;
@@ -81,7 +62,6 @@ export default function initAuthStore() {
8162 session : sessionStore ,
8263 deviceId,
8364 deviceIdStore,
84- pollSession,
8565 clearSession,
8666 updateSession
8767 } ;
0 commit comments