| "use strict"; | ||
| // 提供一个异步同时等待的方法 | ||
| var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
| function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
| return new (P || (P = Promise))(function (resolve, reject) { | ||
| function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
| function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
| function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
| step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
| }); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.AwaitCall = void 0; | ||
| const CacheMoudle_1 = require("./CacheMoudle"); | ||
| class AwaitUnit { | ||
| constructor(cb) { | ||
| this.running = false; | ||
| this.resloves = []; | ||
| this.rejects = []; | ||
| this.cbFunc = cb; | ||
| } | ||
| call() { | ||
| return new Promise((resolve, reject) => { | ||
| this.resloves.push(resolve); | ||
| this.rejects.push(reject); | ||
| if (!this.running) { | ||
| this.running = true; | ||
| this.cbFunc().then((v) => { | ||
| this.finishCall(null, v); | ||
| }).catch(e => { | ||
| this.finishCall(e, null); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| finishCall(err, result) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| if (err) { | ||
| for (let i = 0; i < this.rejects.length; i++) { | ||
| this.rejects[i](err); | ||
| } | ||
| } | ||
| else { | ||
| for (let i = 0; i < this.resloves.length; i++) { | ||
| this.resloves[i](result); | ||
| } | ||
| } | ||
| this.running = false; | ||
| }); | ||
| } | ||
| } | ||
| var cache = CacheMoudle_1.CacheMoudle.createCache("awaitCall", 10 * 1000); | ||
| function awaitCall(name, cbFunc) { | ||
| if (!cache.has(name)) | ||
| cache.set(name, new AwaitUnit(cbFunc)); | ||
| let t = cache.get(name); | ||
| return t.call(); | ||
| } | ||
| /** | ||
| * 等待完成后的再执行 | ||
| * @param posList 一致性检查需要的参数列表 不填表示全部参与 | ||
| */ | ||
| function AwaitCall(posList = []) { | ||
| return doAwaitCall.bind(undefined, posList); | ||
| } | ||
| exports.AwaitCall = AwaitCall; | ||
| function doAwaitCall(posList, target, propertyKey, descriptor) { | ||
| let tv = descriptor.value; | ||
| descriptor.value = function (...args) { | ||
| let _this = this; | ||
| let key = propertyKey.toString(); | ||
| if (posList.length == 0) { | ||
| key += JSON.stringify(args); | ||
| } | ||
| else { | ||
| for (let i = 0; i < posList.length; i++) { | ||
| key += (posList[i] || "").toString(); | ||
| } | ||
| } | ||
| return awaitCall(key, function () { | ||
| return tv.apply(_this, args); | ||
| }); | ||
| }; | ||
| } |
| // 提供一个异步同时等待的方法 | ||
| import { CacheMoudle } from "./CacheMoudle"; | ||
| class AwaitUnit { | ||
| cbFunc: () => Promise<any> | ||
| constructor(cb: () => Promise<any>) { | ||
| this.cbFunc = cb; | ||
| } | ||
| private running: boolean = false; | ||
| private resloves: any[] = []; | ||
| private rejects: any[] = []; | ||
| call<T>() { | ||
| return new Promise<T>((resolve, reject) => { | ||
| this.resloves.push(resolve); | ||
| this.rejects.push(reject); | ||
| if (!this.running) { | ||
| this.running = true; | ||
| this.cbFunc().then((v) => { | ||
| this.finishCall(null, v); | ||
| }).catch(e => { | ||
| this.finishCall(e, null); | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
| private async finishCall(err: any, result: any) { | ||
| if (err) { | ||
| for (let i = 0; i < this.rejects.length; i++) { | ||
| this.rejects[i](err); | ||
| } | ||
| } | ||
| else { | ||
| for (let i = 0; i < this.resloves.length; i++) { | ||
| this.resloves[i](result); | ||
| } | ||
| } | ||
| this.running = false; | ||
| } | ||
| } | ||
| var cache = CacheMoudle.createCache<AwaitUnit>("awaitCall", 10 * 1000); | ||
| function awaitCall<T>(name: string, cbFunc: () => Promise<any>) { | ||
| if (!cache.has(name)) cache.set(name, new AwaitUnit(cbFunc)); | ||
| let t = cache.get<AwaitUnit>(name) as AwaitUnit; | ||
| return t.call<T>() | ||
| } | ||
| /** | ||
| * 等待完成后的再执行 | ||
| * @param posList 一致性检查需要的参数列表 不填表示全部参与 | ||
| */ | ||
| export function AwaitCall(posList: number[] = []): MethodDecorator { | ||
| return doAwaitCall.bind(undefined, posList) | ||
| } | ||
| function doAwaitCall(posList: number[], target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>): void | TypedPropertyDescriptor<any> { | ||
| let tv = descriptor.value; | ||
| descriptor.value = function (...args: any[]) { | ||
| let _this = this; | ||
| let key = propertyKey.toString(); | ||
| if (posList.length == 0) { | ||
| key += JSON.stringify(args) | ||
| } | ||
| else { | ||
| for (let i = 0; i < posList.length; i++) { | ||
| key += (posList[i] || "").toString() | ||
| } | ||
| } | ||
| return awaitCall(key, function () { | ||
| return tv.apply(_this, args) | ||
| }) | ||
| } | ||
| } |
+1
-1
| { | ||
| "name": "mx-tool", | ||
| "version": "1.0.13", | ||
| "version": "1.0.14", | ||
| "description": "#### Description 提供摩西的标准时间 等基础能力", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
+2
-0
@@ -21,1 +21,3 @@ "use strict"; | ||
| __exportStar(require("./HttpQuest"), exports); | ||
| __exportStar(require("./CacheMoudle"), exports); | ||
| __exportStar(require("./promiseAwait"), exports); |
+3
-1
@@ -8,2 +8,4 @@ export * from './InitMoudle' | ||
| export * from './version' | ||
| export * from './HttpQuest' | ||
| export * from './HttpQuest' | ||
| export * from './CacheMoudle' | ||
| export * from './promiseAwait' |
161573
3.59%32
6.67%3652
4.46%