@spcsn/taro-runtime 0.1.0 → 0.1.1

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/dist/index.cjs.js CHANGED
@@ -255,70 +255,68 @@ var RuntimeCache = class {
255
255
  //#region src/bom/history.ts
256
256
  const cache$1 = new RuntimeCache("history");
257
257
  var TaroHistory = class extends _spcsn_taro_shared.Events {
258
- #location;
259
- #stack = [];
260
- #cur = 0;
261
- #window;
262
258
  constructor(location, options) {
263
259
  super();
264
- this.#window = options.window;
265
- this.#location = location;
266
- this.#location.on("__record_history__", (href) => {
267
- this.#cur++;
268
- this.#stack = this.#stack.slice(0, this.#cur);
269
- this.#stack.push({
260
+ this._stack = [];
261
+ this._cur = 0;
262
+ this._window = options.window;
263
+ this._location = location;
264
+ this._location.on("__record_history__", (href) => {
265
+ this._cur++;
266
+ this._stack = this._stack.slice(0, this._cur);
267
+ this._stack.push({
270
268
  state: null,
271
269
  title: "",
272
270
  url: href
273
271
  });
274
272
  }, null);
275
- this.#location.on("__reset_history__", (href) => {
276
- this.#reset(href);
273
+ this._location.on("__reset_history__", (href) => {
274
+ this._reset(href);
277
275
  }, null);
278
276
  this.on("0", () => {
279
- this.#reset();
277
+ this._reset();
280
278
  }, null);
281
279
  this.on("1", (pageId) => {
282
280
  cache$1.set(pageId, {
283
- location: this.#location,
284
- stack: this.#stack.slice(),
285
- cur: this.#cur
281
+ location: this._location,
282
+ stack: this._stack.slice(),
283
+ cur: this._cur
286
284
  });
287
285
  }, null);
288
286
  this.on("2", (pageId) => {
289
287
  if (cache$1.has(pageId)) {
290
288
  const ctx = cache$1.get(pageId);
291
- this.#location = ctx.location;
292
- this.#stack = ctx.stack;
293
- this.#cur = ctx.cur;
289
+ this._location = ctx.location;
290
+ this._stack = ctx.stack;
291
+ this._cur = ctx.cur;
294
292
  }
295
293
  }, null);
296
294
  this.on("3", (pageId) => {
297
295
  cache$1.delete(pageId);
298
296
  }, null);
299
- this.#reset();
297
+ this._reset();
300
298
  }
301
- #reset(href = "") {
302
- this.#stack = [{
299
+ _reset(href = "") {
300
+ this._stack = [{
303
301
  state: null,
304
302
  title: "",
305
- url: href || this.#location.href
303
+ url: href || this._location.href
306
304
  }];
307
- this.#cur = 0;
305
+ this._cur = 0;
308
306
  }
309
307
  get length() {
310
- return this.#stack.length;
308
+ return this._stack.length;
311
309
  }
312
310
  get state() {
313
- return this.#stack[this.#cur].state;
311
+ return this._stack[this._cur].state;
314
312
  }
315
313
  go(delta) {
316
314
  if (!(0, _spcsn_taro_shared.isNumber)(delta) || isNaN(delta)) return;
317
- let targetIdx = this.#cur + delta;
315
+ let targetIdx = this._cur + delta;
318
316
  targetIdx = Math.min(Math.max(targetIdx, 0), this.length - 1);
319
- this.#cur = targetIdx;
320
- this.#location.trigger("__set_href_without_history__", this.#stack[this.#cur].url);
321
- this.#window.trigger("popstate", this.#stack[this.#cur]);
317
+ this._cur = targetIdx;
318
+ this._location.trigger("__set_href_without_history__", this._stack[this._cur].url);
319
+ this._window.trigger("popstate", this._stack[this._cur]);
322
320
  }
323
321
  back() {
324
322
  this.go(-1);
@@ -328,23 +326,23 @@ var TaroHistory = class extends _spcsn_taro_shared.Events {
328
326
  }
329
327
  pushState(state, title, url) {
330
328
  if (!url || !(0, _spcsn_taro_shared.isString)(url)) return;
331
- this.#stack = this.#stack.slice(0, this.#cur + 1);
332
- this.#stack.push({
329
+ this._stack = this._stack.slice(0, this._cur + 1);
330
+ this._stack.push({
333
331
  state,
334
332
  title,
335
333
  url
336
334
  });
337
- this.#cur = this.length - 1;
338
- this.#location.trigger("__set_href_without_history__", url);
335
+ this._cur = this.length - 1;
336
+ this._location.trigger("__set_href_without_history__", url);
339
337
  }
340
338
  replaceState(state, title, url) {
341
339
  if (!url || !(0, _spcsn_taro_shared.isString)(url)) return;
342
- this.#stack[this.#cur] = {
340
+ this._stack[this._cur] = {
343
341
  state,
344
342
  title,
345
343
  url
346
344
  };
347
- this.#location.trigger("__set_href_without_history__", url);
345
+ this._location.trigger("__set_href_without_history__", url);
348
346
  }
349
347
  get cache() {
350
348
  return cache$1;
@@ -390,10 +388,10 @@ function encode(str) {
390
388
  return encodeURIComponent(str).replace(findReg, replacer);
391
389
  }
392
390
  const URLSearchParams = process.env.TARO_PLATFORM === "web" ? env.window.URLSearchParams : class {
393
- #dict = Object.create(null);
394
391
  constructor(query) {
392
+ this._dict = Object.create(null);
395
393
  query ??= "";
396
- const dict = this.#dict;
394
+ const dict = this._dict;
397
395
  if (typeof query === "string") {
398
396
  if (query.charAt(0) === "?") query = query.slice(1);
399
397
  for (let pairs = query.split("&"), i = 0, length = pairs.length; i < length; i++) {
@@ -414,30 +412,30 @@ const URLSearchParams = process.env.TARO_PLATFORM === "web" ? env.window.URLSear
414
412
  else for (const key in query) appendTo(dict, key, query[key]);
415
413
  }
416
414
  append(name, value) {
417
- appendTo(this.#dict, name, value);
415
+ appendTo(this._dict, name, value);
418
416
  }
419
417
  delete(name) {
420
- delete this.#dict[name];
418
+ delete this._dict[name];
421
419
  }
422
420
  get(name) {
423
- const dict = this.#dict;
421
+ const dict = this._dict;
424
422
  return name in dict ? dict[name][0] : null;
425
423
  }
426
424
  getAll(name) {
427
- const dict = this.#dict;
425
+ const dict = this._dict;
428
426
  return name in dict ? dict[name].slice(0) : [];
429
427
  }
430
428
  has(name) {
431
- return name in this.#dict;
429
+ return name in this._dict;
432
430
  }
433
431
  keys() {
434
- return Object.keys(this.#dict);
432
+ return Object.keys(this._dict);
435
433
  }
436
434
  set(name, value) {
437
- this.#dict[name] = ["" + value];
435
+ this._dict[name] = ["" + value];
438
436
  }
439
437
  forEach(callback, thisArg) {
440
- const dict = this.#dict;
438
+ const dict = this._dict;
441
439
  Object.getOwnPropertyNames(dict).forEach(function(name) {
442
440
  dict[name].forEach(function(value) {
443
441
  callback.call(thisArg, value, name, this);
@@ -448,7 +446,7 @@ const URLSearchParams = process.env.TARO_PLATFORM === "web" ? env.window.URLSear
448
446
  return {};
449
447
  }
450
448
  toString() {
451
- const dict = this.#dict;
449
+ const dict = this._dict;
452
450
  const query = [];
453
451
  for (const key in dict) {
454
452
  const name = encode(key);
@@ -466,27 +464,26 @@ var TaroURL = class {
466
464
  static revokeObjectURL() {
467
465
  throw new Error("Oops, not support URL.revokeObjectURL() in miniprogram.");
468
466
  }
469
- #hash = "";
470
- #hostname = "";
471
- #pathname = "";
472
- #port = "";
473
- #protocol = "";
474
- #search;
475
467
  constructor(url, base) {
468
+ this._hash = "";
469
+ this._hostname = "";
470
+ this._pathname = "";
471
+ this._port = "";
472
+ this._protocol = "";
476
473
  if (!(0, _spcsn_taro_shared.isString)(url)) url = String(url);
477
474
  const { hash, hostname, pathname, port, protocol, search } = parseUrlBase(url, base);
478
- this.#hash = hash;
479
- this.#hostname = hostname;
480
- this.#pathname = pathname || "/";
481
- this.#port = port;
482
- this.#protocol = protocol;
483
- this.#search = new URLSearchParams(search);
475
+ this._hash = hash;
476
+ this._hostname = hostname;
477
+ this._pathname = pathname || "/";
478
+ this._port = port;
479
+ this._protocol = protocol;
480
+ this._search = new URLSearchParams(search);
484
481
  }
485
482
  get protocol() {
486
- return this.#protocol;
483
+ return this._protocol;
487
484
  }
488
485
  set protocol(val) {
489
- (0, _spcsn_taro_shared.isString)(val) && (this.#protocol = val.trim());
486
+ (0, _spcsn_taro_shared.isString)(val) && (this._protocol = val.trim());
490
487
  }
491
488
  get host() {
492
489
  return this.hostname + (this.port ? ":" + this.port : "");
@@ -500,19 +497,19 @@ var TaroURL = class {
500
497
  }
501
498
  }
502
499
  get hostname() {
503
- return this.#hostname;
500
+ return this._hostname;
504
501
  }
505
502
  set hostname(val) {
506
- val && (0, _spcsn_taro_shared.isString)(val) && (this.#hostname = val.trim());
503
+ val && (0, _spcsn_taro_shared.isString)(val) && (this._hostname = val.trim());
507
504
  }
508
505
  get port() {
509
- return this.#port;
506
+ return this._port;
510
507
  }
511
508
  set port(val) {
512
- (0, _spcsn_taro_shared.isString)(val) && (this.#port = val.trim());
509
+ (0, _spcsn_taro_shared.isString)(val) && (this._port = val.trim());
513
510
  }
514
511
  get pathname() {
515
- return this.#pathname;
512
+ return this._pathname;
516
513
  }
517
514
  set pathname(val) {
518
515
  if ((0, _spcsn_taro_shared.isString)(val)) {
@@ -520,28 +517,28 @@ var TaroURL = class {
520
517
  const HEAD_REG = /^(\/|\.\/|\.\.\/)/;
521
518
  let temp = val;
522
519
  while (HEAD_REG.test(temp)) temp = temp.replace(HEAD_REG, "");
523
- if (temp) this.#pathname = "/" + temp;
524
- else this.#pathname = "/";
520
+ if (temp) this._pathname = "/" + temp;
521
+ else this._pathname = "/";
525
522
  }
526
523
  }
527
524
  get search() {
528
- const val = this.#search.toString();
525
+ const val = this._search.toString();
529
526
  return val.length === 0 || val.startsWith("?") ? val : `?${val}`;
530
527
  }
531
528
  set search(val) {
532
529
  if ((0, _spcsn_taro_shared.isString)(val)) {
533
530
  val = val.trim();
534
- this.#search = new URLSearchParams(val);
531
+ this._search = new URLSearchParams(val);
535
532
  }
536
533
  }
537
534
  get hash() {
538
- return this.#hash;
535
+ return this._hash;
539
536
  }
540
537
  set hash(val) {
541
538
  if ((0, _spcsn_taro_shared.isString)(val)) {
542
539
  val = val.trim();
543
- if (val) this.#hash = val.startsWith("#") ? val : `#${val}`;
544
- else this.#hash = "";
540
+ if (val) this._hash = val.startsWith("#") ? val : `#${val}`;
541
+ else this._hash = "";
545
542
  }
546
543
  }
547
544
  get href() {
@@ -572,7 +569,7 @@ var TaroURL = class {
572
569
  }
573
570
  }
574
571
  get searchParams() {
575
- return this.#search;
572
+ return this._search;
576
573
  }
577
574
  toString() {
578
575
  return this.href;
@@ -644,22 +641,21 @@ function parseUrlBase(url, base) {
644
641
  const INIT_URL = "https://taro.com";
645
642
  const cache = new RuntimeCache("location");
646
643
  var TaroLocation = class extends _spcsn_taro_shared.Events {
647
- #url = new TaroURLProvider(INIT_URL);
648
- #noCheckUrl = false;
649
- #window;
650
644
  constructor(options) {
651
645
  super();
652
- this.#window = options.window;
653
- this.#reset();
646
+ this._url = new TaroURLProvider(INIT_URL);
647
+ this._noCheckUrl = false;
648
+ this._window = options.window;
649
+ this._reset();
654
650
  this.on("__set_href_without_history__", (href) => {
655
- this.#noCheckUrl = true;
656
- const lastHash = this.#url.hash;
657
- this.#url.href = generateFullUrl(href);
658
- if (lastHash !== this.#url.hash) this.#window.trigger("hashchange");
659
- this.#noCheckUrl = false;
651
+ this._noCheckUrl = true;
652
+ const lastHash = this._url.hash;
653
+ this._url.href = generateFullUrl(href);
654
+ if (lastHash !== this._url.hash) this._window.trigger("hashchange");
655
+ this._noCheckUrl = false;
660
656
  }, null);
661
657
  this.on("0", () => {
662
- this.#reset();
658
+ this._reset();
663
659
  }, null);
664
660
  this.on("1", (pageId) => {
665
661
  cache.set(pageId, { lastHref: this.href });
@@ -667,16 +663,16 @@ var TaroLocation = class extends _spcsn_taro_shared.Events {
667
663
  this.on("2", (pageId) => {
668
664
  if (cache.has(pageId)) {
669
665
  const ctx = cache.get(pageId);
670
- this.#noCheckUrl = true;
671
- this.#url.href = ctx.lastHref;
672
- this.#noCheckUrl = false;
666
+ this._noCheckUrl = true;
667
+ this._url.href = ctx.lastHref;
668
+ this._noCheckUrl = false;
673
669
  }
674
670
  }, null);
675
671
  this.on("3", (pageId) => {
676
672
  cache.delete(pageId);
677
673
  }, null);
678
674
  }
679
- #reset() {
675
+ _reset() {
680
676
  const router = getCurrentInstance().router;
681
677
  if (router) {
682
678
  const { path, params } = router;
@@ -685,127 +681,127 @@ var TaroLocation = class extends _spcsn_taro_shared.Events {
685
681
  });
686
682
  const searchStr = searchArr.length > 0 ? "?" + searchArr.join("&") : "";
687
683
  const url = `${INIT_URL}${path.startsWith("/") ? path : "/" + path}${searchStr}`;
688
- this.#url = new TaroURLProvider(url);
684
+ this._url = new TaroURLProvider(url);
689
685
  this.trigger("__reset_history__", this.href);
690
686
  }
691
687
  }
692
- #getPreValue() {
693
- return this.#url._toRaw();
688
+ _getPreValue() {
689
+ return this._url._toRaw();
694
690
  }
695
- #rollBack(href) {
696
- this.#url.href = href;
691
+ _rollBack(href) {
692
+ this._url.href = href;
697
693
  }
698
- #recordHistory() {
694
+ _recordHistory() {
699
695
  this.trigger("__record_history__", this.href);
700
696
  }
701
697
  /**
702
698
  * 校验url的变化,是否需要更新history
703
699
  */
704
- #checkUrlChange(preValue) {
705
- if (this.#noCheckUrl) return false;
706
- const { protocol, hostname, port, pathname, search, hash } = this.#url._toRaw();
700
+ _checkUrlChange(preValue) {
701
+ if (this._noCheckUrl) return false;
702
+ const { protocol, hostname, port, pathname, search, hash } = this._url._toRaw();
707
703
  if (protocol !== preValue.protocol || hostname !== preValue.hostname || port !== preValue.port) {
708
- this.#rollBack(preValue.href);
704
+ this._rollBack(preValue.href);
709
705
  return false;
710
706
  }
711
707
  if (pathname !== preValue.pathname) return true;
712
708
  if (search !== preValue.search) return true;
713
709
  if (hash !== preValue.hash) {
714
- this.#window.trigger("hashchange");
710
+ this._window.trigger("hashchange");
715
711
  return true;
716
712
  }
717
- this.#rollBack(preValue.href);
713
+ this._rollBack(preValue.href);
718
714
  return false;
719
715
  }
720
716
  get protocol() {
721
- return this.#url.protocol;
717
+ return this._url.protocol;
722
718
  }
723
719
  set protocol(val) {
724
720
  if (!val || !(0, _spcsn_taro_shared.isString)(val) || !/^(http|https):$/i.test(val.trim())) return;
725
721
  val = val.trim();
726
- const preValue = this.#getPreValue();
727
- this.#url.protocol = val;
728
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
722
+ const preValue = this._getPreValue();
723
+ this._url.protocol = val;
724
+ if (this._checkUrlChange(preValue)) this._recordHistory();
729
725
  }
730
726
  get host() {
731
- return this.#url.host;
727
+ return this._url.host;
732
728
  }
733
729
  set host(val) {
734
730
  if (!val || !(0, _spcsn_taro_shared.isString)(val)) return;
735
731
  val = val.trim();
736
- const preValue = this.#getPreValue();
737
- this.#url.host = val;
738
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
732
+ const preValue = this._getPreValue();
733
+ this._url.host = val;
734
+ if (this._checkUrlChange(preValue)) this._recordHistory();
739
735
  }
740
736
  get hostname() {
741
- return this.#url.hostname;
737
+ return this._url.hostname;
742
738
  }
743
739
  set hostname(val) {
744
740
  if (!val || !(0, _spcsn_taro_shared.isString)(val)) return;
745
741
  val = val.trim();
746
- const preValue = this.#getPreValue();
747
- this.#url.hostname = val;
748
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
742
+ const preValue = this._getPreValue();
743
+ this._url.hostname = val;
744
+ if (this._checkUrlChange(preValue)) this._recordHistory();
749
745
  }
750
746
  get port() {
751
- return this.#url.port;
747
+ return this._url.port;
752
748
  }
753
749
  set port(val) {
754
750
  const xVal = Number(val = val.trim());
755
751
  if (!(0, _spcsn_taro_shared.isNumber)(xVal) || xVal <= 0) return;
756
- const preValue = this.#getPreValue();
757
- this.#url.port = val;
758
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
752
+ const preValue = this._getPreValue();
753
+ this._url.port = val;
754
+ if (this._checkUrlChange(preValue)) this._recordHistory();
759
755
  }
760
756
  get pathname() {
761
- return this.#url.pathname;
757
+ return this._url.pathname;
762
758
  }
763
759
  set pathname(val) {
764
760
  if (!val || !(0, _spcsn_taro_shared.isString)(val)) return;
765
761
  val = val.trim();
766
- const preValue = this.#getPreValue();
767
- this.#url.pathname = val;
768
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
762
+ const preValue = this._getPreValue();
763
+ this._url.pathname = val;
764
+ if (this._checkUrlChange(preValue)) this._recordHistory();
769
765
  }
770
766
  get search() {
771
- return this.#url.search;
767
+ return this._url.search;
772
768
  }
773
769
  set search(val) {
774
770
  if (!val || !(0, _spcsn_taro_shared.isString)(val)) return;
775
771
  val = val.trim();
776
772
  val = val.startsWith("?") ? val : `?${val}`;
777
- const preValue = this.#getPreValue();
778
- this.#url.search = val;
779
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
773
+ const preValue = this._getPreValue();
774
+ this._url.search = val;
775
+ if (this._checkUrlChange(preValue)) this._recordHistory();
780
776
  }
781
777
  get hash() {
782
- return this.#url.hash;
778
+ return this._url.hash;
783
779
  }
784
780
  set hash(val) {
785
781
  if (!val || !(0, _spcsn_taro_shared.isString)(val)) return;
786
782
  val = val.trim();
787
783
  val = val.startsWith("#") ? val : `#${val}`;
788
- const preValue = this.#getPreValue();
789
- this.#url.hash = val;
790
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
784
+ const preValue = this._getPreValue();
785
+ this._url.hash = val;
786
+ if (this._checkUrlChange(preValue)) this._recordHistory();
791
787
  }
792
788
  get href() {
793
- return this.#url.href;
789
+ return this._url.href;
794
790
  }
795
791
  set href(val) {
796
792
  if (!val || !(0, _spcsn_taro_shared.isString)(val) || !/^(http:|https:)?\/\/.+/.test(val = val.trim())) return;
797
- const preValue = this.#getPreValue();
798
- this.#url.href = val;
799
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
793
+ const preValue = this._getPreValue();
794
+ this._url.href = val;
795
+ if (this._checkUrlChange(preValue)) this._recordHistory();
800
796
  }
801
797
  get origin() {
802
- return this.#url.origin;
798
+ return this._url.origin;
803
799
  }
804
800
  set origin(val) {
805
801
  if (!val || !(0, _spcsn_taro_shared.isString)(val) || !/^(http:|https:)?\/\/.+/.test(val = val.trim())) return;
806
- const preValue = this.#getPreValue();
807
- this.#url.origin = val;
808
- if (this.#checkUrlChange(preValue)) this.#recordHistory();
802
+ const preValue = this._getPreValue();
803
+ this._url.origin = val;
804
+ if (this._checkUrlChange(preValue)) this._recordHistory();
809
805
  }
810
806
  assign() {
811
807
  (0, _spcsn_taro_shared.warn)(true, "小程序环境中调用location.assign()无效.");
@@ -2384,7 +2380,7 @@ var Performance = class {
2384
2380
  if (!(prev >= 0)) return;
2385
2381
  this.recorder.delete(id);
2386
2382
  const time = now - prev;
2387
- console.log(`${id} 时长: ${time}ms 开始时间:${this.#parseTime(prev)} 结束时间:${this.#parseTime(now)}`);
2383
+ console.log(`${id} 时长: ${time}ms 开始时间:${this.parseTime(prev)} 结束时间:${this.parseTime(now)}`);
2388
2384
  }
2389
2385
  delayStop(id, delay = 500) {
2390
2386
  if (!options.debug) return;
@@ -2393,7 +2389,7 @@ var Performance = class {
2393
2389
  cb?.();
2394
2390
  }, delay);
2395
2391
  }
2396
- #parseTime(time) {
2392
+ parseTime(time) {
2397
2393
  const d = new Date(time);
2398
2394
  return `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}.${`${d.getMilliseconds()}`.padStart(3, "0")}`;
2399
2395
  }