From c3247f89997f8d6762d3fb478d0738a6467be964 Mon Sep 17 00:00:00 2001 From: wuhy Date: Mon, 22 Oct 2018 12:49:05 +0800 Subject: [PATCH] fix(okam-core): fix native api cannot promisify --- packages/okam-core/src/base/application.js | 13 +++++++- packages/okam-core/src/base/base.js | 9 +----- packages/okam-core/src/base/component.js | 2 ++ packages/okam-core/src/na/api.js | 37 ++++++++++++++-------- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/packages/okam-core/src/base/application.js b/packages/okam-core/src/base/application.js index fcc56b17..947fc7da 100644 --- a/packages/okam-core/src/base/application.js +++ b/packages/okam-core/src/base/application.js @@ -7,10 +7,11 @@ import base from './base'; import {promisifyApis, interceptApis} from '../na/api'; +import {getApis, setApis} from '../na/api'; /** * Initialize promisify APIs and interception APIs. - * Return false, if inited. + * Return false, if initialized. * * @inner * @return {boolean} @@ -22,6 +23,16 @@ function initApis() { let promiseApis = this.$promisifyApis; let interceptAPis = this.$interceptApis; + let apis = getApis(); + if ((promiseApis && promiseApis.length) + || (interceptAPis && Object.keys(interceptAPis).length) + ) { + // create new apis to ami to override existed apis + apis = Object.assign({}, apis); + setApis(apis); + } + this.$api = apis; + promisifyApis(promiseApis, this); interceptApis(interceptAPis, '$api', this); diff --git a/packages/okam-core/src/base/base.js b/packages/okam-core/src/base/base.js index acee348c..a45bef42 100644 --- a/packages/okam-core/src/base/base.js +++ b/packages/okam-core/src/base/base.js @@ -5,18 +5,11 @@ 'use strict'; -import {env, global} from '../na/index'; +import {global} from '../na/index'; import request from '../na/request'; export default { - /** - * The native api - * - * @type {Object} - */ - $api: env, - /** * The global object * diff --git a/packages/okam-core/src/base/component.js b/packages/okam-core/src/base/component.js index 71c57641..cfe8c5cb 100644 --- a/packages/okam-core/src/base/component.js +++ b/packages/okam-core/src/base/component.js @@ -7,6 +7,7 @@ import {env, getCurrApp} from '../na/index'; import EventListener from '../util/EventListener'; +import {getApis} from '../na/api'; import base from './base'; /** @@ -100,6 +101,7 @@ export default { created() { // cannot call setData this.$app = getCurrApp(); + this.$api = getApis(); Object.assign(this, base); this.$listener = new EventListener(); diff --git a/packages/okam-core/src/na/api.js b/packages/okam-core/src/na/api.js index d2911227..413fcfe9 100644 --- a/packages/okam-core/src/na/api.js +++ b/packages/okam-core/src/na/api.js @@ -3,7 +3,28 @@ * @author sparklewhy@gmail.com */ -import {isFunction, isObject, isPromise, isPropertyWritable} from '../util/index'; +import {isFunction, isObject, isPromise} from '../util/index'; +import {env} from './index'; + +let myPromisifyApis; + +/** + * Get mini program APIs + * + * @return {Object} + */ +export function getApis() { + return myPromisifyApis || env; +} + +/** + * Set mini program APIs + * + * @param {Object} apis the apis to set + */ +export function setApis(apis) { + myPromisifyApis = apis; +} /** * Promisify the given function. @@ -61,12 +82,7 @@ export function promisifyApis(apis, context) { return; } - if (isPropertyWritable(context.$api, key)) { - context.$api[key] = promisify(api, context.$api); - } - else { - console.warn(`API ${key} readonly cannot promisify`); - } + context.$api[key] = promisify(api, context.$api); }); } @@ -154,12 +170,7 @@ export function interceptApis(apis, key, ctx) { Object.keys(apis).forEach(apiName => { let rawApi = allApis[apiName]; if (rawApi) { - if (isPropertyWritable(allApis, apiName)) { - allApis[apiName] = proxyAPI.bind(null, ctx, rawApi, apis[apiName]); - } - else { - console.warn(`API ${apiName} readonly cannot intercept`); - } + allApis[apiName] = proxyAPI.bind(null, ctx, rawApi, apis[apiName]); } }); }