mol_text_distance 0.0.954 → 0.0.956

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/node.test.js CHANGED
@@ -274,7 +274,11 @@ var $node = new Proxy({ require }, {
274
274
  try {
275
275
  $$.$mol_exec('.', 'npm', 'install', '--omit=dev', '@types/' + name);
276
276
  }
277
- catch { }
277
+ catch (e) {
278
+ if ($$.$mol_fail_catch(e)) {
279
+ $$.$mol_fail_log(e);
280
+ }
281
+ }
278
282
  break;
279
283
  }
280
284
  else {
@@ -285,7 +289,7 @@ var $node = new Proxy({ require }, {
285
289
  return target.require(name);
286
290
  }
287
291
  catch (error) {
288
- if (error.code === 'ERR_REQUIRE_ESM') {
292
+ if ($.$mol_fail_catch(error) && error.code === 'ERR_REQUIRE_ESM') {
289
293
  const module = cache.get(name);
290
294
  if (module)
291
295
  return module;
@@ -305,162 +309,6 @@ require = (req => Object.assign(function require(name) {
305
309
  return $node[name];
306
310
  }, req))(require);
307
311
 
308
- ;
309
- "use strict";
310
- var $;
311
- (function ($) {
312
- function $mol_log3_area_lazy(event) {
313
- const self = this;
314
- const stack = self.$mol_log3_stack;
315
- const deep = stack.length;
316
- let logged = false;
317
- stack.push(() => {
318
- logged = true;
319
- self.$mol_log3_area.call(self, event);
320
- });
321
- return () => {
322
- if (logged)
323
- self.console.groupEnd();
324
- if (stack.length > deep)
325
- stack.length = deep;
326
- };
327
- }
328
- $.$mol_log3_area_lazy = $mol_log3_area_lazy;
329
- $.$mol_log3_stack = [];
330
- })($ || ($ = {}));
331
-
332
- ;
333
- "use strict";
334
- var $;
335
- (function ($) {
336
- class $mol_term_color {
337
- static reset = this.ansi(0, 0);
338
- static bold = this.ansi(1, 22);
339
- static italic = this.ansi(3, 23);
340
- static underline = this.ansi(4, 24);
341
- static inverse = this.ansi(7, 27);
342
- static hidden = this.ansi(8, 28);
343
- static strike = this.ansi(9, 29);
344
- static gray = this.ansi(90, 39);
345
- static red = this.ansi(91, 39);
346
- static green = this.ansi(92, 39);
347
- static yellow = this.ansi(93, 39);
348
- static blue = this.ansi(94, 39);
349
- static magenta = this.ansi(95, 39);
350
- static cyan = this.ansi(96, 39);
351
- static Gray = (str) => this.inverse(this.gray(str));
352
- static Red = (str) => this.inverse(this.red(str));
353
- static Green = (str) => this.inverse(this.green(str));
354
- static Yellow = (str) => this.inverse(this.yellow(str));
355
- static Blue = (str) => this.inverse(this.blue(str));
356
- static Magenta = (str) => this.inverse(this.magenta(str));
357
- static Cyan = (str) => this.inverse(this.cyan(str));
358
- static ansi(open, close) {
359
- if (typeof process === 'undefined')
360
- return String;
361
- if (!process.stdout.isTTY)
362
- return String;
363
- const prefix = `\x1b[${open}m`;
364
- const postfix = `\x1b[${close}m`;
365
- const suffix_regexp = new RegExp(postfix.replace('[', '\\['), 'g');
366
- return function colorer(str) {
367
- str = String(str);
368
- if (str === '')
369
- return str;
370
- const suffix = str.replace(suffix_regexp, prefix);
371
- return prefix + suffix + postfix;
372
- };
373
- }
374
- }
375
- $.$mol_term_color = $mol_term_color;
376
- })($ || ($ = {}));
377
-
378
- ;
379
- "use strict";
380
- var $;
381
- (function ($) {
382
- function $mol_log3_node_make(level, output, type, color) {
383
- return function $mol_log3_logger(event) {
384
- if (!event.time)
385
- event = { time: new Date().toISOString(), ...event };
386
- let tree = this.$mol_tree2_from_json(event);
387
- tree = tree.struct(type, tree.kids);
388
- let str = color(tree.toString());
389
- this.console[level](str);
390
- const self = this;
391
- return () => self.console.groupEnd();
392
- };
393
- }
394
- $.$mol_log3_node_make = $mol_log3_node_make;
395
- $.$mol_log3_come = $mol_log3_node_make('info', 'stdout', 'come', $mol_term_color.blue);
396
- $.$mol_log3_done = $mol_log3_node_make('info', 'stdout', 'done', $mol_term_color.green);
397
- $.$mol_log3_fail = $mol_log3_node_make('error', 'stderr', 'fail', $mol_term_color.red);
398
- $.$mol_log3_warn = $mol_log3_node_make('warn', 'stderr', 'warn', $mol_term_color.yellow);
399
- $.$mol_log3_rise = $mol_log3_node_make('log', 'stdout', 'rise', $mol_term_color.magenta);
400
- $.$mol_log3_area = $mol_log3_node_make('log', 'stdout', 'area', $mol_term_color.cyan);
401
- })($ || ($ = {}));
402
-
403
- ;
404
- "use strict";
405
- var $;
406
- (function ($_1) {
407
- $mol_test_mocks.push($ => {
408
- $.$mol_log3_come = () => { };
409
- $.$mol_log3_done = () => { };
410
- $.$mol_log3_fail = () => { };
411
- $.$mol_log3_warn = () => { };
412
- $.$mol_log3_rise = () => { };
413
- $.$mol_log3_area = () => () => { };
414
- });
415
- })($ || ($ = {}));
416
-
417
- ;
418
- "use strict";
419
- var $;
420
- (function ($) {
421
- function $mol_env() {
422
- return {};
423
- }
424
- $.$mol_env = $mol_env;
425
- })($ || ($ = {}));
426
-
427
- ;
428
- "use strict";
429
- var $;
430
- (function ($) {
431
- $.$mol_env = function $mol_env() {
432
- return this.process.env;
433
- };
434
- })($ || ($ = {}));
435
-
436
- ;
437
- "use strict";
438
- var $;
439
- (function ($) {
440
- function $mol_exec(dir, command, ...args) {
441
- let [app, ...args0] = command.split(' ');
442
- args = [...args0, ...args];
443
- this.$mol_log3_come({
444
- place: '$mol_exec',
445
- dir: $node.path.relative('', dir),
446
- message: 'Run',
447
- command: `${app} ${args.join(' ')}`,
448
- });
449
- var res = $node['child_process'].spawnSync(app, args, {
450
- cwd: $node.path.resolve(dir),
451
- shell: true,
452
- env: this.$mol_env(),
453
- });
454
- if (res.status || res.error) {
455
- return $mol_fail(res.error || new Error(res.stderr.toString(), { cause: res.stdout }));
456
- }
457
- if (!res.stdout)
458
- res.stdout = Buffer.from([]);
459
- return res;
460
- }
461
- $.$mol_exec = $mol_exec;
462
- })($ || ($ = {}));
463
-
464
312
  ;
465
313
  "use strict";
466
314
  var $;
@@ -1440,6 +1288,115 @@ var $;
1440
1288
  });
1441
1289
  })($ || ($ = {}));
1442
1290
 
1291
+ ;
1292
+ "use strict";
1293
+ var $;
1294
+ (function ($) {
1295
+ function $mol_log3_area_lazy(event) {
1296
+ const self = this;
1297
+ const stack = self.$mol_log3_stack;
1298
+ const deep = stack.length;
1299
+ let logged = false;
1300
+ stack.push(() => {
1301
+ logged = true;
1302
+ self.$mol_log3_area.call(self, event);
1303
+ });
1304
+ return () => {
1305
+ if (logged)
1306
+ self.console.groupEnd();
1307
+ if (stack.length > deep)
1308
+ stack.length = deep;
1309
+ };
1310
+ }
1311
+ $.$mol_log3_area_lazy = $mol_log3_area_lazy;
1312
+ $.$mol_log3_stack = [];
1313
+ })($ || ($ = {}));
1314
+
1315
+ ;
1316
+ "use strict";
1317
+ var $;
1318
+ (function ($) {
1319
+ class $mol_term_color {
1320
+ static reset = this.ansi(0, 0);
1321
+ static bold = this.ansi(1, 22);
1322
+ static italic = this.ansi(3, 23);
1323
+ static underline = this.ansi(4, 24);
1324
+ static inverse = this.ansi(7, 27);
1325
+ static hidden = this.ansi(8, 28);
1326
+ static strike = this.ansi(9, 29);
1327
+ static gray = this.ansi(90, 39);
1328
+ static red = this.ansi(91, 39);
1329
+ static green = this.ansi(92, 39);
1330
+ static yellow = this.ansi(93, 39);
1331
+ static blue = this.ansi(94, 39);
1332
+ static magenta = this.ansi(95, 39);
1333
+ static cyan = this.ansi(96, 39);
1334
+ static Gray = (str) => this.inverse(this.gray(str));
1335
+ static Red = (str) => this.inverse(this.red(str));
1336
+ static Green = (str) => this.inverse(this.green(str));
1337
+ static Yellow = (str) => this.inverse(this.yellow(str));
1338
+ static Blue = (str) => this.inverse(this.blue(str));
1339
+ static Magenta = (str) => this.inverse(this.magenta(str));
1340
+ static Cyan = (str) => this.inverse(this.cyan(str));
1341
+ static ansi(open, close) {
1342
+ if (typeof process === 'undefined')
1343
+ return String;
1344
+ if (!process.stdout.isTTY)
1345
+ return String;
1346
+ const prefix = `\x1b[${open}m`;
1347
+ const postfix = `\x1b[${close}m`;
1348
+ const suffix_regexp = new RegExp(postfix.replace('[', '\\['), 'g');
1349
+ return function colorer(str) {
1350
+ str = String(str);
1351
+ if (str === '')
1352
+ return str;
1353
+ const suffix = str.replace(suffix_regexp, prefix);
1354
+ return prefix + suffix + postfix;
1355
+ };
1356
+ }
1357
+ }
1358
+ $.$mol_term_color = $mol_term_color;
1359
+ })($ || ($ = {}));
1360
+
1361
+ ;
1362
+ "use strict";
1363
+ var $;
1364
+ (function ($) {
1365
+ function $mol_log3_node_make(level, output, type, color) {
1366
+ return function $mol_log3_logger(event) {
1367
+ if (!event.time)
1368
+ event = { time: new Date().toISOString(), ...event };
1369
+ let tree = this.$mol_tree2_from_json(event);
1370
+ tree = tree.struct(type, tree.kids);
1371
+ let str = color(tree.toString());
1372
+ this.console[level](str);
1373
+ const self = this;
1374
+ return () => self.console.groupEnd();
1375
+ };
1376
+ }
1377
+ $.$mol_log3_node_make = $mol_log3_node_make;
1378
+ $.$mol_log3_come = $mol_log3_node_make('info', 'stdout', 'come', $mol_term_color.blue);
1379
+ $.$mol_log3_done = $mol_log3_node_make('info', 'stdout', 'done', $mol_term_color.green);
1380
+ $.$mol_log3_fail = $mol_log3_node_make('error', 'stderr', 'fail', $mol_term_color.red);
1381
+ $.$mol_log3_warn = $mol_log3_node_make('warn', 'stderr', 'warn', $mol_term_color.yellow);
1382
+ $.$mol_log3_rise = $mol_log3_node_make('log', 'stdout', 'rise', $mol_term_color.magenta);
1383
+ $.$mol_log3_area = $mol_log3_node_make('log', 'stdout', 'area', $mol_term_color.cyan);
1384
+ })($ || ($ = {}));
1385
+
1386
+ ;
1387
+ "use strict";
1388
+ var $;
1389
+ (function ($_1) {
1390
+ $mol_test_mocks.push($ => {
1391
+ $.$mol_log3_come = () => { };
1392
+ $.$mol_log3_done = () => { };
1393
+ $.$mol_log3_fail = () => { };
1394
+ $.$mol_log3_warn = () => { };
1395
+ $.$mol_log3_rise = () => { };
1396
+ $.$mol_log3_area = () => () => { };
1397
+ });
1398
+ })($ || ($ = {}));
1399
+
1443
1400
  ;
1444
1401
  "use strict";
1445
1402
  var $;
@@ -2311,6 +2268,1337 @@ var $;
2311
2268
  });
2312
2269
  })($ || ($ = {}));
2313
2270
 
2271
+ ;
2272
+ "use strict";
2273
+ var $;
2274
+ (function ($) {
2275
+ class $mol_error_mix extends AggregateError {
2276
+ cause;
2277
+ name = $$.$mol_func_name(this.constructor).replace(/^\$/, '') + '_Error';
2278
+ constructor(message, cause = {}, ...errors) {
2279
+ super(errors, message, { cause });
2280
+ this.cause = cause;
2281
+ const stack_get = Object.getOwnPropertyDescriptor(this, 'stack')?.get ?? (() => super.stack);
2282
+ Object.defineProperty(this, 'stack', {
2283
+ get: () => (stack_get.call(this) ?? this.message) + '\n' + [JSON.stringify(this.cause, null, ' ') ?? 'no cause', ...this.errors.map(e => e.stack)].map(e => e.trim()
2284
+ .replace(/at /gm, ' at ')
2285
+ .replace(/^(?! +at )(.*)/gm, ' at | $1 (#)')).join('\n')
2286
+ });
2287
+ }
2288
+ static [Symbol.toPrimitive]() {
2289
+ return this.toString();
2290
+ }
2291
+ static toString() {
2292
+ return $$.$mol_func_name(this);
2293
+ }
2294
+ static make(...params) {
2295
+ return new this(...params);
2296
+ }
2297
+ }
2298
+ $.$mol_error_mix = $mol_error_mix;
2299
+ })($ || ($ = {}));
2300
+
2301
+ ;
2302
+ "use strict";
2303
+ var $;
2304
+ (function ($) {
2305
+ $mol_test({
2306
+ 'auto name'() {
2307
+ class Invalid extends $mol_error_mix {
2308
+ }
2309
+ const mix = new Invalid('foo');
2310
+ $mol_assert_equal(mix.name, 'Invalid_Error');
2311
+ },
2312
+ 'simpe mix'() {
2313
+ const mix = new $mol_error_mix('foo', {}, new Error('bar'), new Error('lol'));
2314
+ $mol_assert_equal(mix.message, 'foo');
2315
+ $mol_assert_equal(mix.errors.map(e => e.message), ['bar', 'lol']);
2316
+ },
2317
+ 'provide additional info'() {
2318
+ class Invalid extends $mol_error_mix {
2319
+ }
2320
+ const mix = new $mol_error_mix('Wrong password', {}, new Invalid('Too short', { value: 'p@ssw0rd', hint: '> 8 letters' }), new Invalid('Too simple', { value: 'p@ssw0rd', hint: 'need capital letter' }));
2321
+ const hints = [];
2322
+ if (mix instanceof $mol_error_mix) {
2323
+ for (const er of mix.errors) {
2324
+ if (er instanceof Invalid) {
2325
+ hints.push(er.cause?.hint ?? '');
2326
+ }
2327
+ }
2328
+ }
2329
+ $mol_assert_equal(hints, ['> 8 letters', 'need capital letter']);
2330
+ },
2331
+ });
2332
+ })($ || ($ = {}));
2333
+
2334
+ ;
2335
+ "use strict";
2336
+ var $;
2337
+ (function ($) {
2338
+ function $mol_env() {
2339
+ return {};
2340
+ }
2341
+ $.$mol_env = $mol_env;
2342
+ })($ || ($ = {}));
2343
+
2344
+ ;
2345
+ "use strict";
2346
+ var $;
2347
+ (function ($) {
2348
+ $.$mol_env = function $mol_env() {
2349
+ return this.process.env;
2350
+ };
2351
+ })($ || ($ = {}));
2352
+
2353
+ ;
2354
+ "use strict";
2355
+ var $;
2356
+ (function ($) {
2357
+ let $mol_wire_cursor;
2358
+ (function ($mol_wire_cursor) {
2359
+ $mol_wire_cursor[$mol_wire_cursor["stale"] = -1] = "stale";
2360
+ $mol_wire_cursor[$mol_wire_cursor["doubt"] = -2] = "doubt";
2361
+ $mol_wire_cursor[$mol_wire_cursor["fresh"] = -3] = "fresh";
2362
+ $mol_wire_cursor[$mol_wire_cursor["final"] = -4] = "final";
2363
+ })($mol_wire_cursor = $.$mol_wire_cursor || ($.$mol_wire_cursor = {}));
2364
+ })($ || ($ = {}));
2365
+
2366
+ ;
2367
+ "use strict";
2368
+ var $;
2369
+ (function ($) {
2370
+ class $mol_wire_pub extends Object {
2371
+ data = [];
2372
+ static get [Symbol.species]() {
2373
+ return Array;
2374
+ }
2375
+ sub_from = 0;
2376
+ get sub_list() {
2377
+ const res = [];
2378
+ for (let i = this.sub_from; i < this.data.length; i += 2) {
2379
+ res.push(this.data[i]);
2380
+ }
2381
+ return res;
2382
+ }
2383
+ get sub_empty() {
2384
+ return this.sub_from === this.data.length;
2385
+ }
2386
+ sub_on(sub, pub_pos) {
2387
+ const pos = this.data.length;
2388
+ this.data.push(sub, pub_pos);
2389
+ return pos;
2390
+ }
2391
+ sub_off(sub_pos) {
2392
+ if (!(sub_pos < this.data.length)) {
2393
+ $mol_fail(new Error(`Wrong pos ${sub_pos}`));
2394
+ }
2395
+ const end = this.data.length - 2;
2396
+ if (sub_pos !== end) {
2397
+ this.peer_move(end, sub_pos);
2398
+ }
2399
+ this.data.pop();
2400
+ this.data.pop();
2401
+ if (this.data.length === this.sub_from)
2402
+ this.reap();
2403
+ }
2404
+ reap() { }
2405
+ promote() {
2406
+ $mol_wire_auto()?.track_next(this);
2407
+ }
2408
+ fresh() { }
2409
+ complete() { }
2410
+ get incompleted() {
2411
+ return false;
2412
+ }
2413
+ emit(quant = $mol_wire_cursor.stale) {
2414
+ for (let i = this.sub_from; i < this.data.length; i += 2) {
2415
+ ;
2416
+ this.data[i].absorb(quant);
2417
+ }
2418
+ }
2419
+ peer_move(from_pos, to_pos) {
2420
+ const peer = this.data[from_pos];
2421
+ const self_pos = this.data[from_pos + 1];
2422
+ this.data[to_pos] = peer;
2423
+ this.data[to_pos + 1] = self_pos;
2424
+ peer.peer_repos(self_pos, to_pos);
2425
+ }
2426
+ peer_repos(peer_pos, self_pos) {
2427
+ this.data[peer_pos + 1] = self_pos;
2428
+ }
2429
+ }
2430
+ $.$mol_wire_pub = $mol_wire_pub;
2431
+ })($ || ($ = {}));
2432
+
2433
+ ;
2434
+ "use strict";
2435
+
2436
+ ;
2437
+ "use strict";
2438
+ var $;
2439
+ (function ($) {
2440
+ $.$mol_wire_auto_sub = null;
2441
+ function $mol_wire_auto(next = $.$mol_wire_auto_sub) {
2442
+ return $.$mol_wire_auto_sub = next;
2443
+ }
2444
+ $.$mol_wire_auto = $mol_wire_auto;
2445
+ $.$mol_wire_affected = [];
2446
+ })($ || ($ = {}));
2447
+
2448
+ ;
2449
+ "use strict";
2450
+ var $;
2451
+ (function ($) {
2452
+ $['devtoolsFormatters'] ||= [];
2453
+ function $mol_dev_format_register(config) {
2454
+ $['devtoolsFormatters'].push(config);
2455
+ }
2456
+ $.$mol_dev_format_register = $mol_dev_format_register;
2457
+ $.$mol_dev_format_head = Symbol('$mol_dev_format_head');
2458
+ $.$mol_dev_format_body = Symbol('$mol_dev_format_body');
2459
+ $mol_dev_format_register({
2460
+ header: (val, config = false) => {
2461
+ if (config)
2462
+ return null;
2463
+ if (!val)
2464
+ return null;
2465
+ if ($.$mol_dev_format_head in val) {
2466
+ try {
2467
+ return val[$.$mol_dev_format_head]();
2468
+ }
2469
+ catch (error) {
2470
+ return $.$mol_dev_format_accent($mol_dev_format_native(val), '💨', $mol_dev_format_native(error), '');
2471
+ }
2472
+ }
2473
+ if (typeof val === 'function') {
2474
+ return $mol_dev_format_native(val);
2475
+ }
2476
+ if (Symbol.toStringTag in val) {
2477
+ return $mol_dev_format_native(val);
2478
+ }
2479
+ return null;
2480
+ },
2481
+ hasBody: val => val[$.$mol_dev_format_body],
2482
+ body: val => val[$.$mol_dev_format_body](),
2483
+ });
2484
+ function $mol_dev_format_native(obj) {
2485
+ if (typeof obj === 'undefined')
2486
+ return $.$mol_dev_format_shade('undefined');
2487
+ return [
2488
+ 'object',
2489
+ {
2490
+ object: obj,
2491
+ config: true,
2492
+ },
2493
+ ];
2494
+ }
2495
+ $.$mol_dev_format_native = $mol_dev_format_native;
2496
+ function $mol_dev_format_auto(obj) {
2497
+ if (obj == null)
2498
+ return $.$mol_dev_format_shade(String(obj));
2499
+ return [
2500
+ 'object',
2501
+ {
2502
+ object: obj,
2503
+ config: false,
2504
+ },
2505
+ ];
2506
+ }
2507
+ $.$mol_dev_format_auto = $mol_dev_format_auto;
2508
+ function $mol_dev_format_element(element, style, ...content) {
2509
+ const styles = [];
2510
+ for (let key in style)
2511
+ styles.push(`${key} : ${style[key]}`);
2512
+ return [
2513
+ element,
2514
+ {
2515
+ style: styles.join(' ; '),
2516
+ },
2517
+ ...content,
2518
+ ];
2519
+ }
2520
+ $.$mol_dev_format_element = $mol_dev_format_element;
2521
+ function $mol_dev_format_span(style, ...content) {
2522
+ return $mol_dev_format_element('span', {
2523
+ ...style,
2524
+ }, ...content);
2525
+ }
2526
+ $.$mol_dev_format_span = $mol_dev_format_span;
2527
+ $.$mol_dev_format_div = $mol_dev_format_element.bind(null, 'div');
2528
+ $.$mol_dev_format_ol = $mol_dev_format_element.bind(null, 'ol');
2529
+ $.$mol_dev_format_li = $mol_dev_format_element.bind(null, 'li');
2530
+ $.$mol_dev_format_table = $mol_dev_format_element.bind(null, 'table');
2531
+ $.$mol_dev_format_tr = $mol_dev_format_element.bind(null, 'tr');
2532
+ $.$mol_dev_format_td = $mol_dev_format_element.bind(null, 'td');
2533
+ $.$mol_dev_format_accent = $mol_dev_format_span.bind(null, {
2534
+ 'color': 'magenta',
2535
+ });
2536
+ $.$mol_dev_format_strong = $mol_dev_format_span.bind(null, {
2537
+ 'font-weight': 'bold',
2538
+ });
2539
+ $.$mol_dev_format_string = $mol_dev_format_span.bind(null, {
2540
+ 'color': 'green',
2541
+ });
2542
+ $.$mol_dev_format_shade = $mol_dev_format_span.bind(null, {
2543
+ 'color': 'gray',
2544
+ });
2545
+ $.$mol_dev_format_indent = $.$mol_dev_format_div.bind(null, {
2546
+ 'margin-left': '13px'
2547
+ });
2548
+ })($ || ($ = {}));
2549
+
2550
+ ;
2551
+ "use strict";
2552
+ var $;
2553
+ (function ($) {
2554
+ class $mol_wire_pub_sub extends $mol_wire_pub {
2555
+ pub_from = 0;
2556
+ cursor = $mol_wire_cursor.stale;
2557
+ get temp() {
2558
+ return false;
2559
+ }
2560
+ get pub_list() {
2561
+ const res = [];
2562
+ const max = this.cursor >= 0 ? this.cursor : this.sub_from;
2563
+ for (let i = this.pub_from; i < max; i += 2) {
2564
+ if (this.data[i])
2565
+ res.push(this.data[i]);
2566
+ }
2567
+ return res;
2568
+ }
2569
+ track_on() {
2570
+ this.cursor = this.pub_from;
2571
+ const sub = $mol_wire_auto();
2572
+ $mol_wire_auto(this);
2573
+ return sub;
2574
+ }
2575
+ promote() {
2576
+ if (this.cursor >= this.pub_from) {
2577
+ $mol_fail(new Error('Circular subscription'));
2578
+ }
2579
+ super.promote();
2580
+ }
2581
+ track_next(pub) {
2582
+ if (this.cursor < 0)
2583
+ $mol_fail(new Error('Promo to non begun sub'));
2584
+ if (this.cursor < this.sub_from) {
2585
+ const next = this.data[this.cursor];
2586
+ if (pub === undefined)
2587
+ return next ?? null;
2588
+ if (next === pub) {
2589
+ this.cursor += 2;
2590
+ return next;
2591
+ }
2592
+ if (next) {
2593
+ if (this.sub_from < this.data.length) {
2594
+ this.peer_move(this.sub_from, this.data.length);
2595
+ }
2596
+ this.peer_move(this.cursor, this.sub_from);
2597
+ this.sub_from += 2;
2598
+ }
2599
+ }
2600
+ else {
2601
+ if (pub === undefined)
2602
+ return null;
2603
+ if (this.sub_from < this.data.length) {
2604
+ this.peer_move(this.sub_from, this.data.length);
2605
+ }
2606
+ this.sub_from += 2;
2607
+ }
2608
+ this.data[this.cursor] = pub;
2609
+ this.data[this.cursor + 1] = pub.sub_on(this, this.cursor);
2610
+ this.cursor += 2;
2611
+ return pub;
2612
+ }
2613
+ track_off(sub) {
2614
+ $mol_wire_auto(sub);
2615
+ if (this.cursor < 0) {
2616
+ $mol_fail(new Error('End of non begun sub'));
2617
+ }
2618
+ for (let cursor = this.pub_from; cursor < this.cursor; cursor += 2) {
2619
+ const pub = this.data[cursor];
2620
+ pub.fresh();
2621
+ }
2622
+ this.cursor = $mol_wire_cursor.fresh;
2623
+ }
2624
+ pub_off(sub_pos) {
2625
+ this.data[sub_pos] = undefined;
2626
+ this.data[sub_pos + 1] = undefined;
2627
+ }
2628
+ destructor() {
2629
+ for (let cursor = this.data.length - 2; cursor >= this.sub_from; cursor -= 2) {
2630
+ const sub = this.data[cursor];
2631
+ const pos = this.data[cursor + 1];
2632
+ sub.pub_off(pos);
2633
+ this.data.pop();
2634
+ this.data.pop();
2635
+ }
2636
+ this.cursor = this.pub_from;
2637
+ this.track_cut();
2638
+ this.cursor = $mol_wire_cursor.final;
2639
+ }
2640
+ track_cut() {
2641
+ if (this.cursor < this.pub_from) {
2642
+ $mol_fail(new Error('Cut of non begun sub'));
2643
+ }
2644
+ let tail = 0;
2645
+ for (let cursor = this.cursor; cursor < this.sub_from; cursor += 2) {
2646
+ const pub = this.data[cursor];
2647
+ pub?.sub_off(this.data[cursor + 1]);
2648
+ if (this.sub_from < this.data.length) {
2649
+ this.peer_move(this.data.length - 2, cursor);
2650
+ this.data.pop();
2651
+ this.data.pop();
2652
+ }
2653
+ else {
2654
+ ++tail;
2655
+ }
2656
+ }
2657
+ for (; tail; --tail) {
2658
+ this.data.pop();
2659
+ this.data.pop();
2660
+ }
2661
+ this.sub_from = this.cursor;
2662
+ }
2663
+ complete() { }
2664
+ complete_pubs() {
2665
+ const limit = this.cursor < 0 ? this.sub_from : this.cursor;
2666
+ for (let cursor = this.pub_from; cursor < limit; cursor += 2) {
2667
+ const pub = this.data[cursor];
2668
+ if (pub?.incompleted)
2669
+ return;
2670
+ }
2671
+ for (let cursor = this.pub_from; cursor < limit; cursor += 2) {
2672
+ const pub = this.data[cursor];
2673
+ pub?.complete();
2674
+ }
2675
+ }
2676
+ absorb(quant = $mol_wire_cursor.stale) {
2677
+ if (this.cursor === $mol_wire_cursor.final)
2678
+ return;
2679
+ if (this.cursor >= quant)
2680
+ return;
2681
+ this.cursor = quant;
2682
+ this.emit($mol_wire_cursor.doubt);
2683
+ }
2684
+ [$mol_dev_format_head]() {
2685
+ return $mol_dev_format_native(this);
2686
+ }
2687
+ get pub_empty() {
2688
+ return this.sub_from === this.pub_from;
2689
+ }
2690
+ }
2691
+ $.$mol_wire_pub_sub = $mol_wire_pub_sub;
2692
+ })($ || ($ = {}));
2693
+
2694
+ ;
2695
+ "use strict";
2696
+ var $;
2697
+ (function ($_1) {
2698
+ $mol_test({
2699
+ 'Collect deps'() {
2700
+ const pub1 = new $mol_wire_pub;
2701
+ const pub2 = new $mol_wire_pub;
2702
+ const sub = new $mol_wire_pub_sub;
2703
+ const bu1 = sub.track_on();
2704
+ try {
2705
+ pub1.promote();
2706
+ pub2.promote();
2707
+ pub2.promote();
2708
+ }
2709
+ finally {
2710
+ sub.track_cut();
2711
+ sub.track_off(bu1);
2712
+ }
2713
+ pub1.emit();
2714
+ pub2.emit();
2715
+ $mol_assert_like(sub.pub_list, [pub1, pub2, pub2]);
2716
+ const bu2 = sub.track_on();
2717
+ try {
2718
+ pub1.promote();
2719
+ pub1.promote();
2720
+ pub2.promote();
2721
+ }
2722
+ finally {
2723
+ sub.track_cut();
2724
+ sub.track_off(bu2);
2725
+ }
2726
+ pub1.emit();
2727
+ pub2.emit();
2728
+ $mol_assert_like(sub.pub_list, [pub1, pub1, pub2]);
2729
+ },
2730
+ 'cyclic detection'($) {
2731
+ const sub1 = new $mol_wire_pub_sub;
2732
+ const sub2 = new $mol_wire_pub_sub;
2733
+ const bu1 = sub1.track_on();
2734
+ try {
2735
+ const bu2 = sub2.track_on();
2736
+ try {
2737
+ $mol_assert_fail(() => sub1.promote(), 'Circular subscription');
2738
+ }
2739
+ finally {
2740
+ sub2.track_cut();
2741
+ sub2.track_off(bu2);
2742
+ }
2743
+ }
2744
+ finally {
2745
+ sub1.track_cut();
2746
+ sub1.track_off(bu1);
2747
+ }
2748
+ },
2749
+ });
2750
+ })($ || ($ = {}));
2751
+
2752
+ ;
2753
+ "use strict";
2754
+ var $;
2755
+ (function ($) {
2756
+ $.$mol_after_mock_queue = [];
2757
+ function $mol_after_mock_warp() {
2758
+ const queue = $.$mol_after_mock_queue.splice(0);
2759
+ for (const task of queue)
2760
+ task();
2761
+ }
2762
+ $.$mol_after_mock_warp = $mol_after_mock_warp;
2763
+ class $mol_after_mock_commmon extends $mol_object2 {
2764
+ task;
2765
+ promise = Promise.resolve();
2766
+ cancelled = false;
2767
+ id;
2768
+ constructor(task) {
2769
+ super();
2770
+ this.task = task;
2771
+ $.$mol_after_mock_queue.push(task);
2772
+ }
2773
+ destructor() {
2774
+ const index = $.$mol_after_mock_queue.indexOf(this.task);
2775
+ if (index >= 0)
2776
+ $.$mol_after_mock_queue.splice(index, 1);
2777
+ }
2778
+ }
2779
+ $.$mol_after_mock_commmon = $mol_after_mock_commmon;
2780
+ class $mol_after_mock_timeout extends $mol_after_mock_commmon {
2781
+ delay;
2782
+ constructor(delay, task) {
2783
+ super(task);
2784
+ this.delay = delay;
2785
+ }
2786
+ }
2787
+ $.$mol_after_mock_timeout = $mol_after_mock_timeout;
2788
+ })($ || ($ = {}));
2789
+
2790
+ ;
2791
+ "use strict";
2792
+ var $;
2793
+ (function ($) {
2794
+ class $mol_after_tick extends $mol_object2 {
2795
+ task;
2796
+ static promise = null;
2797
+ cancelled = false;
2798
+ constructor(task) {
2799
+ super();
2800
+ this.task = task;
2801
+ if (!$mol_after_tick.promise)
2802
+ $mol_after_tick.promise = Promise.resolve().then(() => {
2803
+ $mol_after_tick.promise = null;
2804
+ });
2805
+ $mol_after_tick.promise.then(() => {
2806
+ if (this.cancelled)
2807
+ return;
2808
+ task();
2809
+ });
2810
+ }
2811
+ destructor() {
2812
+ this.cancelled = true;
2813
+ }
2814
+ }
2815
+ $.$mol_after_tick = $mol_after_tick;
2816
+ })($ || ($ = {}));
2817
+
2818
+ ;
2819
+ "use strict";
2820
+ var $;
2821
+ (function ($_1) {
2822
+ $mol_test_mocks.push($ => {
2823
+ $.$mol_after_tick = $mol_after_mock_commmon;
2824
+ });
2825
+ })($ || ($ = {}));
2826
+
2827
+ ;
2828
+ "use strict";
2829
+ var $;
2830
+ (function ($) {
2831
+ const handled = new WeakSet();
2832
+ class $mol_wire_fiber extends $mol_wire_pub_sub {
2833
+ task;
2834
+ host;
2835
+ static warm = true;
2836
+ static planning = new Set();
2837
+ static reaping = new Set();
2838
+ static plan_task = null;
2839
+ static plan() {
2840
+ if (this.plan_task)
2841
+ return;
2842
+ this.plan_task = new $mol_after_tick(() => {
2843
+ try {
2844
+ this.sync();
2845
+ }
2846
+ finally {
2847
+ $mol_wire_fiber.plan_task = null;
2848
+ }
2849
+ });
2850
+ }
2851
+ static sync() {
2852
+ while (this.planning.size) {
2853
+ for (const fiber of this.planning) {
2854
+ this.planning.delete(fiber);
2855
+ if (fiber.cursor >= 0)
2856
+ continue;
2857
+ if (fiber.cursor === $mol_wire_cursor.final)
2858
+ continue;
2859
+ fiber.fresh();
2860
+ }
2861
+ }
2862
+ while (this.reaping.size) {
2863
+ const fibers = this.reaping;
2864
+ this.reaping = new Set;
2865
+ for (const fiber of fibers) {
2866
+ if (!fiber.sub_empty)
2867
+ continue;
2868
+ fiber.destructor();
2869
+ }
2870
+ }
2871
+ }
2872
+ [Symbol.toStringTag];
2873
+ cache = undefined;
2874
+ get args() {
2875
+ return this.data.slice(0, this.pub_from);
2876
+ }
2877
+ result() {
2878
+ if ($mol_promise_like(this.cache))
2879
+ return;
2880
+ if (this.cache instanceof Error)
2881
+ return;
2882
+ return this.cache;
2883
+ }
2884
+ get incompleted() {
2885
+ return $mol_promise_like(this.cache);
2886
+ }
2887
+ field() {
2888
+ return this.task.name + '<>';
2889
+ }
2890
+ constructor(id, task, host, args) {
2891
+ super();
2892
+ this.task = task;
2893
+ this.host = host;
2894
+ if (args)
2895
+ this.data.push(...args);
2896
+ this.pub_from = this.sub_from = args?.length ?? 0;
2897
+ this[Symbol.toStringTag] = id;
2898
+ }
2899
+ plan() {
2900
+ $mol_wire_fiber.planning.add(this);
2901
+ $mol_wire_fiber.plan();
2902
+ return this;
2903
+ }
2904
+ reap() {
2905
+ $mol_wire_fiber.reaping.add(this);
2906
+ $mol_wire_fiber.plan();
2907
+ }
2908
+ toString() {
2909
+ return this[Symbol.toStringTag];
2910
+ }
2911
+ toJSON() {
2912
+ return this[Symbol.toStringTag];
2913
+ }
2914
+ [$mol_dev_format_head]() {
2915
+ const cursor = {
2916
+ [$mol_wire_cursor.stale]: '🔴',
2917
+ [$mol_wire_cursor.doubt]: '🟡',
2918
+ [$mol_wire_cursor.fresh]: '🟢',
2919
+ [$mol_wire_cursor.final]: '🔵',
2920
+ }[this.cursor] ?? this.cursor.toString();
2921
+ return $mol_dev_format_div({}, $mol_owning_check(this, this.cache)
2922
+ ? $mol_dev_format_auto({
2923
+ [$mol_dev_format_head]: () => $mol_dev_format_shade(cursor),
2924
+ [$mol_dev_format_body]: () => $mol_dev_format_native(this),
2925
+ })
2926
+ : $mol_dev_format_shade($mol_dev_format_native(this), cursor), $mol_dev_format_auto(this.cache));
2927
+ }
2928
+ get $() {
2929
+ return (this.host ?? this.task)['$'];
2930
+ }
2931
+ emit(quant = $mol_wire_cursor.stale) {
2932
+ if (this.sub_empty)
2933
+ this.plan();
2934
+ else
2935
+ super.emit(quant);
2936
+ }
2937
+ fresh() {
2938
+ if (this.cursor === $mol_wire_cursor.fresh)
2939
+ return;
2940
+ if (this.cursor === $mol_wire_cursor.final)
2941
+ return;
2942
+ check: if (this.cursor === $mol_wire_cursor.doubt) {
2943
+ for (let i = this.pub_from; i < this.sub_from; i += 2) {
2944
+ ;
2945
+ this.data[i]?.fresh();
2946
+ if (this.cursor !== $mol_wire_cursor.doubt)
2947
+ break check;
2948
+ }
2949
+ this.cursor = $mol_wire_cursor.fresh;
2950
+ return;
2951
+ }
2952
+ const bu = this.track_on();
2953
+ let result;
2954
+ try {
2955
+ switch (this.pub_from) {
2956
+ case 0:
2957
+ result = this.task.call(this.host);
2958
+ break;
2959
+ case 1:
2960
+ result = this.task.call(this.host, this.data[0]);
2961
+ break;
2962
+ default:
2963
+ result = this.task.call(this.host, ...this.args);
2964
+ break;
2965
+ }
2966
+ if ($mol_promise_like(result) && !handled.has(result)) {
2967
+ const put = (res) => {
2968
+ if (this.cache === result)
2969
+ this.put(res);
2970
+ return res;
2971
+ };
2972
+ result = result.then(put, put);
2973
+ }
2974
+ }
2975
+ catch (error) {
2976
+ if (error instanceof Error || $mol_promise_like(error)) {
2977
+ result = error;
2978
+ }
2979
+ else {
2980
+ result = new Error(String(error), { cause: error });
2981
+ }
2982
+ if ($mol_promise_like(result) && !handled.has(result)) {
2983
+ result = result.finally(() => {
2984
+ if (this.cache === result)
2985
+ this.absorb();
2986
+ });
2987
+ }
2988
+ }
2989
+ if ($mol_promise_like(result) && !handled.has(result)) {
2990
+ result = Object.assign(result, {
2991
+ destructor: result['destructor'] ?? (() => { })
2992
+ });
2993
+ handled.add(result);
2994
+ const error = new Error(`Promise in ${this}`);
2995
+ Object.defineProperty(result, 'stack', { get: () => error.stack });
2996
+ }
2997
+ if (!$mol_promise_like(result)) {
2998
+ this.track_cut();
2999
+ }
3000
+ this.track_off(bu);
3001
+ this.put(result);
3002
+ return this;
3003
+ }
3004
+ refresh() {
3005
+ this.cursor = $mol_wire_cursor.stale;
3006
+ this.fresh();
3007
+ }
3008
+ sync() {
3009
+ if (!$mol_wire_fiber.warm) {
3010
+ return this.result();
3011
+ }
3012
+ this.promote();
3013
+ this.fresh();
3014
+ if (this.cache instanceof Error) {
3015
+ return $mol_fail_hidden(this.cache);
3016
+ }
3017
+ if ($mol_promise_like(this.cache)) {
3018
+ return $mol_fail_hidden(this.cache);
3019
+ }
3020
+ return this.cache;
3021
+ }
3022
+ async async() {
3023
+ while (true) {
3024
+ this.fresh();
3025
+ if (this.cache instanceof Error) {
3026
+ $mol_fail_hidden(this.cache);
3027
+ }
3028
+ if (!$mol_promise_like(this.cache))
3029
+ return this.cache;
3030
+ await Promise.race([this.cache, this.step()]);
3031
+ if (!$mol_promise_like(this.cache))
3032
+ return this.cache;
3033
+ if (this.cursor === $mol_wire_cursor.final) {
3034
+ await new Promise(() => { });
3035
+ }
3036
+ }
3037
+ }
3038
+ step() {
3039
+ return new Promise(done => {
3040
+ const sub = new $mol_wire_pub_sub;
3041
+ const prev = sub.track_on();
3042
+ sub.track_next(this);
3043
+ sub.track_off(prev);
3044
+ sub.absorb = () => {
3045
+ done(null);
3046
+ setTimeout(() => sub.destructor());
3047
+ };
3048
+ });
3049
+ }
3050
+ }
3051
+ $.$mol_wire_fiber = $mol_wire_fiber;
3052
+ })($ || ($ = {}));
3053
+
3054
+ ;
3055
+ "use strict";
3056
+ var $;
3057
+ (function ($) {
3058
+ $mol_test({
3059
+ 'Sync execution'() {
3060
+ class Sync extends $mol_object2 {
3061
+ static calc(a, b) {
3062
+ return a + b;
3063
+ }
3064
+ }
3065
+ __decorate([
3066
+ $mol_wire_method
3067
+ ], Sync, "calc", null);
3068
+ $mol_assert_equal(Sync.calc(1, 2), 3);
3069
+ },
3070
+ async 'async <=> sync'() {
3071
+ class SyncAsync extends $mol_object2 {
3072
+ static async val(a) {
3073
+ return a;
3074
+ }
3075
+ static sum(a, b) {
3076
+ const syn = $mol_wire_sync(this);
3077
+ return syn.val(a) + syn.val(b);
3078
+ }
3079
+ static async calc(a, b) {
3080
+ return 5 + await $mol_wire_async(this).sum(a, b);
3081
+ }
3082
+ }
3083
+ $mol_assert_equal(await SyncAsync.calc(1, 2), 8);
3084
+ },
3085
+ async 'Idempotence control'() {
3086
+ class Idempotence extends $mol_object2 {
3087
+ static logs_idemp = 0;
3088
+ static logs_unidemp = 0;
3089
+ static log_idemp() {
3090
+ this.logs_idemp += 1;
3091
+ }
3092
+ static log_unidemp() {
3093
+ this.logs_unidemp += 1;
3094
+ }
3095
+ static async val(a) {
3096
+ return a;
3097
+ }
3098
+ static sum(a, b) {
3099
+ this.log_idemp();
3100
+ this.log_unidemp();
3101
+ const syn = $mol_wire_sync(this);
3102
+ return syn.val(a) + syn.val(b);
3103
+ }
3104
+ static async calc(a, b) {
3105
+ return 5 + await $mol_wire_async(this).sum(a, b);
3106
+ }
3107
+ }
3108
+ __decorate([
3109
+ $mol_wire_method
3110
+ ], Idempotence, "log_idemp", null);
3111
+ $mol_assert_equal(await Idempotence.calc(1, 2), 8);
3112
+ $mol_assert_equal(Idempotence.logs_idemp, 1);
3113
+ $mol_assert_equal(Idempotence.logs_unidemp, 3);
3114
+ },
3115
+ async 'Error handling'() {
3116
+ class Handle extends $mol_object2 {
3117
+ static async sum(a, b) {
3118
+ $mol_fail(new Error('test error ' + (a + b)));
3119
+ }
3120
+ static check() {
3121
+ try {
3122
+ return $mol_wire_sync(Handle).sum(1, 2);
3123
+ }
3124
+ catch (error) {
3125
+ if ($mol_promise_like(error))
3126
+ $mol_fail_hidden(error);
3127
+ $mol_assert_equal(error.message, 'test error 3');
3128
+ }
3129
+ }
3130
+ }
3131
+ await $mol_wire_async(Handle).check();
3132
+ },
3133
+ });
3134
+ })($ || ($ = {}));
3135
+
3136
+ ;
3137
+ "use strict";
3138
+ var $;
3139
+ (function ($) {
3140
+ class $mol_wire_task extends $mol_wire_fiber {
3141
+ static getter(task) {
3142
+ return function $mol_wire_task_get(host, args) {
3143
+ const sub = $mol_wire_auto();
3144
+ const existen = sub?.track_next();
3145
+ reuse: if (existen) {
3146
+ if (!existen.temp)
3147
+ break reuse;
3148
+ if (existen.host !== host)
3149
+ break reuse;
3150
+ if (existen.task !== task)
3151
+ break reuse;
3152
+ if (!$mol_compare_deep(existen.args, args))
3153
+ break reuse;
3154
+ return existen;
3155
+ }
3156
+ const key = (host?.[Symbol.toStringTag] ?? host) + ('.' + task.name + '<#>');
3157
+ const next = new $mol_wire_task(key, task, host, args);
3158
+ if (existen?.temp) {
3159
+ $$.$mol_log3_warn({
3160
+ place: '$mol_wire_task',
3161
+ message: `Non idempotency`,
3162
+ existen,
3163
+ next,
3164
+ hint: 'Ignore it',
3165
+ });
3166
+ }
3167
+ return next;
3168
+ };
3169
+ }
3170
+ get temp() {
3171
+ return true;
3172
+ }
3173
+ complete() {
3174
+ if ($mol_promise_like(this.cache))
3175
+ return;
3176
+ this.destructor();
3177
+ }
3178
+ put(next) {
3179
+ const prev = this.cache;
3180
+ this.cache = next;
3181
+ if ($mol_promise_like(next)) {
3182
+ this.cursor = $mol_wire_cursor.fresh;
3183
+ if (next !== prev)
3184
+ this.emit();
3185
+ return next;
3186
+ }
3187
+ this.cursor = $mol_wire_cursor.final;
3188
+ if (this.sub_empty)
3189
+ this.destructor();
3190
+ else if (next !== prev)
3191
+ this.emit();
3192
+ return next;
3193
+ }
3194
+ }
3195
+ $.$mol_wire_task = $mol_wire_task;
3196
+ })($ || ($ = {}));
3197
+
3198
+ ;
3199
+ "use strict";
3200
+ var $;
3201
+ (function ($) {
3202
+ function $mol_wire_async(obj) {
3203
+ let fiber;
3204
+ const temp = $mol_wire_task.getter(obj);
3205
+ return new Proxy(obj, {
3206
+ get(obj, field) {
3207
+ const val = obj[field];
3208
+ if (typeof val !== 'function')
3209
+ return val;
3210
+ let fiber;
3211
+ const temp = $mol_wire_task.getter(val);
3212
+ return function $mol_wire_async(...args) {
3213
+ fiber?.destructor();
3214
+ fiber = temp(obj, args);
3215
+ return fiber.async();
3216
+ };
3217
+ },
3218
+ apply(obj, self, args) {
3219
+ fiber?.destructor();
3220
+ fiber = temp(self, args);
3221
+ return fiber.async();
3222
+ },
3223
+ });
3224
+ }
3225
+ $.$mol_wire_async = $mol_wire_async;
3226
+ })($ || ($ = {}));
3227
+
3228
+ ;
3229
+ "use strict";
3230
+ var $;
3231
+ (function ($_1) {
3232
+ $mol_test({
3233
+ 'test types'($) {
3234
+ class A {
3235
+ static a() {
3236
+ return '';
3237
+ }
3238
+ static b() {
3239
+ return $mol_wire_async(this).a();
3240
+ }
3241
+ }
3242
+ },
3243
+ async 'Latest method calls wins'($) {
3244
+ class NameLogger extends $mol_object2 {
3245
+ static $ = $;
3246
+ static first = [];
3247
+ static last = [];
3248
+ static send(next) {
3249
+ $mol_wire_sync(this.first).push(next);
3250
+ this.$.$mol_wait_timeout(0);
3251
+ this.last.push(next);
3252
+ }
3253
+ }
3254
+ const name = $mol_wire_async(NameLogger).send;
3255
+ name('john');
3256
+ const promise = name('jin');
3257
+ $.$mol_after_mock_warp();
3258
+ await promise;
3259
+ $mol_assert_like(NameLogger.first, ['john', 'jin']);
3260
+ $mol_assert_like(NameLogger.last, ['jin']);
3261
+ },
3262
+ async 'Latest function calls wins'($) {
3263
+ const first = [];
3264
+ const last = [];
3265
+ function send_name(next) {
3266
+ $mol_wire_sync(first).push(next);
3267
+ $.$mol_wait_timeout(0);
3268
+ last.push(next);
3269
+ }
3270
+ const name = $mol_wire_async(send_name);
3271
+ name('john');
3272
+ const promise = name('jin');
3273
+ $.$mol_after_mock_warp();
3274
+ await promise;
3275
+ $mol_assert_like(first, ['john', 'jin']);
3276
+ $mol_assert_like(last, ['jin']);
3277
+ },
3278
+ });
3279
+ })($ || ($ = {}));
3280
+
3281
+ ;
3282
+ "use strict";
3283
+ var $;
3284
+ (function ($) {
3285
+ function $mol_wire_method(host, field, descr) {
3286
+ if (!descr)
3287
+ descr = Reflect.getOwnPropertyDescriptor(host, field);
3288
+ const orig = descr?.value ?? host[field];
3289
+ const sup = Reflect.getPrototypeOf(host);
3290
+ if (typeof sup[field] === 'function') {
3291
+ Object.defineProperty(orig, 'name', { value: sup[field].name });
3292
+ }
3293
+ const temp = $mol_wire_task.getter(orig);
3294
+ const value = function (...args) {
3295
+ const fiber = temp(this ?? null, args);
3296
+ return fiber.sync();
3297
+ };
3298
+ Object.defineProperty(value, 'name', { value: orig.name + ' ' });
3299
+ Object.assign(value, { orig });
3300
+ const descr2 = { ...descr, value };
3301
+ Reflect.defineProperty(host, field, descr2);
3302
+ return descr2;
3303
+ }
3304
+ $.$mol_wire_method = $mol_wire_method;
3305
+ })($ || ($ = {}));
3306
+
3307
+ ;
3308
+ "use strict";
3309
+ var $;
3310
+ (function ($) {
3311
+ const factories = new WeakMap();
3312
+ function factory(val) {
3313
+ let make = factories.get(val);
3314
+ if (make)
3315
+ return make;
3316
+ make = $mol_func_name_from((...args) => new val(...args), val);
3317
+ factories.set(val, make);
3318
+ return make;
3319
+ }
3320
+ function $mol_wire_sync(obj) {
3321
+ return new Proxy(obj, {
3322
+ get(obj, field) {
3323
+ let val = obj[field];
3324
+ if (typeof val !== 'function')
3325
+ return val;
3326
+ const temp = $mol_wire_task.getter(val);
3327
+ return function $mol_wire_sync(...args) {
3328
+ const fiber = temp(obj, args);
3329
+ return fiber.sync();
3330
+ };
3331
+ },
3332
+ construct(obj, args) {
3333
+ const temp = $mol_wire_task.getter(factory(obj));
3334
+ return temp(obj, args).sync();
3335
+ },
3336
+ apply(obj, self, args) {
3337
+ const temp = $mol_wire_task.getter(obj);
3338
+ return temp(self, args).sync();
3339
+ },
3340
+ });
3341
+ }
3342
+ $.$mol_wire_sync = $mol_wire_sync;
3343
+ })($ || ($ = {}));
3344
+
3345
+ ;
3346
+ "use strict";
3347
+ var $;
3348
+ (function ($_1) {
3349
+ $mol_test({
3350
+ 'test types'($) {
3351
+ class A {
3352
+ static a() {
3353
+ return Promise.resolve('');
3354
+ }
3355
+ static b() {
3356
+ return $mol_wire_sync(this).a();
3357
+ }
3358
+ }
3359
+ },
3360
+ async 'test method from host'($) {
3361
+ let count = 0;
3362
+ class A {
3363
+ static a() {
3364
+ return $mol_wire_sync(this).b();
3365
+ }
3366
+ static b() { return Promise.resolve(++count); }
3367
+ }
3368
+ $mol_assert_equal(await $mol_wire_async(A).a(), 1, count);
3369
+ },
3370
+ async 'test function'($) {
3371
+ let count = 0;
3372
+ class A {
3373
+ static a() {
3374
+ return $mol_wire_sync(this.b)();
3375
+ }
3376
+ static b() { return Promise.resolve(++count); }
3377
+ }
3378
+ $mol_assert_equal(await $mol_wire_async(A).a(), 1, count);
3379
+ },
3380
+ async 'test construct itself'($) {
3381
+ class A {
3382
+ static instances = [];
3383
+ static a() {
3384
+ const a = new ($mol_wire_sync(A))();
3385
+ this.instances.push(a);
3386
+ $mol_wire_sync(this).b();
3387
+ }
3388
+ static b() { return Promise.resolve(); }
3389
+ }
3390
+ await $mol_wire_async(A).a();
3391
+ $mol_assert_equal(A.instances.length, 2);
3392
+ $mol_assert_equal(A.instances[0] instanceof A);
3393
+ $mol_assert_equal(A.instances[0], A.instances[1]);
3394
+ }
3395
+ });
3396
+ })($ || ($ = {}));
3397
+
3398
+ ;
3399
+ "use strict";
3400
+ var $;
3401
+ (function ($) {
3402
+ function $mol_promise() {
3403
+ let done;
3404
+ let fail;
3405
+ const promise = new Promise((d, f) => {
3406
+ done = d;
3407
+ fail = f;
3408
+ });
3409
+ return Object.assign(promise, {
3410
+ done,
3411
+ fail,
3412
+ });
3413
+ }
3414
+ $.$mol_promise = $mol_promise;
3415
+ })($ || ($ = {}));
3416
+
3417
+ ;
3418
+ "use strict";
3419
+ var $;
3420
+ (function ($) {
3421
+ class $mol_after_timeout extends $mol_object2 {
3422
+ delay;
3423
+ task;
3424
+ id;
3425
+ constructor(delay, task) {
3426
+ super();
3427
+ this.delay = delay;
3428
+ this.task = task;
3429
+ this.id = setTimeout(task, delay);
3430
+ }
3431
+ destructor() {
3432
+ clearTimeout(this.id);
3433
+ }
3434
+ }
3435
+ $.$mol_after_timeout = $mol_after_timeout;
3436
+ })($ || ($ = {}));
3437
+
3438
+ ;
3439
+ "use strict";
3440
+ var $;
3441
+ (function ($_1) {
3442
+ $mol_test_mocks.push($ => {
3443
+ $.$mol_after_timeout = $mol_after_mock_timeout;
3444
+ });
3445
+ })($ || ($ = {}));
3446
+
3447
+ ;
3448
+ "use strict";
3449
+ var $;
3450
+ (function ($) {
3451
+ function $mol_wait_timeout_async(timeout) {
3452
+ const promise = $mol_promise();
3453
+ const task = new this.$mol_after_timeout(timeout, () => promise.done());
3454
+ return Object.assign(promise, {
3455
+ destructor: () => task.destructor()
3456
+ });
3457
+ }
3458
+ $.$mol_wait_timeout_async = $mol_wait_timeout_async;
3459
+ function $mol_wait_timeout(timeout) {
3460
+ return this.$mol_wire_sync(this).$mol_wait_timeout_async(timeout);
3461
+ }
3462
+ $.$mol_wait_timeout = $mol_wait_timeout;
3463
+ })($ || ($ = {}));
3464
+
3465
+ ;
3466
+ "use strict";
3467
+ var $;
3468
+ (function ($) {
3469
+ class $mol_run_error extends $mol_error_mix {
3470
+ }
3471
+ $.$mol_run_error = $mol_run_error;
3472
+ const child_process = $node['child_process'];
3473
+ $.$mol_run_spawn = child_process.spawn.bind(child_process);
3474
+ $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
3475
+ function $mol_run_async({ dir, timeout, command, env }) {
3476
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
3477
+ const [app, ...args] = args_raw;
3478
+ if (!env?.MOL_RUN_ASYNC) {
3479
+ this.$mol_log3_come({
3480
+ place: '$mol_run_sync',
3481
+ message: 'Run',
3482
+ command: args_raw.join(' '),
3483
+ dir: $node.path.relative('', dir),
3484
+ });
3485
+ return this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
3486
+ }
3487
+ const sub = this.$mol_run_spawn(app, args, {
3488
+ shell: true,
3489
+ cwd: dir,
3490
+ env
3491
+ });
3492
+ this.$mol_log3_come({
3493
+ place: '$mol_run_async',
3494
+ pid: sub.pid,
3495
+ message: 'Run',
3496
+ command: args_raw.join(' '),
3497
+ dir: $node.path.relative('', dir),
3498
+ });
3499
+ let killed = false;
3500
+ let timer;
3501
+ const std_data = [];
3502
+ const error_data = [];
3503
+ const add = (std_chunk, error_chunk) => {
3504
+ if (std_chunk)
3505
+ std_data.push(std_chunk);
3506
+ if (error_chunk)
3507
+ error_data.push(error_chunk);
3508
+ if (!timeout)
3509
+ return;
3510
+ clearTimeout(timer);
3511
+ timer = setTimeout(() => {
3512
+ const signal = killed ? 'SIGKILL' : 'SIGTERM';
3513
+ killed = true;
3514
+ add();
3515
+ sub.kill(signal);
3516
+ }, timeout);
3517
+ };
3518
+ add();
3519
+ sub.stdout?.on('data', data => add(data));
3520
+ sub.stderr?.on('data', data => add(undefined, data));
3521
+ const promise = new Promise((done, fail) => {
3522
+ const close = (error, status = null, signal = null) => {
3523
+ if (!timer && timeout)
3524
+ return;
3525
+ clearTimeout(timer);
3526
+ timer = undefined;
3527
+ const res = {
3528
+ pid: sub.pid,
3529
+ status,
3530
+ signal,
3531
+ get stdout() { return Buffer.concat(std_data); },
3532
+ get stderr() { return Buffer.concat(error_data); }
3533
+ };
3534
+ this.$mol_log3_done({
3535
+ place: '$mol_run_async',
3536
+ pid: sub.pid,
3537
+ message: 'Run',
3538
+ status,
3539
+ command: args_raw.join(' '),
3540
+ dir: $node.path.relative('', dir),
3541
+ });
3542
+ if (error || status || killed)
3543
+ return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
3544
+ done(res);
3545
+ };
3546
+ sub.on('disconnect', () => close(new Error('Disconnected')));
3547
+ sub.on('error', err => close(err));
3548
+ sub.on('exit', (status, signal) => close(null, status, signal));
3549
+ });
3550
+ return Object.assign(promise, { destructor: () => {
3551
+ clearTimeout(timer);
3552
+ sub.kill('SIGKILL');
3553
+ } });
3554
+ }
3555
+ $.$mol_run_async = $mol_run_async;
3556
+ function $mol_run(options) {
3557
+ if (!options.env)
3558
+ options = { ...options, env: this.$mol_env() };
3559
+ return $mol_wire_sync(this).$mol_run_async(options);
3560
+ }
3561
+ $.$mol_run = $mol_run;
3562
+ })($ || ($ = {}));
3563
+
3564
+ ;
3565
+ "use strict";
3566
+ var $;
3567
+ (function ($_1) {
3568
+ $mol_test({
3569
+ async 'exec timeout auto kill child process'($) {
3570
+ let close_mock = () => { };
3571
+ const context_mock = $.$mol_ambient({
3572
+ $mol_run_spawn: () => ({
3573
+ on(name, cb) {
3574
+ if (name === 'exit')
3575
+ close_mock = cb;
3576
+ },
3577
+ kill() { close_mock(); }
3578
+ })
3579
+ });
3580
+ let message = '';
3581
+ try {
3582
+ const res = await $mol_wire_async(context_mock).$mol_run({ command: 'sleep 10', dir: '.', timeout: 10, env: { 'MOL_RUN_ASYNC': '1' } });
3583
+ }
3584
+ catch (e) {
3585
+ message = e.message;
3586
+ }
3587
+ $mol_assert_equal(message, 'Run error, timeout');
3588
+ }
3589
+ });
3590
+ })($ || ($ = {}));
3591
+
3592
+ ;
3593
+ "use strict";
3594
+ var $;
3595
+ (function ($) {
3596
+ function $mol_exec(dir, command, ...args) {
3597
+ return this.$mol_run({ command: [command, ...args], dir });
3598
+ }
3599
+ $.$mol_exec = $mol_exec;
3600
+ })($ || ($ = {}));
3601
+
2314
3602
  ;
2315
3603
  "use strict";
2316
3604
  var $;