KJ
This commit is contained in:
dopeuni444
2025-08-06 11:08:49 +04:00
parent b5a22951ae
commit ae726301f8
8715 changed files with 588619 additions and 243113 deletions

79
unified-ai-platform/node_modules/run-async/README.md generated vendored Normal file
View File

@@ -0,0 +1,79 @@
Run Async
=========
[![npm](https://badge.fury.io/js/run-async.svg)](http://badge.fury.io/js/run-async) [![tests](https://travis-ci.org/SBoudrias/run-async.svg?branch=master)](http://travis-ci.org/SBoudrias/run-async) [![dependencies](https://david-dm.org/SBoudrias/run-async.svg?theme=shields.io)](https://david-dm.org/SBoudrias/run-async)
Utility method to run a function either synchronously or asynchronously using a series of common patterns. This is useful for library author accepting sync or async functions as parameter. `runAsync` will always run them as an async method, and normalize the multiple signature.
Installation
=========
```bash
npm install --save run-async
```
Usage
=========
Here's a simple example print the function results and three options a user can provide a function.
```js
var runAsync = require('run-async');
var printAfter = function (func) {
var cb = function (err, returnValue) {
console.log(returnValue);
};
runAsync(func, cb)(/* arguments for func */);
};
```
#### Using `this.async`
```js
printAfter(function () {
var done = this.async();
setTimeout(function () {
done(null, 'done running with callback');
}, 10);
});
```
#### Returning a promise
```js
printAfter(function () {
return new Promise(function (resolve, reject) {
resolve('done running with promises');
});
});
```
#### Synchronous function
```js
printAfter(function () {
return 'done running sync function';
});
```
### runAsync.cb
`runAsync.cb` supports all the function types that `runAsync` does and additionally a traditional **callback as the last argument** signature:
```js
var runAsync = require('run-async');
// IMPORTANT: The wrapped function must have a fixed number of parameters.
runAsync.cb(function(a, b, cb) {
cb(null, a + b);
}, function(err, result) {
console.log(result)
})(1, 2)
```
If your version of node support Promises natively (node >= 0.12), `runAsync` will return a promise. Example: `runAsync(func)(arg1, arg2).then(cb)`
Licence
========
Copyright (c) 2014 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.

98
unified-ai-platform/node_modules/run-async/index.js generated vendored Normal file
View File

@@ -0,0 +1,98 @@
'use strict';
function isPromise(obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
}
/**
* Return a function that will run a function asynchronously or synchronously
*
* example:
* runAsync(wrappedFunction, callback)(...args);
*
* @param {Function} func Function to run
* @param {Function} cb Callback function passed the `func` returned value
* @return {Function(arguments)} Arguments to pass to `func`. This function will in turn
* return a Promise (Node >= 0.12) or call the callbacks.
*/
var runAsync = module.exports = function (func, cb) {
cb = cb || function () {};
return function () {
var args = arguments;
var promise = new Promise(function (resolve, reject) {
var resolved = false;
const wrappedResolve = function (value) {
if (resolved) {
console.warn('Run-async promise already resolved.')
}
resolved = true;
resolve(value);
}
var rejected = false;
const wrappedReject = function (value) {
if (rejected) {
console.warn('Run-async promise already rejected.')
}
rejected = true;
reject(value);
}
var usingCallback = false;
var callbackConflict = false;
var contextEnded = false;
var answer = func.apply({
async: function () {
if (contextEnded) {
console.warn('Run-async async() called outside a valid run-async context, callback will be ignored.');
return function() {};
}
if (callbackConflict) {
console.warn('Run-async wrapped function (async) returned a promise.\nCalls to async() callback can have unexpected results.');
}
usingCallback = true;
return function (err, value) {
if (err) {
wrappedReject(err);
} else {
wrappedResolve(value);
}
};
}
}, Array.prototype.slice.call(args));
if (usingCallback) {
if (isPromise(answer)) {
console.warn('Run-async wrapped function (sync) returned a promise but async() callback must be executed to resolve.');
}
} else {
if (isPromise(answer)) {
callbackConflict = true;
answer.then(wrappedResolve, wrappedReject);
} else {
wrappedResolve(answer);
}
}
contextEnded = true;
});
promise.then(cb.bind(null, null), cb);
return promise;
}
};
runAsync.cb = function (func, cb) {
return runAsync(function () {
var args = Array.prototype.slice.call(arguments);
if (args.length === func.length - 1) {
args.push(this.async());
}
return func.apply(this, args);
}, cb);
};

View File

@@ -0,0 +1,27 @@
{
"name": "run-async",
"version": "2.4.1",
"description": "Utility method to run function either synchronously or asynchronously using the common `this.async()` style.",
"main": "index.js",
"scripts": {
"test": "mocha -R spec"
},
"engines": {
"node": ">=0.12.0"
},
"repository": "SBoudrias/run-async",
"keywords": [
"flow",
"flow-control",
"async"
],
"files": [
"index.js"
],
"author": "Simon Boudrias <admin@simonboudrias.com>",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"mocha": "^7.1.0"
}
}