@basthon/kernel-base 0.60.2 → 0.60.3

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.
package/lib/kernel.d.ts CHANGED
@@ -8,6 +8,7 @@ export declare class KernelBase {
8
8
  private readonly _loaded;
9
9
  protected _execution_count: number;
10
10
  protected _pendingInput: boolean;
11
+ private _listeners;
11
12
  constructor(options: any);
12
13
  /**
13
14
  * Kernel version number (string).
@@ -88,6 +89,11 @@ export declare class KernelBase {
88
89
  * manage the event lookup to retreive relevent data.
89
90
  */
90
91
  addEventListener(eventName: string, callback: (_: any) => void): void;
92
+ /**
93
+ * Wrapper around document.removeEventListener.
94
+ * It manages the 'basthon.' prefix to each event name.
95
+ */
96
+ removeEventListener(eventName: string, callback: (_: any) => void): void;
91
97
  /**
92
98
  * Send eval.input event then wait for the user response and return it.
93
99
  */
package/lib/kernel.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { PromiseDelegate } from "promise-delegate";
2
- import { VERSION } from './version';
2
+ import { VERSION } from "./version";
3
3
  /**
4
4
  * An error thrown by not implemented API functions.
5
5
  */
@@ -28,77 +28,88 @@ export class KernelBase {
28
28
  this._loaded = new PromiseDelegate();
29
29
  this._execution_count = 0;
30
30
  this._pendingInput = false;
31
+ // a map to register wrapped listeners to allow later remove
32
+ this._listeners = new Map();
31
33
  // root path where kernel is installed
32
34
  this._rootPath = options.rootPath;
33
35
  }
34
36
  /**
35
37
  * Kernel version number (string).
36
38
  */
37
- version() { return VERSION; }
38
- ;
39
+ version() {
40
+ return VERSION;
41
+ }
39
42
  /**
40
43
  * Language implemented in the kernel (string).
41
44
  * Generally lower case.
42
45
  */
43
- language() { throw new NotImplementedError("language"); }
44
- ;
46
+ language() {
47
+ throw new NotImplementedError("language");
48
+ }
45
49
  /**
46
50
  * Language name implemented in the kernel (string).
47
51
  * As it should be displayed in text.
48
52
  */
49
- languageName() { throw new NotImplementedError("languageName"); }
50
- ;
53
+ languageName() {
54
+ throw new NotImplementedError("languageName");
55
+ }
51
56
  /**
52
57
  * Script (module) file extensions
53
58
  */
54
59
  moduleExts() {
55
60
  throw new NotImplementedError("moduleExts");
56
61
  }
57
- ;
58
62
  /**
59
63
  * Launch the kernel.
60
64
  */
61
- async launch() { throw new NotImplementedError("launch"); }
62
- ;
65
+ async launch() {
66
+ throw new NotImplementedError("launch");
67
+ }
63
68
  /**
64
69
  * Execution count getter.
65
70
  */
66
- get execution_count() { return this._execution_count; }
71
+ get execution_count() {
72
+ return this._execution_count;
73
+ }
67
74
  /**
68
75
  * Async code evaluation that resolves with the result.
69
76
  */
70
77
  evalAsync(code, outCallback, errCallback, data = null) {
71
78
  throw new NotImplementedError("evalAsync");
72
79
  }
73
- ;
74
- restart() { throw new NotImplementedError("restart"); }
75
- ;
80
+ restart() {
81
+ throw new NotImplementedError("restart");
82
+ }
76
83
  async putFile(filename, content) {
77
84
  throw new NotImplementedError("putFile");
78
85
  }
79
- ;
80
86
  async putModule(filename, content) {
81
87
  throw new NotImplementedError("putModule");
82
88
  }
83
- ;
84
- userModules() { return []; }
85
- ;
86
- getFile(path) { throw new NotImplementedError("getFile"); }
87
- ;
89
+ userModules() {
90
+ return [];
91
+ }
92
+ getFile(path) {
93
+ throw new NotImplementedError("getFile");
94
+ }
88
95
  getUserModuleFile(filename) {
89
96
  throw new NotImplementedError("getUserModuleFile");
90
97
  }
91
- ;
92
- more(source) { throw new NotImplementedError("more"); }
93
- ;
94
- complete(code) { return []; }
95
- ;
96
- banner() { return `Welcome to the ${this.languageName()} REPL!`; }
97
- ;
98
- ps1() { return ">>> "; }
99
- ;
100
- ps2() { return "... "; }
101
- ;
98
+ more(source) {
99
+ throw new NotImplementedError("more");
100
+ }
101
+ complete(code) {
102
+ return [];
103
+ }
104
+ banner() {
105
+ return `Welcome to the ${this.languageName()} REPL!`;
106
+ }
107
+ ps1() {
108
+ return ">>> ";
109
+ }
110
+ ps2() {
111
+ return "... ";
112
+ }
102
113
  /**
103
114
  * Initialize the kernel.
104
115
  */
@@ -118,11 +129,15 @@ export class KernelBase {
118
129
  /**
119
130
  * Is the kernel ready?
120
131
  */
121
- get ready() { return this._ready; }
132
+ get ready() {
133
+ return this._ready;
134
+ }
122
135
  /**
123
136
  * Promise that resolve when the kernel is loaded.
124
137
  */
125
- async loaded() { await this._loaded.promise; }
138
+ async loaded() {
139
+ await this._loaded.promise;
140
+ }
126
141
  /**
127
142
  * Root for kernel files. This is always the language directory
128
143
  * inside the version number directory inside the kernel directory.
@@ -131,7 +146,7 @@ export class KernelBase {
131
146
  let url = this._rootPath + "/" + this.version() + "/" + this.language();
132
147
  if (absolute && !url.startsWith("http")) {
133
148
  const base = window.location.origin + window.location.pathname;
134
- url = base.substring(0, base.lastIndexOf('/')) + "/" + url;
149
+ url = base.substring(0, base.lastIndexOf("/")) + "/" + url;
135
150
  }
136
151
  return url;
137
152
  }
@@ -140,7 +155,7 @@ export class KernelBase {
140
155
  * (opening browser dialog).
141
156
  */
142
157
  download(data, filename) {
143
- if (!(typeof data === 'string' || data instanceof String)) {
158
+ if (!(typeof data === "string" || data instanceof String)) {
144
159
  const blob = new Blob([data], { type: "application/octet-stream" });
145
160
  data = window.URL.createObjectURL(blob);
146
161
  }
@@ -153,20 +168,18 @@ export class KernelBase {
153
168
  anchor.click();
154
169
  document.body.removeChild(anchor);
155
170
  }
156
- ;
157
171
  /**
158
172
  * Dynamically load a script asynchronously.
159
173
  */
160
174
  static loadScript(url) {
161
175
  return new Promise(function (resolve, reject) {
162
- let script = document.createElement('script');
176
+ let script = document.createElement("script");
163
177
  script.onload = resolve;
164
178
  script.onerror = reject;
165
179
  script.src = url;
166
180
  document.head.appendChild(script);
167
181
  });
168
182
  }
169
- ;
170
183
  /**
171
184
  * Wrapper around document.dispatchEvent.
172
185
  * It adds the 'basthon.' prefix to each event name and
@@ -175,16 +188,32 @@ export class KernelBase {
175
188
  dispatchEvent(eventName, data) {
176
189
  document.dispatchEvent(new BasthonEvent(`basthon.${eventName}`, data));
177
190
  }
178
- ;
179
191
  /**
180
192
  * Wrapper around document.addEventListener.
181
193
  * It manages the 'basthon.' prefix to each event name and
182
194
  * manage the event lookup to retreive relevent data.
183
195
  */
184
196
  addEventListener(eventName, callback) {
185
- document.addEventListener(`basthon.${eventName}`, function (event) { callback(event.detail); });
197
+ // wrapped callback
198
+ const _callback = (event) => callback(event.detail);
199
+ document.addEventListener(`basthon.${eventName}`, _callback);
200
+ // register the wrapped callback in order to remove it
201
+ let listeners = this._listeners.get(eventName);
202
+ if (listeners == null) {
203
+ listeners = new Map();
204
+ this._listeners.set(eventName, listeners);
205
+ }
206
+ listeners.set(callback, _callback);
207
+ }
208
+ /**
209
+ * Wrapper around document.removeEventListener.
210
+ * It manages the 'basthon.' prefix to each event name.
211
+ */
212
+ removeEventListener(eventName, callback) {
213
+ // get the wrapped callback
214
+ const listeners = this._listeners.get(eventName);
215
+ document.removeEventListener(`basthon.${eventName}`, listeners === null || listeners === void 0 ? void 0 : listeners.get(callback));
186
216
  }
187
- ;
188
217
  /**
189
218
  * Send eval.input event then wait for the user response and return it.
190
219
  */
@@ -208,14 +237,13 @@ export class KernelBase {
208
237
  // simple trick that is enough for our purpose.
209
238
  return JSON.parse(JSON.stringify(obj));
210
239
  }
211
- ;
212
240
  /**
213
241
  * Put a ressource (file or module).
214
242
  * Detection is based on extension.
215
243
  */
216
244
  async putRessource(filename, content) {
217
245
  var _a;
218
- const ext = (_a = filename.split('.').pop()) !== null && _a !== void 0 ? _a : "";
246
+ const ext = (_a = filename.split(".").pop()) !== null && _a !== void 0 ? _a : "";
219
247
  if (this.moduleExts().includes(ext)) {
220
248
  return await this.putModule(filename, content);
221
249
  }
@@ -223,7 +251,6 @@ export class KernelBase {
223
251
  return await this.putFile(filename, content);
224
252
  }
225
253
  }
226
- ;
227
254
  /**
228
255
  * Is an input pending?
229
256
  */
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.60.2";
1
+ export declare const VERSION = "0.60.3";
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.60.2";
1
+ export const VERSION = "0.60.3";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basthon/kernel-base",
3
- "version": "0.60.2",
3
+ "version": "0.60.3",
4
4
  "description": "Basthon - Base Kernel",
5
5
  "homepage": "https://basthon.fr",
6
6
  "bugs": {
@@ -36,5 +36,5 @@
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "75f68d07a74fffc6b347ab8c42fa043d9f321678"
39
+ "gitHead": "f1521bed7826b7ec3c1d3a746c5e2de6adf1368d"
40
40
  }