da23451467d257a7d7a2d139eea92722a1d66a1ffebb24398c3f50f651a52f73.json 6.7 KB

1
  1. {"ast":null,"code":"import _asyncToGenerator from \"F:/workspace/202226701027/huinongbao-app/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js\";\nimport { Deferred } from \"./deferred.js\";\n/**\n * Provides a simple way of creating the rough equivalent of an async critical section.\n *\n * @example\n * ```typescript\n * const myLock = new AsyncLock();\n *\n * private async MyFuncAsync(): Promise<void> {\n * await myLock.lockAsync(async () => {\n * await operation1Async();\n * await operation2Async();\n * });\n * }\n * ```\n */\nexport class AsyncLock {\n constructor() {\n this._currentOperation = Promise.resolve();\n }\n /**\n * Executes the provided function when the lock is acquired (e.g. when the previous operation finishes).\n * @param func The function to execute.\n * @param signal An optional signal that can be used to abort the operation.\n * @returns A promise that resolves when the func finishes executing.\n */\n lockAsync(func, signal) {\n signal === null || signal === void 0 || signal.throwIfAborted();\n const wrappedFunc = signal ? () => {\n signal.throwIfAborted();\n return func();\n } : func;\n const newOperation = this._currentOperation.then(wrappedFunc);\n // NOTE: It would be simpler to just hold a Promise<unknown>, but this class should not prevent an object held by the returned promise from being garbage collected.\n this._currentOperation = new Promise(resolve => newOperation.then(() => resolve(), resolve));\n return newOperation;\n }\n /**\n * Executes the provided function when all the specified locks are acquired.\n * @param func The function to execute.\n * @param locks The locks to acquire.\n * @param signal An optional signal that can be used to abort the operation.\n * @returns A promise that resolves when the func finishes executing.\n */\n static LockAsync(func, locks, signal) {\n return _asyncToGenerator(function* () {\n signal === null || signal === void 0 || signal.throwIfAborted();\n if (locks.length === 0) {\n return yield func();\n }\n const deferred = new Deferred();\n let acquiredLocks = 0;\n locks.forEach(lock => lock.lockAsync( /*#__PURE__*/_asyncToGenerator(function* () {\n acquiredLocks++;\n if (acquiredLocks === locks.length) {\n deferred.resolve(yield func());\n }\n return deferred.promise;\n }), signal).catch(e => deferred.reject(e)));\n return deferred.promise;\n })();\n }\n}","map":{"version":3,"names":["Deferred","AsyncLock","constructor","_currentOperation","Promise","resolve","lockAsync","func","signal","throwIfAborted","wrappedFunc","newOperation","then","LockAsync","locks","_asyncToGenerator","length","deferred","acquiredLocks","forEach","lock","promise","catch","e","reject"],"sources":["F:/workspace/202226701027/huinongbao-app/node_modules/@babylonjs/core/Misc/asyncLock.js"],"sourcesContent":["import { Deferred } from \"./deferred.js\";\n/**\n * Provides a simple way of creating the rough equivalent of an async critical section.\n *\n * @example\n * ```typescript\n * const myLock = new AsyncLock();\n *\n * private async MyFuncAsync(): Promise<void> {\n * await myLock.lockAsync(async () => {\n * await operation1Async();\n * await operation2Async();\n * });\n * }\n * ```\n */\nexport class AsyncLock {\n constructor() {\n this._currentOperation = Promise.resolve();\n }\n /**\n * Executes the provided function when the lock is acquired (e.g. when the previous operation finishes).\n * @param func The function to execute.\n * @param signal An optional signal that can be used to abort the operation.\n * @returns A promise that resolves when the func finishes executing.\n */\n lockAsync(func, signal) {\n signal?.throwIfAborted();\n const wrappedFunc = signal\n ? () => {\n signal.throwIfAborted();\n return func();\n }\n : func;\n const newOperation = this._currentOperation.then(wrappedFunc);\n // NOTE: It would be simpler to just hold a Promise<unknown>, but this class should not prevent an object held by the returned promise from being garbage collected.\n this._currentOperation = new Promise((resolve) => newOperation.then(() => resolve(), resolve));\n return newOperation;\n }\n /**\n * Executes the provided function when all the specified locks are acquired.\n * @param func The function to execute.\n * @param locks The locks to acquire.\n * @param signal An optional signal that can be used to abort the operation.\n * @returns A promise that resolves when the func finishes executing.\n */\n static async LockAsync(func, locks, signal) {\n signal?.throwIfAborted();\n if (locks.length === 0) {\n return await func();\n }\n const deferred = new Deferred();\n let acquiredLocks = 0;\n locks.forEach((lock) => lock\n .lockAsync(async () => {\n acquiredLocks++;\n if (acquiredLocks === locks.length) {\n deferred.resolve(await func());\n }\n return deferred.promise;\n }, signal)\n .catch((e) => deferred.reject(e)));\n return deferred.promise;\n }\n}\n"],"mappings":";AAAA,SAASA,QAAQ,QAAQ,eAAe;AACxC;AACA;AACA;AACA;A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