Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
feat: optimize async process
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Dec 20, 2020
1 parent 01ad446 commit 4e50e8d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
25 changes: 15 additions & 10 deletions campusphere/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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))
Expand Down
32 changes: 19 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand All @@ -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

Expand All @@ -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)))
})()

0 comments on commit 4e50e8d

Please sign in to comment.