mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-07-01 07:40:10 +00:00
Merge pull request #22365 from DefinitelyTyped/not-needed-asyncblock
asyncblock: Provides its own types
This commit is contained in:
@@ -78,6 +78,12 @@
|
||||
"sourceRepoURL": "https://github.com/AlexTeixeira/Askmethat-Rating",
|
||||
"asOfVersion": "0.4.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "asyncblock",
|
||||
"typingsPackageName": "asyncblock",
|
||||
"sourceRepoURL": "https://github.com/scriby/asyncblock",
|
||||
"asOfVersion": "2.2.11"
|
||||
},
|
||||
{
|
||||
"libraryName": "aurelia-knockout",
|
||||
"typingsPackageName": "aurelia-knockout",
|
||||
|
||||
@@ -1,189 +0,0 @@
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
import asyncblock = require('asyncblock');
|
||||
import fs = require('fs');
|
||||
|
||||
|
||||
// Sleeping in series
|
||||
asyncblock((flow: asyncblock.IFlow) => {
|
||||
console.time('time');
|
||||
|
||||
setTimeout(flow.add(), 1000);
|
||||
flow.wait(); //Wait for the first setTimeout to finish
|
||||
|
||||
setTimeout(flow.add(), 2000);
|
||||
flow.wait(); //Wait for the second setTimeout to finish
|
||||
|
||||
console.timeEnd('time'); //3 seconds
|
||||
});
|
||||
|
||||
// Trapping results
|
||||
asyncblock((flow) => {
|
||||
var path1 = '', path2 = '';
|
||||
//Start two parallel file reads
|
||||
fs.readFile(path1, 'utf8', flow.set('contents1'));
|
||||
fs.readFile(path2, 'utf8', flow.set('contents2'));
|
||||
|
||||
//Print the concatenation of the results when both reads are finished
|
||||
console.log(flow.get<string>('contents1') + flow.get<string>('contents2'));
|
||||
|
||||
var paths: string[] = [];
|
||||
//Wait for a large number of tasks
|
||||
for(var i = 0; i < 100; i++){
|
||||
//Add each task in parallel with i as the key
|
||||
fs.readFile(paths[i], 'utf8', flow.add(i));
|
||||
}
|
||||
|
||||
//Wait for all the tasks to finish. Results is an object of the form {key1: value1, key2: value2, ...}
|
||||
var results = flow.wait();
|
||||
|
||||
var path = '';
|
||||
//One-liner syntax for waiting on a single task
|
||||
var contents = flow.sync( fs.readFile(path, 'utf8', flow.callback()) );
|
||||
|
||||
//See overview & API docs for more extensive description of techniques
|
||||
});
|
||||
|
||||
// Error handling
|
||||
var asyncTask = (callback: (err: any) => void) => {
|
||||
asyncblock((flow) => {
|
||||
flow.errorCallback = callback; //Setting the errorCallback is the easiest way to perform error handling. If erroCallback isn't set, and an error occurs, it will be thrown instead of returned to the callback
|
||||
|
||||
var path = '';
|
||||
fs.readFile(path, 'utf8', flow.add()); //If readFile encountered an error, it would automatically get passed to the callback
|
||||
var contents = flow.wait();
|
||||
|
||||
console.log(contents); //If an error occured above, this code won't run
|
||||
});
|
||||
};
|
||||
|
||||
// Returning results
|
||||
var asyncTask2 = (callback: (err: any, res: string) => void) => {
|
||||
asyncblock((flow) => {
|
||||
var path = '';
|
||||
var contents = flow.sync( fs.readFile(path, 'utf8', flow.callback()) ); //If readFile encountered an error, it would automatically get passed to the callback
|
||||
|
||||
return contents; //Return the value you want to be passed to the callback
|
||||
}, callback); //The callback can be specified as the 2nd arg to asyncblock. It will be called with the value returned from the asyncblock as the 2nd arg.
|
||||
//If an error occurs, the callback will be called with the error as the first argument.
|
||||
};
|
||||
|
||||
|
||||
// Sample
|
||||
asyncblock.nostack((flow) => {
|
||||
fs.readFile('path1', 'utf8', flow.add('first'));
|
||||
fs.readFile('path2', 'utf8', flow.add('second'));
|
||||
|
||||
//Wait until done reading the first and second files, then write them to another file
|
||||
fs.writeFile('path3', flow.wait<string>('first') + flow.wait<string>('second'), flow.add());
|
||||
flow.wait(); //Wait on all outstanding tasks
|
||||
|
||||
fs.readFile('path3', 'utf8', flow.add('data'));
|
||||
|
||||
console.log(flow.wait('data')); //Print the 3rd file's data
|
||||
console.log('all done');
|
||||
});
|
||||
|
||||
|
||||
// Formatting results
|
||||
(function() {
|
||||
|
||||
var asyncTask = function(callback: (err: any, res1: number, res2: number, res3: number) => void) {
|
||||
process.nextTick(function() {
|
||||
callback(null, 1, 2, 3);
|
||||
});
|
||||
}
|
||||
|
||||
asyncblock(function(flow) {
|
||||
asyncTask(flow.add());
|
||||
|
||||
var result = flow.wait();
|
||||
console.log(result); // Prints 1
|
||||
});
|
||||
|
||||
asyncblock(function(flow) {
|
||||
asyncTask(flow.add(['first', 'second', 'third']));
|
||||
|
||||
var result = flow.wait();
|
||||
console.log(result); // Prints { first: 1, second: 2, third: 3 }
|
||||
|
||||
asyncTask(flow.add('key1', ['first', 'second', 'third']));
|
||||
asyncTask(flow.add('key2', ['a', 'b', 'c']));
|
||||
var result = flow.wait();
|
||||
console.log(result); // Prints { key1: { first: 1, second: 2, third: 3 }, key2: { a: 1, b: 2, c: 3} }
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
|
||||
// Parallel task rate limiting
|
||||
(function() {
|
||||
|
||||
asyncblock(function(flow) {
|
||||
flow.queue(function(callback: Function) {
|
||||
setTimeout(callback, 1000);
|
||||
});
|
||||
|
||||
flow.wait(); //This will wait for about a second
|
||||
});
|
||||
|
||||
asyncblock(function(flow) {
|
||||
flow.maxParallel = 2;
|
||||
|
||||
process.nextTick(function(){
|
||||
flow.queue(function(callback: Function) {
|
||||
setTimeout(callback, 1000);
|
||||
});
|
||||
|
||||
flow.queue(function(callback: Function) {
|
||||
setTimeout(callback, 2000);
|
||||
});
|
||||
|
||||
flow.queue(function(callback: Function) {
|
||||
setTimeout(callback, 3000);
|
||||
});
|
||||
|
||||
flow.doneAdding();
|
||||
});
|
||||
|
||||
flow.forceWait();
|
||||
});
|
||||
|
||||
asyncblock(function(flow) {
|
||||
setTimeout(flow.callback(), 1000);
|
||||
|
||||
flow.queue(function(callback: Function) {
|
||||
setTimeout(callback, 1000);
|
||||
});
|
||||
flow.queue((callback: (err: any, res: string) => void) => {
|
||||
callback(null, '');
|
||||
});
|
||||
|
||||
flow.wait();
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
|
||||
// Task timeouts
|
||||
(function() {
|
||||
|
||||
asyncblock(function(flow){
|
||||
setTimeout(flow.add({timeout: 1000, timeoutIsError: false}), 2000);
|
||||
flow.wait(); //The fiber will yield here for 1 second, then continue
|
||||
|
||||
//Code here will run
|
||||
});
|
||||
|
||||
asyncblock(function(flow){
|
||||
flow.timeoutIsError = false;
|
||||
|
||||
setTimeout(flow.add({timeout: 1000}), 2000);
|
||||
flow.wait(); //The fiber will yield here for 1 second, then continue
|
||||
|
||||
//Code here will run
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
71
types/asyncblock/index.d.ts
vendored
71
types/asyncblock/index.d.ts
vendored
@@ -1,71 +0,0 @@
|
||||
// Type definitions for asyncblock 2.1.23
|
||||
// Project: https://github.com/scriby/asyncblock
|
||||
// Definitions by: Hiroki Horiuchi <https://github.com/horiuchi>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
|
||||
|
||||
declare function asyncblock<T>(f: (flow: asyncblock.IFlow) => void, callback?: (err: any, res: T) => void): void;
|
||||
|
||||
declare namespace asyncblock {
|
||||
export function nostack<T>(f: (flow: asyncblock.IFlow) => void, callback?: (err: any, res: T) => void): void;
|
||||
|
||||
export interface IFlow {
|
||||
add(responseFormat?: string[]): IExecuteFunction;
|
||||
add(key: string, responseFormat?: string[]): IExecuteFunction;
|
||||
add(key: number, responseFormat?: string[]): IExecuteFunction;
|
||||
add(options: IFlowOptions): IExecuteFunction;
|
||||
callback(responseFormat?: string[]): IExecuteFunction;
|
||||
callback(key: string, responseFormat?: string[]): IExecuteFunction;
|
||||
callback(key: number, responseFormat?: string[]): IExecuteFunction;
|
||||
callback(options: IFlowOptions): IExecuteFunction;
|
||||
wait<T>(key?: string): T;
|
||||
wait<T>(key?: number): T;
|
||||
|
||||
get<T>(key: string): T;
|
||||
set(key: string, responseFormat?: string[]): IExecuteFunction;
|
||||
set(options: IFlowOptions): IExecuteFunction;
|
||||
del(key: string): void;
|
||||
|
||||
sync<T>(task: any): T;
|
||||
queue(toExecute: IExecuteFunction): void;
|
||||
queue(key: string, toExecute: IExecuteFunction): void;
|
||||
queue(key: number, toExecute: IExecuteFunction): void;
|
||||
queue(responseFormat: string[], toExecute: IExecuteFunction): void;
|
||||
queue(key: string, responseFormat: string[], toExecute: IExecuteFunction): void;
|
||||
queue(key: number, responseFormat: string[], toExecute: IExecuteFunction): void;
|
||||
queue(options: IFlowOptions, toExecute: IExecuteFunction): void;
|
||||
doneAdding(): void;
|
||||
forceWait<T>(): T;
|
||||
|
||||
maxParallel: number;
|
||||
errorCallback: (err: any) => void;
|
||||
taskTimeout: number;
|
||||
timeoutIsError: boolean;
|
||||
}
|
||||
|
||||
export interface IFlowOptions {
|
||||
ignoreError?: boolean; // default false
|
||||
key?: string; // string | number
|
||||
responseFormat?: string[];
|
||||
timeout?: number;
|
||||
timeoutIsError?: boolean;
|
||||
dontWait?: boolean;
|
||||
firstArgIsError?: boolean; // default true
|
||||
}
|
||||
|
||||
export interface IExecuteFunction {
|
||||
<T1, T2, T3>(err: any, res1: T1, res2: T2, res3: T3): any;
|
||||
<T1, T2>(err: any, res1: T1, res2: T2): any;
|
||||
<T>(err: any, res: T): any;
|
||||
(err: any): any;
|
||||
|
||||
// firstArgIsError === false
|
||||
<T1, T2, T3>(res1: T1, res2: T2, res3: T3): any;
|
||||
<T1, T2>(res1: T1, res2: T2): any;
|
||||
<T>(res: T): any;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export = asyncblock;
|
||||
@@ -1,24 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"asyncblock-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
"adjacent-overload-signatures": false,
|
||||
"array-type": false,
|
||||
"arrow-return-shorthand": false,
|
||||
"ban-types": false,
|
||||
"callable-types": false,
|
||||
"comment-format": false,
|
||||
"dt-header": false,
|
||||
"eofline": false,
|
||||
"export-just-namespace": false,
|
||||
"import-spacing": false,
|
||||
"interface-name": false,
|
||||
"interface-over-type-literal": false,
|
||||
"jsdoc-format": false,
|
||||
"max-line-length": false,
|
||||
"member-access": false,
|
||||
"new-parens": false,
|
||||
"no-any-union": false,
|
||||
"no-boolean-literal-compare": false,
|
||||
"no-conditional-assignment": false,
|
||||
"no-consecutive-blank-lines": false,
|
||||
"no-construct": false,
|
||||
"no-declare-current-package": false,
|
||||
"no-duplicate-imports": false,
|
||||
"no-duplicate-variable": false,
|
||||
"no-empty-interface": false,
|
||||
"no-for-in-array": false,
|
||||
"no-inferrable-types": false,
|
||||
"no-internal-module": false,
|
||||
"no-irregular-whitespace": false,
|
||||
"no-mergeable-namespace": false,
|
||||
"no-misused-new": false,
|
||||
"no-namespace": false,
|
||||
"no-object-literal-type-assertion": false,
|
||||
"no-padding": false,
|
||||
"no-redundant-jsdoc": false,
|
||||
"no-redundant-jsdoc-2": false,
|
||||
"no-redundant-undefined": false,
|
||||
"no-reference-import": false,
|
||||
"no-relative-import-in-test": false,
|
||||
"no-self-import": false,
|
||||
"no-single-declare-module": false,
|
||||
"no-string-throw": false,
|
||||
"no-unnecessary-callback-wrapper": false,
|
||||
"no-unnecessary-class": false,
|
||||
"no-unnecessary-generics": false,
|
||||
"no-unnecessary-qualifier": false,
|
||||
"no-unnecessary-type-assertion": false,
|
||||
"no-useless-files": false,
|
||||
"no-var-keyword": false,
|
||||
"no-var-requires": false,
|
||||
"no-void-expression": false,
|
||||
"no-trailing-whitespace": false,
|
||||
"object-literal-key-quotes": false,
|
||||
"object-literal-shorthand": false,
|
||||
"one-line": false,
|
||||
"one-variable-per-declaration": false,
|
||||
"only-arrow-functions": false,
|
||||
"prefer-conditional-expression": false,
|
||||
"prefer-const": false,
|
||||
"prefer-declare-function": false,
|
||||
"prefer-for-of": false,
|
||||
"prefer-method-signature": false,
|
||||
"prefer-template": false,
|
||||
"radix": false,
|
||||
"semicolon": false,
|
||||
"space-before-function-paren": false,
|
||||
"space-within-parens": false,
|
||||
"strict-export-declare-modifiers": false,
|
||||
"trim-file": false,
|
||||
"triple-equals": false,
|
||||
"typedef-whitespace": false,
|
||||
"unified-signatures": false,
|
||||
"void-return": false,
|
||||
"whitespace": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user