-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -380,6 +380,7 @@ | |
"shellSort", | ||
"shuffle", | ||
"SingleEmitter", | ||
"singleton", | ||
"size", | ||
"sizeof", | ||
"sleep", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## CN | ||
|
||
确保一个异步函数返回前只运行一个实例。 | ||
|
||
|参数名|说明| | ||
|-----|---| | ||
|fn|要限制的函数| | ||
|hashFn|计算缓存键名函数| | ||
|返回值|单例运行函数| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Ensure an async function is only called once before it resolves. | ||
* | ||
* |Name |Desc | | ||
* |------|----------------------------| | ||
* |fn |Function to restrict | | ||
* |hashFn|Function to create cache key| | ||
* |return|New restricted function | | ||
*/ | ||
|
||
/* example | ||
* const fetch = singleton(async function fetch(id) {}); | ||
* const f1 = fetch(1); | ||
* const f2 = fetch(1); | ||
* const f3 = fetch(2); | ||
* console.log(f1 === f2); // -> true | ||
* console.log(f1 === f3); // -> false | ||
* | ||
* await f1; | ||
* const f4 = fetch(1); | ||
* console.log(f1 === f4); // -> false | ||
*/ | ||
|
||
/* module | ||
* env: all | ||
*/ | ||
|
||
/* typescript | ||
* export declare function singleton<F extends types.Fn<Promise<any>>>( | ||
* fn: F, | ||
* hashFn?: types.AnyFn | ||
* ): F; | ||
*/ | ||
|
||
_('types has'); | ||
|
||
exports = function(fn, hashFn = JSON.stringify) { | ||
const singleton = function() { | ||
const cache = singleton.cache; | ||
const address = hashFn.apply(this, arguments); | ||
|
||
if (has(cache, address)) { | ||
return cache[address]; | ||
} | ||
|
||
const promise = fn.apply(this, arguments).finally(() => { | ||
delete cache[address]; | ||
}); | ||
cache[address] = promise; | ||
|
||
return promise; | ||
}; | ||
|
||
singleton.cache = {}; | ||
|
||
return singleton; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
it('basic', async function() { | ||
const fetch = singleton(async function fetch(id) { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve(id); | ||
}, 100); | ||
}); | ||
}); | ||
const f1 = fetch(1); | ||
const f2 = fetch(1); | ||
const f3 = fetch(2); | ||
expect(f1).to.equal(f2); | ||
expect(f1).to.not.equal(f3); | ||
|
||
expect(await f1).to.equal(1); | ||
const f4 = fetch(1); | ||
expect(f1).to.not.equal(f4); | ||
}); |