Skip to content

Commit af5a784

Browse files
committed
Fix the problem that the hosts in the folder cannot be refreshed. #452 #487 #498 #507
1 parent 0857af5 commit af5a784

8 files changed

+94
-82
lines changed

app-ui/frame/EditPrompt.jsx

+38-36
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import Group from './Group'
2424
import Agent from '../Agent'
2525
import makeId from '../../app/libs/make-id'
2626
import { WHERE_LOCAL, WHERE_REMOTE, WHERE_GROUP, WHERE_FOLDER } from '../configs/contants'
27+
import treeFunc from '../../app/libs/treeFunc'
2728
import styles from './EditPrompt.less'
2829

2930
const RadioButton = Radio.Button
3031
const RadioGroup = Radio.Group
3132
const Option = Select.Option
3233

3334
export default class EditPrompt extends React.Component {
34-
constructor (props) {
35+
constructor(props) {
3536
super(props)
3637

3738
this.state = {
@@ -50,12 +51,12 @@ export default class EditPrompt extends React.Component {
5051
this.current_hosts = null
5152
}
5253

53-
tryToFocus () {
54+
tryToFocus() {
5455
let el = this.el_body && this.el_body.querySelector('input[type=text]')
5556
el && el.focus()
5657
}
5758

58-
clear () {
59+
clear() {
5960
this.setState({
6061
where: WHERE_LOCAL,
6162
title: '',
@@ -65,7 +66,7 @@ export default class EditPrompt extends React.Component {
6566
})
6667
}
6768

68-
componentDidMount () {
69+
componentDidMount() {
6970
Agent.on('add_hosts', (title, uri) => {
7071
let goWhere = WHERE_LOCAL
7172
if (uri) {
@@ -106,11 +107,12 @@ export default class EditPrompt extends React.Component {
106107
})
107108

108109
Agent.on('list_updated', list => {
109-
let hosts = list.find(i => i.id === this.state.id)
110+
//let hosts = list.find(i => i.id === this.state.id)
111+
let hosts = treeFunc.getItemById(list, this.state.id)
110112
if (hosts) {
111113
this.current_hosts = hosts
112-
this.setState({last_refresh: hosts.last_refresh})
113-
setTimeout(() => this.setState({is_loading: false}), 500)
114+
this.setState({ last_refresh: hosts.last_refresh })
115+
setTimeout(() => this.setState({ is_loading: false }), 500)
114116
}
115117
})
116118

@@ -129,7 +131,7 @@ export default class EditPrompt extends React.Component {
129131
})
130132
}
131133

132-
onOK () {
134+
onOK() {
133135
this.setState({
134136
title: (this.state.title || '').replace(/^\s+|\s+$/g, ''),
135137
url: (this.state.url || '').replace(/^\s+|\s+$/g, '')
@@ -170,15 +172,15 @@ export default class EditPrompt extends React.Component {
170172
this.clear()
171173
}
172174

173-
onCancel () {
175+
onCancel() {
174176
this.setState({
175177
show: false
176178
})
177179
this.clear()
178180
}
179181

180-
confirmDel () {
181-
let {lang} = this.props
182+
confirmDel() {
183+
let { lang } = this.props
182184
if (!confirm(lang.confirm_del)) return
183185
Agent.emit('del_hosts', this.current_hosts)
184186
this.setState({
@@ -187,12 +189,12 @@ export default class EditPrompt extends React.Component {
187189
this.clear()
188190
}
189191

190-
updateInclude (include) {
191-
this.setState({include})
192+
updateInclude(include) {
193+
this.setState({ include })
192194
}
193195

194-
getRefreshOptions () {
195-
let {lang} = this.props
196+
getRefreshOptions() {
197+
let { lang } = this.props
196198
let k = [
197199
[0, `${lang.never}`],
198200
[1 / 60, `1 ${lang.minute}`],
@@ -212,10 +214,10 @@ export default class EditPrompt extends React.Component {
212214
})
213215
}
214216

215-
getEditOperations () {
217+
getEditOperations() {
216218
if (this.state.is_add) return null
217219

218-
let {lang} = this.props
220+
let { lang } = this.props
219221

220222
return (
221223
<div>
@@ -231,7 +233,7 @@ export default class EditPrompt extends React.Component {
231233
)
232234
}
233235

234-
refresh () {
236+
refresh() {
235237
if (this.state.is_loading) return
236238

237239
Agent.emit('check_hosts_refresh', this.current_hosts)
@@ -241,7 +243,7 @@ export default class EditPrompt extends React.Component {
241243

242244
}
243245

244-
renderGroup () {
246+
renderGroup() {
245247
if (this.state.where !== WHERE_GROUP) return null
246248

247249
return <Group
@@ -251,11 +253,11 @@ export default class EditPrompt extends React.Component {
251253
/>
252254
}
253255

254-
renderRemoteInputs () {
256+
renderRemoteInputs() {
255257
if (this.state.where !== WHERE_REMOTE) return null
256258

257-
let {lang} = this.props
258-
let {is_loading} = this.state
259+
let { lang } = this.props
260+
let { is_loading } = this.state
259261

260262
return (
261263
<div className="remote-ipts">
@@ -266,7 +268,7 @@ export default class EditPrompt extends React.Component {
266268
ref={c => this.el_url = c}
267269
value={this.state.url}
268270
placeholder="http:// or file:///"
269-
onChange={e => this.setState({url: e.target.value})}
271+
onChange={e => this.setState({ url: e.target.value })}
270272
onKeyDown={e => (e.keyCode === 13 && this.onOK()) || (e.keyCode === 27 && this.onCancel())}
271273
maxLength={1024}
272274
/>
@@ -277,8 +279,8 @@ export default class EditPrompt extends React.Component {
277279
<div className="cnt">
278280
<Select
279281
value={this.state.refresh_interval}
280-
style={{width: 120}}
281-
onChange={v => this.setState({refresh_interval: parseFloat(v) || 0})}
282+
style={{ width: 120 }}
283+
onChange={v => this.setState({ refresh_interval: parseFloat(v) || 0 })}
282284
>
283285
{this.getRefreshOptions()}
284286
</Select>
@@ -303,17 +305,17 @@ export default class EditPrompt extends React.Component {
303305
)
304306
}
305307

306-
renderFolder () {
308+
renderFolder() {
307309
if (this.state.where !== WHERE_FOLDER) return null
308-
let {lang} = this.props
309-
let {folder_mode} = this.state
310+
let { lang } = this.props
311+
let { folder_mode } = this.state
310312

311313
return (
312314
<div>
313315
<div className="ln">
314316
<div className="title">{lang.pref_choice_mode}</div>
315317
<div className="cnt">
316-
<RadioGroup onChange={e => this.setState({folder_mode: e.target.value})} value={folder_mode}>
318+
<RadioGroup onChange={e => this.setState({ folder_mode: e.target.value })} value={folder_mode}>
317319
<RadioButton value={0}><BorderOuterOutlined/> {lang.default}</RadioButton>
318320
<RadioButton value={1}><CheckCircleOutlined/> {lang.pref_choice_mode_single}</RadioButton>
319321
<RadioButton value={2}><CheckSquareOutlined/> {lang.pref_choice_mode_multiple}</RadioButton>
@@ -324,9 +326,9 @@ export default class EditPrompt extends React.Component {
324326
)
325327
}
326328

327-
body () {
328-
let {lang} = this.props
329-
let {where, title, is_add} = this.state
329+
body() {
330+
let { lang } = this.props
331+
let { where, title, is_add } = this.state
330332

331333
return (
332334
<div className={styles.tab} ref={c => this.el_body = c}>
@@ -336,7 +338,7 @@ export default class EditPrompt extends React.Component {
336338
<Input
337339
ref={c => this.el_title = c}
338340
value={title}
339-
onChange={(e) => this.setState({title: e.target.value})}
341+
onChange={(e) => this.setState({ title: e.target.value })}
340342
onKeyDown={(e) => (e.keyCode === 13 && this.onOK() || e.keyCode === 27 && this.onCancel())}
341343
maxLength={50}
342344
/>
@@ -348,7 +350,7 @@ export default class EditPrompt extends React.Component {
348350
<div className="cnt">
349351
<RadioGroup
350352
disabled={!is_add}
351-
onChange={e => this.setState({where: e.target.value})}
353+
onChange={e => this.setState({ where: e.target.value })}
352354
value={where}
353355
>
354356
<RadioButton value={WHERE_LOCAL}><FileTextOutlined/> {lang.where_local}</RadioButton>
@@ -367,8 +369,8 @@ export default class EditPrompt extends React.Component {
367369
)
368370
}
369371

370-
render () {
371-
let {lang} = this.props
372+
render() {
373+
let { lang } = this.props
372374

373375
return (
374376
<MyFrame

app/package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "switchhosts",
3-
"version": "3.5.5",
3+
"version": "3.5.6",
44
"description": "Switch hosts quickly!",
55
"main": "main.js",
66
"scripts": {
@@ -14,6 +14,7 @@
1414
"electron-window-state": "^5.0.3",
1515
"express": "^4.17.1",
1616
"js-beautify": "^1.13.0",
17+
"lodash": "^4.17.20",
1718
"md5-file": "^5.0.0",
1819
"moment": "^2.27.0",
1920
"node-notifier": "^8.0.0",

app/server/actions/checkNeedRemoteRefresh.js

+7-19
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,20 @@
55

66
'use strict'
77

8+
const lodash = require('lodash')
89
const saveHosts = require('./saveHosts')
910
const checkOne = require('./checkOneRemoteHosts')
1011

11-
function isHostsEqual(hosts1, hosts2) {
12-
return hosts1.id === hosts2.id && hosts1.content === hosts2.content && hosts1.last_refresh === hosts2.last_refresh
13-
}
14-
15-
function isListEqual (list1, list2) {
16-
let len = list1.length
17-
18-
if (len !== list2.length) return false
19-
for (let i = 0; i < len; i ++) {
20-
if (!isHostsEqual(list1[i] ,list2[i])) {
21-
return false
22-
}
23-
}
24-
25-
return true
26-
}
27-
2812
module.exports = (svr, list, hosts = null) => {
29-
return Promise.all(list.map(i => checkOne(svr, i, hosts && hosts.id === i.id)))
13+
let original_list = lodash.cloneDeep(list)
14+
15+
return Promise.all(list.map(i => checkOne(svr, i, hosts && hosts.id)))
3016
.then(list2 => {
31-
if (!isListEqual(list, list2)) {
17+
if (!lodash.isEqual(original_list, list2)) {
3218
// 仅在 list 的内容发生变化时才再次保存
3319
return saveHosts(svr, list2)
20+
} else {
21+
console.log('hosts list is not changed.')
3422
}
3523
})
3624
}

app/server/actions/checkOneRemoteHosts.js

+39-23
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,49 @@ const isExpired = require('../checkIsExpired')
1010
const lineBreakTransform = require('../../libs/lineBreakTransform')
1111
const moment = require('moment')
1212

13-
function now () {
13+
function now() {
1414
return moment().format('YYYY-MM-DD HH:mm:ss')
1515
}
1616

17-
module.exports = (svr, hosts, force = false) => {
18-
return new Promise((resolve, reject) => {
19-
if (hosts.where !== 'remote' || !hosts.url) {
20-
resolve(hosts)
21-
return
17+
const checkChildren = async (svr, children, force_id) => {
18+
let new_children = []
19+
20+
for (let i of children) {
21+
if (typeof i === 'object') {
22+
new_children.push(await checkOne(svr, i, force_id))
2223
}
24+
}
25+
26+
return new_children
27+
}
28+
29+
const checkOne = async (svr, hosts, force_id = null) => {
30+
let force = hosts.id === force_id
31+
32+
if (Array.isArray(hosts.children) && hosts.children.length > 0) {
33+
hosts.children = await checkChildren(svr, hosts.children, force_id)
34+
}
2335

24-
if (force || isExpired(svr, hosts)) {
25-
let hosts2 = Object.assign({}, hosts)
26-
27-
console.log('checkRemote', `'${hosts2.title}'`, force, isExpired(svr, hosts2))
28-
getUrl(svr, hosts2.url)
29-
.then(content => {
30-
hosts2.content = lineBreakTransform(content)
31-
hosts2.last_refresh = now()
32-
})
33-
.then(() => resolve(hosts2))
34-
.catch(e => {
35-
console.log(e)
36-
reject(e)
37-
})
38-
} else {
39-
resolve(hosts)
36+
if (hosts.where !== 'remote' || !hosts.url) {
37+
return hosts
38+
}
39+
40+
if (force || isExpired(svr, hosts)) {
41+
let hosts2 = Object.assign({}, hosts)
42+
console.log('checkRemote', `'${hosts2.title}'`, force, isExpired(svr, hosts2))
43+
44+
try {
45+
let content = await getUrl(svr, hosts2.url)
46+
hosts2.content = lineBreakTransform(content)
47+
hosts2.last_refresh = now()
48+
} catch (e) {
49+
console.error(e)
4050
}
41-
})
51+
52+
return hosts2
53+
}
54+
55+
return hosts
4256
}
57+
58+
module.exports = checkOne

app/ui/app.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = [3, 5, 5, 5541]
1+
module.exports = [3, 5, 6, 5551]

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "switchhosts",
3-
"version": "3.5.5",
3+
"version": "3.5.6",
44
"description": "A tool for managing and switching hosts.",
55
"main": "",
66
"scripts": {

0 commit comments

Comments
 (0)