tracerManager.jsx 5.9 KB
Newer Older
J
Jason Park 已提交
1
import React from 'react';
J
Jason Park 已提交
2
import { Randomize, Seed } from '/core';
J
Jason Park 已提交
3 4 5
import * as Tracers from '/core/tracers';
import { Tracer } from '/core/tracers';
import { Array1DData, Array2DData, ChartData, Data, GraphData, LogData } from '/core/datas';
6
import { Array1DRenderer, Array2DRenderer, ChartRenderer, GraphRenderer, LogRenderer, Renderer } from '/core/renderers';
J
Jason Park 已提交
7

8 9 10 11 12 13
Object.assign(window, {
  modules: {
    'algorithm-visualizer': {
      ...Tracers,
      Randomize,
    },
14
  },
15
});
J
Jason Park 已提交
16 17 18 19 20 21 22

class TracerManager {
  constructor() {
    this.interval = 500;
    this.paused = false;
    this.started = false;
    this.lineIndicator = null;
J
Jason Park 已提交
23
    this.code = '';
J
Jason Park 已提交
24
    this.reset();
J
Jason Park 已提交
25 26
  }

27 28 29
  setOnChangeRenderers(onChangeRenderers) {
    this.onChangeRenderers = onChangeRenderers;
    if (this.onChangeRenderers) this.onChangeRenderers(this.renderers);
J
Jason Park 已提交
30 31 32 33
  }

  setOnUpdateStatus(onUpdateStatus) {
    this.onUpdateStatus = onUpdateStatus;
J
Jason Park 已提交
34 35 36 37
    if (this.onUpdateStatus) {
      const { interval, paused, started } = this;
      this.onUpdateStatus({ interval, paused, started });
    }
J
Jason Park 已提交
38 39 40 41
  }

  setOnUpdateLineIndicator(onUpdateLineIndicator) {
    this.onUpdateLineIndicator = onUpdateLineIndicator;
J
Jason Park 已提交
42
    if (this.onUpdateLineIndicator) this.onUpdateLineIndicator(this.lineIndicator);
J
Jason Park 已提交
43 44
  }

45 46 47 48
  setOnRun(onRun) {
    this.onRun = onRun;
  }

J
Jason Park 已提交
49 50 51 52 53
  setOnError(onError) {
    this.onError = onError;
  }

  render() {
J
Jason Park 已提交
54
    Object.values(this.datas).forEach(data => data.render());
J
Jason Park 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  }

  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 已提交
77 78 79
  setCode(code) {
    this.code = code;
    this.runInitial();
80 81
  }

J
Jason Park 已提交
82
  reset(seed = new Seed()) {
J
Jason Park 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
    this.traces = seed.traces;
    this.resetCursor();
    this.stopTimer();
    this.setPaused(false);
    this.setStarted(false);
    this.setLineIndicator(null);
  }

  resetCursor() {
    this.renderers = [];
    this.datas = {};
    this.cursor = 0;
    this.chunkCursor = 0;
96
    if (this.onChangeRenderers) this.onChangeRenderers(this.renderers);
J
Jason Park 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
  }

  addTracer(className, tracerKey, title, options) {
    const [DataClass, RendererClass] = {
      Tracer: [Data, Renderer],
      LogTracer: [LogData, LogRenderer],
      Array2DTracer: [Array2DData, Array2DRenderer],
      Array1DTracer: [Array1DData, Array1DRenderer],
      ChartTracer: [ChartData, ChartRenderer],
      GraphTracer: [GraphData, GraphRenderer],
    }[className];
    const data = new DataClass(options);
    this.datas[tracerKey] = data;
110 111 112
    const renderer = (
      <RendererClass key={tracerKey} title={title} data={data} wsProps={{ fixed: true }} />
    );
J
Jason Park 已提交
113
    this.renderers.push(renderer);
114
    if (this.onChangeRenderers) this.onChangeRenderers(this.renderers);
J
Jason Park 已提交
115 116 117 118 119 120 121 122 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 155 156 157 158 159 160 161 162 163 164 165 166 167
  }

  applyTrace() {
    if (this.cursor >= this.traces.length) return false;
    const trace = this.traces[this.cursor++];
    const { tracerKey, method, args } = trace;
    try {
      if (method === 'construct') {
        const [className, title, options] = args;
        this.addTracer(className, tracerKey, title, options);
        return true;
      } else {
        const data = this.datas[tracerKey];
        const wait = method === 'wait';
        const newArgs = [...args];
        if (wait) {
          const lineNumber = newArgs.shift();
          this.setLineIndicator({ lineNumber, cursor: this.cursor });
        }
        data[method](...newArgs);
        return !wait;
      }
    } 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;
    }
  }

J
Jason Park 已提交
168
  sandboxEval(code) {
169 170 171 172
    const require = moduleName => window.modules[moduleName]; // fake require
    eval(code);
  }

J
Jason Park 已提交
173
  execute(callback) {
J
Jason Park 已提交
174
    try {
J
Jason Park 已提交
175
      const lines = this.code.split('\n').map((line, i) => line.replace(/(.+\. *wait *)(\( *\))/g, `$1(${i})`));
J
Jason Park 已提交
176 177
      const seed = new Seed();
      Tracer.seed = seed;
178 179
      const { code } = Babel.transform(lines.join('\n'), { presets: ['es2015'] });
      this.sandboxEval(code);
J
Jason Park 已提交
180 181 182 183 184 185 186
      this.reset(seed);
      if (callback) callback();
    } catch (error) {
      return error;
    }
  }

J
Jason Park 已提交
187 188 189 190 191 192
  runInitial() {
    const error = this.execute(() => this.applyTraceChunk());
    if (error) {
      this.reset();
      this.render();
    }
J
Jason Park 已提交
193 194
  }

195
  run() {
J
Jason Park 已提交
196
    const error = this.execute(() => this.resume());
J
Jason Park 已提交
197
    if (error) {
J
Jason Park 已提交
198 199
      this.reset();
      this.render();
J
Jason Park 已提交
200 201 202 203
      this.handleError(error);
    } else {
      this.setStarted(true);
    }
204
    if (this.onRun) this.onRun();
J
Jason Park 已提交
205 206 207 208
  }

  prev() {
    this.pause();
209
    const lastChunk = this.chunkCursor - 1;
J
Jason Park 已提交
210
    this.resetCursor();
211
    do {
J
Jason Park 已提交
212
      this.applyTraceChunk(false);
213
    } while (this.chunkCursor < lastChunk);
J
Jason Park 已提交
214 215 216 217 218 219
    this.render();
  }

  resume() {
    this.startTimer();
    this.setPaused(false);
220
    if (this.onRun) this.onRun();
J
Jason Park 已提交
221 222 223 224 225
  }

  pause() {
    this.stopTimer();
    this.setPaused(true);
226
    if (this.onRun) this.onRun();
J
Jason Park 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  }

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

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

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