tracerManager.jsx 5.5 KB
Newer Older
J
Jason Park 已提交
1
import React from 'react';
2
import Promise from 'bluebird';
3
import { extension } from '/common/util';
J
Jason Park 已提交
4
import { Array1DData, Array2DData, ChartData, Data, GraphData, LogData } from '/core/datas';
5
import { Array1DRenderer, Array2DRenderer, ChartRenderer, GraphRenderer, LogRenderer, Renderer } from '/core/renderers';
J
Jason Park 已提交
6
import { TracerApi } from '/apis';
J
Jason Park 已提交
7 8 9 10 11 12 13

class TracerManager {
  constructor() {
    this.interval = 500;
    this.paused = false;
    this.started = false;
    this.lineIndicator = null;
14
    this.file = { name: '', content: '', contributors: [] };
J
Jason Park 已提交
15
    this.reset();
J
Jason Park 已提交
16 17
  }

18 19 20
  setOnChangeRenderers(onChangeRenderers) {
    this.onChangeRenderers = onChangeRenderers;
    if (this.onChangeRenderers) this.onChangeRenderers(this.renderers);
J
Jason Park 已提交
21 22 23 24
  }

  setOnUpdateStatus(onUpdateStatus) {
    this.onUpdateStatus = onUpdateStatus;
J
Jason Park 已提交
25 26 27 28
    if (this.onUpdateStatus) {
      const { interval, paused, started } = this;
      this.onUpdateStatus({ interval, paused, started });
    }
J
Jason Park 已提交
29 30 31 32
  }

  setOnUpdateLineIndicator(onUpdateLineIndicator) {
    this.onUpdateLineIndicator = onUpdateLineIndicator;
J
Jason Park 已提交
33
    if (this.onUpdateLineIndicator) this.onUpdateLineIndicator(this.lineIndicator);
J
Jason Park 已提交
34 35 36 37 38 39 40
  }

  setOnError(onError) {
    this.onError = onError;
  }

  render() {
J
Jason Park 已提交
41
    Object.values(this.datas).forEach(data => data.render());
J
Jason Park 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  }

  setInterval(interval) {
    this.interval = interval;
    if (this.onUpdateStatus) this.onUpdateStatus({ interval });
  }

  setPaused(paused) {
    this.paused = paused;
    if (this.onUpdateStatus) this.onUpdateStatus({ paused });
  }

  setStarted(started) {
    this.started = started;
    if (this.onUpdateStatus) this.onUpdateStatus({ started });
  }

  setLineIndicator(lineIndicator) {
    this.lineIndicator = lineIndicator;
    if (this.onUpdateLineIndicator) this.onUpdateLineIndicator(lineIndicator);
  }

J
Jason Park 已提交
64
  setFile(file, initialRun) {
65
    this.file = file;
J
Jason Park 已提交
66
    if (initialRun) this.runInitial();
67 68
  }

J
Jason Park 已提交
69 70
  reset(traces = []) {
    this.traces = traces;
J
Jason Park 已提交
71 72 73 74 75 76 77 78 79 80 81 82
    this.resetCursor();
    this.stopTimer();
    this.setPaused(false);
    this.setStarted(false);
    this.setLineIndicator(null);
  }

  resetCursor() {
    this.renderers = [];
    this.datas = {};
    this.cursor = 0;
    this.chunkCursor = 0;
83
    if (this.onChangeRenderers) this.onChangeRenderers(this.renderers);
J
Jason Park 已提交
84 85
  }

86
  addTracer(className, tracerKey, title) {
J
Jason Park 已提交
87 88 89 90 91 92 93 94
    const [DataClass, RendererClass] = {
      Tracer: [Data, Renderer],
      LogTracer: [LogData, LogRenderer],
      Array2DTracer: [Array2DData, Array2DRenderer],
      Array1DTracer: [Array1DData, Array1DRenderer],
      ChartTracer: [ChartData, ChartRenderer],
      GraphTracer: [GraphData, GraphRenderer],
    }[className];
95
    const data = new DataClass();
J
Jason Park 已提交
96
    this.datas[tracerKey] = data;
97 98 99
    const renderer = (
      <RendererClass key={tracerKey} title={title} data={data} wsProps={{ fixed: true }} />
    );
J
Jason Park 已提交
100
    this.renderers.push(renderer);
101
    if (this.onChangeRenderers) this.onChangeRenderers(this.renderers);
J
Jason Park 已提交
102 103 104 105 106 107 108 109
  }

  applyTrace() {
    if (this.cursor >= this.traces.length) return false;
    const trace = this.traces[this.cursor++];
    const { tracerKey, method, args } = trace;
    try {
      if (method === 'construct') {
110 111
        const [className, title] = args;
        this.addTracer(className, tracerKey, title);
J
Jason Park 已提交
112 113 114
        return true;
      } else {
        const data = this.datas[tracerKey];
115
        const delay = method === 'delay';
J
Jason Park 已提交
116
        const newArgs = [...args];
117
        if (delay) {
J
Jason Park 已提交
118 119 120 121
          const lineNumber = newArgs.shift();
          this.setLineIndicator({ lineNumber, cursor: this.cursor });
        }
        data[method](...newArgs);
122
        return !delay;
J
Jason Park 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      }
    } catch (error) {
      if (this.started) this.handleError(error);
      return false;
    }
  }

  applyTraceChunk(render = true) {
    if (this.cursor >= this.traces.length) return false;
    while (this.applyTrace()) ;
    this.chunkCursor++;
    if (render) this.render();
    return true;
  }

  startTimer() {
    this.stopTimer();
    if (this.applyTraceChunk()) {
      this.timer = window.setTimeout(() => this.startTimer(), this.interval);
    } else {
      this.setPaused(false);
      this.setStarted(false);
    }
  }

  stopTimer() {
    if (this.timer) {
      window.clearInterval(this.timer);
      this.timer = null;
    }
  }

155 156 157
  execute() {
    const { name, content } = this.file;
    const ext = extension(name);
J
Jason Park 已提交
158
    if (ext in TracerApi) {
159
      return TracerApi[ext]({ code: content });
160 161 162
    } else {
      return Promise.reject(new Error('Language Not Supported'));
    }
J
Jason Park 已提交
163 164
  }

J
Jason Park 已提交
165
  runInitial() {
J
Jason Park 已提交
166 167
    this.reset();
    this.render();
J
Jason Park 已提交
168
    this.execute()
J
Jason Park 已提交
169 170 171
      .then(traces => {
        this.reset(traces);
        this.applyTraceChunk();
J
Jason Park 已提交
172 173
      })
      .catch(error => {
J
Jason Park 已提交
174
      });
J
Jason Park 已提交
175 176
  }

177
  run() {
J
Jason Park 已提交
178 179
    this.reset();
    this.render();
J
Jason Park 已提交
180
    this.execute()
J
Jason Park 已提交
181 182
      .then(traces => {
        this.reset(traces);
J
Jason Park 已提交
183 184 185 186 187 188
        this.resume();
        this.setStarted(true);
      })
      .catch(error => {
        this.handleError(error);
      });
J
Jason Park 已提交
189 190 191 192
  }

  prev() {
    this.pause();
193
    const lastChunk = this.chunkCursor - 1;
J
Jason Park 已提交
194
    this.resetCursor();
195
    do {
J
Jason Park 已提交
196
      this.applyTraceChunk(false);
197
    } while (this.chunkCursor < lastChunk);
J
Jason Park 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    this.render();
  }

  resume() {
    this.startTimer();
    this.setPaused(false);
  }

  pause() {
    this.stopTimer();
    this.setPaused(true);
  }

  next() {
    this.pause();
    this.applyTraceChunk();
  }

  handleError(error) {
    console.error(error);
    if (this.onError) this.onError(error);
  }
}

const tracerManager = new TracerManager();
export default tracerManager;