提交 ec55a7cf 编写于 作者: J Jason Park

Get ready for supporting languages other than js

上级 6fc67198
Subproject commit 89b000a6887da0db91c6e484d5ea98b0178fb760
Subproject commit 0238af9ecb07af6a1079058be9cc54c738047754
......@@ -73,8 +73,20 @@ const GitHubApi = {
deleteGist: DELETE('https://api.github.com/gists/:id'),
};
let jsWorker = null;
const CompilerApi = {
compileJs: code => new Promise((resolve, reject) => {
if (jsWorker) jsWorker.terminate();
jsWorker = new Worker('/api/compiler/js');
jsWorker.onmessage = e => resolve(e.data);
jsWorker.onerror = reject;
jsWorker.postMessage(code);
}),
};
export {
CategoryApi,
WikiApi,
GitHubApi,
CompilerApi,
};
\ No newline at end of file
......@@ -27,14 +27,16 @@ class CodeEditor extends React.Component {
componentDidMount() {
const { file } = this.props;
tracerManager.setCode(file.content);
tracerManager.setFile(file);
tracerManager.setOnUpdateLineIndicator(lineIndicator => this.setState({ lineMarker: this.createLineMarker(lineIndicator) }));
}
componentWillReceiveProps(nextProps) {
const { file } = nextProps;
tracerManager.setCode(file.content);
if (file !== this.props.file) {
tracerManager.setFile(file);
}
}
componentWillUnmount() {
......@@ -59,7 +61,6 @@ class CodeEditor extends React.Component {
handleChangeCode(code) {
const { file } = this.props;
this.props.modifyFile({ ...file, content: code });
tracerManager.setCode(code);
}
render() {
......
import React from 'react';
import Promise from 'bluebird';
import { extension } from '/common/util';
import { Array1DData, Array2DData, ChartData, Data, GraphData, LogData } from '/core/datas';
import { Array1DRenderer, Array2DRenderer, ChartRenderer, GraphRenderer, LogRenderer, Renderer } from '/core/renderers';
import { CompilerApi } from '../apis';
class TracerManager {
constructor() {
......@@ -9,7 +10,7 @@ class TracerManager {
this.paused = false;
this.started = false;
this.lineIndicator = null;
this.code = '';
this.file = { name: '', content: '', contributors: [] };
this.jsWorker = null;
this.reset();
}
......@@ -60,8 +61,8 @@ class TracerManager {
if (this.onUpdateLineIndicator) this.onUpdateLineIndicator(lineIndicator);
}
setCode(code) {
this.code = code;
setFile(file) {
this.file = file;
this.runInitial();
}
......@@ -151,20 +152,10 @@ class TracerManager {
}
}
execute() { // TODO: consider running on a web worker
return new Promise((resolve, reject) => {
if (this.jsWorker) {
this.jsWorker.terminate();
this.jsWorker = null;
}
this.jsWorker = new Worker('/api/compiler/js');
this.jsWorker.onmessage = e => {
this.reset(e.data);
resolve();
};
this.jsWorker.onerror = reject;
this.jsWorker.postMessage(this.code);
});
execute() {
const { name, content } = this.file;
const ext = extension(name);
return CompilerApi.compileJs(content).then(traces => this.reset(traces));
}
runInitial() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册