|
32 | 32 | root["glvis"] = factory(root["glvis"]);
|
33 | 33 | }
|
34 | 34 | })(this, function (emglvis) {
|
| 35 | + function rand_id() { |
| 36 | + const arr = new Uint8Array(10); |
| 37 | + window.crypto.getRandomValues(arr); |
| 38 | + return arr.reduce( |
| 39 | + (cur, next) => cur + next.toString(36).padStart(2, "0"), |
| 40 | + "" |
| 41 | + ); |
| 42 | + } |
| 43 | + |
35 | 44 | class State {
|
36 | 45 | constructor(div, width = 640, height = 480, canvas = undefined) {
|
37 | 46 | if (div === undefined) {
|
|
72 | 81 | setupCanvas(width, height) {
|
73 | 82 | if (this.canvas_ === undefined) {
|
74 | 83 | this.canvas_ = document.createElement("canvas");
|
75 |
| - this.canvas_.id = "glvis-canvas"; |
| 84 | + this.canvas_.id = `glvis-canvas-${rand_id()}`; |
76 | 85 | }
|
77 | 86 | this.setCanvasSize(width, height);
|
78 | 87 | this.canvas_.innerHTML = "Your browser doesn't support HTML canvas";
|
|
135 | 144 | this.display(data_type, data_str);
|
136 | 145 | }
|
137 | 146 |
|
138 |
| - async updateStream(stream) { |
| 147 | + async update(data_type, data_str) { |
139 | 148 | if (!this.emsetup_) {
|
140 |
| - this.displayStream(stream); |
| 149 | + this.display(data_type, data_str); |
141 | 150 | return;
|
142 | 151 | }
|
143 |
| - const index = stream.indexOf("\n"); |
144 |
| - const data_type = stream.substr(0, index); |
145 |
| - const data_str = stream.substr(index + 1); |
146 | 152 | var g = await this.emglv_;
|
147 | 153 | if (g.updateVisualization(data_type, data_str) != 0) {
|
148 | 154 | console.log("unable to update stream, starting a new one");
|
149 | 155 | this.display(data_type, data_str);
|
150 | 156 | }
|
151 | 157 | }
|
152 | 158 |
|
| 159 | + async updateStream(stream) { |
| 160 | + const index = stream.indexOf("\n"); |
| 161 | + const data_type = stream.substr(0, index); |
| 162 | + const data_str = stream.substr(index + 1); |
| 163 | + await this.update(data_type, data_str); |
| 164 | + } |
| 165 | + |
153 | 166 | sendKey(key) {
|
154 | 167 | var e = new KeyboardEvent("keypress", {
|
155 | 168 | bubbles: true,
|
|
201 | 214 | }
|
202 | 215 |
|
203 | 216 | return {
|
204 |
| - emglvis: glvis, |
| 217 | + emglvis: emglvis, |
205 | 218 | State: State,
|
206 | 219 | info: function () {
|
207 | 220 | console.log("hi from GLVis!");
|
208 | 221 | },
|
| 222 | + rand_id: rand_id, |
209 | 223 | };
|
210 | 224 | });
|
0 commit comments