Skip to content

Commit 172d3ee

Browse files
committed
avoid dispatching handleSuccess/handleError actions
1 parent 068ddb6 commit 172d3ee

File tree

8 files changed

+136
-87
lines changed

8 files changed

+136
-87
lines changed

plugins/editor/index.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@ require('codemirror/addon/selection/mark-selection');
99
require('codemirror/lib/codemirror.css');
1010
require('../../assets/theme/parallax.css');
1111

12+
const React = require('react');
1213
const CodeMirror = require('codemirror');
1314
require('./pbasic')(CodeMirror);
1415

1516
const keyExtension = require('./key-extension');
17+
1618
const consoleStore = require('../../src/stores/console');
1719
const editorStore = require('../../src/stores/editor');
1820
const fileStore = require('../../src/stores/file');
21+
1922
const { handleInput } = require('../../src/actions/editor');
2023
const DocumentsStore = require('../../src/stores/documents');
2124

22-
const React = require('react');
2325
const TransmissionBar = require('./transmission-bar');
2426

27+
const makeToasts = require('../../src/lib/toasts');
28+
2529
function editor(app, opts, done){
2630

2731
var codeEditor;
@@ -37,9 +41,30 @@ function editor(app, opts, done){
3741
}
3842
}
3943

44+
function highlighter(position, length) {
45+
if(!codeEditor){
46+
return;
47+
}
48+
49+
const doc = codeEditor.getDoc();
50+
51+
const anchor = doc.posFromIndex(position);
52+
const head = doc.posFromIndex(position + length);
53+
54+
doc.setSelection(anchor, head);
55+
}
56+
4057
consoleStore.listen(refreshConsole);
4158

42-
var space = app.workspace;
59+
const space = app.workspace;
60+
const compile = app.compile.bind(app);
61+
// seems strange to pass highlighter to toasts
62+
// maybe this should be named "handlers" or something
63+
const toasts = makeToasts(app.toast, highlighter);
64+
65+
editorStore.toasts = toasts;
66+
editorStore.compile = compile;
67+
editorStore.workspace = space;
4368

4469
app.view('editor', function(el, cb){
4570
console.log('editor render');

plugins/overlays/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const overlayStore = require('../../src/stores/overlay');
1111
const projectStore = require('../../src/stores/project');
1212

1313
const { confirmDelete, changeProject, deleteProject } = require('../../src/actions/project');
14-
const { handleError, handleSuccess, deleteFile, saveFileAs } = require('../../src/actions/file');
14+
const { deleteFile, saveFileAs } = require('../../src/actions/file');
1515
const { hideSave, hideDelete, hideDownload, showProjects, hideProjects } = require('../../src/actions/overlay');
1616

1717
function overlays(app, opts, done){
@@ -61,10 +61,7 @@ function overlays(app, opts, done){
6161
if(showDownloadOverlay){
6262
component = (
6363
<DownloadOverlay
64-
onCancel={hideDownload}
65-
handleSuccess={handleSuccess}
66-
handleError={handleError}
67-
handleComplete={hideDownload} />
64+
onCancel={hideDownload} />
6865
);
6966
}
7067

plugins/sidebar/index.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ const FileOperations = require('./file-operations');
1010
const ProjectOperations = require('./project-operations');
1111

1212
const deviceStore = require('../../src/stores/device');
13-
const editorStore = require('../../src/stores/editor');
1413
const fileStore = require('../../src/stores/file');
1514

1615
const { loadFile } = require('../../src/actions/file');
1716

17+
const makeToasts = require('../../src/lib/toasts');
18+
1819
function sidebar(app, opts, done){
1920

2021
const space = app.workspace;
21-
const toast = app.toast;
22-
const overlay = app.overlay;
2322
const userConfig = app.userConfig;
24-
const irken = app;
25-
const compile = app.compile.bind(irken);
26-
const getBoard = app.getBoard.bind(irken);
27-
const scanBoards = app.scanBoards.bind(irken);
23+
const getBoard = app.getBoard.bind(app);
24+
const scanBoards = app.scanBoards.bind(app);
25+
26+
const toasts = makeToasts(app.toast);
2827

2928
function refreshDirectory(){
3029
// TODO: expose a method to refresh directory without changing it
@@ -59,18 +58,13 @@ function sidebar(app, opts, done){
5958

6059
// Store bindings
6160
deviceStore.workspace = space;
62-
deviceStore.toast = toast;
63-
deviceStore.overlay = overlay;
61+
deviceStore.toasts = toasts;
6462
deviceStore.getBoard = getBoard;
6563
deviceStore.scanBoards = scanBoards;
6664

67-
editorStore.toast = toast;
68-
editorStore.compile = compile;
69-
editorStore.workspace = space;
70-
7165
fileStore.workspace = space;
7266
fileStore.userConfig = userConfig;
73-
fileStore.toast = toast;
67+
fileStore.toasts = toasts;
7468

7569
done();
7670
}

src/actions/file.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ class FileActions {
3030
saveFileAs(name) {
3131
this.dispatch(name);
3232
}
33-
34-
handleError(err) {
35-
this.dispatch(err);
36-
}
37-
38-
handleSuccess(msg) {
39-
this.dispatch(msg);
40-
}
4133
}
4234

4335
module.exports = alt.createActions(FileActions);

src/lib/toasts.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const red = '#da2100';
4+
const green = '#159600';
5+
6+
const styles = {
7+
errorToast: {
8+
backgroundColor: red
9+
},
10+
successToast: {
11+
backgroundColor: green
12+
}
13+
};
14+
15+
function toasts(api, highlighter){
16+
17+
function success(msg){
18+
api.show(msg, { style: styles.successToast, timeout: 5000 });
19+
}
20+
21+
function error(err){
22+
// leaving this in for better debugging of errors
23+
console.log(err);
24+
25+
api.show(err.message, { style: styles.errorToast });
26+
27+
if(typeof highlighter !== 'function'){
28+
return;
29+
}
30+
31+
if(err && err.errorLength){
32+
highlighter(err.errorPosition, err.errorLength);
33+
}
34+
}
35+
36+
function clear(){
37+
api.clear();
38+
}
39+
40+
return {
41+
success,
42+
error,
43+
clear
44+
};
45+
}
46+
47+
module.exports = toasts;

src/stores/device.js

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

3-
const alt = require('../alt');
43
const _ = require('lodash');
54

5+
const alt = require('../alt');
6+
67
const { rx, tx } = require('../actions/transmission');
78
const { hideDownload, showDownload } = require('../actions/overlay');
89
const { clearOutput, output } = require('../actions/console');
910
const { enableAuto, disableAuto, reloadDevices, updateSelected } = require('../actions/device');
10-
const { handleSuccess, handleError } = require('../actions/file');
1111

1212
class DeviceStore {
1313
constructor() {
@@ -134,7 +134,7 @@ class DeviceStore {
134134
this.setState({ progress: progress });
135135
}
136136

137-
const { workspace, toast, getBoard } = this.getInstance();
137+
const { workspace, getBoard } = this.getInstance();
138138
const { selectedDevice } = this.state;
139139

140140
const name = workspace.filename.deref();
@@ -155,16 +155,34 @@ class DeviceStore {
155155
.tap(() => clearOutput())
156156
.then(() => board.on('terminal', output))
157157
.then(() => board.on('terminal', rx))
158-
.tap(() => toast.clear())
159-
.tap(() => handleSuccess(`'${name}' downloaded successfully`))
160-
.catch(handleError)
158+
.tap(() => this._handleClear())
159+
.tap(() => this._handleSuccess(`'${name}' downloaded successfully`))
160+
.catch((err) => this._handleError(err))
161161
.finally(() => {
162162
board.removeListener('progress', updateProgress);
163163
this.setState({ progress: 0 });
164164
hideDownload();
165165
});
166166
}
167167

168+
_handleClear(){
169+
const { toasts } = this.getInstance();
170+
171+
toasts.clear();
172+
}
173+
174+
_handleError(err){
175+
const { toasts } = this.getInstance();
176+
177+
toasts.error(err);
178+
}
179+
180+
_handleSuccess(msg){
181+
const { toasts } = this.getInstance();
182+
183+
toasts.success(msg);
184+
}
185+
168186
}
169187

170188
DeviceStore.config = {

src/stores/editor.js

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

33
const alt = require('../alt');
4-
const styles = require('../../plugins/sidebar/styles');
54

65
const { findNext, findPrevious, replace } = require('../actions/find');
7-
const { handleError, handleSuccess } = require('../actions/file');
8-
const { handleInput, highlight, syntaxCheck } = require('../actions/editor');
6+
const { handleInput, syntaxCheck } = require('../actions/editor');
97
const { moveByScrollUpLine, moveByScrollDownLine } = require('../actions/editor-move');
108
const { dedent, indent } = require('../actions/text-move');
119
const { print } = require('../actions/system');
@@ -19,7 +17,6 @@ class EditorStore {
1917
onFindPrevious: findPrevious,
2018
onHandleInput: handleInput,
2119
onIndent: indent,
22-
onHighlight: highlight,
2320
onMoveByScrollUpLine: moveByScrollUpLine,
2421
onMoveByScrollDownLine: moveByScrollDownLine,
2522
onPrint: print,
@@ -88,39 +85,29 @@ class EditorStore {
8885
source: workspace.current.deref()
8986
});
9087
if(result.error){
91-
this.handleError(result.error);
92-
}else{
93-
this.handleSuccess('Tokenization successful!');
88+
this._handleError(result.error);
89+
} else {
90+
this._handleClear();
91+
this._handleSuccess('Tokenization successful!');
9492
}
9593
}
96-
onHighlight(opts) {
97-
const { cm } = this.getInstance();
98-
const doc = cm.getDoc();
9994

100-
const anchor = doc.posFromIndex(opts.position);
101-
const head = doc.posFromIndex(opts.position + opts.length);
95+
_handleClear(){
96+
const { toasts } = this.getInstance();
10297

103-
doc.setSelection(anchor, head);
98+
toasts.clear();
10499
}
105100

101+
_handleError(err){
102+
const { toasts } = this.getInstance();
106103

107-
//duplicated from file store due to dispatch->dispatch invariant
108-
handleError(err){
109-
// leaving this in for better debugging of errors
110-
console.log(err);
111-
const { toast } = this.getInstance();
112-
113-
toast.show(err.message, { style: styles.errorToast });
114-
if(err && err.errorLength){
115-
this.onHighlight({ position: err.errorPosition, length: err.errorLength });
116-
}
104+
toasts.error(err);
117105
}
118106

119-
//duplicated from file store due to dispatch->dispatch invariant
120-
handleSuccess(msg){
121-
const { toast } = this.getInstance();
107+
_handleSuccess(msg){
108+
const { toasts } = this.getInstance();
122109

123-
toast.show(msg, { style: styles.successToast, timeout: 5000 });
110+
toasts.success(msg);
124111
}
125112
}
126113

0 commit comments

Comments
 (0)