Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix async api no opt #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ function groupByType(recordData) {
function sortByCount(groupData) {
return groupData.count;
}
function isPromise(value) {
return value instanceof Promise;
}

let proxyed$1 = false;
const env$2 = getEnv();
Expand Down Expand Up @@ -311,14 +314,7 @@ function proxyAPI() {
checkWarningRules(type, "post");
return result;
} else {
updateMeta(type, (meta) => {
meta.parallelism++;
});
checkWarningRules(type);
const opt = args[0] || {};
const successRaw = opt.success;
const failRaw = opt.fail;
opt.success = function(...args2) {
let asyncSuccessCallbackProxy = function(args2) {
recordData.endTime = +/* @__PURE__ */ new Date();
recordData.duration = recordData.endTime - recordData.startTime;
const postDataGens = getDataGenerators(type, "post");
Expand All @@ -332,9 +328,7 @@ function proxyAPI() {
});
checkWarningRules(type, "post");
recordData = null;
successRaw && successRaw.apply(this, args2);
};
opt.fail = function(...args2) {
}, asyncFailCallbackProxy = function(args2) {
const res = args2[0];
if (env$1 === "ali") {
recordData.errno = res.error;
Expand All @@ -350,10 +344,37 @@ function proxyAPI() {
});
checkWarningRules(type, "post");
recordData = null;
failRaw && failRaw.apply(this, args2);
};
args[0] = opt;
return original.apply(this, args);
updateMeta(type, (meta) => {
meta.parallelism++;
});
checkWarningRules(type);
const opt = args[0];
if (opt) {
const successRaw = opt.success;
const failRaw = opt.fail;
opt.success = function(...args2) {
asyncSuccessCallbackProxy(args2);
successRaw && successRaw.apply(this, args2);
};
opt.fail = function(...args2) {
asyncFailCallbackProxy(args2);
failRaw && failRaw.apply(this, args2);
};
args[0] = opt;
return original.apply(this, args);
}
const originalResult = original.apply(this, args);
if (isPromise(originalResult)) {
return originalResult.then((result) => {
asyncSuccessCallbackProxy([result]);
return Promise.resolve(result);
}).catch((err) => {
asyncFailCallbackProxy([err]);
return Promise.reject(err);
});
}
return originalResult;
}
};
Object.defineProperty(envObj, type, {
Expand Down
43 changes: 33 additions & 10 deletions src/proxyAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEnvObj, getEnv } from './utils'
import { getEnvObj, getEnv, isPromise } from './utils'
import { addRecordData, updateMeta, checkWarningRules, getDataGenerators } from './monitor'
import type { IAnyObject, StackConfig, RecordData } from './types'

Expand Down Expand Up @@ -148,10 +148,9 @@ export function proxyAPI() {
meta.parallelism++
})
checkWarningRules(type)
const opt = args[0] || {}
const successRaw = opt.success
const failRaw = opt.fail
opt.success = function (...args: any[]) {
const opt = args[0]

function asyncSuccessCallbackProxy(args: any[]) {
recordData.endTime = +new Date()
recordData.duration = recordData.endTime - recordData.startTime
const postDataGens = getDataGenerators(type, 'post')
Expand All @@ -166,10 +165,9 @@ export function proxyAPI() {
checkWarningRules(type, 'post')
// for gc
recordData = null as unknown as RecordData
successRaw && successRaw.apply(this, args)
}

opt.fail = function (...args: any[]) {
function asyncFailCallbackProxy(args: any[]) {
const res = args[0]
if (env === 'ali') {
recordData.errno = res.error
Expand All @@ -186,10 +184,35 @@ export function proxyAPI() {
checkWarningRules(type, 'post')
// for gc
recordData = null as unknown as RecordData
failRaw && failRaw.apply(this, args)
}
args[0] = opt
return original.apply(this, args)

if (opt) {
const successRaw = opt.success
const failRaw = opt.fail
opt.success = function (...args: any[]) {
asyncSuccessCallbackProxy(args)
successRaw && successRaw.apply(this, args)
}
opt.fail = function (...args: any[]) {
asyncFailCallbackProxy(args)
failRaw && failRaw.apply(this, args)
}

args[0] = opt

return original.apply(this, args)
}
const originalResult = original.apply(this, args)
if (isPromise(originalResult)) {
return originalResult.then((result: any) => {
asyncSuccessCallbackProxy([result])
return Promise.resolve(result)
}).catch((err: any) => {
asyncFailCallbackProxy([err])
return Promise.reject(err)
})
}
return originalResult
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ export function sortByCount (groupData: GroupData) {
return groupData.count
}

export function isPromise (value: any) {
return value instanceof Promise
}