apollo-link-retry
Advanced tools
Comparing version 0.6.1-beta.4 to 0.6.1-beta.5
@@ -17,2 +17,6 @@ (function (global, factory) { | ||
})(); | ||
var operationFnOrNumber = function (prop) { | ||
return typeof prop === 'number' ? function () { return prop; } : prop; | ||
}; | ||
var defaultInterval = function (delay) { return delay; }; | ||
var RetryLink = (function (_super) { | ||
@@ -22,7 +26,8 @@ __extends(RetryLink, _super); | ||
var _this = _super.call(this) || this; | ||
_this.count = 0; | ||
_this.defaultInterval = function (delay) { return delay; }; | ||
_this.max = (params && params.max) || 10; | ||
_this.delay = (params && params.delay) || 300; | ||
_this.interval = (params && params.interval) || _this.defaultInterval; | ||
_this.subscriptions = {}; | ||
_this.timers = {}; | ||
_this.counts = {}; | ||
_this.max = operationFnOrNumber((params && params.max) || 10); | ||
_this.delay = operationFnOrNumber((params && params.delay) || 300); | ||
_this.interval = (params && params.interval) || defaultInterval; | ||
return _this; | ||
@@ -32,15 +37,18 @@ } | ||
var _this = this; | ||
var key = operation.toKey(); | ||
if (!this.counts[key]) | ||
this.counts[key] = 0; | ||
return new apolloLink.Observable(function (observer) { | ||
var subscriber = { | ||
next: function (data) { | ||
_this.count = 0; | ||
_this.counts[key] = 0; | ||
observer.next(data); | ||
}, | ||
error: function (error) { | ||
_this.count++; | ||
if (_this.count < _this.max) { | ||
_this.timer = setTimeout(function () { | ||
_this.counts[key]++; | ||
if (_this.counts[key] < _this.max(operation)) { | ||
_this.timers[key] = setTimeout(function () { | ||
var observable = forward(operation); | ||
_this.subscription = observable.subscribe(subscriber); | ||
}, _this.interval(_this.delay, _this.count)); | ||
_this.subscriptions[key] = observable.subscribe(subscriber); | ||
}, _this.interval(_this.delay(operation), _this.counts[key])); | ||
} | ||
@@ -53,8 +61,7 @@ else { | ||
}; | ||
_this.subscription = forward(operation).subscribe(subscriber); | ||
_this.subscriptions[key] = forward(operation).subscribe(subscriber); | ||
return function () { | ||
_this.subscription.unsubscribe(); | ||
if (_this.timer) { | ||
clearTimeout(_this.timer); | ||
} | ||
_this.subscriptions[key].unsubscribe(); | ||
if (_this.timers[key]) | ||
clearTimeout(_this.timers[key]); | ||
}; | ||
@@ -61,0 +68,0 @@ }); |
import { ApolloLink, Observable, Operation, NextLink, FetchResult } from 'apollo-link'; | ||
export declare type ParamFnOrNumber = (operation: Operation) => number | number; | ||
export default class RetryLink extends ApolloLink { | ||
private count; | ||
private delay; | ||
private max; | ||
private interval; | ||
private subscription; | ||
private timer; | ||
private subscriptions; | ||
private timers; | ||
private counts; | ||
constructor(params?: { | ||
max?: number; | ||
delay?: number; | ||
max?: ParamFnOrNumber; | ||
delay?: ParamFnOrNumber; | ||
interval?: (delay: number, count: number) => number; | ||
}); | ||
request(operation: Operation, forward: NextLink): Observable<FetchResult>; | ||
private defaultInterval; | ||
} |
@@ -12,2 +12,6 @@ var __extends = (this && this.__extends) || (function () { | ||
import { ApolloLink, Observable, } from 'apollo-link'; | ||
var operationFnOrNumber = function (prop) { | ||
return typeof prop === 'number' ? function () { return prop; } : prop; | ||
}; | ||
var defaultInterval = function (delay) { return delay; }; | ||
var RetryLink = (function (_super) { | ||
@@ -17,7 +21,8 @@ __extends(RetryLink, _super); | ||
var _this = _super.call(this) || this; | ||
_this.count = 0; | ||
_this.defaultInterval = function (delay) { return delay; }; | ||
_this.max = (params && params.max) || 10; | ||
_this.delay = (params && params.delay) || 300; | ||
_this.interval = (params && params.interval) || _this.defaultInterval; | ||
_this.subscriptions = {}; | ||
_this.timers = {}; | ||
_this.counts = {}; | ||
_this.max = operationFnOrNumber((params && params.max) || 10); | ||
_this.delay = operationFnOrNumber((params && params.delay) || 300); | ||
_this.interval = (params && params.interval) || defaultInterval; | ||
return _this; | ||
@@ -27,15 +32,18 @@ } | ||
var _this = this; | ||
var key = operation.toKey(); | ||
if (!this.counts[key]) | ||
this.counts[key] = 0; | ||
return new Observable(function (observer) { | ||
var subscriber = { | ||
next: function (data) { | ||
_this.count = 0; | ||
_this.counts[key] = 0; | ||
observer.next(data); | ||
}, | ||
error: function (error) { | ||
_this.count++; | ||
if (_this.count < _this.max) { | ||
_this.timer = setTimeout(function () { | ||
_this.counts[key]++; | ||
if (_this.counts[key] < _this.max(operation)) { | ||
_this.timers[key] = setTimeout(function () { | ||
var observable = forward(operation); | ||
_this.subscription = observable.subscribe(subscriber); | ||
}, _this.interval(_this.delay, _this.count)); | ||
_this.subscriptions[key] = observable.subscribe(subscriber); | ||
}, _this.interval(_this.delay(operation), _this.counts[key])); | ||
} | ||
@@ -48,8 +56,7 @@ else { | ||
}; | ||
_this.subscription = forward(operation).subscribe(subscriber); | ||
_this.subscriptions[key] = forward(operation).subscribe(subscriber); | ||
return function () { | ||
_this.subscription.unsubscribe(); | ||
if (_this.timer) { | ||
clearTimeout(_this.timer); | ||
} | ||
_this.subscriptions[key].unsubscribe(); | ||
if (_this.timers[key]) | ||
clearTimeout(_this.timers[key]); | ||
}; | ||
@@ -56,0 +63,0 @@ }); |
{ | ||
"name": "apollo-link-retry", | ||
"version": "0.6.1-beta.4", | ||
"version": "0.6.1-beta.5", | ||
"description": "Retry Apollo Link for GraphQL Network Stack", | ||
@@ -26,3 +26,4 @@ "author": "Evans Hauser <evanshauser@gmail.com>", | ||
"scripts": { | ||
"build:browser": "browserify ./lib/bundle.umd.js -o=./lib/bundle.js --i apollo-link && npm run minify:browser", | ||
"build:browser": | ||
"browserify ./lib/bundle.umd.js -o=./lib/bundle.js --i apollo-link && npm run minify:browser", | ||
"build": "tsc -p .", | ||
@@ -32,4 +33,6 @@ "bundle": "rollup -c", | ||
"filesize": "npm run build && npm run build:browser", | ||
"lint": "tslint --type-check -p tsconfig.json -c ../../tslint.json src/*.ts", | ||
"minify:browser": "uglifyjs -c -m -o ./lib/bundle.min.js -- ./lib/bundle.js", | ||
"lint": | ||
"tslint --type-check -p tsconfig.json -c ../../tslint.json src/*.ts", | ||
"minify:browser": | ||
"uglifyjs -c -m -o ./lib/bundle.min.js -- ./lib/bundle.js", | ||
"postbuild": "npm run bundle", | ||
@@ -42,3 +45,3 @@ "prebuild": "npm run clean", | ||
"peerDependencies": { | ||
"apollo-link": "^0.6.1-beta.4" | ||
"apollo-link": "^0.6.1-beta.5" | ||
}, | ||
@@ -48,3 +51,3 @@ "devDependencies": { | ||
"@types/jest": "^20.0.8", | ||
"apollo-link": "^0.6.1-beta.4", | ||
"apollo-link": "^0.6.1-beta.5", | ||
"browserify": "^14.4.0", | ||
@@ -66,9 +69,4 @@ "graphql": "0.10.5", | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"json" | ||
] | ||
"moduleFileExtensions": ["ts", "tsx", "js", "json"] | ||
} | ||
} |
@@ -89,2 +89,54 @@ import gql from 'graphql-tag'; | ||
}); | ||
it('should allow a function for the delay prop', done => { | ||
const retry = new RetryLink({ | ||
delay: operation => operation.getContext().delay, | ||
max: 2, | ||
}); | ||
const error = new Error('I never work'); | ||
const data = <FetchResult>{ | ||
data: { | ||
hello: 'world', | ||
}, | ||
}; | ||
const stub = jest.fn(); | ||
stub.mockReturnValueOnce(new Observable(observer => observer.error(error))); | ||
stub.mockReturnValueOnce(Observable.of(data)); | ||
const link = ApolloLink.from([retry, stub]); | ||
execute(link, { query, context: { delay: 1 } }).subscribe( | ||
actualData => { | ||
expect(stub).toHaveBeenCalledTimes(2); | ||
expect(data).toEqual(actualData); | ||
}, | ||
() => { | ||
throw new Error(); | ||
}, | ||
done, | ||
); | ||
}); | ||
it('allow setting a function for max', done => { | ||
const max = 10; | ||
const retry = new RetryLink({ delay: 1, max: x => x.getContext().max }); | ||
const error = new Error('I never work'); | ||
const stub = jest.fn(() => { | ||
return new Observable(observer => observer.error(error)); | ||
}); | ||
const link = ApolloLink.from([retry, stub]); | ||
execute(link, { query, context: { max } }).subscribe( | ||
() => { | ||
throw new Error(); | ||
}, | ||
actualError => { | ||
expect(stub).toHaveBeenCalledTimes(max); | ||
expect(error).toEqual(actualError); | ||
done(); | ||
}, | ||
() => { | ||
throw new Error(); | ||
}, | ||
); | ||
}); | ||
}); |
@@ -10,19 +10,26 @@ import { | ||
const operationFnOrNumber = prop => | ||
typeof prop === 'number' ? () => prop : prop; | ||
const defaultInterval = delay => delay; | ||
export type ParamFnOrNumber = (operation: Operation) => number | number; | ||
export default class RetryLink extends ApolloLink { | ||
private count: number = 0; | ||
private delay: number; | ||
private max: number; | ||
private delay: ParamFnOrNumber; | ||
private max: ParamFnOrNumber; | ||
private interval: (delay: number, count: number) => number; | ||
private subscription: ZenObservable.Subscription; | ||
private timer; | ||
private subscriptions: { [key: string]: ZenObservable.Subscription } = {}; | ||
private timers = {}; | ||
private counts: { [key: string]: number } = {}; | ||
constructor(params?: { | ||
max?: number; | ||
delay?: number; | ||
max?: ParamFnOrNumber; | ||
delay?: ParamFnOrNumber; | ||
interval?: (delay: number, count: number) => number; | ||
}) { | ||
super(); | ||
this.max = (params && params.max) || 10; | ||
this.delay = (params && params.delay) || 300; | ||
this.interval = (params && params.interval) || this.defaultInterval; | ||
this.max = operationFnOrNumber((params && params.max) || 10); | ||
this.delay = operationFnOrNumber((params && params.delay) || 300); | ||
this.interval = (params && params.interval) || defaultInterval; | ||
} | ||
@@ -34,15 +41,17 @@ | ||
): Observable<FetchResult> { | ||
const key = operation.toKey(); | ||
if (!this.counts[key]) this.counts[key] = 0; | ||
return new Observable(observer => { | ||
const subscriber = { | ||
next: data => { | ||
this.count = 0; | ||
this.counts[key] = 0; | ||
observer.next(data); | ||
}, | ||
error: error => { | ||
this.count++; | ||
if (this.count < this.max) { | ||
this.timer = setTimeout(() => { | ||
this.counts[key]++; | ||
if (this.counts[key] < this.max(operation)) { | ||
this.timers[key] = setTimeout(() => { | ||
const observable = forward(operation); | ||
this.subscription = observable.subscribe(subscriber); | ||
}, this.interval(this.delay, this.count)); | ||
this.subscriptions[key] = observable.subscribe(subscriber); | ||
}, this.interval(this.delay(operation), this.counts[key])); | ||
} else { | ||
@@ -55,14 +64,10 @@ observer.error(error); | ||
this.subscription = forward(operation).subscribe(subscriber); | ||
this.subscriptions[key] = forward(operation).subscribe(subscriber); | ||
return () => { | ||
this.subscription.unsubscribe(); | ||
if (this.timer) { | ||
clearTimeout(this.timer); | ||
} | ||
this.subscriptions[key].unsubscribe(); | ||
if (this.timers[key]) clearTimeout(this.timers[key]); | ||
}; | ||
}); | ||
} | ||
private defaultInterval = delay => delay; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
23910
11
361
0
47
0