tracer_manager.js 5.9 KB
Newer Older
J
Jason Park 已提交
1
const stepLimit = 1e6;
2

J
Jason Park 已提交
3
var TracerManager = function () {
J
Jason Park 已提交
4
    this.timer = null;
J
Jason Park 已提交
5 6
    this.pause = false;
    this.capsules = [];
J
Jason Park 已提交
7
    this.interval = 500;
J
Jason Park 已提交
8 9 10 11 12
};

TracerManager.prototype = {
    add: function (tracer) {
        var $container = $('<section class="module_wrapper">');
J
Jason Park 已提交
13
        $('.module_container').append($container);
J
Jason Park 已提交
14 15 16 17
        var capsule = {
            module: tracer.module,
            tracer: tracer,
            allocated: true,
J
Jason Park 已提交
18
            defaultName: null,
J
Jason Park 已提交
19 20 21 22 23 24 25 26
            $container: $container,
            new: true
        };
        this.capsules.push(capsule);
        return capsule;
    },
    allocate: function (newTracer) {
        var selectedCapsule = null;
J
Jason Park 已提交
27
        var count = 0;
J
Jason Park 已提交
28
        $.each(this.capsules, function (i, capsule) {
J
Jason Park 已提交
29 30 31 32 33 34 35 36 37
            if (capsule.module == newTracer.module) {
                count++;
                if (!capsule.allocated) {
                    capsule.tracer = newTracer;
                    capsule.allocated = true;
                    capsule.new = false;
                    selectedCapsule = capsule;
                    return false;
                }
J
Jason Park 已提交
38 39 40
            }
        });
        if (selectedCapsule == null) {
J
Jason Park 已提交
41
            count++;
J
Jason Park 已提交
42 43
            selectedCapsule = this.add(newTracer);
        }
J
Jason Park 已提交
44
        selectedCapsule.defaultName = newTracer.constructor.name + ' ' + count;
J
Jason Park 已提交
45 46 47
        return selectedCapsule;
    },
    deallocateAll: function () {
J
Jason Park 已提交
48
        this.reset();
J
Jason Park 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        $.each(this.capsules, function (i, capsule) {
            capsule.allocated = false;
        });
    },
    removeUnallocated: function () {
        var changed = false;
        this.capsules = $.grep(this.capsules, function (capsule) {
            var removed = !capsule.allocated;
            if (capsule.new || removed) changed = true;
            if (removed) {
                capsule.$container.remove();
            }
            return !removed;
        });
        if (changed) this.place();
    },
    place: function () {
        var capsules = this.capsules;
        $.each(capsules, function (i, capsule) {
J
Jason Park 已提交
68 69
            var width = 100;
            var height = (100 / capsules.length);
J
Jason Park 已提交
70
            var top = height * i;
J
Jason Park 已提交
71
            capsule.$container.css({
J
Jason Park 已提交
72 73 74
                top: top + '%',
                width: width + '%',
                height: height + '%'
J
Jason Park 已提交
75 76 77 78
            });
            capsule.tracer.resize();
        });
    },
J
Jason Park 已提交
79 80
    resize: function () {
        this.command('resize');
J
Jason Park 已提交
81 82 83
    },
    isPause: function () {
        return this.pause;
84 85
    },
    setInterval: function (interval) {
J
Jason Park 已提交
86
        $('#interval').val(interval);
87 88 89
    },
    reset: function () {
        this.traces = [];
J
Jason Park 已提交
90
        this.traceIndex = -1;
91
        this.stepCnt = 0;
J
Jason Park 已提交
92
        if (this.timer) clearTimeout(this.timer);
93 94 95 96 97 98 99 100 101 102 103 104 105
        this.command('clear');
    },
    pushStep: function (capsule, step) {
        if (this.stepCnt++ > stepLimit) throw "Tracer's stack overflow";
        var len = this.traces.length;
        var last = [];
        if (len == 0) {
            this.traces.push(last);
        } else {
            last = this.traces[len - 1];
        }
        last.push($.extend(step, {capsule: capsule}));
    },
J
Jason Park 已提交
106
    newStep: function () {
107 108 109 110 111
        this.traces.push([]);
    },
    pauseStep: function () {
        if (this.traceIndex < 0) return;
        this.pause = true;
J
Jason Park 已提交
112
        if (this.timer) clearTimeout(this.timer);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        $('#btn_pause').addClass('active');
    },
    resumeStep: function () {
        this.pause = false;
        this.step(this.traceIndex + 1);
        $('#btn_pause').removeClass('active');
    },
    step: function (i, options) {
        var tracer = this;

        if (isNaN(i) || i >= this.traces.length || i < 0) return;
        options = options || {};

        this.traceIndex = i;
        var trace = this.traces[i];
        trace.forEach(function (step) {
J
Jason Park 已提交
129
            step.capsule.tracer.processStep(step, options);
130 131 132 133 134
        });
        if (!options.virtual) {
            this.command('refresh');
        }
        if (this.pause) return;
J
Jason Park 已提交
135
        this.timer = setTimeout(function () {
136
            tracer.step(i + 1, options);
J
Jason Park 已提交
137
        }, this.interval);
138 139 140 141 142 143
    },
    prevStep: function () {
        this.command('clear');
        var finalIndex = this.traceIndex - 1;
        if (finalIndex < 0) {
            this.traceIndex = -1;
J
Jason Park 已提交
144
            this.command('refresh');
145 146 147 148 149 150 151 152 153 154 155 156 157
            return;
        }
        for (var i = 0; i < finalIndex; i++) {
            this.step(i, {virtual: true});
        }
        this.step(finalIndex);
    },
    nextStep: function () {
        this.step(this.traceIndex + 1);
    },
    visualize: function () {
        this.traceIndex = -1;
        this.resumeStep();
J
Jason Park 已提交
158 159 160 161 162 163 164 165 166 167
    },
    command: function () {
        var args = Array.prototype.slice.call(arguments);

        var functionName = args.shift();
        $.each(this.capsules, function (i, capsule) {
            if (capsule.allocated) {
                capsule.tracer.module.prototype[functionName].apply(capsule.tracer, args);
            }
        });
168 169 170 171 172 173 174 175 176 177
    },
    findOwner: function (container) {
        var selectedCapsule = null;
        $.each(this.capsules, function (i, capsule) {
            if (capsule.$container[0] == container) {
                selectedCapsule = capsule;
                return false;
            }
        });
        return selectedCapsule.tracer;
J
Jason Park 已提交
178
    }
J
Jason Park 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191
};

var TracerUtil = {
    toJSON: function (obj) {
        return JSON.stringify(obj, function (key, value) {
            return value === Infinity ? "Infinity" : value;
        });
    },
    fromJSON: function (obj) {
        return JSON.parse(obj, function (key, value) {
            return value === "Infinity" ? Infinity : value;
        });
    },
N
Nemanja Stojanovic 已提交
192 193 194 195
    refineByType: function (item) {
      return $.isNumeric(item) ? this.refineNumber(item) : this.refineString(item);  
    },
    refineString: function (string) {
N
Nemanja Stojanovic 已提交
196
        return string === '' ? ' ' : string;
N
Nemanja Stojanovic 已提交
197
    },
J
Jason Park 已提交
198 199 200
    refineNumber: function (number) {
        return number === Infinity ? '' : number;
    }
J
Jason Park 已提交
201
};