Skip to content

Commit

Permalink
🐛 fixed: Termination after catch
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Aug 21, 2018
1 parent 6713178 commit 547f7b6
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 13 deletions.
35 changes: 33 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export interface ICacheOptions<T> {
* Cache of fetch data
* @author
* zswang (http://weibo.com/zswang)
* @version 0.1.9
* @date 2018-07-03
* @version 0.1.15
* @date 2018-08-21
*/
export declare class Cache<T> {
/**
Expand Down Expand Up @@ -211,6 +211,37 @@ export declare class Cache<T> {
// > 666
})
```
* @example fetch():resume
```js
let error
const cache7 = new jfetchs.Cache({
fetch: () => {
if (error) {
return Promise.reject(error)
}
return Promise.resolve('ok')
},
})
error = '#1'
cache7
.fetch()
.then()
.catch(err => {
console.log(err)
// > #1
})
setTimeout(() => {
error = null
cache7
.fetch()
.then(reply => {
console.log(reply)
// > ok
// * done
})
.catch()
}, 100)
```
*/
fetch(key?: string | number): Promise<T>;
/**
Expand Down
44 changes: 39 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var __assign = (this && this.__assign) || Object.assign || function(t) {
* Cache of fetch data
* @author
* zswang (http://weibo.com/zswang)
* @version 0.1.9
* @date 2018-07-03
* @version 0.1.15
* @date 2018-08-21
*/
var Cache = /** @class */ (function () {
function Cache(options) {
Expand Down Expand Up @@ -214,6 +214,37 @@ var __assign = (this && this.__assign) || Object.assign || function(t) {
// > 666
})
```
* @example fetch():resume
```js
let error
const cache7 = new jfetchs.Cache({
fetch: () => {
if (error) {
return Promise.reject(error)
}
return Promise.resolve('ok')
},
})
error = '#1'
cache7
.fetch()
.then()
.catch(err => {
console.log(err)
// > #1
})
setTimeout(() => {
error = null
cache7
.fetch()
.then(reply => {
console.log(reply)
// > ok
// * done
})
.catch()
}, 100)
```
*/
Cache.prototype.fetch = function (key) {
var _this = this;
Expand All @@ -224,13 +255,13 @@ var __assign = (this && this.__assign) || Object.assign || function(t) {
: '';
if (now - (this.fetchedAt[key] || 0) <= this.options.expire * 1000) {
if (this.options.debug) {
console.log("jfetchs/src/index.ts:106" + prefix + " hitting cache");
console.log("jfetchs/src/index.ts:110" + prefix + " hitting cache");
}
return Promise.resolve(this.fetchData[key]);
}
if (this.fetching[key]) {
if (this.options.debug) {
console.log("jfetchs/src/index.ts:113" + prefix + " fetching in queue");
console.log("jfetchs/src/index.ts:117" + prefix + " fetching in queue");
}
return new Promise(function (resolve, reject) {
_this.queue[key] = _this.queue[key] || [];
Expand All @@ -241,10 +272,11 @@ var __assign = (this && this.__assign) || Object.assign || function(t) {
});
}
if (this.options.debug) {
console.log("jfetchs/src/index.ts:125" + prefix + " missing cache");
console.log("jfetchs/src/index.ts:129" + prefix + " missing cache");
}
this.flush();
this.fetching[key] = true;
this.fetchData[key] = null;
return new Promise(function (resolve, reject) {
_this.options
.fetch(key)
Expand All @@ -261,6 +293,8 @@ var __assign = (this && this.__assign) || Object.assign || function(t) {
resolve(data);
})
.catch(function (err) {
_this.fetchedAt[key] = 0;
_this.fetching[key] = false;
if (_this.queue[key]) {
var item = void 0;
while ((item = _this.queue[key].shift())) {
Expand Down
44 changes: 39 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export interface ICacheOptions<T> {
* Cache of fetch data
* @author
* zswang (http://weibo.com/zswang)
* @version 0.1.9
* @date 2018-07-03
* @version 0.1.15
* @date 2018-08-21
*/
export class Cache<T> {
/**
Expand Down Expand Up @@ -222,6 +222,37 @@ cache6.fetch(6).then(data => {
// > 666
})
```
* @example fetch():resume
```js
let error
const cache7 = new jfetchs.Cache({
fetch: () => {
if (error) {
return Promise.reject(error)
}
return Promise.resolve('ok')
},
})
error = '#1'
cache7
.fetch()
.then()
.catch(err => {
console.log(err)
// > #1
})
setTimeout(() => {
error = null
cache7
.fetch()
.then(reply => {
console.log(reply)
// > ok
// * done
})
.catch()
}, 100)
```
*/
fetch(key: string | number = ''): Promise<T> {
const now = Date.now()
Expand All @@ -233,13 +264,13 @@ cache6.fetch(6).then(data => {
: ''
if (now - (this.fetchedAt[key] || 0) <= this.options.expire * 1000) {
if (this.options.debug) {
console.log(`jfetchs/src/index.ts:106${prefix} hitting cache`)
console.log(`jfetchs/src/index.ts:110${prefix} hitting cache`)
}
return Promise.resolve(this.fetchData[key])
}
if (this.fetching[key]) {
if (this.options.debug) {
console.log(`jfetchs/src/index.ts:113${prefix} fetching in queue`)
console.log(`jfetchs/src/index.ts:117${prefix} fetching in queue`)
}
return new Promise((resolve, reject) => {
this.queue[key] = this.queue[key] || []
Expand All @@ -250,10 +281,11 @@ cache6.fetch(6).then(data => {
})
}
if (this.options.debug) {
console.log(`jfetchs/src/index.ts:125${prefix} missing cache`)
console.log(`jfetchs/src/index.ts:129${prefix} missing cache`)
}
this.flush()
this.fetching[key] = true
this.fetchData[key] = null
return new Promise((resolve, reject) => {
this.options
.fetch(key)
Expand All @@ -270,6 +302,8 @@ cache6.fetch(6).then(data => {
resolve(data)
})
.catch(err => {
this.fetchedAt[key] = 0
this.fetching[key] = false
if (this.queue[key]) {
let item
while ((item = this.queue[key].shift())) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jfetchs",
"version": "0.1.9",
"version": "0.1.15",
"description": "Cache of fetch data",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
40 changes: 40 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export class Cache<T> {
```js
(*<jdists import="?debug[desc='key']" />*)
```
* @example fetch():resume
```js
(*<jdists import="?debug[desc='resume']" />*)
```
*/
fetch(key: string | number = ''): Promise<T> {
const now = Date.now()
Expand Down Expand Up @@ -127,6 +131,7 @@ export class Cache<T> {

this.flush()
this.fetching[key] = true
this.fetchData[key] = null
return new Promise((resolve, reject) => {
this.options
.fetch(key)
Expand All @@ -143,6 +148,8 @@ export class Cache<T> {
resolve(data)
})
.catch(err => {
this.fetchedAt[key] = 0
this.fetching[key] = false
if (this.queue[key]) {
let item
while ((item = this.queue[key].shift())) {
Expand Down Expand Up @@ -351,4 +358,37 @@ cache6.fetch(6).then(data => {
// > 666
})
/*</debug>*/

/*<debug desc="resume">*/
let error
const cache7 = new jfetchs.Cache({
fetch: () => {
if (error) {
return Promise.reject(error)
}
return Promise.resolve('ok')
},
})

error = '#1'
cache7
.fetch()
.then()
.catch(err => {
console.log(err)
// > #1
})

setTimeout(() => {
error = null
cache7
.fetch()
.then(reply => {
console.log(reply)
// > ok
// * done
})
.catch()
}, 100)
/*</debug>*/
/*</remove>*/
32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,37 @@ cache6.fetch(6).then(data => {
})
});

it("fetch():resume", function (done) {
examplejs_printLines = [];
let error
const cache7 = new jfetchs.Cache({
fetch: () => {
if (error) {
return Promise.reject(error)
}
return Promise.resolve('ok')
},
})
error = '#1'
cache7
.fetch()
.then()
.catch(err => {
examplejs_print(err)
assert.equal(examplejs_printLines.join("\n"), "#1"); examplejs_printLines = [];
})
setTimeout(() => {
error = null
cache7
.fetch()
.then(reply => {
examplejs_print(reply)
assert.equal(examplejs_printLines.join("\n"), "ok"); examplejs_printLines = [];
done();
})
.catch()
}, 100)
});

});

0 comments on commit 547f7b6

Please sign in to comment.