mergeState.js
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { MERGE_STATE_KEY } from './constants.js'
import { normalizePath } from './pathCache.js'
function emptyState() {
return { anchoredRoot: '', handledAlternates: [] }
}
export function loadMergeState() {
try {
const raw = uni.getStorageSync(MERGE_STATE_KEY)
if (!raw) return emptyState()
const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw
if (!parsed || typeof parsed !== 'object') return emptyState()
return {
anchoredRoot: normalizePath(parsed.anchoredRoot || ''),
handledAlternates: (parsed.handledAlternates || [])
.map(normalizePath)
.filter(Boolean)
}
} catch (_) {
return emptyState()
}
}
function saveMergeState(state) {
uni.setStorageSync(MERGE_STATE_KEY, JSON.stringify(state))
}
/** 强制归并时清空「已处理 alternate」,便于重新扫描 */
export function resetAlternateHandledForForce(currentRoot) {
const root = normalizePath(currentRoot)
saveMergeState({ anchoredRoot: root, handledAlternates: [] })
}
/**
* 包根切换 → 清空已处理列表;未切换且全部 alternate 已处理 → 可跳过扫描
* @param {string} currentRoot 当前运行包根
* @param {{ path: string }[]} nonCurrentItems getNonCurrentPaths 结果
* @param {boolean} force 手动强制归并
*/
export function planAlternateScan(currentRoot, nonCurrentItems, force) {
const root = normalizePath(currentRoot)
const all = (nonCurrentItems || []).map(item => normalizePath(item.path)).filter(Boolean)
if (force) {
return {
skip: false,
paths: all,
reason: '手动强制归并,重新扫描全部 alternate 包根'
}
}
if (!root || all.length === 0) {
return { skip: true, paths: [], reason: '无 alternate 包根需扫描' }
}
let state = loadMergeState()
const anchored = normalizePath(state.anchoredRoot)
if (anchored !== root) {
state = { anchoredRoot: root, handledAlternates: [] }
saveMergeState(state)
return {
skip: false,
paths: all,
reason: '包根已切换,重新扫描 alternate',
previousRoot: anchored || '(无)'
}
}
const handledSet = new Set(state.handledAlternates)
const pending = all.filter(p => !handledSet.has(p))
if (pending.length === 0) {
return {
skip: true,
paths: [],
reason: '当前包根未切换,本应用 alternate 包根均已处理过',
anchoredRoot: root,
handledAlternates: [...handledSet]
}
}
return {
skip: false,
paths: pending,
reason: '存在尚未处理的 alternate 包根',
pending,
handledAlternates: [...handledSet]
}
}
/** 探测/合并结束且无硬错误时,标记该 alternate 包根已处理(仅针对当前 anchoredRoot) */
export function markAlternateRootHandled(currentRoot, alternateRoot) {
const root = normalizePath(currentRoot)
const alt = normalizePath(alternateRoot)
if (!root || !alt || root === alt) return
let state = loadMergeState()
if (normalizePath(state.anchoredRoot) !== root) {
state = { anchoredRoot: root, handledAlternates: [] }
}
const set = new Set(state.handledAlternates)
set.add(alt)
state.handledAlternates = [...set]
saveMergeState(state)
}