Skip to content

Commit

Permalink
fix(okam-core): fix native api cannot promisify
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhy committed Oct 22, 2018
1 parent 8f1c443 commit c3247f8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
13 changes: 12 additions & 1 deletion packages/okam-core/src/base/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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);

Expand Down
9 changes: 1 addition & 8 deletions packages/okam-core/src/base/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 2 additions & 0 deletions packages/okam-core/src/base/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {env, getCurrApp} from '../na/index';
import EventListener from '../util/EventListener';
import {getApis} from '../na/api';
import base from './base';

/**
Expand Down Expand Up @@ -100,6 +101,7 @@ export default {
created() {
// cannot call setData
this.$app = getCurrApp();
this.$api = getApis();
Object.assign(this, base);

this.$listener = new EventListener();
Expand Down
37 changes: 24 additions & 13 deletions packages/okam-core/src/na/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,28 @@
* @author [email protected]
*/

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.
Expand Down Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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]);
}
});
}

0 comments on commit c3247f8

Please sign in to comment.