@livelybone/singleton 1.2.2 → 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 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
  [![NPM Version](http://img.shields.io/npm/v/@livelybone/singleton.svg?style=flat-square)](https://www.npmjs.com/package/@livelybone/singleton)
3
4
  [![Download Month](http://img.shields.io/npm/dm/@livelybone/singleton.svg?style=flat-square)](https://www.npmjs.com/package/@livelybone/singleton)
4
5
  ![gzip with dependencies: 1kb](https://img.shields.io/badge/gzip--with--dependencies-1kb-brightgreen.svg "gzip with dependencies: 1kb")
@@ -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
- * @desc 返回 id 对应的一个对象
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 any object(such as Promise, Function, Object...) corresponding to the id
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 singletonObj<T = {}>(id: ID, defaultValue?: () => T): T
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<T = any>(
78
- proFn: () => Promise<T>,
108
+ declare function promiseOnPending<P extends PromiseLike<any>>(
109
+ proFn: () => P,
79
110
  options: PromiseOnPendingOptions,
80
- ): Promise<T>
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
@@ -28,12 +28,27 @@ export interface PromiseOnPendingOptions {
28
28
  }
29
29
 
30
30
  /**
31
- * @desc 返回 id 对应的一个对象
31
+ * @deprecated 这个方法使用多了会导致内存泄漏,建议使用 singleton 方法代替
32
+ * @desc 返回 id 对应的一个单例对象
32
33
  *
33
- * Return a singleton of any object(such as Promise, Function, Object...) corresponding to the id
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 状态的时间段内只执行一次,
@@ -67,4 +82,4 @@ declare function runInterval(id: ID, createFn: Fn): () => void
67
82
  * */
68
83
  declare function onceRun(fn: Fn, id?: any): void
69
84
 
70
- 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: 2021-02-09
4
- * Version: 1.2.2
3
+ * Generated: 2023-01-29
4
+ * Version: 1.3.0
5
5
  * License: MIT
6
6
  * Author: 2631541504@qq.com
7
7
  */
@@ -11,32 +11,50 @@
11
11
  *
12
12
  * The id of a singleton
13
13
  * */
14
+
14
15
  function getIdsMap() {
15
16
  var $global = typeof window !== 'undefined' ? window : global;
16
-
17
17
  if (!$global.$$SingletonIdsMap || !($global.$$SingletonIdsMap instanceof Map)) {
18
18
  $global.$$SingletonIdsMap = new Map();
19
19
  }
20
-
21
20
  return $global.$$SingletonIdsMap;
22
21
  }
22
+
23
23
  /**
24
- * @desc 返回 id 对应的一个对象
24
+ * @deprecated 这个方法使用多了会导致内存泄漏,建议使用 singleton 方法代替
25
+ * @desc 返回 id 对应的一个单例对象
25
26
  *
26
- * Return a singleton of any object(such as Promise, Function, Object...) corresponding to the id
27
+ * Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
27
28
  * */
28
-
29
-
30
29
  function singletonObj(id, defaultValue) {
31
30
  var ids = getIdsMap();
32
31
  var k = "singleton-any-".concat(id || 'default');
33
-
34
32
  if (!ids.has(k)) {
35
33
  ids.set(k, defaultValue ? defaultValue() : {});
36
34
  }
37
-
38
35
  return ids.get(k);
39
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
+
40
58
  /**
41
59
  * @desc 保证一个 id 对应的 promise 在同一时间只存在一个,
42
60
  * 并且生成 promise 的函数在 promise pending 状态的时间段内只执行一次,
@@ -47,22 +65,18 @@ function singletonObj(id, defaultValue) {
47
65
  * during the period of promise pending.
48
66
  * This method can be used to reduce redundant requests at the same time
49
67
  * */
50
-
51
68
  function promiseOnPending(proFn, options) {
52
69
  var ids = getIdsMap();
53
70
  var id = options.id;
54
71
  var cacheTime = options.cacheTime || options.delayTime;
55
72
  var k = id ? "promise-".concat(id) : proFn;
56
-
57
73
  var del = function del() {
58
74
  return ids.delete(k);
59
75
  };
60
-
61
76
  var del1 = function del1() {
62
77
  if (cacheTime === undefined) del();else setTimeout(del, cacheTime);
63
78
  return true;
64
79
  };
65
-
66
80
  if (!ids.has(k)) {
67
81
  ids.set(k, proFn().then(function (res) {
68
82
  return del1() && res;
@@ -70,9 +84,9 @@ function promiseOnPending(proFn, options) {
70
84
  return Promise.reject(del1() && e);
71
85
  }));
72
86
  }
73
-
74
87
  return ids.get(k);
75
88
  }
89
+
76
90
  /**
77
91
  * @desc 封装 setInterval 函数,
78
92
  * 保证同一个 id 对应的计时器只有一个在运行,
@@ -82,16 +96,13 @@ function promiseOnPending(proFn, options) {
82
96
  * make sure only one timer for the same id is running,
83
97
  * and returns a function to clear the timer so it can be terminated at any time
84
98
  * */
85
-
86
99
  function runInterval(id, createFn) {
87
100
  var ids = getIdsMap();
88
101
  var k = "timer-".concat(id || 'default');
89
-
90
102
  if (ids.has(k)) {
91
103
  // clear old interval
92
104
  ids.get(k)();
93
105
  }
94
-
95
106
  var $id = createFn();
96
107
  ids.set(k, function () {
97
108
  clearInterval($id);
@@ -99,19 +110,18 @@ function runInterval(id, createFn) {
99
110
  });
100
111
  return ids.get(k);
101
112
  }
113
+
102
114
  /**
103
115
  * @desc 保证传入的函数在程序的运行期间只运行一次
104
116
  *
105
117
  * Ensure that the incoming function runs only once during the run time of the program
106
118
  * */
107
-
108
119
  function onceRun(fn, id) {
109
120
  var ids = getIdsMap();
110
121
  var k = id ? "once-run-".concat(id) : fn;
111
-
112
122
  if (!ids.has(k)) {
113
123
  ids.set(k, fn());
114
124
  }
115
125
  }
116
126
 
117
- 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: 2021-02-09
4
- * Version: 1.2.2
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";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");t.has(o)&&t.get(o)();var i=n();return t.set(o,function(){clearInterval(i),t.delete(o)}),t.get(o)},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})});
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.2.2",
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",