@heathenjs/taro-router 1.0.0

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.
Files changed (62) hide show
  1. package/dist/constants.d.ts +2 -0
  2. package/dist/constants.js +6 -0
  3. package/dist/constants.js.map +1 -0
  4. package/dist/exception/index.d.ts +1 -0
  5. package/dist/exception/index.js +6 -0
  6. package/dist/exception/index.js.map +1 -0
  7. package/dist/exception/no-page.d.ts +3 -0
  8. package/dist/exception/no-page.js +10 -0
  9. package/dist/exception/no-page.js.map +1 -0
  10. package/dist/func/getCurrentRouteKey.d.ts +1 -0
  11. package/dist/func/getCurrentRouteKey.js +16 -0
  12. package/dist/func/getCurrentRouteKey.js.map +1 -0
  13. package/dist/func/index.d.ts +2 -0
  14. package/dist/func/index.js +19 -0
  15. package/dist/func/index.js.map +1 -0
  16. package/dist/func/isNil.d.ts +1 -0
  17. package/dist/func/isNil.js +7 -0
  18. package/dist/func/isNil.js.map +1 -0
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.js +18 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/lib/compose.d.ts +9 -0
  23. package/dist/lib/compose.js +46 -0
  24. package/dist/lib/compose.js.map +1 -0
  25. package/dist/middleware/index.d.ts +10 -0
  26. package/dist/middleware/index.js +50 -0
  27. package/dist/middleware/index.js.map +1 -0
  28. package/dist/middleware/type.d.ts +13 -0
  29. package/dist/middleware/type.js +3 -0
  30. package/dist/middleware/type.js.map +1 -0
  31. package/dist/page-data/index.d.ts +15 -0
  32. package/dist/page-data/index.js +52 -0
  33. package/dist/page-data/index.js.map +1 -0
  34. package/dist/router/index.d.ts +55 -0
  35. package/dist/router/index.js +201 -0
  36. package/dist/router/index.js.map +1 -0
  37. package/dist/router/type.d.ts +35 -0
  38. package/dist/router/type.js +15 -0
  39. package/dist/router/type.js.map +1 -0
  40. package/dist/router-back-listener/index.d.ts +7 -0
  41. package/dist/router-back-listener/index.js +21 -0
  42. package/dist/router-back-listener/index.js.map +1 -0
  43. package/dist/router-back-listener/type.d.ts +2 -0
  44. package/dist/router-back-listener/type.js +3 -0
  45. package/dist/router-back-listener/type.js.map +1 -0
  46. package/package.json +30 -0
  47. package/src/constants.ts +2 -0
  48. package/src/exception/index.ts +1 -0
  49. package/src/exception/no-page.ts +5 -0
  50. package/src/func/getCurrentRouteKey.ts +10 -0
  51. package/src/func/index.ts +2 -0
  52. package/src/func/isNil.ts +3 -0
  53. package/src/index.ts +12 -0
  54. package/src/lib/compose.ts +37 -0
  55. package/src/middleware/index.ts +40 -0
  56. package/src/middleware/type.ts +16 -0
  57. package/src/page-data/index.ts +67 -0
  58. package/src/router/index.ts +179 -0
  59. package/src/router/type.ts +38 -0
  60. package/src/router-back-listener/index.ts +21 -0
  61. package/src/router-back-listener/type.ts +3 -0
  62. package/tsconfig.json +25 -0
@@ -0,0 +1,2 @@
1
+ export declare const ROUTE_KEY = "route_key";
2
+ export declare const isBrowser: boolean;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBrowser = exports.ROUTE_KEY = void 0;
4
+ exports.ROUTE_KEY = 'route_key';
5
+ exports.isBrowser = typeof document !== 'undefined' && !!document.scripts;
6
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,SAAS,GAAG,WAAW,CAAC;AACxB,QAAA,SAAS,GAAG,OAAO,QAAQ,KAAK,WAAW,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC"}
@@ -0,0 +1 @@
1
+ export { NoPageException } from './no-page';
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NoPageException = void 0;
4
+ var no_page_1 = require("./no-page");
5
+ Object.defineProperty(exports, "NoPageException", { enumerable: true, get: function () { return no_page_1.NoPageException; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/exception/index.ts"],"names":[],"mappings":";;;AAAA,qCAA4C;AAAnC,0GAAA,eAAe,OAAA"}
@@ -0,0 +1,3 @@
1
+ export declare class NoPageException extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NoPageException = void 0;
4
+ class NoPageException extends Error {
5
+ constructor() {
6
+ super('没有页面可以回退了');
7
+ }
8
+ }
9
+ exports.NoPageException = NoPageException;
10
+ //# sourceMappingURL=no-page.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-page.js","sourceRoot":"","sources":["../../src/exception/no-page.ts"],"names":[],"mappings":";;;AAAA,MAAa,eAAgB,SAAQ,KAAK;IACxC;QACE,KAAK,CAAC,WAAW,CAAC,CAAC;IACrB,CAAC;CACF;AAJD,0CAIC"}
@@ -0,0 +1 @@
1
+ export declare function getCurrentRouteKey(): string;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getCurrentRouteKey = getCurrentRouteKey;
7
+ const taro_1 = __importDefault(require("@tarojs/taro"));
8
+ const constants_1 = require("../constants");
9
+ function getCurrentRouteKey() {
10
+ if (!taro_1.default.Current.page) {
11
+ return '';
12
+ }
13
+ // TODO: taro官方缺少类型定义,as any过渡一下
14
+ return taro_1.default.Current.page[constants_1.ROUTE_KEY];
15
+ }
16
+ //# sourceMappingURL=getCurrentRouteKey.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getCurrentRouteKey.js","sourceRoot":"","sources":["../../src/func/getCurrentRouteKey.ts"],"names":[],"mappings":";;;;;AAGA,gDAMC;AATD,wDAAgC;AAChC,4CAAyC;AAEzC,SAAgB,kBAAkB;IAChC,IAAI,CAAC,cAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;QACvB,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,gCAAgC;IAChC,OAAQ,cAAI,CAAC,OAAO,CAAC,IAAY,CAAC,qBAAS,CAAC,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './getCurrentRouteKey';
2
+ export * from './isNil';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./getCurrentRouteKey"), exports);
18
+ __exportStar(require("./isNil"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/func/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAqC;AACrC,0CAAwB"}
@@ -0,0 +1 @@
1
+ export declare function isNil(val: any): boolean;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNil = isNil;
4
+ function isNil(val) {
5
+ return val === undefined || val === null;
6
+ }
7
+ //# sourceMappingURL=isNil.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isNil.js","sourceRoot":"","sources":["../../src/func/isNil.ts"],"names":[],"mappings":";;AAAA,sBAEC;AAFD,SAAgB,KAAK,CAAC,GAAQ;IAC5B,OAAO,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,IAAI,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { Router } from './router';
2
+ export { NoPageException } from './exception';
3
+ export { execMiddlewares, getMiddlewares, registerMiddleware, registerMiddlewares } from './middleware';
4
+ export { NavigateType, Router } from './router';
5
+ export { registerRouterBackListener } from './router-back-listener';
6
+ export type { Middleware, MiddlewareCondition, RouteContext } from './middleware';
7
+ export type { NavigateOptions, Route } from './router';
8
+ export type { RouterBackListener } from './router-back-listener';
9
+ export default Router;
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerRouterBackListener = exports.Router = exports.NavigateType = exports.registerMiddlewares = exports.registerMiddleware = exports.getMiddlewares = exports.execMiddlewares = exports.NoPageException = void 0;
4
+ const router_1 = require("./router");
5
+ var exception_1 = require("./exception");
6
+ Object.defineProperty(exports, "NoPageException", { enumerable: true, get: function () { return exception_1.NoPageException; } });
7
+ var middleware_1 = require("./middleware");
8
+ Object.defineProperty(exports, "execMiddlewares", { enumerable: true, get: function () { return middleware_1.execMiddlewares; } });
9
+ Object.defineProperty(exports, "getMiddlewares", { enumerable: true, get: function () { return middleware_1.getMiddlewares; } });
10
+ Object.defineProperty(exports, "registerMiddleware", { enumerable: true, get: function () { return middleware_1.registerMiddleware; } });
11
+ Object.defineProperty(exports, "registerMiddlewares", { enumerable: true, get: function () { return middleware_1.registerMiddlewares; } });
12
+ var router_2 = require("./router");
13
+ Object.defineProperty(exports, "NavigateType", { enumerable: true, get: function () { return router_2.NavigateType; } });
14
+ Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return router_2.Router; } });
15
+ var router_back_listener_1 = require("./router-back-listener");
16
+ Object.defineProperty(exports, "registerRouterBackListener", { enumerable: true, get: function () { return router_back_listener_1.registerRouterBackListener; } });
17
+ exports.default = router_1.Router;
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAAkC;AAElC,yCAA8C;AAArC,4GAAA,eAAe,OAAA;AACxB,2CAAwG;AAA/F,6GAAA,eAAe,OAAA;AAAE,4GAAA,cAAc,OAAA;AAAE,gHAAA,kBAAkB,OAAA;AAAE,iHAAA,mBAAmB,OAAA;AACjF,mCAAgD;AAAvC,sGAAA,YAAY,OAAA;AAAE,gGAAA,MAAM,OAAA;AAC7B,+DAAoE;AAA3D,kIAAA,0BAA0B,OAAA;AAMnC,kBAAe,eAAM,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Compose `middleware` returning
3
+ * a fully valid middleware comprised
4
+ * of all those which are passed.
5
+ *
6
+ * @param {Array} middleware
7
+ * @return {Function}
8
+ */
9
+ export declare function compose(middleware: Function[]): (context: any, next?: Function) => Promise<any>;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compose = compose;
4
+ /**
5
+ * Compose `middleware` returning
6
+ * a fully valid middleware comprised
7
+ * of all those which are passed.
8
+ *
9
+ * @param {Array} middleware
10
+ * @return {Function}
11
+ */
12
+ function compose(middleware) {
13
+ if (!Array.isArray(middleware))
14
+ throw new TypeError('Middleware stack must be an array!');
15
+ for (const fn of middleware) {
16
+ if (typeof fn !== 'function')
17
+ throw new TypeError('Middleware must be composed of functions!');
18
+ }
19
+ /**
20
+ * @param {Object} context
21
+ * @return {Promise}
22
+ * @api public
23
+ */
24
+ return function (context, next) {
25
+ // last called middleware #
26
+ let index = -1;
27
+ const dispatch = (i) => {
28
+ if (i <= index)
29
+ return Promise.reject(new Error('next() called multiple times'));
30
+ index = i;
31
+ let fn = middleware[i];
32
+ if (i === middleware.length)
33
+ fn = next;
34
+ if (!fn)
35
+ return Promise.resolve();
36
+ try {
37
+ return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
38
+ }
39
+ catch (err) {
40
+ return Promise.reject(err);
41
+ }
42
+ };
43
+ return dispatch(0);
44
+ };
45
+ }
46
+ //# sourceMappingURL=compose.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compose.js","sourceRoot":"","sources":["../../src/lib/compose.ts"],"names":[],"mappings":";;AAQA,0BA4BC;AApCD;;;;;;;GAOG;AACH,SAAgB,OAAO,CAAC,UAAsB;IAC5C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,oCAAoC,CAAC,CAAC;IAC1F,KAAK,MAAM,EAAE,IAAI,UAAU,EAAE,CAAC;QAC5B,IAAI,OAAO,EAAE,KAAK,UAAU;YAAE,MAAM,IAAI,SAAS,CAAC,2CAA2C,CAAC,CAAC;IACjG,CAAC;IAED;;;;OAIG;IACH,OAAO,UAAU,OAAY,EAAE,IAAe;QAC5C,2BAA2B;QAC3B,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC;QACf,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAgB,EAAE;YAC3C,IAAI,CAAC,IAAI,KAAK;gBAAE,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC,CAAC;YACjF,KAAK,GAAG,CAAC,CAAC;YACV,IAAI,EAAE,GAAyB,UAAU,CAAC,CAAC,CAAC,CAAC;YAC7C,IAAI,CAAC,KAAK,UAAU,CAAC,MAAM;gBAAE,EAAE,GAAG,IAAI,CAAC;YACvC,IAAI,CAAC,EAAE;gBAAE,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;YAClC,IAAI,CAAC;gBACH,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;YAClE,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAC7B,CAAC;QACH,CAAC,CAAC;QACF,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { Middleware, MiddlewareCondition, RouteContext } from './type';
2
+ export type { Middleware, RouteContext, MiddlewareCondition };
3
+ export declare const middlewareCollection: {
4
+ middlewares: Middleware[];
5
+ condition?: MiddlewareCondition;
6
+ }[];
7
+ export declare function registerMiddleware(middleware: Middleware, condition?: MiddlewareCondition): void;
8
+ export declare function registerMiddlewares(middlewares: Middleware[], condition?: MiddlewareCondition): void;
9
+ export declare function getMiddlewares(ctx: RouteContext): Middleware[];
10
+ export declare function execMiddlewares(middlewares: Middleware[], ctx: RouteContext): Promise<any>;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.middlewareCollection = void 0;
13
+ exports.registerMiddleware = registerMiddleware;
14
+ exports.registerMiddlewares = registerMiddlewares;
15
+ exports.getMiddlewares = getMiddlewares;
16
+ exports.execMiddlewares = execMiddlewares;
17
+ const compose_1 = require("../lib/compose");
18
+ exports.middlewareCollection = [];
19
+ function registerMiddleware(middleware, condition) {
20
+ exports.middlewareCollection.push({
21
+ middlewares: [middleware],
22
+ condition,
23
+ });
24
+ }
25
+ function registerMiddlewares(middlewares, condition) {
26
+ exports.middlewareCollection.push({
27
+ middlewares,
28
+ condition,
29
+ });
30
+ }
31
+ function getMiddlewares(ctx) {
32
+ return exports.middlewareCollection
33
+ .filter((mc) => {
34
+ if (!mc.condition)
35
+ return true;
36
+ else
37
+ return mc.condition(ctx);
38
+ })
39
+ .map((mc) => mc.middlewares)
40
+ .reduce((pre, cur) => {
41
+ return [...pre, ...cur];
42
+ }, []);
43
+ }
44
+ function execMiddlewares(middlewares, ctx) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ const fn = (0, compose_1.compose)(middlewares);
47
+ return yield fn(ctx);
48
+ });
49
+ }
50
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAUA,gDAKC;AAED,kDAKC;AAED,wCAUC;AAED,0CAGC;AAvCD,4CAAyC;AAK5B,QAAA,oBAAoB,GAG3B,EAAE,CAAC;AAET,SAAgB,kBAAkB,CAAC,UAAsB,EAAE,SAA+B;IACxF,4BAAoB,CAAC,IAAI,CAAC;QACxB,WAAW,EAAE,CAAC,UAAU,CAAC;QACzB,SAAS;KACV,CAAC,CAAC;AACL,CAAC;AAED,SAAgB,mBAAmB,CAAC,WAAyB,EAAE,SAA+B;IAC5F,4BAAoB,CAAC,IAAI,CAAC;QACxB,WAAW;QACX,SAAS;KACV,CAAC,CAAC;AACL,CAAC;AAED,SAAgB,cAAc,CAAC,GAAiB;IAC9C,OAAO,4BAAoB;SACxB,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE;QACb,IAAI,CAAC,EAAE,CAAC,SAAS;YAAE,OAAO,IAAI,CAAC;;YAC1B,OAAO,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC,CAAC;SACD,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,WAAW,CAAC;SAC3B,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACnB,OAAO,CAAC,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC,CAAC;IAC1B,CAAC,EAAE,EAAE,CAAC,CAAC;AACX,CAAC;AAED,SAAsB,eAAe,CAAC,WAAyB,EAAE,GAAiB;;QAChF,MAAM,EAAE,GAAG,IAAA,iBAAO,EAAC,WAAW,CAAC,CAAC;QAChC,OAAO,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;CAAA"}
@@ -0,0 +1,13 @@
1
+ import { NavigateType, Route } from '../router/type';
2
+ export interface RouteContext<E = any> {
3
+ /** 目标路由 */
4
+ route: Route<E>;
5
+ /** 路由参数 */
6
+ params: any;
7
+ /** 跳转类型 */
8
+ type: NavigateType;
9
+ /** 路由所在包 */
10
+ packageName?: string;
11
+ }
12
+ export type Middleware<E = any> = (ctx: RouteContext<E>, next: () => Promise<any>) => Promise<void>;
13
+ export type MiddlewareCondition = (ctx: RouteContext) => boolean;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/middleware/type.ts"],"names":[],"mappings":""}
@@ -0,0 +1,15 @@
1
+ export declare class PageData {
2
+ private static pageData;
3
+ private static pagePromise;
4
+ private static backResult;
5
+ static getPageData<T = any>(default_value?: T): T;
6
+ private static delPageData;
7
+ private static delPagePromise;
8
+ static setPageData(route_key: string, data: any): void;
9
+ static setPagePromise(route_key: string, options: {
10
+ res: (val: any) => void;
11
+ rej: (err: any) => void;
12
+ }): void;
13
+ static emitBack(route_key: string): void;
14
+ static setBackResult(result: any): void;
15
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PageData = void 0;
4
+ const func_1 = require("../func");
5
+ class PageData {
6
+ static getPageData(default_value) {
7
+ const route_key = (0, func_1.getCurrentRouteKey)();
8
+ const result = PageData.pageData.get(route_key) || default_value;
9
+ return result;
10
+ }
11
+ static delPageData(route_key) {
12
+ PageData.pageData.delete(route_key);
13
+ }
14
+ static delPagePromise(route_key) {
15
+ PageData.pagePromise.delete(route_key);
16
+ }
17
+ static setPageData(route_key, data) {
18
+ this.pageData.set(route_key, data);
19
+ }
20
+ static setPagePromise(route_key, options) {
21
+ this.pagePromise.set(route_key, options);
22
+ }
23
+ static emitBack(route_key) {
24
+ const pme = PageData.pagePromise.get(route_key);
25
+ if (!pme)
26
+ return;
27
+ const result = PageData.backResult.get(route_key);
28
+ PageData.delPageData(route_key);
29
+ PageData.delPagePromise(route_key);
30
+ if (result) {
31
+ PageData.backResult.delete(route_key);
32
+ if (result instanceof Error) {
33
+ pme.rej(result);
34
+ }
35
+ else {
36
+ pme.res(result);
37
+ }
38
+ }
39
+ else {
40
+ pme.res(null);
41
+ }
42
+ }
43
+ static setBackResult(result) {
44
+ const route_key = (0, func_1.getCurrentRouteKey)();
45
+ PageData.backResult.set(route_key, result);
46
+ }
47
+ }
48
+ exports.PageData = PageData;
49
+ PageData.pageData = new Map();
50
+ PageData.pagePromise = new Map();
51
+ PageData.backResult = new Map();
52
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/page-data/index.ts"],"names":[],"mappings":";;;AAAA,kCAA6C;AAE7C,MAAa,QAAQ;IAYnB,MAAM,CAAC,WAAW,CAAU,aAAiB;QAC3C,MAAM,SAAS,GAAG,IAAA,yBAAkB,GAAE,CAAC;QACvC,MAAM,MAAM,GAAG,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,aAAa,CAAC;QACjE,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,MAAM,CAAC,WAAW,CAAC,SAAiB;QAC1C,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC;IAEO,MAAM,CAAC,cAAc,CAAC,SAAiB;QAC7C,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACzC,CAAC;IAED,MAAM,CAAC,WAAW,CAAC,SAAiB,EAAE,IAAS;QAC7C,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACrC,CAAC;IAED,MAAM,CAAC,cAAc,CACnB,SAAiB,EACjB,OAGC;QAED,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM,CAAC,QAAQ,CAAC,SAAiB;QAC/B,MAAM,GAAG,GAAG,QAAQ,CAAC,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAChD,IAAI,CAAC,GAAG;YAAE,OAAO;QACjB,MAAM,MAAM,GAAG,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAElD,QAAQ,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC;QAChC,QAAQ,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;QAEnC,IAAI,MAAM,EAAE,CAAC;YACX,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YACtC,IAAI,MAAM,YAAY,KAAK,EAAE,CAAC;gBAC5B,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YAClB,CAAC;iBAAM,CAAC;gBACN,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YAClB,CAAC;QACH,CAAC;aAAM,CAAC;YACN,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAChB,CAAC;IACH,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,MAAW;QAC9B,MAAM,SAAS,GAAG,IAAA,yBAAkB,GAAE,CAAC;QACvC,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IAC7C,CAAC;;AA/DH,4BAgEC;AA/DgB,iBAAQ,GAAqB,IAAI,GAAG,EAAE,CAAC;AACvC,oBAAW,GAMtB,IAAI,GAAG,EAAE,CAAC;AAEC,mBAAU,GAAqB,IAAI,GAAG,EAAE,CAAC"}
@@ -0,0 +1,55 @@
1
+ import { NavigateOptions, Route } from './type';
2
+ export { NavigateType } from './type';
3
+ export type { NavigateOptions, Route } from './type';
4
+ export declare class Router {
5
+ /**
6
+ * 页面跳转
7
+ * @param route 目标路由对象
8
+ * @param options 跳转选项
9
+ */
10
+ static navigate<T = any>(route: Route, options?: NavigateOptions): Promise<T>;
11
+ /**
12
+ * 返回上一个页面
13
+ * @param result 返回给上一个页面的数据,如果 result 是 Error 的实例,则是抛出异常给上一个页面
14
+ * @param options 其他选项
15
+ */
16
+ static back(result?: unknown, options?: {
17
+ /** 返回的页面数,如果 delta 大于现有页面数,则返回到首页。 */
18
+ delta?: number;
19
+ }): Promise<TaroGeneral.CallbackResult>;
20
+ /**
21
+ * 设置页面返回的数据
22
+ * 当物理键返回和左上角返回也需要带数据时会使用到
23
+ */
24
+ static setBackResult(result: any): void;
25
+ /**
26
+ * 获取页面携带过来的数据,混合顺序为 路由参数 > URL参数
27
+ * @param default_value 默认参数
28
+ */
29
+ static getParams<T extends Record<string, any> = {}>(): T;
30
+ /**
31
+ * 获取页面携带过来的数据,混合顺序为 路由参数 > URL参数
32
+ * @param default_value 默认参数
33
+ */
34
+ static useParams<T extends Record<string, any> = {}>(): T;
35
+ static setParams<T extends Record<string, any> = {}>(value: T): void;
36
+ /** [View page component](/Users/h3/Documents/project/front-end/taro-router/apps/demo/src/pages/main/index/index.tsx) */
37
+ static toIndex: <TBackData = any, TParams = unknown>(options?: NavigateOptions & Params<NoInfer<TParams>>) => Promise<TBackData>;
38
+ private static _pages;
39
+ static getPages: () => {
40
+ packageName: string;
41
+ dirName: string;
42
+ dirPath: string;
43
+ path: string;
44
+ pageFileName: string;
45
+ }[];
46
+ }
47
+ type NoInfer<T> = T extends infer U ? U : never;
48
+ type RequiredKeys<T> = {
49
+ [K in keyof T]-?: {} extends Pick<T, K> ? never : K;
50
+ }[keyof T];
51
+ type Params<P> = RequiredKeys<P> extends never ? {
52
+ params?: P;
53
+ } : {
54
+ params: P;
55
+ };
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.Router = exports.NavigateType = void 0;
36
+ const taro_1 = __importStar(require("@tarojs/taro"));
37
+ const constants_1 = require("../constants");
38
+ const no_page_1 = require("../exception/no-page");
39
+ const func_1 = require("../func");
40
+ const middleware_1 = require("../middleware");
41
+ const page_data_1 = require("../page-data");
42
+ const router_back_listener_1 = require("../router-back-listener");
43
+ const type_1 = require("./type");
44
+ const react_1 = require("react");
45
+ var type_2 = require("./type");
46
+ Object.defineProperty(exports, "NavigateType", { enumerable: true, get: function () { return type_2.NavigateType; } });
47
+ class Router {
48
+ /**
49
+ * 页面跳转
50
+ * @param route 目标路由对象
51
+ * @param options 跳转选项
52
+ */
53
+ static navigate(route, options) {
54
+ var _a, _b;
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ options = Object.assign({ type: type_1.NavigateType.navigateTo, params: {} }, options);
57
+ options.params = Object.assign({}, options.params);
58
+ const route_key = Date.now() + '';
59
+ // TODO: taro官方缺少类型定义,as any过渡一下
60
+ taro_1.Current['_page'] = taro_1.Current.page;
61
+ Object.defineProperties(taro_1.Current, {
62
+ page: {
63
+ set: function (page) {
64
+ if (page === undefined || page === null) {
65
+ this._page = page;
66
+ return;
67
+ }
68
+ if (!page[constants_1.ROUTE_KEY]) {
69
+ const originOnUnload = page.onUnload;
70
+ page.onUnload = function () {
71
+ originOnUnload && originOnUnload.apply(this);
72
+ page_data_1.PageData.emitBack(route_key);
73
+ setTimeout(() => (0, router_back_listener_1.execRouterBackListener)(route));
74
+ };
75
+ page[constants_1.ROUTE_KEY] = route_key;
76
+ }
77
+ this._page = page;
78
+ },
79
+ get: function () {
80
+ return this._page;
81
+ },
82
+ },
83
+ });
84
+ if (options.params) {
85
+ page_data_1.PageData.setPageData(route_key, options.params);
86
+ }
87
+ const pages = ((_a = Router === null || Router === void 0 ? void 0 : Router['_pages']) !== null && _a !== void 0 ? _a : []);
88
+ const context = {
89
+ route,
90
+ type: options.type,
91
+ params: options.params,
92
+ packageName: (_b = pages.find((page) => page.path === route.url)) === null || _b === void 0 ? void 0 : _b.packageName,
93
+ };
94
+ const middlewares = (0, middleware_1.getMiddlewares)(context);
95
+ middlewares.push((_, next) => __awaiter(this, void 0, void 0, function* () {
96
+ switch (options.type) {
97
+ case type_1.NavigateType.reLaunch:
98
+ yield taro_1.default.reLaunch({
99
+ url: route.url,
100
+ complete: options === null || options === void 0 ? void 0 : options.complete,
101
+ fail: options === null || options === void 0 ? void 0 : options.fail,
102
+ success: options === null || options === void 0 ? void 0 : options.success,
103
+ });
104
+ break;
105
+ case type_1.NavigateType.redirectTo:
106
+ yield taro_1.default.redirectTo({
107
+ url: route.url,
108
+ complete: options === null || options === void 0 ? void 0 : options.complete,
109
+ fail: options === null || options === void 0 ? void 0 : options.fail,
110
+ success: options === null || options === void 0 ? void 0 : options.success,
111
+ });
112
+ break;
113
+ case type_1.NavigateType.switchTab:
114
+ yield taro_1.default.switchTab({
115
+ url: route.url,
116
+ complete: options === null || options === void 0 ? void 0 : options.complete,
117
+ fail: options === null || options === void 0 ? void 0 : options.fail,
118
+ success: options === null || options === void 0 ? void 0 : options.success,
119
+ });
120
+ break;
121
+ default:
122
+ yield taro_1.default.navigateTo({
123
+ url: route.url,
124
+ complete: options === null || options === void 0 ? void 0 : options.complete,
125
+ fail: options === null || options === void 0 ? void 0 : options.fail,
126
+ success: options === null || options === void 0 ? void 0 : options.success,
127
+ });
128
+ break;
129
+ }
130
+ next();
131
+ }));
132
+ return new Promise((res, rej) => __awaiter(this, void 0, void 0, function* () {
133
+ try {
134
+ page_data_1.PageData.setPagePromise(route_key, { res, rej });
135
+ yield (0, middleware_1.execMiddlewares)(middlewares, context);
136
+ }
137
+ catch (err) {
138
+ rej(err);
139
+ }
140
+ }));
141
+ });
142
+ }
143
+ /**
144
+ * 返回上一个页面
145
+ * @param result 返回给上一个页面的数据,如果 result 是 Error 的实例,则是抛出异常给上一个页面
146
+ * @param options 其他选项
147
+ */
148
+ static back(result, options) {
149
+ if (!(0, func_1.isNil)(result)) {
150
+ page_data_1.PageData.setBackResult(result);
151
+ }
152
+ const currentPages = taro_1.default.getCurrentPages();
153
+ if (currentPages.length > 1) {
154
+ return taro_1.default.navigateBack(options);
155
+ }
156
+ throw new no_page_1.NoPageException();
157
+ }
158
+ /**
159
+ * 设置页面返回的数据
160
+ * 当物理键返回和左上角返回也需要带数据时会使用到
161
+ */
162
+ static setBackResult(result) {
163
+ page_data_1.PageData.setBackResult(result);
164
+ }
165
+ /**
166
+ * 获取页面携带过来的数据,混合顺序为 路由参数 > URL参数
167
+ * @param default_value 默认参数
168
+ */
169
+ static getParams() {
170
+ var _a;
171
+ const urlParams = Object.assign({}, (_a = (0, taro_1.getCurrentInstance)().router) === null || _a === void 0 ? void 0 : _a.params);
172
+ const routerParams = page_data_1.PageData.getPageData();
173
+ return Object.assign(Object.assign({}, urlParams), routerParams);
174
+ }
175
+ /**
176
+ * 获取页面携带过来的数据,混合顺序为 路由参数 > URL参数
177
+ * @param default_value 默认参数
178
+ */
179
+ static useParams() {
180
+ // eslint-disable-next-line react-hooks/rules-of-hooks
181
+ const { current } = (0, react_1.useRef)({
182
+ obj: {},
183
+ initialized: false,
184
+ });
185
+ if (current.initialized === false) {
186
+ current.obj = this.getParams();
187
+ current.initialized = true;
188
+ }
189
+ return current.obj;
190
+ }
191
+ static setParams(value) {
192
+ const route_key = (0, func_1.getCurrentRouteKey)();
193
+ page_data_1.PageData.setPageData(route_key, value);
194
+ }
195
+ }
196
+ exports.Router = Router;
197
+ /** [View page component](/Users/h3/Documents/project/front-end/taro-router/apps/demo/src/pages/main/index/index.tsx) */
198
+ Router.toIndex = function (options) { return Router.navigate({ url: "/pages/main/index/index" }, options); };
199
+ Router._pages = [{ "packageName": "main", "dirName": "index", "dirPath": "/Users/h3/Documents/project/front-end/taro-router/apps/demo/src/pages/main/index", "path": "/pages/main/index/index", "pageFileName": "index.tsx" }];
200
+ Router.getPages = () => { return Router._pages; };
201
+ //# sourceMappingURL=index.js.map