new license file version [CI SKIP]

This commit is contained in:
2023-03-15 13:43:57 +00:00
parent d8a3063735
commit 00359d25c1
5600 changed files with 523898 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
{
"name": "responselike",
"version": "2.0.1",
"description": "A response-like object for mocking a Node.js HTTP response stream",
"funding": "https://github.com/sponsors/sindresorhus",
"main": "src/index.js",
"scripts": {
"test": "xo && nyc ava",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"xo": {
"extends": "xo-lukechilds"
},
"keywords": [
"http",
"https",
"response",
"mock",
"request",
"responselike"
],
"repository": {
"type": "git",
"url": "https://github.com/sindresorhus/responselike.git"
},
"author": "lukechilds",
"license": "MIT",
"devDependencies": {
"ava": "^0.25.0",
"coveralls": "^3.0.0",
"eslint-config-xo-lukechilds": "^1.0.0",
"get-stream": "^3.0.0",
"nyc": "^11.8.0",
"xo": "^0.19.0"
},
"dependencies": {
"lowercase-keys": "^2.0.0"
}
}

View File

@@ -0,0 +1,182 @@
import { Subject, SubjectSubscriber } from '../Subject';
import { Operator } from '../Operator';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { Subscription } from '../Subscription';
import { TeardownLogic } from '../types';
import { refCount as higherOrderRefCount } from '../operators/refCount';
/**
* @class ConnectableObservable<T>
*/
export class ConnectableObservable<T> extends Observable<T> {
protected _subject: Subject<T>;
protected _refCount: number = 0;
protected _connection: Subscription;
/** @internal */
_isComplete = false;
constructor(public source: Observable<T>,
protected subjectFactory: () => Subject<T>) {
super();
}
/** @deprecated This is an internal implementation detail, do not use. */
_subscribe(subscriber: Subscriber<T>) {
return this.getSubject().subscribe(subscriber);
}
protected getSubject(): Subject<T> {
const subject = this._subject;
if (!subject || subject.isStopped) {
this._subject = this.subjectFactory();
}
return this._subject;
}
connect(): Subscription {
let connection = this._connection;
if (!connection) {
this._isComplete = false;
connection = this._connection = new Subscription();
connection.add(this.source
.subscribe(new ConnectableSubscriber(this.getSubject(), this)));
if (connection.closed) {
this._connection = null;
connection = Subscription.EMPTY;
}
}
return connection;
}
refCount(): Observable<T> {
return higherOrderRefCount()(this) as Observable<T>;
}
}
export const connectableObservableDescriptor: PropertyDescriptorMap = (() => {
const connectableProto = <any>ConnectableObservable.prototype;
return {
operator: { value: null as null },
_refCount: { value: 0, writable: true },
_subject: { value: null as null, writable: true },
_connection: { value: null as null, writable: true },
_subscribe: { value: connectableProto._subscribe },
_isComplete: { value: connectableProto._isComplete, writable: true },
getSubject: { value: connectableProto.getSubject },
connect: { value: connectableProto.connect },
refCount: { value: connectableProto.refCount }
};
})();
class ConnectableSubscriber<T> extends SubjectSubscriber<T> {
constructor(destination: Subject<T>,
private connectable: ConnectableObservable<T>) {
super(destination);
}
protected _error(err: any): void {
this._unsubscribe();
super._error(err);
}
protected _complete(): void {
this.connectable._isComplete = true;
this._unsubscribe();
super._complete();
}
protected _unsubscribe() {
const connectable = <any>this.connectable;
if (connectable) {
this.connectable = null;
const connection = connectable._connection;
connectable._refCount = 0;
connectable._subject = null;
connectable._connection = null;
if (connection) {
connection.unsubscribe();
}
}
}
}
class RefCountOperator<T> implements Operator<T, T> {
constructor(private connectable: ConnectableObservable<T>) {
}
call(subscriber: Subscriber<T>, source: any): TeardownLogic {
const { connectable } = this;
(<any> connectable)._refCount++;
const refCounter = new RefCountSubscriber(subscriber, connectable);
const subscription = source.subscribe(refCounter);
if (!refCounter.closed) {
(<any> refCounter).connection = connectable.connect();
}
return subscription;
}
}
class RefCountSubscriber<T> extends Subscriber<T> {
private connection: Subscription;
constructor(destination: Subscriber<T>,
private connectable: ConnectableObservable<T>) {
super(destination);
}
protected _unsubscribe() {
const { connectable } = this;
if (!connectable) {
this.connection = null;
return;
}
this.connectable = null;
const refCount = (<any> connectable)._refCount;
if (refCount <= 0) {
this.connection = null;
return;
}
(<any> connectable)._refCount = refCount - 1;
if (refCount > 1) {
this.connection = null;
return;
}
///
// Compare the local RefCountSubscriber's connection Subscription to the
// connection Subscription on the shared ConnectableObservable. In cases
// where the ConnectableObservable source synchronously emits values, and
// the RefCountSubscriber's downstream Observers synchronously unsubscribe,
// execution continues to here before the RefCountOperator has a chance to
// supply the RefCountSubscriber with the shared connection Subscription.
// For example:
// ```
// range(0, 10).pipe(
// publish(),
// refCount(),
// take(5),
// ).subscribe();
// ```
// In order to account for this case, RefCountSubscriber should only dispose
// the ConnectableObservable's shared connection Subscription if the
// connection Subscription exists, *and* either:
// a. RefCountSubscriber doesn't have a reference to the shared connection
// Subscription yet, or,
// b. RefCountSubscriber's connection Subscription reference is identical
// to the shared connection Subscription
///
const { connection } = this;
const sharedConnection = (<any> connectable)._connection;
this.connection = null;
if (sharedConnection && (!connection || sharedConnection === connection)) {
sharedConnection.unsubscribe();
}
}
}

View File

@@ -0,0 +1,82 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cosmiconfig = cosmiconfig;
exports.cosmiconfigSync = cosmiconfigSync;
exports.defaultLoaders = void 0;
var _os = _interopRequireDefault(require("os"));
var _Explorer = require("./Explorer");
var _ExplorerSync = require("./ExplorerSync");
var _loaders = require("./loaders");
var _types = require("./types");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function cosmiconfig(moduleName, options = {}) {
const normalizedOptions = normalizeOptions(moduleName, options);
const explorer = new _Explorer.Explorer(normalizedOptions);
return {
search: explorer.search.bind(explorer),
load: explorer.load.bind(explorer),
clearLoadCache: explorer.clearLoadCache.bind(explorer),
clearSearchCache: explorer.clearSearchCache.bind(explorer),
clearCaches: explorer.clearCaches.bind(explorer)
};
} // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function cosmiconfigSync(moduleName, options = {}) {
const normalizedOptions = normalizeOptions(moduleName, options);
const explorerSync = new _ExplorerSync.ExplorerSync(normalizedOptions);
return {
search: explorerSync.searchSync.bind(explorerSync),
load: explorerSync.loadSync.bind(explorerSync),
clearLoadCache: explorerSync.clearLoadCache.bind(explorerSync),
clearSearchCache: explorerSync.clearSearchCache.bind(explorerSync),
clearCaches: explorerSync.clearCaches.bind(explorerSync)
};
} // do not allow mutation of default loaders. Make sure it is set inside options
const defaultLoaders = Object.freeze({
'.cjs': _loaders.loaders.loadJs,
'.js': _loaders.loaders.loadJs,
'.json': _loaders.loaders.loadJson,
'.yaml': _loaders.loaders.loadYaml,
'.yml': _loaders.loaders.loadYaml,
noExt: _loaders.loaders.loadYaml
});
exports.defaultLoaders = defaultLoaders;
const identity = function identity(x) {
return x;
};
function normalizeOptions(moduleName, options) {
const defaults = {
packageProp: moduleName,
searchPlaces: ['package.json', `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.js`, `.${moduleName}rc.cjs`, `${moduleName}.config.js`, `${moduleName}.config.cjs`],
ignoreEmptySearchPlaces: true,
stopDir: _os.default.homedir(),
cache: true,
transform: identity,
loaders: defaultLoaders
};
const normalizedOptions = { ...defaults,
...options,
loaders: { ...defaults.loaders,
...options.loaders
}
};
return normalizedOptions;
}
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"skipWhile.js","sources":["../src/operators/skipWhile.ts"],"names":[],"mappings":";;;;;AAAA,qDAAgD"}

View File

@@ -0,0 +1,69 @@
/** PURE_IMPORTS_START _Observable,_util_isArray,_operators_map,_util_isObject,_from PURE_IMPORTS_END */
import { Observable } from '../Observable';
import { isArray } from '../util/isArray';
import { map } from '../operators/map';
import { isObject } from '../util/isObject';
import { from } from './from';
export function forkJoin() {
var sources = [];
for (var _i = 0; _i < arguments.length; _i++) {
sources[_i] = arguments[_i];
}
if (sources.length === 1) {
var first_1 = sources[0];
if (isArray(first_1)) {
return forkJoinInternal(first_1, null);
}
if (isObject(first_1) && Object.getPrototypeOf(first_1) === Object.prototype) {
var keys = Object.keys(first_1);
return forkJoinInternal(keys.map(function (key) { return first_1[key]; }), keys);
}
}
if (typeof sources[sources.length - 1] === 'function') {
var resultSelector_1 = sources.pop();
sources = (sources.length === 1 && isArray(sources[0])) ? sources[0] : sources;
return forkJoinInternal(sources, null).pipe(map(function (args) { return resultSelector_1.apply(void 0, args); }));
}
return forkJoinInternal(sources, null);
}
function forkJoinInternal(sources, keys) {
return new Observable(function (subscriber) {
var len = sources.length;
if (len === 0) {
subscriber.complete();
return;
}
var values = new Array(len);
var completed = 0;
var emitted = 0;
var _loop_1 = function (i) {
var source = from(sources[i]);
var hasValue = false;
subscriber.add(source.subscribe({
next: function (value) {
if (!hasValue) {
hasValue = true;
emitted++;
}
values[i] = value;
},
error: function (err) { return subscriber.error(err); },
complete: function () {
completed++;
if (completed === len || !hasValue) {
if (emitted === len) {
subscriber.next(keys ?
keys.reduce(function (result, key, i) { return (result[key] = values[i], result); }, {}) :
values);
}
subscriber.complete();
}
}
}));
};
for (var i = 0; i < len; i++) {
_loop_1(i);
}
});
}
//# sourceMappingURL=forkJoin.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"observable.js","sources":["../../src/internal/symbol/observable.ts"],"names":[],"mappings":";;AACa,QAAA,UAAU,GAAG,CAAC,cAAM,OAAA,OAAO,MAAM,KAAK,UAAU,IAAI,MAAM,CAAC,UAAU,IAAI,cAAc,EAAnE,CAAmE,CAAC,EAAE,CAAC"}

View File

@@ -0,0 +1,33 @@
let Declaration = require('../declaration')
class GridStart extends Declaration {
/**
* Do not add prefix for unsupported value in IE
*/
check (decl) {
let value = decl.value
return !value.includes('/') || value.includes('span')
}
/**
* Return a final spec property
*/
normalize (prop) {
return prop.replace('-start', '')
}
/**
* Change property name for IE
*/
prefixed (prop, prefix) {
let result = super.prefixed(prop, prefix)
if (prefix === '-ms-') {
result = result.replace('-start', '')
}
return result
}
}
GridStart.names = ['grid-row-start', 'grid-column-start']
module.exports = GridStart

View File

@@ -0,0 +1 @@
{"version":3,"file":"forkJoin.js","sources":["../../src/add/observable/forkJoin.ts"],"names":[],"mappings":";;AAAA,+CAA6C"}

View File

@@ -0,0 +1,14 @@
import { RequestHeaders } from "./RequestHeaders";
import { RequestMethod } from "./RequestMethod";
import { RequestRequestOptions } from "./RequestRequestOptions";
import { Url } from "./Url";
/**
* Generic request options as they are returned by the `endpoint()` method
*/
export declare type RequestOptions = {
method: RequestMethod;
url: Url;
headers: RequestHeaders;
body?: any;
request?: RequestRequestOptions;
};

View File

@@ -0,0 +1,4 @@
import v35 from './v35.js';
import sha1 from './sha1.js';
var v5 = v35('v5', 0x50, sha1);
export default v5;

View File

@@ -0,0 +1,28 @@
declare const pathExists: {
/**
Check if a path exists.
@returns Whether the path exists.
@example
```
// foo.ts
import pathExists = require('path-exists');
(async () => {
console.log(await pathExists('foo.ts'));
//=> true
})();
```
*/
(path: string): Promise<boolean>;
/**
Synchronously check if a path exists.
@returns Whether the path exists.
*/
sync(path: string): boolean;
};
export = pathExists;

View File

@@ -0,0 +1 @@
module.exports={A:{A:{"1":"B","4":"J E F G A BC"},B:{"1":"C K L H M","129":"N O P Q R S T U V W X Y Z a b c d f g h i j k l m n o p q r s D t"},C:{"1":"0 1 2 3 4 5 6 7 8 9 J E F G A B C K L H M N O v w x y z AB BB CB DB EB FB GB HB IB JB KB LB MB NB OB PB QB","4":"CC tB I u DC EC","129":"RB SB TB UB VB WB XB YB uB ZB vB aB bB cB dB eB fB gB hB iB jB kB e lB mB nB oB pB P Q R wB S T U V W X Y Z a b c d f g h i j k l m n o p q r s D t xB yB"},D:{"1":"LB MB NB OB PB QB RB SB TB UB","4":"I u J","129":"0 1 2 3 4 5 6 7 8 9 E F G A B C K L H M N O v w x y z AB BB CB DB EB FB GB HB IB JB KB VB WB XB YB uB ZB vB aB bB cB dB eB fB gB hB iB jB kB e lB mB nB oB pB P Q R S T U V W X Y Z a b c d f g h i j k l m n o p q r s D t xB yB FC"},E:{"4":"I u GC zB","129":"J E F G A B C K L H HC IC JC KC 0B qB rB 1B LC MC 2B 3B 4B 5B sB 6B 7B 8B NC"},F:{"1":"8 9 C AB BB CB DB EB FB GB HB qB 9B SC rB","4":"G B OC PC QC RC","129":"0 1 2 3 4 5 6 7 H M N O v w x y z IB JB KB LB MB NB OB PB QB RB SB TB UB VB WB XB YB ZB aB bB cB dB eB fB gB hB iB jB kB e lB mB nB oB pB P Q R wB S T U V W X Y Z a b c d"},G:{"4":"zB TC AC","129":"F UC VC WC XC YC ZC aC bC cC dC eC fC gC hC iC jC kC lC mC 2B 3B 4B 5B sB 6B 7B 8B"},H:{"4":"nC"},I:{"4":"oC pC qC","129":"tB I D rC AC sC tC"},J:{"129":"E A"},K:{"1":"C qB 9B rB","4":"A B","129":"e"},L:{"129":"D"},M:{"129":"D"},N:{"1":"B","4":"A"},O:{"129":"uC"},P:{"129":"I vC wC xC yC zC 0B 0C 1C 2C 3C 4C sB 5C 6C 7C"},Q:{"129":"1B"},R:{"129":"8C"},S:{"1":"9C"}},B:1,C:"dataset & data-* attributes"};

View File

@@ -0,0 +1 @@
{"version":3,"file":"interval.js","sources":["../../../src/internal/observable/interval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAmD9C,MAAM,UAAU,QAAQ,CAAC,MAAU,EACV,SAAgC;IADhC,uBAAA,EAAA,UAAU;IACV,0BAAA,EAAA,iBAAgC;IACvD,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE;QACpC,MAAM,GAAG,CAAC,CAAC;KACZ;IAED,IAAI,CAAC,SAAS,IAAI,OAAO,SAAS,CAAC,QAAQ,KAAK,UAAU,EAAE;QAC1D,SAAS,GAAG,KAAK,CAAC;KACnB;IAED,OAAO,IAAI,UAAU,CAAS,UAAA,UAAU;QACtC,UAAU,CAAC,GAAG,CACZ,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,UAAU,YAAA,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,QAAA,EAAE,CAAC,CACzE,CAAC;QACF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,QAAQ,CAAuC,KAAoB;IAClE,IAAA,6BAAU,EAAE,uBAAO,EAAE,qBAAM,CAAW;IAC9C,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACzB,IAAI,CAAC,QAAQ,CAAC,EAAE,UAAU,YAAA,EAAE,OAAO,EAAE,OAAO,GAAG,CAAC,EAAE,MAAM,QAAA,EAAE,EAAE,MAAM,CAAC,CAAC;AACtE,CAAC"}

View File

@@ -0,0 +1 @@
{"version":3,"file":"first.js","sources":["../src/operators/first.ts"],"names":[],"mappings":";;;;;AAAA,iDAA4C"}

View File

@@ -0,0 +1,109 @@
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
import {OperatorFunction} from '../types';
export function find<T, S extends T>(predicate: (value: T, index: number, source: Observable<T>) => value is S,
thisArg?: any): OperatorFunction<T, S | undefined>;
export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any): OperatorFunction<T, T | undefined>;
/**
* Emits only the first value emitted by the source Observable that meets some
* condition.
*
* <span class="informal">Finds the first value that passes some test and emits
* that.</span>
*
* ![](find.png)
*
* `find` searches for the first item in the source Observable that matches the
* specified condition embodied by the `predicate`, and returns the first
* occurrence in the source. Unlike {@link first}, the `predicate` is required
* in `find`, and does not emit an error if a valid value is not found.
*
* ## Example
* Find and emit the first click that happens on a DIV element
* ```ts
* import { fromEvent } from 'rxjs';
* import { find } from 'rxjs/operators';
*
* const clicks = fromEvent(document, 'click');
* const result = clicks.pipe(find(ev => ev.target.tagName === 'DIV'));
* result.subscribe(x => console.log(x));
* ```
*
* @see {@link filter}
* @see {@link first}
* @see {@link findIndex}
* @see {@link take}
*
* @param {function(value: T, index: number, source: Observable<T>): boolean} predicate
* A function called with each item to test for condition matching.
* @param {any} [thisArg] An optional argument to determine the value of `this`
* in the `predicate` function.
* @return {Observable<T>} An Observable of the first item that matches the
* condition.
* @method find
* @owner Observable
*/
export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any): OperatorFunction<T, T | undefined> {
if (typeof predicate !== 'function') {
throw new TypeError('predicate is not a function');
}
return (source: Observable<T>) => source.lift(new FindValueOperator(predicate, source, false, thisArg)) as Observable<T | undefined>;
}
export class FindValueOperator<T> implements Operator<T, T | number | undefined> {
constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
private source: Observable<T>,
private yieldIndex: boolean,
private thisArg?: any) {
}
call(observer: Subscriber<T>, source: any): any {
return source.subscribe(new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
export class FindValueSubscriber<T> extends Subscriber<T> {
private index: number = 0;
constructor(destination: Subscriber<T>,
private predicate: (value: T, index: number, source: Observable<T>) => boolean,
private source: Observable<T>,
private yieldIndex: boolean,
private thisArg?: any) {
super(destination);
}
private notifyComplete(value: any): void {
const destination = this.destination;
destination.next(value);
destination.complete();
this.unsubscribe();
}
protected _next(value: T): void {
const {predicate, thisArg} = this;
const index = this.index++;
try {
const result = predicate.call(thisArg || this, value, index, this.source);
if (result) {
this.notifyComplete(this.yieldIndex ? index : value);
}
} catch (err) {
this.destination.error(err);
}
}
protected _complete(): void {
this.notifyComplete(this.yieldIndex ? -1 : undefined);
}
}

View File

@@ -0,0 +1,82 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isFQDN;
var _assertString = _interopRequireDefault(require("./util/assertString"));
var _merge = _interopRequireDefault(require("./util/merge"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var default_fqdn_options = {
require_tld: true,
allow_underscores: false,
allow_trailing_dot: false,
allow_numeric_tld: false
};
function isFQDN(str, options) {
(0, _assertString.default)(str);
options = (0, _merge.default)(options, default_fqdn_options);
/* Remove the optional trailing dot before checking validity */
if (options.allow_trailing_dot && str[str.length - 1] === '.') {
str = str.substring(0, str.length - 1);
}
var parts = str.split('.');
var tld = parts[parts.length - 1];
if (options.require_tld) {
// disallow fqdns without tld
if (parts.length < 2) {
return false;
}
if (!/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
return false;
} // disallow spaces && special characers
if (/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20\u00A9\uFFFD]/.test(tld)) {
return false;
}
} // reject numeric TLDs
if (!options.allow_numeric_tld && /^\d+$/.test(tld)) {
return false;
}
return parts.every(function (part) {
if (part.length > 63) {
return false;
}
if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) {
return false;
} // disallow full-width chars
if (/[\uff01-\uff5e]/.test(part)) {
return false;
} // disallow parts starting or ending with hyphen
if (/^-|-$/.test(part)) {
return false;
}
if (!options.allow_underscores && /_/.test(part)) {
return false;
}
return true;
});
}
module.exports = exports.default;
module.exports.default = exports.default;

View File

@@ -0,0 +1,158 @@
import { Operator } from '../Operator';
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { Subscription } from '../Subscription';
import { MonoTypeOperatorFunction, SubscribableOrPromise, TeardownLogic } from '../types';
import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
export interface ThrottleConfig {
leading?: boolean;
trailing?: boolean;
}
export const defaultThrottleConfig: ThrottleConfig = {
leading: true,
trailing: false
};
/**
* Emits a value from the source Observable, then ignores subsequent source
* values for a duration determined by another Observable, then repeats this
* process.
*
* <span class="informal">It's like {@link throttleTime}, but the silencing
* duration is determined by a second Observable.</span>
*
* ![](throttle.png)
*
* `throttle` emits the source Observable values on the output Observable
* when its internal timer is disabled, and ignores source values when the timer
* is enabled. Initially, the timer is disabled. As soon as the first source
* value arrives, it is forwarded to the output Observable, and then the timer
* is enabled by calling the `durationSelector` function with the source value,
* which returns the "duration" Observable. When the duration Observable emits a
* value or completes, the timer is disabled, and this process repeats for the
* next source value.
*
* ## Example
* Emit clicks at a rate of at most one click per second
* ```ts
* import { fromEvent } from 'rxjs';
* import { throttle } from 'rxjs/operators';
*
* const clicks = fromEvent(document, 'click');
* const result = clicks.pipe(throttle(ev => interval(1000)));
* result.subscribe(x => console.log(x));
* ```
*
* @see {@link audit}
* @see {@link debounce}
* @see {@link delayWhen}
* @see {@link sample}
* @see {@link throttleTime}
*
* @param {function(value: T): SubscribableOrPromise} durationSelector A function
* that receives a value from the source Observable, for computing the silencing
* duration for each source value, returned as an Observable or a Promise.
* @param {Object} config a configuration object to define `leading` and `trailing` behavior. Defaults
* to `{ leading: true, trailing: false }`.
* @return {Observable<T>} An Observable that performs the throttle operation to
* limit the rate of emissions from the source.
* @method throttle
* @owner Observable
*/
export function throttle<T>(durationSelector: (value: T) => SubscribableOrPromise<any>,
config: ThrottleConfig = defaultThrottleConfig): MonoTypeOperatorFunction<T> {
return (source: Observable<T>) => source.lift(new ThrottleOperator(durationSelector, !!config.leading, !!config.trailing));
}
class ThrottleOperator<T> implements Operator<T, T> {
constructor(private durationSelector: (value: T) => SubscribableOrPromise<any>,
private leading: boolean,
private trailing: boolean) {
}
call(subscriber: Subscriber<T>, source: any): TeardownLogic {
return source.subscribe(
new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing)
);
}
}
/**
* We need this JSDoc comment for affecting ESDoc
* @ignore
* @extends {Ignored}
*/
class ThrottleSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
private _throttled?: Subscription;
private _sendValue?: T;
private _hasValue = false;
constructor(protected destination: Subscriber<T>,
private durationSelector: (value: T) => SubscribableOrPromise<number>,
private _leading: boolean,
private _trailing: boolean) {
super(destination);
}
protected _next(value: T): void {
this._hasValue = true;
this._sendValue = value;
if (!this._throttled) {
if (this._leading) {
this.send();
} else {
this.throttle(value);
}
}
}
private send() {
const { _hasValue, _sendValue } = this;
if (_hasValue) {
this.destination.next(_sendValue);
this.throttle(_sendValue!);
}
this._hasValue = false;
this._sendValue = undefined;
}
private throttle(value: T): void {
const duration = this.tryDurationSelector(value);
if (!!duration) {
this.add(this._throttled = innerSubscribe(duration, new SimpleInnerSubscriber(this)));
}
}
private tryDurationSelector(value: T): SubscribableOrPromise<any> | null {
try {
return this.durationSelector(value);
} catch (err) {
this.destination.error(err);
return null;
}
}
private throttlingDone() {
const { _throttled, _trailing } = this;
if (_throttled) {
_throttled.unsubscribe();
}
this._throttled = undefined;
if (_trailing) {
this.send();
}
}
notifyNext(): void {
this.throttlingDone();
}
notifyComplete(): void {
this.throttlingDone();
}
}

View File

@@ -0,0 +1,35 @@
import { Observable } from '../Observable';
import { SchedulerLike } from '../types';
/** @deprecated resultSelector is deprecated, pipe to map instead */
export declare function bindNodeCallback(callbackFunc: Function, resultSelector: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any>;
export declare function bindNodeCallback<R1, R2, R3, R4>(callbackFunc: (callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
export declare function bindNodeCallback<R1, R2, R3>(callbackFunc: (callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2, R3]>;
export declare function bindNodeCallback<R1, R2>(callbackFunc: (callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): () => Observable<[R1, R2]>;
export declare function bindNodeCallback<R1>(callbackFunc: (callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): () => Observable<R1>;
export declare function bindNodeCallback(callbackFunc: (callback: (err: any) => any) => any, scheduler?: SchedulerLike): () => Observable<void>;
export declare function bindNodeCallback<A1, R1, R2, R3, R4>(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
export declare function bindNodeCallback<A1, R1, R2, R3>(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2, R3]>;
export declare function bindNodeCallback<A1, R1, R2>(callbackFunc: (arg1: A1, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<[R1, R2]>;
export declare function bindNodeCallback<A1, R1>(callbackFunc: (arg1: A1, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<R1>;
export declare function bindNodeCallback<A1>(callbackFunc: (arg1: A1, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1) => Observable<void>;
export declare function bindNodeCallback<A1, A2, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
export declare function bindNodeCallback<A1, A2, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2, R3]>;
export declare function bindNodeCallback<A1, A2, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<[R1, R2]>;
export declare function bindNodeCallback<A1, A2, R1>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<R1>;
export declare function bindNodeCallback<A1, A2>(callbackFunc: (arg1: A1, arg2: A2, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2) => Observable<void>;
export declare function bindNodeCallback<A1, A2, A3, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
export declare function bindNodeCallback<A1, A2, A3, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2, R3]>;
export declare function bindNodeCallback<A1, A2, A3, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<[R1, R2]>;
export declare function bindNodeCallback<A1, A2, A3, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<R1>;
export declare function bindNodeCallback<A1, A2, A3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3) => Observable<void>;
export declare function bindNodeCallback<A1, A2, A3, A4, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
export declare function bindNodeCallback<A1, A2, A3, A4, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2, R3]>;
export declare function bindNodeCallback<A1, A2, A3, A4, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<[R1, R2]>;
export declare function bindNodeCallback<A1, A2, A3, A4, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<R1>;
export declare function bindNodeCallback<A1, A2, A3, A4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Observable<void>;
export declare function bindNodeCallback<A1, A2, A3, A4, A5, R1, R2, R3, R4>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2, res3: R3, res4: R4, ...args: any[]) => any) => any, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;
export declare function bindNodeCallback<A1, A2, A3, A4, A5, R1, R2, R3>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2, res3: R3) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2, R3]>;
export declare function bindNodeCallback<A1, A2, A3, A4, A5, R1, R2>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1, res2: R2) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<[R1, R2]>;
export declare function bindNodeCallback<A1, A2, A3, A4, A5, R1>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, res1: R1) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<R1>;
export declare function bindNodeCallback<A1, A2, A3, A4, A5>(callbackFunc: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any) => any) => any, scheduler?: SchedulerLike): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Observable<void>;
export declare function bindNodeCallback(callbackFunc: Function, scheduler?: SchedulerLike): (...args: any[]) => Observable<any[]>;