From 4e50e8d938c7029775e2e59caef419a9900b7316 Mon Sep 17 00:00:00 2001 From: beet <63141491+beetcb@users.noreply.github.com> Date: Sun, 20 Dec 2020 09:09:04 +0800 Subject: [PATCH] feat: optimize async process --- campusphere/app.js | 25 +++++++++++++++---------- index.js | 32 +++++++++++++++++++------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/campusphere/app.js b/campusphere/app.js index 4bdc89e..a0404df 100644 --- a/campusphere/app.js +++ b/campusphere/app.js @@ -31,16 +31,19 @@ exports.signApp = class signApp extends ( async signInfo() { const { signApi, headers } = this - const res = await fetch(signApi.list, { - method: 'POST', - headers, - body: JSON.stringify({}), - }) - - if (res.headers.hasOwnProperty('set-cookie')) return true - const signQ = await res.json() - this.curTask = signQ.datas.unSignedTasks[0] - return false + try { + const res = await fetch(signApi.list, { + method: 'POST', + headers, + body: JSON.stringify({}), + }) + if (res.headers.hasOwnProperty('set-cookie')) return true + const signQ = await res.json() + this.curTask = signQ.datas.unSignedTasks[0] + return false + } catch (e) { + return true + } } async signWithForm() { @@ -63,6 +66,8 @@ exports.signApp = class signApp extends ( isNeedExtra, signedStuInfo, } = signDetails.datas + + log.object(signedStuInfo) // format coordinates length ;[longitude, latitude] = this.randomLocale(signPlaceSelected[0]).map(e => Number(e.toFixed(6)) diff --git a/index.js b/index.js index 538dee0..822feee 100644 --- a/index.js +++ b/index.js @@ -21,9 +21,10 @@ log.object(users) * @swms continuing log into your school's swms [stu work magagement system] */ let cookie -let storeCookiePath, sign -/* get/store/update cookie synchronizedly */ +// purely for handleCookie func +let storeCookiePath, sign +/* get|store|update cookie synchronizedly */ async function handleCookie() { for (let i of users) { storeCookiePath = `cookie.${i.alias || i.username}` @@ -46,22 +47,13 @@ async function handleCookie() { } } -/* sign in asynchronizedly */ async function signIn(i) { - sign = new signApp(school, cookie, i) + const cookie = i.cookie || conf.get(`cookie.${i.alias || i.username}`) + const sign = new signApp(school, cookie, i) await sign.signInfo() await sign.signWithForm() } -;(async () => { - await handleCookie() - - // start to sign - for (let i of users) { - await signIn(i) - } -})() - async function reLogin(i) { const name = i.alias || i.username @@ -82,3 +74,17 @@ function storeCookie(path, i) { cookie = conf.get(path) log.success(`${name}: Using stored cookie`) } + +async function sleep(timeout) { + return new Promise(r => setTimeout(r, timeout * 1000 * 60)) +} + +;(async () => { + // Pre-loading cookie for sign in + await handleCookie() + // wait 1 minute for signing + await sleep(1) + + // sign in asynchronizedly with promise all and diff instance of signApp class + Promise.all(users.map(e => signIn(e))) +})()