UNPKG

1.81 kBPlain TextView Raw
1import assert = require('assert');
2import types = require('../types');
3export = Config;
4
5
6/** A config object holds the configuration options for a variant of the async function. */
7class Config implements types.AsyncOptions {
8
9 /** Construct a new Config instance. */
10 constructor(options?: types.AsyncOptions) {
11 if (options) {
12 this.returnValue = options.returnValue;
13 this.acceptsCallback = options.acceptsCallback;
14 this.isIterable = options.isIterable;
15 this.maxConcurrency = options.maxConcurrency;
16 }
17 }
18
19 /** Recognised values: 'none', 'promise', 'thunk', 'result'. */
20 returnValue: string = Config.PROMISE;
21
22 /** Indicates whether a callback function, if supplied, will be used to notify waiters of results. */
23 acceptsCallback: boolean = false;
24
25 /** Indicates whether the suspendable function has iterator semantics or normal semantics. */
26 isIterable: boolean = false;
27
28 /** Indicates whether top-level concurrency should be limited to a specified ceiling. */
29 maxConcurrency: number = null;
30
31 // Constants for use with returnValue and callbackArg
32 static PROMISE = 'promise';
33 static THUNK = 'thunk';
34 static RESULT = 'result';
35 static NONE = 'none';
36
37 /** Checks all configuration values and throw an error if anything is invalid. */
38 validate() {
39 var knownRetVal = [Config.PROMISE, Config.THUNK, Config.RESULT, Config.NONE].indexOf(this.returnValue) !== -1;
40 assert(knownRetVal, 'Unrecognised return value: ' + this.returnValue);
41
42 var hasNotifier = this.returnValue !== Config.NONE || this.acceptsCallback;
43 assert(hasNotifier, 'At least one notification method must be enabled.');
44 }
45}