-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathPreview.js
More file actions
85 lines (72 loc) · 1.99 KB
/
Preview.js
File metadata and controls
85 lines (72 loc) · 1.99 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
import {connect} from 'react-redux';
import uuid from 'uuid/v4';
import throttle from 'lodash-es/throttle';
import Preview from '../components/Preview';
import {
addRuntimeError,
consoleErrorProduced,
consoleLogBatchProduced,
consoleValueProduced,
popOutProject,
refreshPreview,
} from '../actions';
import {
getCompiledProjects,
getConsoleHistory,
getFocusedSelector,
isCurrentProjectSyntacticallyValid,
isUserTyping,
} from '../selectors';
function mapStateToProps(state) {
return {
compiledProjects: getCompiledProjects(state),
consoleEntries: getConsoleHistory(state),
focusedSelector: getFocusedSelector(state),
showingErrors: (
!isUserTyping(state) &&
!isCurrentProjectSyntacticallyValid(state)
),
};
}
function generateConsoleLogDispatcher(dispatch, timeout) {
let queue = [];
function flushQueue() {
dispatch(consoleLogBatchProduced(queue));
queue = [];
}
const throttledFlushQueue = throttle(flushQueue, timeout, {
leading: true,
trailing: true,
});
return function addLogEntry(value, compiledProjectKey) {
queue.push({value, compiledProjectKey, key: uuid().toString()});
throttledFlushQueue();
};
}
function mapDispatchToProps(dispatch) {
const dispatchConsoleLog = generateConsoleLogDispatcher(dispatch, 1000);
return {
onConsoleError(key, name, message, compiledProjectKey) {
dispatch(consoleErrorProduced(key, name, message, compiledProjectKey));
},
onConsoleValue(key, value, compiledProjectKey) {
dispatch(consoleValueProduced(key, value, compiledProjectKey));
},
onConsoleLog(value, compiledProjectKey) {
dispatchConsoleLog(value, compiledProjectKey);
},
onPopOutProject() {
dispatch(popOutProject());
},
onRefreshClick() {
dispatch(refreshPreview(Date.now()));
},
onRuntimeError(error) {
dispatch(addRuntimeError('javascript', error));
},
};
}
export default connect(
mapStateToProps,
mapDispatchToProps,
)(Preview);