Skip to content

Commit

Permalink
优化部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
slime7 committed Dec 6, 2018
1 parent 16f9ae1 commit a3f6cb1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 30 deletions.
39 changes: 34 additions & 5 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@

<script>
import { remote, ipcRenderer } from 'electron'; // eslint-disable-line
import { mapMutations } from 'vuex';
import { mapMutations, mapState } from 'vuex';
import Command from './components/Command.vue';
import RequestLine from './components/RequestLine.vue';
import Result from './components/Result.vue';
export default {
name: 'app',
computed: {
...mapState(['selected']),
},
components: {
Command,
RequestLine,
Expand All @@ -41,13 +46,37 @@ export default {
},
onReqReply() {
if (ipcRenderer) {
ipcRenderer.on('kancolle-command-ipc-reply', (event, requests) => {
const r = JSON.parse(JSON.stringify(requests));
this.setRequests(r);
ipcRenderer.on('kancolle-command-reply', (event, { requestIndex, requests }) => {
const requestsCopy = JSON.parse(JSON.stringify(requests));
if (requestsCopy.length === 0) {
this.selectEditingRequest(null);
}
if (typeof requestIndex === 'number') {
const [requestsComplated, requestsProgressing] = [[], []];
while (requestsCopy.length) {
if (requestsCopy[0].error || requestsCopy[0].response) {
requestsComplated.push(requestsCopy.shift());
} else {
requestsProgressing.push(requestsCopy.shift());
}
}
this.setRequests(requestsProgressing);
this.setLastRequests(requestsComplated);
if (requestsProgressing.length <= this.selected) {
this.selectEditingRequest(null);
}
} else {
this.setRequests(requestsCopy);
}
});
}
},
...mapMutations(['setRequests']),
...mapMutations([
'selectEditingRequest',
'setRequests',
'setLastRequests',
]),
},
mounted() {
Expand Down
11 changes: 5 additions & 6 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,23 @@ function createWindow() {
reqInd,
direction,
}) => {
const reply = (requests, requestIndex) => {
event.sender.send('kancolle-command-reply', { requests, requestIndex });
};
const init = (force) => {
if (force || !kanmand) {
kanmand = new KancolleRequest(reqData.gameLink);
kanmand.setStageEndCallback(reply);
}
};
const reply = (data) => {
event.sender.send('kancolle-command-ipc-reply', data);
};

switch (type) {
case 'start':
if (!kanmand) {
console.log('请求列表为空');
break;
}
await kanmand.start(() => {
event.sender.send('kancolle-command-ipc-reply', kanmand.requestInfo().requests);
});
await kanmand.start();
kanmand.clear();
kanmand = null;
break;
Expand Down
13 changes: 0 additions & 13 deletions src/components/Command.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ export default {
({ route: this.gameRoute, data: this.gameData } = this.requests[this.selected]);
}
},
requests() {
if (this.requests.length === 0) {
this.selectEditingRequest(null);
}
this.requests.forEach((req) => {
if (req.error || req.response) {
this.pushLastRequests(JSON.parse(JSON.stringify(req)));
this.removeRequest(this.requests.indexOf(req));
}
});
},
},
methods: {
Expand Down Expand Up @@ -155,8 +144,6 @@ export default {
},
...mapMutations([
'selectEditingRequest',
'pushLastRequests',
'removeRequest',
'clearLastRequests',
]),
},
Expand Down
4 changes: 3 additions & 1 deletion src/components/Result.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<div>
{{
lastRequests[resultIndex] ?
`${lastRequests[resultIndex].route.name}(${lastRequests[resultIndex].route.path})` : '-'
`${resultIndex + 1}.${lastRequests[resultIndex].route.name}
(${lastRequests[resultIndex].route.path})`
: '-'
}}
</div>
<div>
Expand Down
3 changes: 3 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export default new Vuex.Store({
pushLastRequests(state, req) {
state.lastRequests.push(req);
},
setLastRequests(state, req) {
state.lastRequests = req;
},
clearLastRequests(state) {
state.lastRequests = [];
},
Expand Down
15 changes: 10 additions & 5 deletions src/utils/KancolleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export default class KancolleRequest {
this.loading = 0;
this.requests = [];
this.requestIndex = 0;
this.stageEndCallback = null;
}

setStageEndCallback(stageEndCallback) {
this.stageEndCallback = stageEndCallback;
}

add(route, data) {
Expand All @@ -58,6 +63,7 @@ export default class KancolleRequest {
this.requests = [];
this.loading = 0;
this.requestIndex = 0;
// this.stageEndCallback = null;
}

remove(reqInd) {
Expand Down Expand Up @@ -104,7 +110,7 @@ export default class KancolleRequest {
};
}

async start(stageEndCallback) {
async start() {
this.loading += 1;
try {
const response = await this.connect();
Expand All @@ -121,10 +127,6 @@ export default class KancolleRequest {
}
await this.endTask(true);
}

if (typeof stageEndCallback === 'function') {
stageEndCallback(this.requestIndex, this.requests);
}
}

async connect() {
Expand Down Expand Up @@ -188,6 +190,9 @@ export default class KancolleRequest {
this.requests[this.requestIndex].api_error = true;
}

if (typeof this.stageEndCallback === 'function') {
this.stageEndCallback(this.requests, this.requestIndex);
}
this.requestIndex += 1;
if (this.requestIndex < this.requests.length) {
await this.start();
Expand Down

0 comments on commit a3f6cb1

Please sign in to comment.