@naanlang/naan 1.4.1 → 1.4.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.
@@ -34,7 +34,7 @@ exports.NaanControllerWeb = function() {
34
34
  "use strict";
35
35
  /*jshint -W024 */
36
36
  var undefined; // JavaScript should make this a keyword
37
-
37
+
38
38
  //
39
39
  // Persistent
40
40
  //
@@ -43,17 +43,11 @@ exports.NaanControllerWeb = function() {
43
43
  kStateFirstVersion = 200, // increment when losing backwards compatbility
44
44
  kStateCurrentVersion = 200; // increment when adding features
45
45
 
46
-
46
+
47
47
  //
48
48
  // Environment
49
49
  //
50
-
51
- var pageLoading = true; // assume we are loading the page
52
50
 
53
- window.addEventListener("load", function (e) {
54
- pageLoading = false;
55
- });
56
-
57
51
  var querystrings = (function(a) {
58
52
  if (a === "") return {};
59
53
  var b = {};
@@ -67,12 +61,12 @@ exports.NaanControllerWeb = function() {
67
61
  }
68
62
  return b;
69
63
  })(window.location.search.substr(1).split('&'));
70
-
71
-
64
+
65
+
72
66
  //
73
67
  // Initialization
74
68
  //
75
-
69
+
76
70
  var naanlib = new exports.Naanlib();
77
71
  var dontSaveUntilWorking; // true when state loaded, must be reset to save state again
78
72
  var contSelf = this; // this is us, for access within nested functions
@@ -129,7 +123,7 @@ exports.NaanControllerWeb = function() {
129
123
  //
130
124
  // Read any pending Naan output and send to terminal.
131
125
  //
132
-
126
+
133
127
  function takePending() {
134
128
  var outputq = replqueue;
135
129
  replqueue = false;
@@ -145,13 +139,13 @@ exports.NaanControllerWeb = function() {
145
139
  //==========================================================================
146
140
  // New Terminal Interface
147
141
  //--------------------------------------------------------------------------
148
-
142
+
149
143
  //
150
144
  // StateGet
151
145
  //
152
146
  // Report the saved state we are keeping for the terminal.
153
147
  //
154
-
148
+
155
149
  this.StateGet = function StateGet() {
156
150
  termlist.every(function(term) {
157
151
  var state;
@@ -170,20 +164,20 @@ exports.NaanControllerWeb = function() {
170
164
  //
171
165
  // Attach a terminal to our interpreter and immediately start sending it messages.
172
166
  //
173
-
167
+
174
168
  this.Attach = function Attach(terminal) {
175
169
  var repdex = termlist.indexOf(terminal);
176
170
  if (repdex < 0)
177
171
  termlist.push(terminal);
178
172
  takePending(); // output all pending terminal messages
179
173
  };
180
-
174
+
181
175
  //
182
176
  // Detach
183
177
  //
184
178
  // Detach the terminal from the interpreter.
185
179
  //
186
-
180
+
187
181
  this.Detach = function Detach(terminal) {
188
182
  var repdex = termlist.indexOf(terminal);
189
183
  if (repdex >= 0)
@@ -191,17 +185,17 @@ exports.NaanControllerWeb = function() {
191
185
  if (termlist.length === 0)
192
186
  replstate = terminal.StateSave(); // save ultimate repl state
193
187
  };
194
-
188
+
195
189
  //
196
190
  // Interrupt
197
191
  //
198
192
  // Interrupt the interpreter in response to a user request.
199
193
  //
200
-
194
+
201
195
  this.Interrupt = function Interrupt(terminal) {
202
- window.setTimeout(function () {
196
+ Promise.resolve().then(function () {
203
197
  naanlib.escape();
204
- }, 0);
198
+ });
205
199
  };
206
200
 
207
201
  //
@@ -209,7 +203,7 @@ exports.NaanControllerWeb = function() {
209
203
  //
210
204
  // Enter line of text to instance.
211
205
  //
212
-
206
+
213
207
  this.Return = function Return(text, terminal) {
214
208
  termlist.forEach(function(term) {
215
209
  if (term !== terminal)
@@ -240,13 +234,13 @@ exports.NaanControllerWeb = function() {
240
234
  });
241
235
  return (data);
242
236
  };
243
-
237
+
244
238
  var nideListener;
245
239
  this.OnMessage = function OnMessage(proc) {
246
240
  nideListener = proc;
247
241
  return (proc);
248
242
  };
249
-
243
+
250
244
  this.DispatchMessage = function DispatchMessage(data) { // "local" -> "remote"
251
245
  if (nideListener)
252
246
  setTimeout(function() {
@@ -267,7 +261,7 @@ exports.NaanControllerWeb = function() {
267
261
  debugListener = proc;
268
262
  return (proc);
269
263
  };
270
-
264
+
271
265
  this.DispatchDebugger = function DispatchDebugger(data) { // "local" -> "remote"
272
266
  if (debugListener)
273
267
  setTimeout(function() {
@@ -280,7 +274,7 @@ exports.NaanControllerWeb = function() {
280
274
  //
281
275
  // Report status to the terminal.
282
276
  //
283
-
277
+
284
278
  setInterval(function() {
285
279
  var samples = naanlib.status(1);
286
280
  if (samples.length > 0)
@@ -297,29 +291,30 @@ exports.NaanControllerWeb = function() {
297
291
  //
298
292
  // Enter keystroke to instance.
299
293
  //
300
-
294
+
301
295
  this.Keystroke = function Keystroke(text, keycode) {
302
296
  console.log("Naan keystroke mode not implemented");
303
297
  };
304
-
298
+
305
299
  //
306
300
  // Dimensions
307
301
  //
308
302
  // Report terminal dimensions to instance.
309
303
  //
310
-
304
+
311
305
  this.Dimensions = function Dimensions(rows, cols) {
312
306
  console.log("Naan terminal dimensions now (" + rows + ", " + cols + ")");
313
307
  };
314
-
308
+
315
309
  //
316
310
  // VsiteRefresh
317
311
  //
318
312
  // Check if our underlying data has changed, and reload if needed.
319
313
  //
320
-
314
+
321
315
  this.VsiteRefresh = function VsiteRefresh(url_origin) {
322
316
  if (vsiteName && window.location.href.startsWith(url_origin)) {
317
+ virtualClose();
323
318
  window.location.reload(); // we have changed underneath
324
319
  window.scroll(0,0); // Chrome 106 had flood pants 🤓
325
320
  return (true);
@@ -330,7 +325,7 @@ exports.NaanControllerWeb = function() {
330
325
  //==========================================================================
331
326
  // Browser Console Terminal
332
327
  //--------------------------------------------------------------------------
333
-
328
+
334
329
  // Commands:
335
330
  // Naanlang.debug(<text>) -- command line text entry
336
331
  // Naanlang.int() -- interrupt and enter debugger
@@ -373,7 +368,7 @@ exports.NaanControllerWeb = function() {
373
368
  return (false);
374
369
  };
375
370
  }
376
-
371
+
377
372
  //
378
373
  // debug
379
374
  //
@@ -429,7 +424,7 @@ exports.NaanControllerWeb = function() {
429
424
  debugListener(msg.data); // target received a message
430
425
  }
431
426
  });
432
-
427
+
433
428
  //
434
429
  // report messages back to opener
435
430
  //
@@ -437,7 +432,7 @@ exports.NaanControllerWeb = function() {
437
432
  window.opener.postMessage(msg, window.origin);
438
433
  return (msg);
439
434
  };
440
-
435
+
441
436
  //
442
437
  // report messages back to opener
443
438
  //
@@ -459,7 +454,7 @@ exports.NaanControllerWeb = function() {
459
454
  };
460
455
  return (termSelf.OnMessage(msg));
461
456
  };
462
-
457
+
463
458
  //
464
459
  // report status back to opener
465
460
  //
@@ -471,64 +466,47 @@ exports.NaanControllerWeb = function() {
471
466
  return (termSelf.OnMessage(msg));
472
467
  };
473
468
  }
474
-
469
+
475
470
  //==========================================================================
476
471
  // Host Environment
477
472
  //--------------------------------------------------------------------------
478
-
473
+
479
474
  //
480
475
  // SavePref
481
476
  //
482
477
  // Save a persistent preference object that can be retrieved in the future.
483
478
  //
484
-
479
+
485
480
  this.SavePref = function SavePref(key, value) {
486
481
  return (prefs[key] = value);
487
482
  };
488
-
483
+
489
484
  //
490
485
  // LoadPref
491
486
  //
492
487
  // Load a previously-saved preference object for future retrieval.
493
488
  //
494
-
489
+
495
490
  this.LoadPref = function LoadPref(key) {
496
491
  return (prefs[key]);
497
492
  };
498
-
493
+
499
494
  //
500
495
  // Working
501
496
  //
502
497
  // The application is working, so it is safe to save state.
503
498
  //
504
-
499
+
505
500
  this.Working = function Working() {
506
501
  dontSaveUntilWorking = false;
507
502
  };
508
-
509
- var saveEvent = new Event("NaanSave");
510
-
511
- window.document.addEventListener("visibilitychange", function (e) {
512
- if (window.document.visibilityState != "hidden")
513
- return;
514
- window.dispatchEvent(saveEvent);
515
- saveLocal();
516
- });
517
503
 
518
- window.addEventListener("pagehide", function (e) {
519
- window.dispatchEvent(saveEvent);
520
- saveLocal();
521
- });
522
-
523
- window.addEventListener("unload", function (e) {
524
- window.dispatchEvent(saveEvent);
525
- saveLocal();
526
- virtualClose();
527
- });
528
-
529
- window.addEventListener("beforeunload", function (e) {
530
- window.dispatchEvent(saveEvent);
531
- saveLocal();
504
+ window.document.addEventListener("visibilitychange", function (e) {
505
+ virtualVisibilityChange();
506
+ if (window.document.visibilityState == "hidden") {
507
+ window.dispatchEvent(new Event("NaanSave"));
508
+ saveLocal();
509
+ }
532
510
  });
533
511
 
534
512
  function makeState() {
@@ -536,7 +514,7 @@ exports.NaanControllerWeb = function() {
536
514
  statedoc.curversion = kStateCurrentVersion;
537
515
  statedoc.firstver = kStateFirstVersion;
538
516
  statedoc.licensee = "MIT-License";
539
- statedoc.verstring = "1.4.1+1";
517
+ statedoc.verstring = "1.4.3+1";
540
518
  statedoc.date = new Date().toISOString();
541
519
  statedoc.prefs = prefs;
542
520
  statedoc.naan = naanlib.saveState(false); // true to optimize, which is a bit slower
@@ -548,24 +526,24 @@ exports.NaanControllerWeb = function() {
548
526
  statedoc.replstate = replstate;
549
527
  return (statedoc);
550
528
  }
551
-
529
+
552
530
  function loadState(statedoc) {
553
531
  if (statedoc===undefined || statedoc.naan === undefined
554
532
  || statedoc.firstver > kStateCurrentVersion
555
533
  || statedoc.curversion < kStateFirstVersion
556
534
  || statedoc.licensee != "MIT-License"
557
- || statedoc.verstring != "1.4.1+1")
535
+ || statedoc.verstring != "1.4.3+1"
536
+ || !(naanstate = statedoc.naan))
558
537
  {
559
538
  localStorage.removeItem("NaanState_Hosted");
560
539
  return (false);
561
540
  }
562
- naanstate = statedoc.naan;
563
541
  replstate = statedoc.replstate; // get terminal state, if any
564
542
  if (typeof(statedoc.prefs) == "object")
565
543
  prefs = statedoc.prefs;
566
544
  return (true);
567
545
  }
568
-
546
+
569
547
  function saveLocal() {
570
548
  /*jshint sub:true */
571
549
  if (dontSaveUntilWorking) { // saved state is problematic
@@ -588,7 +566,7 @@ exports.NaanControllerWeb = function() {
588
566
  }
589
567
  return (false);
590
568
  }
591
-
569
+
592
570
  function loadLocal() {
593
571
  try {
594
572
  /*jshint sub:true */
@@ -604,7 +582,7 @@ exports.NaanControllerWeb = function() {
604
582
  }
605
583
  return (false);
606
584
  }
607
-
585
+
608
586
  function virtualOpen() {
609
587
  try {
610
588
  if (window.opener && window.opener.Naanlang) {
@@ -613,13 +591,13 @@ exports.NaanControllerWeb = function() {
613
591
  op: "VsiteOpen",
614
592
  name: vsiteName,
615
593
  naancont: contSelf,
616
- title: vsiteName + " 1.4.1+1"
594
+ title: vsiteName + " 1.4.3+1"
617
595
  });
618
596
  }
619
597
  } catch (e) {
620
598
  }
621
599
  }
622
-
600
+
623
601
  function virtualClose() {
624
602
  try {
625
603
  if (window.opener && window.opener.Naanlang) {
@@ -627,7 +605,7 @@ exports.NaanControllerWeb = function() {
627
605
  op: "VsiteClose",
628
606
  name: vsiteName,
629
607
  naancont: contSelf,
630
- title: vsiteName + " 1.4.1+1"
608
+ title: vsiteName + " 1.4.3+1"
631
609
  });
632
610
  termTextOut("\x1b[90m\x1b[3m".concat("\nwindow closed", "\x1b[0m\n"));
633
611
  }
@@ -636,14 +614,30 @@ exports.NaanControllerWeb = function() {
636
614
  vsiteName = false; // window is going away
637
615
  }
638
616
 
617
+ function virtualVisibilityChange() {
618
+ try {
619
+ if (window.opener && window.opener.Naanlang) {
620
+ window.opener.Naanlang.naancon.DispatchMessage({
621
+ op: "VsiteVisibilityChange",
622
+ name: vsiteName,
623
+ naancont: contSelf,
624
+ title: vsiteName + " 1.4.3+1",
625
+ hidden: window.document.visibilityState == "hidden",
626
+ window: window
627
+ });
628
+ }
629
+ } catch (e) {
630
+ }
631
+ }
632
+
639
633
  /*jshint sub:true */
640
634
  if (querystrings["restart"] || !loadLocal())
641
635
  { // don't load state or can't load state
642
636
  naanlib.banner();
643
637
  var hostpath = window.location.origin.concat(naanlib.js.r("path").dirname(window.location.pathname));
644
638
  naanlib.start({
645
- cmd: 'App.version = "1.4.1+1";;\r\n'
646
- + 'App.cache = "e319a60c34bb3a4866dfc9f6c698b9aa";;\r\n'
639
+ cmd: 'App.version = "1.4.3+1";;\r\n'
640
+ + 'App.cache = "cd897fb5b9f8c4fe564a32db8f9048ad";;\r\n'
647
641
  + 'Naan.module.requireQuery({ naanver: App.cache });;\r\n'
648
642
  + 'Naan.module.webparse("naan_init.nlg", "' + hostpath + '", { naanver: App.cache });;\r\n'
649
643
  });
@@ -656,6 +650,7 @@ exports.NaanControllerWeb = function() {
656
650
  localStorage.removeItem("NaanState_Hosted");
657
651
  window.location.reload();
658
652
  }
653
+ naanstate = false; // reclaim memory
659
654
  }
660
655
 
661
656
  if (window.opener) {