Skip to content

Commit a75b7df

Browse files
committed
add action queueing to support different behaviors between changing files or creating new files
1 parent d704ab5 commit a75b7df

File tree

10 files changed

+107
-35
lines changed

10 files changed

+107
-35
lines changed

src/constants/action-types.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ const actionTypes = {
2323
UPDATE_SELECTED_DEVICE: 'UPDATE_SELECTED_DEVICE',
2424
RESET_DOWNLOAD_PROGRESS: 'RESET_DOWNLOAD_PROGRESS',
2525
UPDATE_DOWNLOAD_PROGRESS: 'UPDATE_DOWNLOAD_PROGRESS',
26-
RESET_FILE_QUEUE: 'RESET_FILE_QUEUE',
27-
QUEUE_FILE_CHANGE: 'QUEUE_FILE_CHANGE'
26+
QUEUE_NEW_FILE: 'QUEUE_NEW_FILE',
27+
QUEUE_CHANGE_FILE: 'QUEUE_CHANGE_FILE',
28+
RESET_ACTION_QUEUE: 'RESET_ACTION_QUEUE'
2829
};
2930

3031
module.exports = actionTypes;

src/constants/queued-action-types.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const queuedActionTypes = {
4+
NEW_FILE: 'NEW_FILE',
5+
CHANGE_FILE: 'CHANGE_FILE'
6+
};
7+
8+
module.exports = queuedActionTypes;

src/creators/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ const creators = {
3030
updateSelectedDevice: require('./update-selected-device'),
3131
resetDownloadProgress: require('./reset-download-progress'),
3232
updateDownloadProgress: require('./update-download-progress'),
33-
// file creators
34-
resetFileQueue: require('./reset-file-queue'),
35-
queueFileChange: require('./queue-file-change')
33+
// action queue creators
34+
queueNewFile: require('./queue-new-file'),
35+
queueChangeFile: require('./queue-change-file'),
36+
resetActionQueue: require('./reset-action-queue')
3637
};
3738

3839
module.exports = creators;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
'use strict';
22

33
const {
4-
QUEUE_FILE_CHANGE
4+
QUEUE_CHANGE_FILE
55
} = require('../constants/action-types');
66

7-
function queueFileChange(filename){
7+
function queueChangeFile(filename){
88
return {
9-
type: QUEUE_FILE_CHANGE,
9+
type: QUEUE_CHANGE_FILE,
1010
payload: {
1111
filename
1212
}
1313
};
1414
}
1515

16-
module.exports = queueFileChange;
16+
module.exports = queueChangeFile;

src/creators/queue-new-file.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const {
4+
QUEUE_NEW_FILE
5+
} = require('../constants/action-types');
6+
7+
function queueNewFile(){
8+
return {
9+
type: QUEUE_NEW_FILE,
10+
payload: {}
11+
};
12+
}
13+
14+
module.exports = queueNewFile;

src/creators/reset-file-queue.js renamed to src/creators/reset-action-queue.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

33
const {
4-
RESET_FILE_QUEUE
4+
RESET_ACTION_QUEUE
55
} = require('../constants/action-types');
66

77
function resetFileQueue(){
88
return {
9-
type: RESET_FILE_QUEUE,
9+
type: RESET_ACTION_QUEUE,
1010
payload: {}
1111
};
1212
}

src/plugins/handlers.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const Terminal = require('../lib/terminal');
1313
const Documents = require('../lib/documents');
1414
const highlighter = require('../lib/highlighter');
1515

16+
const {
17+
NEW_FILE,
18+
CHANGE_FILE
19+
} = require('../constants/queued-action-types');
20+
1621
// TODO: move somewhere else?
1722
const red = '#da2100';
1823
const green = '#159600';
@@ -56,8 +61,21 @@ function handlers(app, opts, done){
5661
userConfig
5762
} = app;
5863

64+
function handleActionQueue(){
65+
const { nextAction, nextFile } = store.getState();
66+
67+
store.dispatch(creators.resetActionQueue());
68+
69+
switch(nextAction){
70+
case NEW_FILE:
71+
return newFile();
72+
case CHANGE_FILE:
73+
return changeFile(nextFile);
74+
}
75+
}
76+
5977
function newFile(){
60-
const { cwd, directory } = workspace.getState();
78+
const { cwd, directory, isNew, content } = workspace.getState();
6179

6280
// TODO: utility function
6381
const untitledNums = _.reduce(directory, function(untitled, dirfile) {
@@ -74,6 +92,13 @@ function handlers(app, opts, done){
7492

7593
const builtName = `untitled${untitledLast + 1}`;
7694

95+
// TODO: DRY this up
96+
if(isNew && _.trim(content).length){
97+
store.dispatch(creators.queueNewFile());
98+
showSaveOverlay();
99+
return;
100+
}
101+
77102
workspace.newFile(builtName, '')
78103
.then(() => userConfig.set('last-file', builtName))
79104
.then(function(){
@@ -100,30 +125,22 @@ function handlers(app, opts, done){
100125
return;
101126
}
102127

103-
const { nextFile } = store.getState();
104128
const { cwd, content } = workspace.getState();
105129

106130
workspace.updateFilename(filename)
107-
.then(function(){
108-
return workspace.saveFile(filename, content);
109-
})
131+
.then(() => workspace.saveFile(filename, content))
110132
.then(function(){
111133
documents.swap(path.join(cwd, filename));
112-
if(nextFile){
113-
changeFile(nextFile);
114-
}
134+
handleActionQueue();
115135
});
116136
}
117137

118138
function dontSaveFile(){
119139
const { nextFile } = store.getState();
120140

141+
// TODO: handle error
121142
workspace.resetFile()
122-
.then(function(){
123-
if(nextFile){
124-
changeFile(nextFile);
125-
}
126-
});
143+
.then(handleActionQueue);
127144
}
128145

129146
function deleteFile(filename){
@@ -147,19 +164,20 @@ function handlers(app, opts, done){
147164
cwd
148165
} = workspace.getState();
149166

150-
store.dispatch(creators.resetFileQueue());
151-
167+
// TODO: DRY this up
152168
if(isNew && _.trim(content).length){
153-
store.dispatch(creators.queueFileChange(filename));
169+
store.dispatch(creators.queueChangeFile(filename));
154170
showSaveOverlay();
155171
return;
156172
}
157173

158174
const doc = documents.swap(path.join(cwd, filename));
159175
if(doc){
160-
workspace.updateFilename(filename);
161-
workspace.updateContent(doc.getValue());
162-
documents.focus();
176+
workspace.changeFile(filename)
177+
.then(() => workspace.updateContent(doc.getValue()))
178+
.then(function(){
179+
documents.focus();
180+
});
163181
return;
164182
}
165183

src/reducers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const reducers = {
77
device: require('./device'),
88
deviceList: require('./device-list'),
99
downloadProgress: require('./download-progress'),
10-
nextFile: require('./next-file')
10+
nextFile: require('./next-file'),
11+
nextAction: require('./next-action')
1112
};
1213

1314
module.exports = reducers;

src/reducers/next-action.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const {
4+
QUEUE_NEW_FILE,
5+
QUEUE_CHANGE_FILE,
6+
RESET_ACTION_QUEUE
7+
} = require('../constants/action-types');
8+
9+
const {
10+
NEW_FILE,
11+
CHANGE_FILE
12+
} = require('../constants/queued-action-types');
13+
14+
const initial = '';
15+
16+
function nextAction(state = initial, { type }){
17+
switch(type){
18+
case QUEUE_NEW_FILE:
19+
return NEW_FILE;
20+
case QUEUE_CHANGE_FILE:
21+
return CHANGE_FILE;
22+
case RESET_ACTION_QUEUE:
23+
return initial;
24+
default:
25+
return state;
26+
}
27+
}
28+
29+
module.exports = nextAction;

src/reducers/next-file.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use strict';
22

33
const {
4-
RESET_FILE_QUEUE,
5-
QUEUE_FILE_CHANGE
4+
QUEUE_CHANGE_FILE,
5+
RESET_ACTION_QUEUE
66
} = require('../constants/action-types');
77

88
const initial = '';
99

1010
function nextFile(state = initial, { type, payload }){
1111
switch(type){
12-
case QUEUE_FILE_CHANGE:
12+
case QUEUE_CHANGE_FILE:
1313
return payload.filename;
14-
case RESET_FILE_QUEUE:
14+
case RESET_ACTION_QUEUE:
1515
return initial;
1616
default:
1717
return state;

0 commit comments

Comments
 (0)