first commit
This commit is contained in:
21
node_modules/run-async/LICENSE
generated
vendored
Normal file
21
node_modules/run-async/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Simon Boudrias
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
104
node_modules/run-async/README.md
generated
vendored
Normal file
104
node_modules/run-async/README.md
generated
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
# Run Async
|
||||
|
||||
[](http://badge.fury.io/js/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";
|
||||
});
|
||||
```
|
||||
|
||||
#### Custom done factory
|
||||
|
||||
```js
|
||||
var runAsync = require("run-async");
|
||||
|
||||
runAsync(function () {
|
||||
var callback = this.customAsync();
|
||||
callback(null, a + b);
|
||||
}, "customAsync")(1, 2);
|
||||
```
|
||||
|
||||
#### Passing context to async method
|
||||
|
||||
```js
|
||||
var runAsync = require("run-async");
|
||||
|
||||
runAsync(function () {
|
||||
assert(this.isBound);
|
||||
var callback = this.async();
|
||||
callback(null, a + b);
|
||||
}).call({ isBound: true }, 1, 2);
|
||||
```
|
||||
|
||||
### 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.
|
||||
5
node_modules/run-async/index.d.ts
generated
vendored
Normal file
5
node_modules/run-async/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
declare function runAsync<F extends (...args: any[]) => any>(
|
||||
func: F,
|
||||
): (...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>;
|
||||
|
||||
export = runAsync;
|
||||
131
node_modules/run-async/index.js
generated
vendored
Normal file
131
node_modules/run-async/index.js
generated
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
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
|
||||
* @param {string} [proxyProperty] `this` property to be used for the callback factory
|
||||
* @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, proxyProperty = "async") {
|
||||
if (typeof cb === "string") {
|
||||
proxyProperty = cb;
|
||||
cb = undefined;
|
||||
}
|
||||
cb = cb || function () {};
|
||||
|
||||
return function () {
|
||||
var args = arguments;
|
||||
var originalThis = this;
|
||||
|
||||
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 doneFactory = 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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
var _this;
|
||||
if (originalThis && proxyProperty && Proxy) {
|
||||
_this = new Proxy(originalThis, {
|
||||
get(_target, prop) {
|
||||
if (prop === proxyProperty) {
|
||||
if (prop in _target) {
|
||||
console.warn(
|
||||
`${proxyProperty} property is been shadowed by run-sync`,
|
||||
);
|
||||
}
|
||||
return doneFactory;
|
||||
}
|
||||
|
||||
return Reflect.get(...arguments);
|
||||
},
|
||||
});
|
||||
} else {
|
||||
_this = { [proxyProperty]: doneFactory };
|
||||
}
|
||||
|
||||
var answer = func.apply(_this, 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);
|
||||
};
|
||||
36
node_modules/run-async/package.json
generated
vendored
Normal file
36
node_modules/run-async/package.json
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"name": "run-async",
|
||||
"version": "4.0.6",
|
||||
"description": "Utility method to run function either synchronously or asynchronously using the common `this.async()` style.",
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": {
|
||||
"types": "./index.d.ts",
|
||||
"default": "./index.js"
|
||||
}
|
||||
},
|
||||
"sideEffects": false,
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "node --test test.js",
|
||||
"lint": "npx oxlint && npx prettier --check ."
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.12.0"
|
||||
},
|
||||
"repository": "SBoudrias/run-async",
|
||||
"keywords": [
|
||||
"flow",
|
||||
"flow-control",
|
||||
"async"
|
||||
],
|
||||
"author": "Simon Boudrias <admin@simonboudrias.com>",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"oxlint": "^1.2.0",
|
||||
"prettier": "^3.5.3"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user