Skip to content

Commit

Permalink
✅ 异步方法通过。
Browse files Browse the repository at this point in the history
  • Loading branch information
hairyf committed Apr 2, 2021
1 parent ddf10f2 commit aaa4f09
Show file tree
Hide file tree
Showing 16 changed files with 27,057 additions and 9,555 deletions.
1 change: 0 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Vue, { VueConstructor } from 'vue';
export * from './lifecycle-hooks';
export * from '@vue/composition-api';
declare const _default: (Vue: VueConstructor<Vue>) => void;
export default _default;
3 changes: 0 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import VueCompositionAPI from '@vue/composition-api';
import compositionPatch from './applets-repair';
import { PLATFORM } from './global';
export * from './lifecycle-hooks';
export * from '@vue/composition-api';
export default (Vue) => {
const createHookMixinFuc = (lifecycle) => {
const containerName = `__${lifecycle.toLocaleUpperCase()}_HOOKS__`;
Expand Down Expand Up @@ -50,5 +48,4 @@ export default (Vue) => {
});
if (PLATFORM !== 'h5')
compositionPatch();
Vue.use(VueCompositionAPI);
};
16,947 changes: 16,931 additions & 16 deletions test-project/package-lock.json

Large diffs are not rendered by default.

15 changes: 2 additions & 13 deletions test-project/src/pages/index/List.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
<!--
* @Author: Mr.Mao
* @Date: 2021-03-21 15:30:59
* @LastEditTime: 2021-03-22 16:05:58
* @Date: 2021-03-22 14:26:49
* @LastEditTime: 2021-04-02 09:15:30
* @Description:
* @LastEditors: Mr.Mao
* @autograph: 任何一个傻子都能写出让电脑能懂的代码,而只有好的程序员可以写出让人能看懂的代码
-->
<template>
<div>list</div>
</template>
<script lang="ts">
import { defineComponent, onShow } from '../../uni-composition-api';
export default defineComponent({
setup() {
return {}
}
});
</script>
19 changes: 19 additions & 0 deletions test-project/src/pages/index/hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* @Author: Mr.Mao
* @Date: 2021-04-02 09:12:31
* @LastEditTime: 2021-04-02 09:16:47
* @Description:
* @LastEditors: Mr.Mao
* @autograph: 任何一个傻子都能写出让电脑能懂的代码,而只有好的程序员可以写出让人能看懂的代码
*/
export const useXX = () => {
const func =async () => {
try {
console.log('-----')
await Promise.resolve(123132)
} catch (error) {
console.log(error)
}
}
return {func}
}
23 changes: 9 additions & 14 deletions test-project/src/pages/index/index.vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
<!--
* @Author: Mr.Mao
* @Date: 2021-03-22 12:43:52
* @LastEditTime: 2021-04-02 01:22:42
* @LastEditTime: 2021-04-02 09:16:21
* @Description:
* @LastEditors: Mr.Mao
* @autograph: 任何一个傻子都能写出让电脑能懂的代码,而只有好的程序员可以写出让人能看懂的代码
-->
<template>
<div class="index">{{form.count}}</div>
<div class="index">index</div>
</template>

<script>
import { onLoad } from 'uni-composition-api';
import { reactive, defineComponent} from '@vue/composition-api'
export default defineComponent({
import { useXX } from "./hook.js";

export default {
components: {},
setup() {
onLoad(() => console.log('---onload---'));
const form = reactive({
count: 1
})
setTimeout(() => {
form.count++
}, 1000)
return { form };
const aaa = useXX()
aaa.func()
return { };
},
});
};
</script>

<style>
Expand Down
2 changes: 2 additions & 0 deletions test-project/src/uni-composition-api/applets-repair.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const compositionPatch: () => void;
export default compositionPatch;
43 changes: 43 additions & 0 deletions test-project/src/uni-composition-api/applets-repair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* @Author: Mr.Mao
* @Date: 2021-03-20 12:42:41
* @LastEditTime: 2021-03-20 16:54:21
* @Description: 小程序 compositionApi 补丁
* @LastEditors: Mr.Mao
* @autograph: 任何一个傻子都能写出让电脑能懂的代码,而只有好的程序员可以写出让人能看懂的代码
*/
import Vue from 'vue';
import { isRef, isReadonly } from '@vue/composition-api';
const compositionPatch = () => {
/** uni-app 会在 beforeUpdate 的时候调用这个函数来进行和微信进程数据的交换 */
const oldPatch = Vue.prototype.__patch__;
Vue.prototype.__patch__ = function (...arg) {
var _a;
if (this._computedWatchers === undefined) {
this._computedWatchers = {};
}
const currentData = this._data;
const rawBindings = (_a = this.__composition_api_state__) === null || _a === void 0 ? void 0 : _a.rawBindings;
if (rawBindings) {
Object.keys(rawBindings).forEach((key) => {
if (process.env.NODE_ENV === 'development') {
const vitem = rawBindings[key];
if (isRef(vitem)) {
/** 只读的值一般是计算属性,所以放到computed中,要是放到data中uni-app会尝试写,这样导致报错 */
if (isReadonly(vitem)) {
this._computedWatchers[key] = vitem;
}
else {
currentData[key] = vitem;
}
}
}
else {
currentData[key] = undefined;
}
});
}
oldPatch.call(this, ...arg);
};
};
export default compositionPatch;
3 changes: 3 additions & 0 deletions test-project/src/uni-composition-api/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** 当前环境类型 */
export declare type UniPlatforms = 'app-plus' | 'app-plus-nvue' | 'h5' | 'mp-weixin' | 'mp-alipay' | 'mp-baidu' | 'mp-toutiao' | 'mp-qq' | 'mp-360' | 'mp' | 'quickapp-webview' | 'quickapp-webview-union' | 'quickapp-webview-huawei' | undefined;
export declare const PLATFORM: UniPlatforms;
4 changes: 4 additions & 0 deletions test-project/src/uni-composition-api/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var _a;
export const PLATFORM = typeof process !== 'undefined'
? (_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.VUE_APP_PLATFORM
: undefined;
4 changes: 4 additions & 0 deletions test-project/src/uni-composition-api/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Vue, { VueConstructor } from 'vue';
export * from './lifecycle-hooks';
declare const _default: (Vue: VueConstructor<Vue>) => void;
export default _default;
51 changes: 51 additions & 0 deletions test-project/src/uni-composition-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import compositionPatch from './applets-repair';
import { PLATFORM } from './global';
export * from './lifecycle-hooks';
export default (Vue) => {
const createHookMixinFuc = (lifecycle) => {
const containerName = `__${lifecycle.toLocaleUpperCase()}_HOOKS__`;
return function (...args) {
if (Array.isArray(this[containerName])) {
let currentResult = undefined;
this[containerName].forEach((hook) => {
const result = hook(...args);
if (typeof result !== 'undefined') {
currentResult = result;
}
});
if (typeof currentResult !== 'undefined') {
return currentResult;
}
}
};
};
Vue.mixin({
onLoad: createHookMixinFuc('onLoad'),
onShow: createHookMixinFuc('onShow'),
onReady: createHookMixinFuc('onReady'),
onHide: createHookMixinFuc('onHide'),
onUnload: createHookMixinFuc('onUnload'),
onPullDownRefresh: createHookMixinFuc('onPullDownRefresh'),
onReachBottom: createHookMixinFuc('onReachBottom'),
onShareAppMessage: createHookMixinFuc('onShareAppMessage'),
onShareTimeline: createHookMixinFuc('onShareTimeline'),
onAddToFavorites: createHookMixinFuc('onAddToFavorites'),
onPageScroll: createHookMixinFuc('onPageScroll'),
onResize: createHookMixinFuc('onResize'),
onTabItemTap: createHookMixinFuc('onTabItemTap'),
onNavigationBarButtonTap: createHookMixinFuc('onNavigationBarButtonTap'),
onBackPress: createHookMixinFuc('onBackPress'),
onNavigationBarSearchInputChanged: createHookMixinFuc('onNavigationBarSearchInputChanged'),
onNavigationBarSearchInputConfirmed: createHookMixinFuc('onNavigationBarSearchInputConfirmed'),
onNavigationBarSearchInputClicked: createHookMixinFuc('onNavigationBarSearchInputClicked'),
onInit: createHookMixinFuc('onInit'),
onLaunch: createHookMixinFuc('onLaunch'),
onError: createHookMixinFuc('onError'),
onPageNotFound: createHookMixinFuc('onPageNotFound'),
onThemeChange: createHookMixinFuc('onThemeChange'),
onUnhandledRejection: createHookMixinFuc('onUnhandledRejection'),
onUniNViewMessage: createHookMixinFuc('onUniNViewMessage'),
});
if (PLATFORM !== 'h5')
compositionPatch();
};
148 changes: 148 additions & 0 deletions test-project/src/uni-composition-api/lifecycle-hooks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/// <reference types="@dcloudio/types" />
/**
* 生命周期回调 监听页面加载
*
* 页面加载时触发。一个页面只会调用一次,可以在 onLoad 的参数中获取打开当前页面路径中的参数。
* @param query 打开当前页面路径中的参数
*/
export declare const onLoad: (hook: ((query?: AnyObject | undefined) => void) | undefined) => void;
/**
* 生命周期回调 监听页面初次渲染完成
*
* 页面初次渲染完成时触发。一个页面只会调用一次,代表页面已经准备妥当,可以和视图层进行交互。
*/
export declare const onShow: (hook: (() => void) | undefined) => void;
/**
* 生命周期回调 监听页面初次渲染完成
*
* 页面初次渲染完成时触发。一个页面只会调用一次,代表页面已经准备妥当,可以和视图层进行交互。
*/
export declare const onReady: (hook: (() => void) | undefined) => void;
/**
* 生命周期回调 监听页面隐藏
*
* 页面隐藏/切入后台时触发。 如 `navigateTo` 或底部 `tab` 切换到其他页面,应用切入后台等。
*/
export declare const onHide: (hook: (() => void) | undefined) => void;
/**
* 生命周期回调 监听页面卸载
*
* 页面卸载时触发。如 `redirectTo` 或 `navigateBack` 到其他页面时。
*/
export declare const onUnload: (hook: (() => void) | undefined) => void;
/**
* 监听用户下拉动作
* - 需要在 `pages.json` 的页面配置中开启 `enablePullDownRefresh` 。
* - 可以通过 `uni.startPullDownRefresh` 触发下拉刷新,调用后触发下拉刷新动画,效果与用户手动下拉刷新一致。
* - 当处理完数据刷新后,`uni.stopPullDownRefresh` 可以停止当前页面的下拉刷新。
*/
export declare const onPullDownRefresh: (hook: (() => void) | undefined) => void;
/**
* 页面上拉触底事件的处理函数
* - 可以在 `pages.json` 的页面配置中设置触发距离 `onReachBottomDistance` 。
* - 在触发距离内滑动期间,本事件只会被触发一次。
*/
export declare const onReachBottom: (hook: (() => void) | undefined) => void;
/**
* 用户点击右上角转发
*
* 监听用户点击页面内转发按钮(`<button>` 组件 `open-type="share"`)或右上角菜单“转发”按钮的行为,并自定义转发内容。
* @param options 分享发起来源参数
* @return 转发内容
*/
export declare const onShareAppMessage: (hook: ((options: Page.ShareAppMessageOption) => Page.CustomShareContent) | undefined) => void;
/**
* 用户点击右上角转发到朋友圈
*
* 监听右上角菜单“分享到朋友圈”按钮的行为,并自定义发享内容。
*/
export declare const onShareTimeline: (hook: (() => Page.ShareTimelineContent) | undefined) => void;
/**
* 用户点击右上角收藏
*
* 监听用户点击右上角菜单“收藏”按钮的行为,并自定义收藏内容。
*/
export declare const onAddToFavorites: (hook: ((options: Page.AddToFavoritesOption) => Page.CustomFavoritesContent) | undefined) => void;
/**
* 页面滚动触发事件的处理函数
*
* 监听用户滑动页面事件。
* @param options 页面滚动参数
*/
export declare const onPageScroll: (hook: ((options: Page.PageScrollOption) => void) | undefined) => void;
/**
* 页面尺寸改变时触发
* @param options 页面滚动参数
*/
export declare const onResize: (hook: ((options: Page.PageScrollOption) => void) | undefined) => void;
/**
* 当前是 tab 页时,点击 tab 时触发
* @param options tab 点击参数
*/
export declare const onTabItemTap: (hook: ((options: Page.TabItemTapOption) => void) | undefined) => void;
/**
* 监听原生标题栏按钮点击事件
* @param options tab 点击参数
*/
export declare const onNavigationBarButtonTap: (hook: ((options: Page.NavigationBarButtonTapOption) => void) | undefined) => void;
/**
* 监听页面返回
* @param options tab 点击参数
* @return 返回 `true` 时阻止页面返回
*/
export declare const onBackPress: (hook: ((options: Page.BackPressOption) => any) | undefined) => void;
/**
* 监听原生标题栏搜索输入框输入内容变化事件
*/
export declare const onNavigationBarSearchInputChanged: (hook: ((event: Page.NavigationBarSearchInputEvent) => void) | undefined) => void;
/**
* 监听原生标题栏搜索输入框搜索事件,用户点击软键盘上的“搜索”按钮时触发。
*/
export declare const onNavigationBarSearchInputConfirmed: (hook: ((event: Page.NavigationBarSearchInputEvent) => void) | undefined) => void;
/**
* 监听原生标题栏搜索输入框点击事件
*/
export declare const onNavigationBarSearchInputClicked: (hook: (() => void) | undefined) => void;
/**
* 生命周期回调 监听页面初始化
*
* 页面初始化时触发。一个页面只会调用一次,可以在 onInit 的参数中获取打开当前页面路径中的参数。
* @param query 打开当前页面路径中的参数
*/
export declare const onInit: (hook: ((query?: AnyObject | undefined) => void) | undefined) => void;
/**
* 错误监听函数
* 小程序发生脚本错误或 API 调用报错时触发
* @param error 错误信息,包含堆栈
*/
export declare const onError: (hook: ((error: string) => void) | undefined) => void;
/**
* 生命周期回调 监听应用初始化
*
* 应用初始化完成时触发,全局只触发一次。
*/
export declare const onLaunch: (hook: ((options?: App.LaunchShowOption | undefined) => void) | undefined) => void;
/**
* 页面不存在监听函数
*
* 应用要打开的页面不存在时触发,会带上页面信息回调该函数
*
* **注意:**
* 1. 如果开发者没有添加 `onPageNotFound` 监听,当跳转页面不存在时,将推入微信客户端原生的页面不存在提示页面。
* 2. 如果 `onPageNotFound` 回调中又重定向到另一个不存在的页面,将推入微信客户端原生的页面不存在提示页面,并且不再回调 `onPageNotFound`。
*/
export declare const onPageNotFound: (hook: ((options: App.PageNotFoundOption) => void) | undefined) => void;
/**
* 监听系统主题变化
*/
export declare const onThemeChange: (hook: ((options: UniApp.OnThemeChangeCallbackResult) => void) | undefined) => void;
/**
* 未处理的 Promise 拒绝事件监听函数
*/
export declare const onUnhandledRejection: (hook: ((options: UniApp.OnUnhandledRejectionCallbackResult) => void) | undefined) => void;
/**
* 监听 nvue 页面消息
*
* nvue 页面使用 `uni.postMessage` 发送消息时触发
*/
export declare const onUniNViewMessage: (hook: ((options: AnyObject) => void) | undefined) => void;
Loading

0 comments on commit aaa4f09

Please sign in to comment.