MCPcopy Index your code
hub / github.com/chavyleung/scripts / execSwitch

Function execSwitch

box/switcher/box.switcher.js:16–94  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14 .finally(() => $.done())
15
16function execSwitch() {
17 if (Array.isArray($.appIds) && $.appIds.length) {
18 $.log(`指定切换: ${$.appIds.join(',')}`)
19 }
20 $.subt = ''
21 $.desc = []
22 return new Promise((resove) => {
23 const sessions = getSessions()
24 const curSessions = getCurSessions()
25 // 会话排序: `创建时间`升序
26 sessions.sort(
27 (a, b) =>
28 a.createTime.replace(/-|:| /g, '') - b.createTime.replace(/-|:| /g, '')
29 )
30 const apps = {}
31 sessions.forEach((session) => {
32 const appId = session.appId
33 if ($.appIds.length == 0 || $.appIds.includes(appId)) {
34 const appName = session.appName
35 apps[appId] = apps[appId]
36 ? apps[appId]
37 : { id: appId, name: appName, sessions: [] }
38 const app = apps[appId]
39 app.sessions.push(session)
40 }
41 })
42 const switchkeys = {}
43 Object.keys(apps).forEach((appId) => {
44 const app = apps[appId]
45 if (app.sessions.length <= 1) {
46 $.desc.push(`${app.name}: 跳过! 原因: 只有 1 个会话?`)
47 return true
48 }
49 let curSessionIdx = app.sessions.findIndex(
50 (session) => session.id === curSessions[appId]
51 )
52 if (curSessionIdx === -1) {
53 curSessionIdx = app.sessions.length - 1
54 }
55 const curSession = app.sessions[curSessionIdx]
56 const isNewRound = curSessionIdx + 1 === app.sessions.length
57 const nextSessionIdx = isNewRound ? 0 : curSessionIdx + 1
58 const nextSession = app.sessions[nextSessionIdx]
59 nextSession.datas.forEach((_data) => {
60 const key = _data.key
61 const val = [undefined, null, 'undefined', 'null', ''].includes(
62 _data.val
63 )
64 ? ''
65 : _data.val
66 if (switchkeys[key]) {
67 const swData = switchkeys[key]
68 const isClash = swData.val !== val ? true : false
69 if (isClash) {
70 nextSession.isClash = true
71 $.log(
72 '',
73 `⚠️【${key}】冲突: `,

Callers 1

box.switcher.jsFile · 0.85

Calls 4

getSessionsFunction · 0.85
getCurSessionsFunction · 0.70
logMethod · 0.45
setdataMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…