feat: add posterus types (#42751)

This commit is contained in:
davidgovea 2020-05-04 09:52:30 -07:00 committed by GitHub
parent 904aad7a9e
commit d09bdf8d8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 0 deletions

3
types/posterus/fiber.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
import { Future } from './index';
export function fiber(iterator: IterableIterator<any>): Future;

40
types/posterus/index.d.ts vendored Normal file
View File

@ -0,0 +1,40 @@
// Type definitions for posterus 0.4
// Project: https://github.com/Mitranim/posterus#readme
// Definitions by: David Govea <https://github.com/DefinitelyTyped>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
export class Future<T = any, E extends Error = Error> {
static from: <T = any, E extends Error = Error>(result?: T, error?: E) => Future<T, E>;
static fromResult: <T = any>(...args: T extends undefined ? [] | [undefined] : [T]) => Future<T>;
static fromError: <E extends Error = Error>(error: E) => Future<undefined, E>;
static fromPromise: <T>(promise: Promise<T>) => Future<T>;
static all: (values: any[]) => Future;
static race: (values: any[]) => Future;
static onUnhandledRejection: (future: Future) => void;
static scheduler: Scheduler;
map: <U = any, V extends Error = Error>(
mapper: (error?: E, result?: T) => U | Promise<U> | Future<U, V>,
) => Future<U, V>;
mapResult: <U = any, V extends Error = Error>(mapper: (result: T) => U | Promise<U> | Future<U, V>) => Future<U, V>;
mapError: <U = any, V extends Error = Error>(mapper: (error: E) => U | Promise<U> | Future<U, V>) => Future<U, V>;
finally: (mapper: (error?: E, result?: T) => any) => Future<T, E>;
deinit: () => void;
weak: () => Future<T, E>;
settle: (error?: E, result?: T) => void;
toPromise: () => Promise<T>;
then: Promise<T>['then'];
catch: Promise<T>['catch'];
finishPending: () => void;
deref: () => T | undefined;
}
export class Scheduler {
tick(): void;
deinit(): void;
asap(callback: () => void): void;
}
export function isFuture(value: any): boolean;
export function isDeinitError(error: any): boolean;

View File

@ -0,0 +1,30 @@
import { Future, Scheduler } from 'posterus';
import { fiber } from 'posterus/fiber';
const anyFuture = new Future();
anyFuture.settle(undefined, 10);
anyFuture.settle(undefined, 'foo');
anyFuture.settle('not an Error'); // $ExpectError
const future = new Future<string>();
future.settle(undefined, 'result');
future.settle(undefined, 10); // $ExpectError
const undefinedResult = Future.fromResult();
const futureString = future.mapResult<string>(() => 'result');
const futureWrongType = future.mapResult<string>(() => 9000); // $ExpectError
function* generatorTask() {
yield Promise.resolve();
return Promise.resolve(10);
}
const task = fiber(generatorTask())
.mapResult(res => 11)
.finally(() => ({}));
Future.scheduler.asap(() => {});
const scheduler = new Scheduler();
scheduler.tick();
scheduler.deinit();

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"posterus-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }