@live-codes/browser-compilers 0.4.10 → 0.4.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ // This is a minimal interface to the gnuplot worker. More sophisticated include virtual file-system lists and a reset function.
2
+
3
+ var Gnuplot = function (js_filename) {
4
+ this.worker = new Worker(js_filename);
5
+ this.output = [];
6
+ this.error = [];
7
+ this.isRunning = false;
8
+
9
+ // These two should be overwritten by application.
10
+ this.onOutput = function (text) {
11
+ console.log('Gnuplot output: ' + text);
12
+ };
13
+ this.onError = function (text) {
14
+ console.log('Gnuplot error: ' + text);
15
+ };
16
+
17
+ this.transaction = 1;
18
+ this.callbacks = [];
19
+ this.postCommand = function (cmd_block, callback) {
20
+ var id = this.transaction; // fresh id
21
+ cmd_block.transaction = id; // give data object a tag
22
+ this.callbacks[id] = callback;
23
+ this.worker.postMessage(cmd_block);
24
+ this.transaction++;
25
+ };
26
+
27
+ var that = this;
28
+ this.worker.addEventListener(
29
+ 'message',
30
+ function (e) {
31
+ // console.log('gnuplot: ', e.data); //enable for debug
32
+ var data = e.data;
33
+ if (data.transaction < 0) {
34
+ if (data.transaction == -1) {
35
+ that.output.push(data.content);
36
+ that.onOutput(data.content);
37
+ }
38
+ if (data.transaction == -2) {
39
+ that.error.push(data.content);
40
+ that.onError(data.content);
41
+ }
42
+ return;
43
+ }
44
+ if (data.content == 'FINISH') that.isRunning = false;
45
+ if (data.transaction && that.callbacks[data.transaction]) {
46
+ that.callbacks[data.transaction](data);
47
+ delete that.callbacks[data.transaction];
48
+ }
49
+ },
50
+ false,
51
+ );
52
+
53
+ this.worker.postMessage({}); // supposed to do this to start the worker?
54
+ };
55
+
56
+ Gnuplot.prototype.putFile = function (name_, contents, callback) {
57
+ var data = {
58
+ name: name_,
59
+ content: contents,
60
+ cmd: 'putFile',
61
+ };
62
+ this.postCommand(data, callback);
63
+ };
64
+
65
+ // to read output
66
+ Gnuplot.prototype.getFile = function (name_, callback) {
67
+ var data = {
68
+ name: name_,
69
+ cmd: 'getFile',
70
+ };
71
+ this.postCommand(data, callback);
72
+ };
73
+
74
+ Gnuplot.prototype.getFileList = function (callback) {
75
+ var data = {
76
+ cmd: 'getFileList',
77
+ };
78
+ this.postCommand(data, callback);
79
+ };
80
+
81
+ Gnuplot.prototype.removeFiles = function (files, callback) {
82
+ var data = {
83
+ name: files,
84
+ cmd: 'removeFiles',
85
+ };
86
+ this.postCommand(data, callback);
87
+ };
88
+
89
+ Gnuplot.prototype.run = function (script, onFinish) {
90
+ if (this.isRunning) return false;
91
+ this.putFile('foo', script);
92
+ var data = {
93
+ content: ['foo'],
94
+ cmd: 'run',
95
+ };
96
+ this.isRunning = true;
97
+ this.postCommand(data, onFinish);
98
+ return true;
99
+ };