@rsmax/framework-shared 1.0.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE +22 -0
  3. package/README.md +11 -0
  4. package/cjs/AppInstanceContext.d.ts +6 -0
  5. package/cjs/AppInstanceContext.js +13 -0
  6. package/cjs/ComponentInstanceContext.d.ts +3 -0
  7. package/cjs/ComponentInstanceContext.js +28 -0
  8. package/cjs/PageInstanceContext.d.ts +3 -0
  9. package/cjs/PageInstanceContext.js +28 -0
  10. package/cjs/PluginDriver.d.ts +21 -0
  11. package/cjs/PluginDriver.js +64 -0
  12. package/cjs/RuntimeOptions.d.ts +17 -0
  13. package/cjs/RuntimeOptions.js +54 -0
  14. package/cjs/createPageWrapper.d.ts +65 -0
  15. package/cjs/createPageWrapper.js +69 -0
  16. package/cjs/formatDisplayName.d.ts +1 -0
  17. package/cjs/formatDisplayName.js +14 -0
  18. package/cjs/hooks.d.ts +8 -0
  19. package/cjs/hooks.js +37 -0
  20. package/cjs/index.d.ts +13 -0
  21. package/cjs/index.js +51 -0
  22. package/cjs/lifecycle.d.ts +35 -0
  23. package/cjs/lifecycle.js +59 -0
  24. package/cjs/promisify.d.ts +5 -0
  25. package/cjs/promisify.js +24 -0
  26. package/cjs/shim.d.ts +2 -0
  27. package/cjs/shim.js +22 -0
  28. package/cjs/utils/capitalize.d.ts +1 -0
  29. package/cjs/utils/capitalize.js +6 -0
  30. package/cjs/utils/isClassComponent.d.ts +2 -0
  31. package/cjs/utils/isClassComponent.js +6 -0
  32. package/cjs/utils/lowercase.d.ts +1 -0
  33. package/cjs/utils/lowercase.js +6 -0
  34. package/esm/AppInstanceContext.d.ts +6 -0
  35. package/esm/AppInstanceContext.js +11 -0
  36. package/esm/ComponentInstanceContext.d.ts +3 -0
  37. package/esm/ComponentInstanceContext.js +3 -0
  38. package/esm/PageInstanceContext.d.ts +3 -0
  39. package/esm/PageInstanceContext.js +3 -0
  40. package/esm/PluginDriver.d.ts +21 -0
  41. package/esm/PluginDriver.js +61 -0
  42. package/esm/RuntimeOptions.d.ts +17 -0
  43. package/esm/RuntimeOptions.js +45 -0
  44. package/esm/createPageWrapper.d.ts +65 -0
  45. package/esm/createPageWrapper.js +40 -0
  46. package/esm/formatDisplayName.d.ts +1 -0
  47. package/esm/formatDisplayName.js +10 -0
  48. package/esm/hooks.d.ts +8 -0
  49. package/esm/hooks.js +27 -0
  50. package/esm/index.d.ts +13 -0
  51. package/esm/index.js +13 -0
  52. package/esm/lifecycle.d.ts +35 -0
  53. package/esm/lifecycle.js +50 -0
  54. package/esm/promisify.d.ts +5 -0
  55. package/esm/promisify.js +20 -0
  56. package/esm/shim.d.ts +2 -0
  57. package/esm/shim.js +17 -0
  58. package/esm/utils/capitalize.d.ts +1 -0
  59. package/esm/utils/capitalize.js +3 -0
  60. package/esm/utils/isClassComponent.d.ts +2 -0
  61. package/esm/utils/isClassComponent.js +3 -0
  62. package/esm/utils/lowercase.d.ts +1 -0
  63. package/esm/utils/lowercase.js +3 -0
  64. package/package.json +36 -0
  65. package/vitest.config.js +7 -0
@@ -0,0 +1,5 @@
1
+ export interface PromisifyArgs<SuccessArg, FailArg> {
2
+ success?: (args: SuccessArg) => void;
3
+ fail?: (args: FailArg) => void;
4
+ }
5
+ export declare function promisify<Arg = any, SuccessArg = any, FailArg = any>(api: (arg: Arg & PromisifyArgs<SuccessArg, FailArg>) => void): (arg?: Arg & PromisifyArgs<SuccessArg, FailArg>) => Promise<SuccessArg>;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.promisify = void 0;
4
+ function promisify(api) {
5
+ // todo 补充类型注释
6
+ // @ts-ignore
7
+ return (arg = {}) => {
8
+ return new Promise((resolve, reject) => {
9
+ const promisifyArg = arg;
10
+ api(Object.assign(Object.assign({}, promisifyArg), { success: (res) => {
11
+ if (promisifyArg && typeof promisifyArg.success === 'function') {
12
+ promisifyArg.success(res);
13
+ }
14
+ resolve(res);
15
+ }, fail: (res) => {
16
+ if (promisifyArg && typeof promisifyArg.fail === 'function') {
17
+ promisifyArg.fail(res);
18
+ }
19
+ reject(res);
20
+ } }));
21
+ });
22
+ };
23
+ }
24
+ exports.promisify = promisify;
package/cjs/shim.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function find<P>(list: P[], predicate: (value: P, i: number, list: P[]) => boolean): P | undefined;
2
+ export declare function includes<P>(list: P[], searchElement: any): boolean;
package/cjs/shim.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.includes = exports.find = void 0;
4
+ function find(list, predicate) {
5
+ for (let i = 0; i < list.length; i++) {
6
+ const value = list[i];
7
+ if (predicate(value, i, list)) {
8
+ return value;
9
+ }
10
+ }
11
+ }
12
+ exports.find = find;
13
+ function includes(list, searchElement) {
14
+ for (let i = 0; i < list.length; i++) {
15
+ const value = list[i];
16
+ if (value === searchElement) {
17
+ return true;
18
+ }
19
+ }
20
+ return false;
21
+ }
22
+ exports.includes = includes;
@@ -0,0 +1 @@
1
+ export default function capitalize(str: string): string;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function capitalize(str) {
4
+ return str.charAt(0).toUpperCase() + str.slice(1);
5
+ }
6
+ exports.default = capitalize;
@@ -0,0 +1,2 @@
1
+ import { ComponentClass } from 'react';
2
+ export default function isClassComponent(Component: any): Component is ComponentClass;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function isClassComponent(Component) {
4
+ return Component.prototype && typeof Component.prototype.render === 'function';
5
+ }
6
+ exports.default = isClassComponent;
@@ -0,0 +1 @@
1
+ export default function lowercase(str: string): string;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function lowercase(str) {
4
+ return str.charAt(0).toLowerCase() + str.slice(1);
5
+ }
6
+ exports.default = lowercase;
@@ -0,0 +1,6 @@
1
+ import { AppLifecycle } from './lifecycle';
2
+ declare const AppInstanceContext: {
3
+ lifecycleCallback: any;
4
+ registerLifecycle(lifecycle: AppLifecycle, callback: () => any): () => void;
5
+ };
6
+ export default AppInstanceContext;
@@ -0,0 +1,11 @@
1
+ const AppInstanceContext = {
2
+ lifecycleCallback: {},
3
+ registerLifecycle(lifecycle, callback) {
4
+ this.lifecycleCallback[lifecycle] = this.lifecycleCallback[lifecycle] || [];
5
+ this.lifecycleCallback[lifecycle].push(callback);
6
+ return () => {
7
+ this.lifecycleCallback[lifecycle].splice(this.lifecycleCallback[lifecycle].indexOf(callback), 1);
8
+ };
9
+ },
10
+ };
11
+ export default AppInstanceContext;
@@ -0,0 +1,3 @@
1
+ import * as React from 'react';
2
+ declare const ComponentInstanceContext: React.Context<any>;
3
+ export default ComponentInstanceContext;
@@ -0,0 +1,3 @@
1
+ import * as React from 'react';
2
+ const ComponentInstanceContext = React.createContext(null);
3
+ export default ComponentInstanceContext;
@@ -0,0 +1,3 @@
1
+ import * as React from 'react';
2
+ declare const PageInstanceContext: React.Context<any>;
3
+ export default PageInstanceContext;
@@ -0,0 +1,3 @@
1
+ import * as React from 'react';
2
+ const PageInstanceContext = React.createContext(null);
3
+ export default PageInstanceContext;
@@ -0,0 +1,21 @@
1
+ import type { RuntimePlugin } from '@rsmax/types';
2
+ export default class PluginDriver {
3
+ plugins: RuntimePlugin[];
4
+ constructor(plugins: RuntimePlugin[]);
5
+ onAppConfig(config: any): any;
6
+ onPageConfig({ config, page }: {
7
+ config: any;
8
+ page: string;
9
+ }): any;
10
+ onAppComponent(component: React.ComponentType): import("react").ComponentType<{}>;
11
+ onPageComponent({ component, page }: {
12
+ component: React.ComponentType;
13
+ page: string;
14
+ }): import("react").ComponentType<{}>;
15
+ onMiniComponent({ component, context }: {
16
+ component: React.ComponentType;
17
+ context: any;
18
+ }): import("react").ComponentType<{}>;
19
+ onCreateHostComponent<P>(component: React.ForwardRefExoticComponent<P & React.RefAttributes<any>> | React.ComponentType<P>): any;
20
+ onCreateHostComponentElement<P>(element: React.ReactElement<P>): import("react").ReactElement<P, string | import("react").JSXElementConstructor<any>>;
21
+ }
@@ -0,0 +1,61 @@
1
+ export default class PluginDriver {
2
+ constructor(plugins) {
3
+ this.plugins = plugins;
4
+ }
5
+ onAppConfig(config) {
6
+ return this.plugins.reduce((acc, plugin) => {
7
+ if (typeof plugin.onAppConfig === 'function') {
8
+ acc = plugin.onAppConfig({ config: acc });
9
+ }
10
+ return acc;
11
+ }, config);
12
+ }
13
+ onPageConfig({ config, page }) {
14
+ return this.plugins.reduce((acc, plugin) => {
15
+ if (typeof plugin.onPageConfig === 'function') {
16
+ acc = plugin.onPageConfig({ config: acc, page });
17
+ }
18
+ return acc;
19
+ }, config);
20
+ }
21
+ onAppComponent(component) {
22
+ return this.plugins.reduce((acc, plugin) => {
23
+ if (typeof plugin.onAppComponent === 'function') {
24
+ acc = plugin.onAppComponent({ component: acc });
25
+ }
26
+ return acc;
27
+ }, component);
28
+ }
29
+ onPageComponent({ component, page }) {
30
+ return this.plugins.reduce((acc, plugin) => {
31
+ if (typeof plugin.onPageComponent === 'function') {
32
+ acc = plugin.onPageComponent({ component: acc, page });
33
+ }
34
+ return acc;
35
+ }, component);
36
+ }
37
+ onMiniComponent({ component, context }) {
38
+ return this.plugins.reduce((acc, plugin) => {
39
+ if (typeof plugin.onMiniComponent === 'function') {
40
+ acc = plugin.onMiniComponent({ component: acc, context });
41
+ }
42
+ return acc;
43
+ }, component);
44
+ }
45
+ onCreateHostComponent(component) {
46
+ return this.plugins.reduce((acc, plugin) => {
47
+ if (typeof plugin.onCreateHostComponent === 'function') {
48
+ acc = plugin.onCreateHostComponent({ component: acc });
49
+ }
50
+ return acc;
51
+ }, component);
52
+ }
53
+ onCreateHostComponentElement(element) {
54
+ return this.plugins.reduce((acc, plugin) => {
55
+ if (typeof plugin.onCreateHostComponentElement === 'function') {
56
+ acc = plugin.onCreateHostComponentElement({ element: acc });
57
+ }
58
+ return acc;
59
+ }, element);
60
+ }
61
+ }
@@ -0,0 +1,17 @@
1
+ import type { HostComponent, Platform } from '@rsmax/types';
2
+ import PluginDriver from './PluginDriver';
3
+ interface RuntimeOptions {
4
+ platform?: Platform | '';
5
+ pxToRpx: boolean;
6
+ debug: boolean;
7
+ hostComponents: Record<string, HostComponent>;
8
+ pluginDriver: PluginDriver;
9
+ pageEvents: Record<string, string[]>;
10
+ appEvents: string[];
11
+ history: any;
12
+ mpa: boolean;
13
+ }
14
+ export declare function apply(options: Partial<RuntimeOptions>): void;
15
+ export declare function get<K extends keyof RuntimeOptions>(key: K): RuntimeOptions[K];
16
+ export declare function reset(): void;
17
+ export {};
@@ -0,0 +1,45 @@
1
+ import PluginDriver from './PluginDriver';
2
+ const defaultRuntimeOptions = {
3
+ pxToRpx: true,
4
+ hostComponents: {},
5
+ debug: false,
6
+ appEvents: [],
7
+ pageEvents: {},
8
+ pluginDriver: new PluginDriver([]),
9
+ history: {},
10
+ mpa: false,
11
+ };
12
+ let runtimeOptions = defaultRuntimeOptions;
13
+ function merge(...options) {
14
+ return options.reduce((acc, option) => {
15
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
16
+ acc.appEvents = (_a = option.appEvents) !== null && _a !== void 0 ? _a : acc.appEvents;
17
+ acc.debug = (_b = option.debug) !== null && _b !== void 0 ? _b : acc.debug;
18
+ acc.history = (_c = option.history) !== null && _c !== void 0 ? _c : acc.history;
19
+ Object.keys((_d = option.hostComponents) !== null && _d !== void 0 ? _d : {}).forEach(k => {
20
+ var _a, _b, _c, _d, _e, _f, _g;
21
+ const inputHostComponent = (_a = option.hostComponents) === null || _a === void 0 ? void 0 : _a[k];
22
+ acc.hostComponents[k] = (_b = acc.hostComponents[k]) !== null && _b !== void 0 ? _b : {};
23
+ acc.hostComponents[k].additional = (_c = inputHostComponent === null || inputHostComponent === void 0 ? void 0 : inputHostComponent.additional) !== null && _c !== void 0 ? _c : acc.hostComponents[k].additional;
24
+ acc.hostComponents[k].alias = Object.assign((_d = acc.hostComponents[k].alias) !== null && _d !== void 0 ? _d : {}, (_e = inputHostComponent === null || inputHostComponent === void 0 ? void 0 : inputHostComponent.alias) !== null && _e !== void 0 ? _e : {});
25
+ acc.hostComponents[k].props = [
26
+ ...new Set([...((_f = acc.hostComponents[k].props) !== null && _f !== void 0 ? _f : []), ...((_g = inputHostComponent === null || inputHostComponent === void 0 ? void 0 : inputHostComponent.props) !== null && _g !== void 0 ? _g : [])]),
27
+ ];
28
+ });
29
+ acc.pluginDriver = (_e = option.pluginDriver) !== null && _e !== void 0 ? _e : acc.pluginDriver;
30
+ acc.pageEvents = (_f = option.pageEvents) !== null && _f !== void 0 ? _f : acc.pageEvents;
31
+ acc.platform = (_g = option.platform) !== null && _g !== void 0 ? _g : acc.platform;
32
+ acc.pxToRpx = (_h = option.pxToRpx) !== null && _h !== void 0 ? _h : acc.pxToRpx;
33
+ acc.mpa = (_j = option.mpa) !== null && _j !== void 0 ? _j : acc.mpa;
34
+ return acc;
35
+ }, runtimeOptions);
36
+ }
37
+ export function apply(options) {
38
+ runtimeOptions = merge(options);
39
+ }
40
+ export function get(key) {
41
+ return runtimeOptions[key];
42
+ }
43
+ export function reset() {
44
+ runtimeOptions = defaultRuntimeOptions;
45
+ }
@@ -0,0 +1,65 @@
1
+ import * as React from 'react';
2
+ import { Callback } from './lifecycle';
3
+ export interface PageProps<Q = Record<string, string | undefined>> {
4
+ location: {
5
+ query: Q;
6
+ };
7
+ }
8
+ export default function createPageWrapper(Page: React.ComponentType<any>, name: string): {
9
+ new (props: any): {
10
+ pageComponentInstance: any;
11
+ callbacks: Map<string, {
12
+ callbacks: Callback[];
13
+ }>;
14
+ callLifecycle(phase: string, ...args: any[]): any;
15
+ render(): React.FunctionComponentElement<React.ProviderProps<any>>;
16
+ context: unknown;
17
+ setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<{
18
+ page: any;
19
+ query: any;
20
+ }>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;
21
+ forceUpdate(callback?: (() => void) | undefined): void;
22
+ readonly props: Readonly<{
23
+ page: any;
24
+ query: any;
25
+ }>;
26
+ state: Readonly<{}>;
27
+ refs: {
28
+ [key: string]: React.ReactInstance;
29
+ };
30
+ componentDidMount?(): void;
31
+ shouldComponentUpdate?(nextProps: Readonly<{
32
+ page: any;
33
+ query: any;
34
+ }>, nextState: Readonly<{}>, nextContext: any): boolean;
35
+ componentWillUnmount?(): void;
36
+ componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
37
+ getSnapshotBeforeUpdate?(prevProps: Readonly<{
38
+ page: any;
39
+ query: any;
40
+ }>, prevState: Readonly<{}>): any;
41
+ componentDidUpdate?(prevProps: Readonly<{
42
+ page: any;
43
+ query: any;
44
+ }>, prevState: Readonly<{}>, snapshot?: any): void;
45
+ componentWillMount?(): void;
46
+ UNSAFE_componentWillMount?(): void;
47
+ componentWillReceiveProps?(nextProps: Readonly<{
48
+ page: any;
49
+ query: any;
50
+ }>, nextContext: any): void;
51
+ UNSAFE_componentWillReceiveProps?(nextProps: Readonly<{
52
+ page: any;
53
+ query: any;
54
+ }>, nextContext: any): void;
55
+ componentWillUpdate?(nextProps: Readonly<{
56
+ page: any;
57
+ query: any;
58
+ }>, nextState: Readonly<{}>, nextContext: any): void;
59
+ UNSAFE_componentWillUpdate?(nextProps: Readonly<{
60
+ page: any;
61
+ query: any;
62
+ }>, nextState: Readonly<{}>, nextContext: any): void;
63
+ };
64
+ contextType?: React.Context<any> | undefined;
65
+ };
@@ -0,0 +1,40 @@
1
+ import * as React from 'react';
2
+ import { ForwardRef } from 'react-is';
3
+ import isClassComponent from './utils/isClassComponent';
4
+ import { Lifecycle, callbackName } from './lifecycle';
5
+ import PageInstanceContext from './PageInstanceContext';
6
+ import * as RuntimeOptions from './RuntimeOptions';
7
+ export default function createPageWrapper(Page, name) {
8
+ const WrappedPage = RuntimeOptions.get('pluginDriver').onPageComponent({ component: Page, page: name });
9
+ return class PageWrapper extends React.Component {
10
+ constructor(props) {
11
+ super(props);
12
+ // 页面组件的实例
13
+ this.pageComponentInstance = null;
14
+ this.callbacks = new Map();
15
+ Object.keys(Lifecycle).forEach(phase => {
16
+ const callback = callbackName(phase);
17
+ this[callback] = (...args) => {
18
+ return this.callLifecycle(phase, ...args);
19
+ };
20
+ });
21
+ }
22
+ callLifecycle(phase, ...args) {
23
+ const callback = callbackName(phase);
24
+ if (this.pageComponentInstance && typeof this.pageComponentInstance[callback] === 'function') {
25
+ return this.pageComponentInstance[callback](...args);
26
+ }
27
+ }
28
+ render() {
29
+ const props = {
30
+ location: {
31
+ query: this.props.query || {},
32
+ },
33
+ };
34
+ if (isClassComponent(Page) || Page.$$typeof === ForwardRef) {
35
+ props.ref = (node) => (this.pageComponentInstance = node);
36
+ }
37
+ return React.createElement(PageInstanceContext.Provider, { value: this.props.page }, React.createElement(WrappedPage, props));
38
+ }
39
+ };
40
+ }
@@ -0,0 +1 @@
1
+ export declare function formatDisplayName(name: string): string;
@@ -0,0 +1,10 @@
1
+ export function formatDisplayName(name) {
2
+ return name
3
+ .replace(/-(.)/g, $1 => {
4
+ return $1.toUpperCase();
5
+ })
6
+ .replace(/-/g, '')
7
+ .replace(/^(.)/, $1 => {
8
+ return $1.toUpperCase();
9
+ });
10
+ }
package/esm/hooks.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { Callback } from './lifecycle';
2
+ export declare function usePageEvent(eventName: string, callback: Callback): void;
3
+ export declare function useComponentInstance(): any;
4
+ export declare function usePageInstance(): any;
5
+ /**
6
+ * App Hooks
7
+ */
8
+ export declare function useAppEvent(eventName: string, callback: Callback): void;
package/esm/hooks.js ADDED
@@ -0,0 +1,27 @@
1
+ import { useLayoutEffect, useContext } from 'react';
2
+ import { lifeCycleName, registerLifecycle } from './lifecycle';
3
+ import PageInstanceContext from './PageInstanceContext';
4
+ import ComponentInstanceContext from './ComponentInstanceContext';
5
+ import AppInstanceContext from './AppInstanceContext';
6
+ export function usePageEvent(eventName, callback) {
7
+ const pageInstance = useContext(PageInstanceContext);
8
+ const lifeCycle = lifeCycleName(eventName);
9
+ useLayoutEffect(() => {
10
+ return registerLifecycle(pageInstance, lifeCycle, callback);
11
+ });
12
+ }
13
+ export function useComponentInstance() {
14
+ return useContext(ComponentInstanceContext);
15
+ }
16
+ export function usePageInstance() {
17
+ return useContext(PageInstanceContext);
18
+ }
19
+ /**
20
+ * App Hooks
21
+ */
22
+ export function useAppEvent(eventName, callback) {
23
+ const lifeCycle = lifeCycleName(eventName);
24
+ useLayoutEffect(() => {
25
+ return registerLifecycle(AppInstanceContext, lifeCycle, callback);
26
+ });
27
+ }
package/esm/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import * as RuntimeOptions from './RuntimeOptions';
2
+ export { RuntimeOptions };
3
+ export { default as AppInstanceContext } from './AppInstanceContext';
4
+ export { default as PageInstanceContext } from './PageInstanceContext';
5
+ export { default as ComponentInstanceContext } from './ComponentInstanceContext';
6
+ export { default as createPageWrapper, PageProps } from './createPageWrapper';
7
+ export { default as PluginDriver } from './PluginDriver';
8
+ export { default as isClassComponent } from './utils/isClassComponent';
9
+ export * from './lifecycle';
10
+ export * from './hooks';
11
+ export * from './formatDisplayName';
12
+ export * from './promisify';
13
+ export * from './shim';
package/esm/index.js ADDED
@@ -0,0 +1,13 @@
1
+ import * as RuntimeOptions from './RuntimeOptions';
2
+ export { RuntimeOptions };
3
+ export { default as AppInstanceContext } from './AppInstanceContext';
4
+ export { default as PageInstanceContext } from './PageInstanceContext';
5
+ export { default as ComponentInstanceContext } from './ComponentInstanceContext';
6
+ export { default as createPageWrapper } from './createPageWrapper';
7
+ export { default as PluginDriver } from './PluginDriver';
8
+ export { default as isClassComponent } from './utils/isClassComponent';
9
+ export * from './lifecycle';
10
+ export * from './hooks';
11
+ export * from './formatDisplayName';
12
+ export * from './promisify';
13
+ export * from './shim';
@@ -0,0 +1,35 @@
1
+ export type Callback = (...args: any[]) => any;
2
+ export declare enum Lifecycle {
3
+ load = "load",
4
+ show = "show",
5
+ hide = "hide",
6
+ ready = "ready",
7
+ pullDownRefresh = "pullDownRefresh",
8
+ reachBottom = "reachBottom",
9
+ pageScroll = "pageScroll",
10
+ shareAppMessage = "shareAppMessage",
11
+ shareTimeline = "shareTimeline",
12
+ titleClick = "titleClick",
13
+ optionMenuClick = "optionMenuClick",
14
+ popMenuClick = "popMenuClick",
15
+ pullIntercept = "pullIntercept",
16
+ back = "back",
17
+ keyboardHeight = "keyboardHeight",
18
+ tabItemTap = "tabItemTap",
19
+ beforeTabItemTap = "beforeTabItemTap",
20
+ resize = "resize",
21
+ unload = "unload"
22
+ }
23
+ export declare enum AppLifecycle {
24
+ launch = "launch",
25
+ show = "show",
26
+ hide = "hide",
27
+ error = "error",
28
+ shareAppMessage = "shareAppMessage",
29
+ pageNotFound = "pageNotFound",
30
+ unhandledRejection = "unhandledRejection",
31
+ themeChange = "themeChange"
32
+ }
33
+ export declare function lifeCycleName(name: string): Lifecycle;
34
+ export declare function callbackName(name: string): string;
35
+ export declare function registerLifecycle(instance: any, method: Lifecycle | AppLifecycle, callback: Callback): any;
@@ -0,0 +1,50 @@
1
+ import capitalize from './utils/capitalize';
2
+ import lowercase from './utils/lowercase';
3
+ export var Lifecycle;
4
+ (function (Lifecycle) {
5
+ Lifecycle["load"] = "load";
6
+ Lifecycle["show"] = "show";
7
+ Lifecycle["hide"] = "hide";
8
+ Lifecycle["ready"] = "ready";
9
+ Lifecycle["pullDownRefresh"] = "pullDownRefresh";
10
+ Lifecycle["reachBottom"] = "reachBottom";
11
+ Lifecycle["pageScroll"] = "pageScroll";
12
+ Lifecycle["shareAppMessage"] = "shareAppMessage";
13
+ Lifecycle["shareTimeline"] = "shareTimeline";
14
+ Lifecycle["titleClick"] = "titleClick";
15
+ Lifecycle["optionMenuClick"] = "optionMenuClick";
16
+ Lifecycle["popMenuClick"] = "popMenuClick";
17
+ Lifecycle["pullIntercept"] = "pullIntercept";
18
+ Lifecycle["back"] = "back";
19
+ Lifecycle["keyboardHeight"] = "keyboardHeight";
20
+ Lifecycle["tabItemTap"] = "tabItemTap";
21
+ Lifecycle["beforeTabItemTap"] = "beforeTabItemTap";
22
+ Lifecycle["resize"] = "resize";
23
+ Lifecycle["unload"] = "unload";
24
+ })(Lifecycle || (Lifecycle = {}));
25
+ export var AppLifecycle;
26
+ (function (AppLifecycle) {
27
+ AppLifecycle["launch"] = "launch";
28
+ AppLifecycle["show"] = "show";
29
+ AppLifecycle["hide"] = "hide";
30
+ AppLifecycle["error"] = "error";
31
+ AppLifecycle["shareAppMessage"] = "shareAppMessage";
32
+ AppLifecycle["pageNotFound"] = "pageNotFound";
33
+ AppLifecycle["unhandledRejection"] = "unhandledRejection";
34
+ AppLifecycle["themeChange"] = "themeChange";
35
+ })(AppLifecycle || (AppLifecycle = {}));
36
+ export function lifeCycleName(name) {
37
+ if (name.startsWith('before')) {
38
+ return name;
39
+ }
40
+ return lowercase(name.slice(2));
41
+ }
42
+ export function callbackName(name) {
43
+ if (name.startsWith('before')) {
44
+ return name;
45
+ }
46
+ return 'on' + capitalize(name);
47
+ }
48
+ export function registerLifecycle(instance, method, callback) {
49
+ return instance.registerLifecycle(method, callback);
50
+ }
@@ -0,0 +1,5 @@
1
+ export interface PromisifyArgs<SuccessArg, FailArg> {
2
+ success?: (args: SuccessArg) => void;
3
+ fail?: (args: FailArg) => void;
4
+ }
5
+ export declare function promisify<Arg = any, SuccessArg = any, FailArg = any>(api: (arg: Arg & PromisifyArgs<SuccessArg, FailArg>) => void): (arg?: Arg & PromisifyArgs<SuccessArg, FailArg>) => Promise<SuccessArg>;
@@ -0,0 +1,20 @@
1
+ export function promisify(api) {
2
+ // todo 补充类型注释
3
+ // @ts-ignore
4
+ return (arg = {}) => {
5
+ return new Promise((resolve, reject) => {
6
+ const promisifyArg = arg;
7
+ api(Object.assign(Object.assign({}, promisifyArg), { success: (res) => {
8
+ if (promisifyArg && typeof promisifyArg.success === 'function') {
9
+ promisifyArg.success(res);
10
+ }
11
+ resolve(res);
12
+ }, fail: (res) => {
13
+ if (promisifyArg && typeof promisifyArg.fail === 'function') {
14
+ promisifyArg.fail(res);
15
+ }
16
+ reject(res);
17
+ } }));
18
+ });
19
+ };
20
+ }
package/esm/shim.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function find<P>(list: P[], predicate: (value: P, i: number, list: P[]) => boolean): P | undefined;
2
+ export declare function includes<P>(list: P[], searchElement: any): boolean;
package/esm/shim.js ADDED
@@ -0,0 +1,17 @@
1
+ export function find(list, predicate) {
2
+ for (let i = 0; i < list.length; i++) {
3
+ const value = list[i];
4
+ if (predicate(value, i, list)) {
5
+ return value;
6
+ }
7
+ }
8
+ }
9
+ export function includes(list, searchElement) {
10
+ for (let i = 0; i < list.length; i++) {
11
+ const value = list[i];
12
+ if (value === searchElement) {
13
+ return true;
14
+ }
15
+ }
16
+ return false;
17
+ }
@@ -0,0 +1 @@
1
+ export default function capitalize(str: string): string;
@@ -0,0 +1,3 @@
1
+ export default function capitalize(str) {
2
+ return str.charAt(0).toUpperCase() + str.slice(1);
3
+ }
@@ -0,0 +1,2 @@
1
+ import { ComponentClass } from 'react';
2
+ export default function isClassComponent(Component: any): Component is ComponentClass;
@@ -0,0 +1,3 @@
1
+ export default function isClassComponent(Component) {
2
+ return Component.prototype && typeof Component.prototype.render === 'function';
3
+ }
@@ -0,0 +1 @@
1
+ export default function lowercase(str: string): string;
@@ -0,0 +1,3 @@
1
+ export default function lowercase(str) {
2
+ return str.charAt(0).toLowerCase() + str.slice(1);
3
+ }