From 547f7b6eb1f6ffe975c7deb1cb41fc2f11c35705 Mon Sep 17 00:00:00 2001 From: zswang Date: Tue, 21 Aug 2018 20:56:42 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fixed:=20Termination=20after=20c?= =?UTF-8?q?atch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/index.d.ts | 35 +++++++++++++++++++++++++++++++++-- lib/index.js | 44 +++++++++++++++++++++++++++++++++++++++----- lib/index.ts | 44 +++++++++++++++++++++++++++++++++++++++----- package.json | 2 +- src/index.ts | 40 ++++++++++++++++++++++++++++++++++++++++ test/index.js | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 184 insertions(+), 13 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 99c1d0c..c78dd24 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -20,8 +20,8 @@ export interface ICacheOptions { * 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 { /** @@ -211,6 +211,37 @@ export declare class Cache { // > 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; /** diff --git a/lib/index.js b/lib/index.js index 62d7961..1dbf1f9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { @@ -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; @@ -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] || []; @@ -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) @@ -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())) { diff --git a/lib/index.ts b/lib/index.ts index d7250ff..150c676 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -20,8 +20,8 @@ export interface ICacheOptions { * 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 { /** @@ -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 { const now = Date.now() @@ -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] || [] @@ -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) @@ -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())) { diff --git a/package.json b/package.json index 57671d3..4b84405 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 501a03c..790c166 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,6 +92,10 @@ export class Cache { ```js (**) ``` + * @example fetch():resume + ```js + (**) + ``` */ fetch(key: string | number = ''): Promise { const now = Date.now() @@ -127,6 +131,7 @@ export class Cache { this.flush() this.fetching[key] = true + this.fetchData[key] = null return new Promise((resolve, reject) => { this.options .fetch(key) @@ -143,6 +148,8 @@ export class Cache { resolve(data) }) .catch(err => { + this.fetchedAt[key] = 0 + this.fetching[key] = false if (this.queue[key]) { let item while ((item = this.queue[key].shift())) { @@ -351,4 +358,37 @@ cache6.fetch(6).then(data => { // > 666 }) /**/ + +/**/ +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) +/**/ /**/ diff --git a/test/index.js b/test/index.js index 46a593c..9debf40 100644 --- a/test/index.js +++ b/test/index.js @@ -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) + }); + }); \ No newline at end of file