@mpcore/simulator 0.4.2 → 0.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.
package/dist/index.mjs CHANGED
@@ -582,9 +582,7 @@ function createHeadlessWx(driver) {
582
582
  showToast: (option) => invokeWxApi(() => driver.showToast(option), option),
583
583
  startPullDownRefresh: (option) => invokeWxApi(() => driver.startPullDownRefresh(), option),
584
584
  stopPullDownRefresh: () => driver.stopPullDownRefresh(),
585
- switchTab: (option) => invokeWxApi(() => {
586
- driver.switchTab(option);
587
- }, option),
585
+ switchTab: (option) => driver.switchTab(option),
588
586
  uploadFile: (option) => driver.uploadFile(option),
589
587
  removeTabBarBadge: (option) => invokeWxApi(() => driver.removeTabBarBadge(option), option),
590
588
  setTabBarBadge: (option) => invokeWxApi(() => driver.setTabBarBadge(option), option),
@@ -2238,6 +2236,22 @@ function renderBrowserPageTree(context, page) {
2238
2236
  };
2239
2237
  }
2240
2238
  //#endregion
2239
+ //#region src/host/wx/navigation.ts
2240
+ function invokePreparedNavigationApi(prepare, option) {
2241
+ try {
2242
+ const finalize = prepare()();
2243
+ try {
2244
+ option?.success?.();
2245
+ } finally {
2246
+ if (typeof finalize === "function") finalize();
2247
+ }
2248
+ option?.complete?.();
2249
+ } catch (error) {
2250
+ option?.fail?.(error);
2251
+ option?.complete?.();
2252
+ }
2253
+ }
2254
+ //#endregion
2241
2255
  //#region src/project/pageConfig.ts
2242
2256
  const DEFAULT_NAVIGATION_BAR_BACKGROUND_COLOR = "#ffffff";
2243
2257
  const DEFAULT_NAVIGATION_BAR_FRONT_COLOR = "#000000";
@@ -5445,7 +5459,7 @@ var BrowserHeadlessSession = class {
5445
5459
  showToast: (option) => this.wxState.showToast(option),
5446
5460
  startPullDownRefresh: () => this.startPullDownRefresh(),
5447
5461
  stopPullDownRefresh: () => this.stopPullDownRefresh(),
5448
- switchTab: (option) => this.switchTab(option.url),
5462
+ switchTab: (option) => invokePreparedNavigationApi(() => this.prepareSwitchTab(option.url), option),
5449
5463
  uploadFile: (option) => this.wxState.uploadFile(option),
5450
5464
  removeTabBarBadge: (option) => this.removeTabBarBadge(option.index),
5451
5465
  setTabBarBadge: (option) => this.setTabBarBadge(option.index, option.text),
@@ -5990,22 +6004,26 @@ var BrowserHeadlessSession = class {
5990
6004
  return nextPage;
5991
6005
  }
5992
6006
  switchTab(url) {
6007
+ this.prepareSwitchTab(url)()();
6008
+ return this.currentPageInstance;
6009
+ }
6010
+ prepareSwitchTab(url) {
5993
6011
  const target = this.resolveNavigationTarget(url);
5994
- this.bootstrap(createAppLaunchOptions$1(target.normalizedRoute, target.query));
5995
6012
  if (Object.keys(target.query).length > 0) throw new Error(`wx.switchTab() url cannot contain query in browser simulator runtime: ${url}`);
5996
6013
  if (!this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.switchTab() can only open a tabBar page in browser simulator runtime: ${url}`);
6014
+ this.bootstrap(createAppLaunchOptions$1(target.normalizedRoute, target.query));
6015
+ return () => this.commitSwitchTab(target);
6016
+ }
6017
+ commitSwitchTab(target) {
5997
6018
  const current = this.currentPageInstance;
5998
6019
  const cachedTarget = this.tabPages.get(target.routeRecord.route) ?? null;
5999
- if (current === cachedTarget && current) return current;
6020
+ if (current === cachedTarget && current) return () => {};
6000
6021
  if (current && current !== cachedTarget) {
6001
6022
  current.onHide?.();
6002
6023
  this.runPageComponentLifetime(current.route, "hide");
6003
6024
  }
6004
- for (const page of [...this.pages].reverse()) {
6005
- if (this.isTabBarRoute(stripLeadingSlash$1(page.route))) continue;
6006
- this.removePageInstance(page);
6007
- this.unloadPage(page);
6008
- }
6025
+ const pagesToUnload = [...this.pages].reverse().filter((page) => !this.isTabBarRoute(stripLeadingSlash$1(page.route)));
6026
+ for (const page of pagesToUnload) this.removePageInstance(page);
6009
6027
  let nextPage = cachedTarget;
6010
6028
  let shouldRunInitialLifecycles = false;
6011
6029
  if (!nextPage) {
@@ -6021,7 +6039,9 @@ var BrowserHeadlessSession = class {
6021
6039
  nextPage.onShow?.();
6022
6040
  this.runPageComponentLifetime(nextPage.route, "show");
6023
6041
  }
6024
- return nextPage;
6042
+ return () => {
6043
+ for (const page of pagesToUnload) this.unloadPage(page);
6044
+ };
6025
6045
  }
6026
6046
  pageScrollTo(option) {
6027
6047
  const current = this.requireCurrentPage("wx.pageScrollTo()");
@@ -7425,7 +7445,7 @@ var HeadlessSession = class {
7425
7445
  showToast: (option) => this.wxState.showToast(option),
7426
7446
  startPullDownRefresh: () => this.startPullDownRefresh(),
7427
7447
  stopPullDownRefresh: () => this.stopPullDownRefresh(),
7428
- switchTab: (option) => this.switchTab(option.url),
7448
+ switchTab: (option) => invokePreparedNavigationApi(() => this.prepareSwitchTab(option.url), option),
7429
7449
  uploadFile: (option) => this.wxState.uploadFile(option),
7430
7450
  removeTabBarBadge: (option) => this.removeTabBarBadge(option.index),
7431
7451
  setTabBarBadge: (option) => this.setTabBarBadge(option.index, option.text),
@@ -7944,22 +7964,26 @@ var HeadlessSession = class {
7944
7964
  return nextPage;
7945
7965
  }
7946
7966
  switchTab(url) {
7967
+ this.prepareSwitchTab(url)()();
7968
+ return this.currentPageInstance;
7969
+ }
7970
+ prepareSwitchTab(url) {
7947
7971
  const target = this.resolveNavigationTarget(url);
7948
- this.bootstrap(createAppLaunchOptions(target.normalizedRoute, target.query));
7949
7972
  if (Object.keys(target.query).length > 0) throw new Error(`wx.switchTab() url cannot contain query in headless runtime: ${url}`);
7950
7973
  if (!this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.switchTab() can only open a tabBar page in headless runtime: ${url}`);
7974
+ this.bootstrap(createAppLaunchOptions(target.normalizedRoute, target.query));
7975
+ return () => this.commitSwitchTab(target);
7976
+ }
7977
+ commitSwitchTab(target) {
7951
7978
  const current = this.currentPageInstance;
7952
7979
  const cachedTarget = this.tabPages.get(target.routeRecord.route) ?? null;
7953
- if (current === cachedTarget && current) return current;
7980
+ if (current === cachedTarget && current) return () => {};
7954
7981
  if (current && current !== cachedTarget) {
7955
7982
  current.onHide?.();
7956
7983
  this.runPageComponentLifetime(current.route, "hide");
7957
7984
  }
7958
- for (const page of [...this.pages].reverse()) {
7959
- if (this.isTabBarRoute(stripLeadingSlash(page.route))) continue;
7960
- this.removePageInstance(page);
7961
- this.unloadPage(page);
7962
- }
7985
+ const pagesToUnload = [...this.pages].reverse().filter((page) => !this.isTabBarRoute(stripLeadingSlash(page.route)));
7986
+ for (const page of pagesToUnload) this.removePageInstance(page);
7963
7987
  let nextPage = cachedTarget;
7964
7988
  let shouldRunInitialLifecycles = false;
7965
7989
  if (!nextPage) {
@@ -7975,7 +7999,9 @@ var HeadlessSession = class {
7975
7999
  nextPage.onShow?.();
7976
8000
  this.runPageComponentLifetime(nextPage.route, "show");
7977
8001
  }
7978
- return nextPage;
8002
+ return () => {
8003
+ for (const page of pagesToUnload) this.unloadPage(page);
8004
+ };
7979
8005
  }
7980
8006
  pageScrollTo(option) {
7981
8007
  const current = this.requireCurrentPage("wx.pageScrollTo()");
@@ -8721,6 +8747,9 @@ var HeadlessTestingSessionHandle = class {
8721
8747
  async close() {
8722
8748
  this.session.close();
8723
8749
  }
8750
+ async toolInfo() {
8751
+ return { version: "mpcore-simulator" };
8752
+ }
8724
8753
  on(eventName, handler) {
8725
8754
  this.session.on(eventName, handler);
8726
8755
  return this;
@@ -8836,30 +8865,23 @@ var HeadlessTestingSessionHandle = class {
8836
8865
  return page;
8837
8866
  }
8838
8867
  async evaluateInRuntime(evaluator, args) {
8839
- const runtimeGlobals = {
8868
+ const runtimeGlobals = createMiniProgramRuntimeGlobals({
8869
+ clearInterval: globalThis.clearInterval,
8870
+ clearTimeout: globalThis.clearTimeout,
8840
8871
  getApp: () => this.session.getApp(),
8841
8872
  getCurrentPages: () => this.session.getCurrentPages(),
8873
+ setInterval: globalThis.setInterval,
8874
+ setTimeout: globalThis.setTimeout,
8875
+ TextDecoder: globalThis.TextDecoder,
8876
+ TextEncoder: globalThis.TextEncoder,
8877
+ URL: globalThis.URL,
8842
8878
  wx: this.session.getWx()
8843
- };
8844
- const host = globalThis;
8845
- const previous = /* @__PURE__ */ new Map();
8846
- for (const [name, value] of Object.entries(runtimeGlobals)) {
8847
- previous.set(name, Object.getOwnPropertyDescriptor(host, name));
8848
- Object.defineProperty(host, name, {
8849
- configurable: true,
8850
- enumerable: false,
8851
- value,
8852
- writable: true
8853
- });
8854
- }
8855
- try {
8856
- const fn = typeof evaluator === "string" ? new vm.Script(`(${evaluator})`).runInThisContext() : evaluator;
8857
- if (typeof fn !== "function") throw new TypeError("Headless evaluator must be a function or function source string.");
8858
- return await fn(...args);
8859
- } finally {
8860
- for (const [name, descriptor] of previous) if (descriptor) Object.defineProperty(host, name, descriptor);
8861
- else delete host[name];
8862
- }
8879
+ }, {});
8880
+ runtimeGlobals.globalThis = runtimeGlobals;
8881
+ const source = typeof evaluator === "string" ? evaluator : Function.prototype.toString.call(evaluator);
8882
+ const fn = new vm.Script(`(${source})`).runInNewContext(runtimeGlobals);
8883
+ if (typeof fn !== "function") throw new TypeError("Headless evaluator must be a function or function source string.");
8884
+ return await fn(...args);
8863
8885
  }
8864
8886
  };
8865
8887
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mpcore/simulator",
3
3
  "type": "module",
4
- "version": "0.4.2",
4
+ "version": "0.4.3",
5
5
  "description": "Headless mini-program runtime and testing kernel for the weapp-vite ecosystem",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",