@livelybone/singleton 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +31 -0
- package/CHANGELOG.md +24 -0
- package/README.md +39 -8
- package/index.d.ts +19 -5
- package/lib/es/index.js +42 -19
- package/lib/umd/index.js +3 -3
- package/package.json +2 -2
- package/pnpm-lock.yaml +6100 -0
package/.editorconfig
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
root = true
|
2
|
+
|
3
|
+
[*]
|
4
|
+
charset = utf-8
|
5
|
+
end_of_line = lf
|
6
|
+
indent_size = 2
|
7
|
+
indent_style = space
|
8
|
+
insert_final_newline = true
|
9
|
+
max_line_length = 120
|
10
|
+
tab_width = 2
|
11
|
+
trim_trailing_whitespace = true
|
12
|
+
|
13
|
+
[*.less]
|
14
|
+
indent_size = 2
|
15
|
+
|
16
|
+
[*.sass]
|
17
|
+
indent_size = 2
|
18
|
+
|
19
|
+
[*.scss]
|
20
|
+
indent_size = 2
|
21
|
+
|
22
|
+
[{*.bash,*.sh,*.zsh,commit-msg}]
|
23
|
+
indent_size = 2
|
24
|
+
tab_width = 2
|
25
|
+
|
26
|
+
[{*.har,*.jsb2,*.jsb3,*.json,.babelrc,.eslintrc,.prettierrc,.stylelintrc,bowerrc,jest.config}]
|
27
|
+
indent_size = 2
|
28
|
+
|
29
|
+
[{*.htm,*.html,*.ng,*.sht,*.shtm,*.shtml}]
|
30
|
+
indent_size = 2
|
31
|
+
tab_width = 2
|
package/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# 1.3.0 (2023-01-29)
|
2
|
+
|
3
|
+
|
4
|
+
### Bug Fixes
|
5
|
+
|
6
|
+
* **promiseOnPending:** Update ([32d73d6](https://github.com/livelybone/singleton/commit/32d73d62bd093446148c018cdc8cfa71dcd81e94))
|
7
|
+
* type bug ([7b79ee4](https://github.com/livelybone/singleton/commit/7b79ee4d3d08a7e17ac8da79de139b49b5287b01))
|
8
|
+
|
9
|
+
|
10
|
+
### Features
|
11
|
+
|
12
|
+
* **any function:** Rename to singletonObj ([328cf74](https://github.com/livelybone/singleton/commit/328cf747c2d871215204a64a3213c1738abbc204))
|
13
|
+
* **functions:** Seperate ([267a2c9](https://github.com/livelybone/singleton/commit/267a2c9de78fc73938eb317fe9931c959fbfe8ff))
|
14
|
+
* **ids:** get ids from global ([9b552f2](https://github.com/livelybone/singleton/commit/9b552f23afdde11f81023168747a8e2f3a34d0ba))
|
15
|
+
* Init ([60c3a16](https://github.com/livelybone/singleton/commit/60c3a161febd2d0f2d0b5d3de0b797196c2af852))
|
16
|
+
* Module dev ([9ec11b7](https://github.com/livelybone/singleton/commit/9ec11b76b066a2a35155427c8c1708f0eaaec94d))
|
17
|
+
* **singletonObj:** Update ([022c668](https://github.com/livelybone/singleton/commit/022c668b4097dba48adf4e866c617fb7b1af6ef1))
|
18
|
+
* **ts:** Add type Fn ([021364a](https://github.com/livelybone/singleton/commit/021364a3415bba685ca464e92a2fda7a65281b9d))
|
19
|
+
* update ([9584106](https://github.com/livelybone/singleton/commit/958410610fcfcc84b5daa065c837b3cf48a661db))
|
20
|
+
* Update ([50cfc0c](https://github.com/livelybone/singleton/commit/50cfc0cfdc7ddc358e3cf0098ca87c224031c1e2))
|
21
|
+
* **v1.3.0:** add singleton ([8f80e82](https://github.com/livelybone/singleton/commit/8f80e8253264680d161b566cfbc26b43534b1fe3))
|
22
|
+
|
23
|
+
|
24
|
+
|
package/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# @livelybone/singleton
|
2
|
+
|
2
3
|
[](https://www.npmjs.com/package/@livelybone/singleton)
|
3
4
|
[](https://www.npmjs.com/package/@livelybone/singleton)
|
4
5
|

|
@@ -10,25 +11,31 @@
|
|
10
11
|
A util of singleton wrapping
|
11
12
|
|
12
13
|
## repository
|
14
|
+
|
13
15
|
https://github.com/livelybone/singleton.git
|
14
16
|
|
15
17
|
## Demo
|
18
|
+
|
16
19
|
https://github.com/livelybone/singleton#readme
|
17
20
|
|
18
21
|
## Installation
|
22
|
+
|
19
23
|
```bash
|
20
24
|
npm i -S @livelybone/singleton
|
21
25
|
```
|
22
26
|
|
23
27
|
## Global name
|
28
|
+
|
24
29
|
`Singleton`
|
25
30
|
|
26
31
|
## Usage
|
32
|
+
|
27
33
|
```js
|
28
34
|
import * as Singleton from '@livelybone/singleton'
|
29
35
|
```
|
30
36
|
|
31
37
|
Use in html, see what your can use in [CDN: unpkg](https://unpkg.com/@livelybone/singleton/lib/umd/)
|
38
|
+
|
32
39
|
```html
|
33
40
|
<-- use what you want -->
|
34
41
|
<script src="https://unpkg.com/@livelybone/singleton/lib/umd/<--module-->.js"></script>
|
@@ -45,7 +52,7 @@ declare type Fn<T = any> = () => T
|
|
45
52
|
* */
|
46
53
|
declare type ID = string | number
|
47
54
|
|
48
|
-
interface PromiseOnPendingOptions {
|
55
|
+
export interface PromiseOnPendingOptions {
|
49
56
|
id?: ID
|
50
57
|
/**
|
51
58
|
* 用于延迟 Promise 实例删除,
|
@@ -53,16 +60,40 @@ interface PromiseOnPendingOptions {
|
|
53
60
|
*
|
54
61
|
* Used to delay the deletion of Promise instance,
|
55
62
|
* if it is undefined, the promise will be deleted immediately after the state changed
|
63
|
+
*
|
64
|
+
* This option will be deprecated in next version
|
56
65
|
* */
|
57
66
|
delayTime?: number
|
67
|
+
/**
|
68
|
+
* 同 delayTime
|
69
|
+
* 推荐使用这个属性
|
70
|
+
*
|
71
|
+
* As same as delayTime
|
72
|
+
* */
|
73
|
+
cacheTime?: number
|
58
74
|
}
|
59
75
|
|
60
76
|
/**
|
61
|
-
* @
|
77
|
+
* @deprecated 这个方法使用多了会导致内存泄漏,建议使用 singleton 方法代替
|
78
|
+
* @desc 返回 id 对应的一个单例对象
|
79
|
+
*
|
80
|
+
* Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
|
81
|
+
* */
|
82
|
+
declare function singletonObj<T extends any>(id: ID, defaultValue?: () => T): T
|
83
|
+
|
84
|
+
/**
|
85
|
+
* @desc 返回 id 对应的一个单例对象,这个方法应当配合返回的 delete 方法一起使用,否则使用多了会导致内存泄漏
|
62
86
|
*
|
63
|
-
* Return a singleton of
|
87
|
+
* Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
|
88
|
+
* This method will cause OOM if it's used too much without calling `delete`.
|
64
89
|
* */
|
65
|
-
declare function
|
90
|
+
declare function singleton<T extends any>(
|
91
|
+
id: ID,
|
92
|
+
defaultValue?: () => T,
|
93
|
+
): {
|
94
|
+
value: T
|
95
|
+
delete(): void
|
96
|
+
}
|
66
97
|
|
67
98
|
/**
|
68
99
|
* @desc 保证一个 id 对应的 promise 在同一时间只存在一个,
|
@@ -74,10 +105,10 @@ declare function singletonObj<T = {}>(id: ID, defaultValue?: () => T): T
|
|
74
105
|
* during the period of promise pending.
|
75
106
|
* This method can be used to reduce redundant requests at the same time
|
76
107
|
* */
|
77
|
-
declare function promiseOnPending<
|
78
|
-
proFn: () =>
|
108
|
+
declare function promiseOnPending<P extends PromiseLike<any>>(
|
109
|
+
proFn: () => P,
|
79
110
|
options: PromiseOnPendingOptions,
|
80
|
-
):
|
111
|
+
): P
|
81
112
|
|
82
113
|
/**
|
83
114
|
* @desc 封装 setInterval 函数,
|
@@ -97,5 +128,5 @@ declare function runInterval(id: ID, createFn: Fn): () => void
|
|
97
128
|
* */
|
98
129
|
declare function onceRun(fn: Fn, id?: any): void
|
99
130
|
|
100
|
-
export { onceRun, promiseOnPending, runInterval, singletonObj }
|
131
|
+
export { onceRun, promiseOnPending, runInterval, singleton, singletonObj }
|
101
132
|
```
|
package/index.d.ts
CHANGED
@@ -6,7 +6,7 @@ declare type Fn<T = any> = () => T
|
|
6
6
|
* */
|
7
7
|
declare type ID = string | number
|
8
8
|
|
9
|
-
interface PromiseOnPendingOptions {
|
9
|
+
export interface PromiseOnPendingOptions {
|
10
10
|
id?: ID
|
11
11
|
/**
|
12
12
|
* 用于延迟 Promise 实例删除,
|
@@ -28,12 +28,27 @@ interface PromiseOnPendingOptions {
|
|
28
28
|
}
|
29
29
|
|
30
30
|
/**
|
31
|
-
* @
|
31
|
+
* @deprecated 这个方法使用多了会导致内存泄漏,建议使用 singleton 方法代替
|
32
|
+
* @desc 返回 id 对应的一个单例对象
|
32
33
|
*
|
33
|
-
* Return a singleton of
|
34
|
+
* Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
|
34
35
|
* */
|
35
36
|
declare function singletonObj<T extends any>(id: ID, defaultValue?: () => T): T
|
36
37
|
|
38
|
+
/**
|
39
|
+
* @desc 返回 id 对应的一个单例对象,这个方法应当配合返回的 delete 方法一起使用,否则使用多了会导致内存泄漏
|
40
|
+
*
|
41
|
+
* Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
|
42
|
+
* This method will cause OOM if it's used too much without calling `delete`.
|
43
|
+
* */
|
44
|
+
declare function singleton<T extends any>(
|
45
|
+
id: ID,
|
46
|
+
defaultValue?: () => T,
|
47
|
+
): {
|
48
|
+
value: T
|
49
|
+
delete(): void
|
50
|
+
}
|
51
|
+
|
37
52
|
/**
|
38
53
|
* @desc 保证一个 id 对应的 promise 在同一时间只存在一个,
|
39
54
|
* 并且生成 promise 的函数在 promise pending 状态的时间段内只执行一次,
|
@@ -58,7 +73,6 @@ declare function promiseOnPending<P extends PromiseLike<any>>(
|
|
58
73
|
* make sure only one timer for the same id is running,
|
59
74
|
* and returns a function to clear the timer so it can be terminated at any time
|
60
75
|
* */
|
61
|
-
|
62
76
|
declare function runInterval(id: ID, createFn: Fn): () => void
|
63
77
|
|
64
78
|
/**
|
@@ -68,4 +82,4 @@ declare function runInterval(id: ID, createFn: Fn): () => void
|
|
68
82
|
* */
|
69
83
|
declare function onceRun(fn: Fn, id?: any): void
|
70
84
|
|
71
|
-
export { onceRun, promiseOnPending, runInterval, singletonObj }
|
85
|
+
export { onceRun, promiseOnPending, runInterval, singleton, singletonObj }
|
package/lib/es/index.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
/**
|
2
2
|
* Bundle of @livelybone/singleton
|
3
|
-
* Generated:
|
4
|
-
* Version: 1.
|
3
|
+
* Generated: 2023-01-29
|
4
|
+
* Version: 1.3.0
|
5
5
|
* License: MIT
|
6
6
|
* Author: 2631541504@qq.com
|
7
7
|
*/
|
@@ -11,22 +11,50 @@
|
|
11
11
|
*
|
12
12
|
* The id of a singleton
|
13
13
|
* */
|
14
|
-
|
14
|
+
|
15
|
+
function getIdsMap() {
|
16
|
+
var $global = typeof window !== 'undefined' ? window : global;
|
17
|
+
if (!$global.$$SingletonIdsMap || !($global.$$SingletonIdsMap instanceof Map)) {
|
18
|
+
$global.$$SingletonIdsMap = new Map();
|
19
|
+
}
|
20
|
+
return $global.$$SingletonIdsMap;
|
21
|
+
}
|
22
|
+
|
15
23
|
/**
|
16
|
-
* @
|
24
|
+
* @deprecated 这个方法使用多了会导致内存泄漏,建议使用 singleton 方法代替
|
25
|
+
* @desc 返回 id 对应的一个单例对象
|
17
26
|
*
|
18
|
-
* Return a singleton of
|
27
|
+
* Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
|
19
28
|
* */
|
20
|
-
|
21
29
|
function singletonObj(id, defaultValue) {
|
30
|
+
var ids = getIdsMap();
|
22
31
|
var k = "singleton-any-".concat(id || 'default');
|
23
|
-
|
24
32
|
if (!ids.has(k)) {
|
25
33
|
ids.set(k, defaultValue ? defaultValue() : {});
|
26
34
|
}
|
27
|
-
|
28
35
|
return ids.get(k);
|
29
36
|
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* @desc 返回 id 对应的一个单例对象,这个方法应当配合返回的 delete 方法一起使用,否则使用多了会导致内存泄漏
|
40
|
+
*
|
41
|
+
* Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
|
42
|
+
* This method will cause OOM if it's used too much without calling `delete`.
|
43
|
+
* */
|
44
|
+
function singleton(id, defaultValue) {
|
45
|
+
var ids = getIdsMap();
|
46
|
+
var k = "singleton-any-".concat(id || 'default');
|
47
|
+
if (!ids.has(k)) {
|
48
|
+
ids.set(k, defaultValue ? defaultValue() : {});
|
49
|
+
}
|
50
|
+
return {
|
51
|
+
value: ids.get(k),
|
52
|
+
delete: function _delete() {
|
53
|
+
return ids.delete(id);
|
54
|
+
}
|
55
|
+
};
|
56
|
+
}
|
57
|
+
|
30
58
|
/**
|
31
59
|
* @desc 保证一个 id 对应的 promise 在同一时间只存在一个,
|
32
60
|
* 并且生成 promise 的函数在 promise pending 状态的时间段内只执行一次,
|
@@ -37,21 +65,18 @@ function singletonObj(id, defaultValue) {
|
|
37
65
|
* during the period of promise pending.
|
38
66
|
* This method can be used to reduce redundant requests at the same time
|
39
67
|
* */
|
40
|
-
|
41
68
|
function promiseOnPending(proFn, options) {
|
69
|
+
var ids = getIdsMap();
|
42
70
|
var id = options.id;
|
43
71
|
var cacheTime = options.cacheTime || options.delayTime;
|
44
72
|
var k = id ? "promise-".concat(id) : proFn;
|
45
|
-
|
46
73
|
var del = function del() {
|
47
74
|
return ids.delete(k);
|
48
75
|
};
|
49
|
-
|
50
76
|
var del1 = function del1() {
|
51
77
|
if (cacheTime === undefined) del();else setTimeout(del, cacheTime);
|
52
78
|
return true;
|
53
79
|
};
|
54
|
-
|
55
80
|
if (!ids.has(k)) {
|
56
81
|
ids.set(k, proFn().then(function (res) {
|
57
82
|
return del1() && res;
|
@@ -59,9 +84,9 @@ function promiseOnPending(proFn, options) {
|
|
59
84
|
return Promise.reject(del1() && e);
|
60
85
|
}));
|
61
86
|
}
|
62
|
-
|
63
87
|
return ids.get(k);
|
64
88
|
}
|
89
|
+
|
65
90
|
/**
|
66
91
|
* @desc 封装 setInterval 函数,
|
67
92
|
* 保证同一个 id 对应的计时器只有一个在运行,
|
@@ -71,15 +96,13 @@ function promiseOnPending(proFn, options) {
|
|
71
96
|
* make sure only one timer for the same id is running,
|
72
97
|
* and returns a function to clear the timer so it can be terminated at any time
|
73
98
|
* */
|
74
|
-
|
75
99
|
function runInterval(id, createFn) {
|
100
|
+
var ids = getIdsMap();
|
76
101
|
var k = "timer-".concat(id || 'default');
|
77
|
-
|
78
102
|
if (ids.has(k)) {
|
79
103
|
// clear old interval
|
80
104
|
ids.get(k)();
|
81
105
|
}
|
82
|
-
|
83
106
|
var $id = createFn();
|
84
107
|
ids.set(k, function () {
|
85
108
|
clearInterval($id);
|
@@ -87,18 +110,18 @@ function runInterval(id, createFn) {
|
|
87
110
|
});
|
88
111
|
return ids.get(k);
|
89
112
|
}
|
113
|
+
|
90
114
|
/**
|
91
115
|
* @desc 保证传入的函数在程序的运行期间只运行一次
|
92
116
|
*
|
93
117
|
* Ensure that the incoming function runs only once during the run time of the program
|
94
118
|
* */
|
95
|
-
|
96
119
|
function onceRun(fn, id) {
|
120
|
+
var ids = getIdsMap();
|
97
121
|
var k = id ? "once-run-".concat(id) : fn;
|
98
|
-
|
99
122
|
if (!ids.has(k)) {
|
100
123
|
ids.set(k, fn());
|
101
124
|
}
|
102
125
|
}
|
103
126
|
|
104
|
-
export { onceRun, promiseOnPending, runInterval, singletonObj };
|
127
|
+
export { onceRun, promiseOnPending, runInterval, singleton, singletonObj };
|
package/lib/umd/index.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
2
|
* Bundle of @livelybone/singleton
|
3
|
-
* Generated:
|
4
|
-
* Version: 1.
|
3
|
+
* Generated: 2023-01-29
|
4
|
+
* Version: 1.3.0
|
5
5
|
* License: MIT
|
6
6
|
* Author: 2631541504@qq.com
|
7
7
|
*/
|
8
8
|
|
9
|
-
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).Singleton={})}(this,function(e){"use strict";var
|
9
|
+
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).Singleton={})}(this,function(e){"use strict";function c(){var e="undefined"!=typeof window?window:global;return e.$$SingletonIdsMap&&e.$$SingletonIdsMap instanceof Map||(e.$$SingletonIdsMap=new Map),e.$$SingletonIdsMap}e.onceRun=function(e,n){var t=c(),n=n?"once-run-".concat(n):e;t.has(n)||t.set(n,e())},e.promiseOnPending=function(e,n){function t(){return i.delete(a)}function o(){return void 0===u?t():setTimeout(t,u),!0}var i=c(),r=n.id,u=n.cacheTime||n.delayTime,a=r?"promise-".concat(r):e;return i.has(a)||i.set(a,e().then(function(e){return o(),e},function(e){return Promise.reject((o(),e))})),i.get(a)},e.runInterval=function(e,n){var t=c(),o="timer-".concat(e||"default"),i=(t.has(o)&&t.get(o)(),n());return t.set(o,function(){clearInterval(i),t.delete(o)}),t.get(o)},e.singleton=function(e,n){var t=c(),o="singleton-any-".concat(e||"default");return t.has(o)||t.set(o,n?n():{}),{value:t.get(o),delete:function(){return t.delete(e)}}},e.singletonObj=function(e,n){var t=c(),e="singleton-any-".concat(e||"default");return t.has(e)||t.set(e,n?n():{}),t.get(e)},Object.defineProperty(e,"__esModule",{value:!0})});
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@livelybone/singleton",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.3.0",
|
4
4
|
"description": "A util of singleton wrapping",
|
5
5
|
"main": "./lib/umd/index.js",
|
6
6
|
"module": "./lib/es/index.js",
|
@@ -59,7 +59,7 @@
|
|
59
59
|
"rollup": "^1.7.0",
|
60
60
|
"rollup-plugin-babel": "^4.3.3",
|
61
61
|
"rollup-plugin-commonjs": "^9.2.1",
|
62
|
-
"rollup-plugin-dts": "
|
62
|
+
"rollup-plugin-dts": "1.2.1",
|
63
63
|
"rollup-plugin-license": "^0.8.1",
|
64
64
|
"rollup-plugin-node-resolve": "^4.0.1",
|
65
65
|
"rollup-plugin-uglify": "^6.0.2",
|