@livelybone/singleton 1.3.1 → 1.3.2

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/index.d.ts CHANGED
@@ -5,8 +5,7 @@ declare type Fn<T = any> = () => T
5
5
  * The id of a singleton
6
6
  * */
7
7
  declare type ID = string | number
8
-
9
- export interface PromiseOnPendingOptions {
8
+ interface PromiseOnPendingOptions {
10
9
  id?: ID
11
10
  /**
12
11
  * 用于延迟 Promise 实例删除,
@@ -26,7 +25,6 @@ export interface PromiseOnPendingOptions {
26
25
  * */
27
26
  cacheTime?: number
28
27
  }
29
-
30
28
  /**
31
29
  * @deprecated 这个方法使用多了会导致内存泄漏,建议使用 singleton 方法代替
32
30
  * @desc 返回 id 对应的一个单例对象
@@ -34,21 +32,20 @@ export interface PromiseOnPendingOptions {
34
32
  * Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
35
33
  * */
36
34
  declare function singletonObj<T extends any>(id: ID, defaultValue?: () => T): T
37
-
38
35
  /**
39
36
  * @desc 返回 id 对应的一个单例对象,这个方法应当配合返回的 delete 方法一起使用,否则使用多了会导致内存泄漏
40
37
  *
41
38
  * Return a singleton of an object(such as Promise, Function, Object...) corresponding to the id.
42
39
  * This method will cause OOM if it's used too much without calling `delete`.
43
40
  * */
44
- declare function singleton<T extends any>(
41
+ declare function singleton<T extends Record<string, unknown>>(
45
42
  id: ID,
46
43
  defaultValue?: () => T,
47
44
  ): {
48
45
  value: T
49
46
  delete(): void
47
+ update(action: T | ((v: T) => T)): void
50
48
  }
51
-
52
49
  /**
53
50
  * @desc 保证一个 id 对应的 promise 在同一时间只存在一个,
54
51
  * 并且生成 promise 的函数在 promise pending 状态的时间段内只执行一次,
@@ -63,7 +60,6 @@ declare function promiseOnPending<P extends PromiseLike<any>>(
63
60
  proFn: () => P,
64
61
  options: PromiseOnPendingOptions,
65
62
  ): P
66
-
67
63
  /**
68
64
  * @desc 封装 setInterval 函数,
69
65
  * 保证同一个 id 对应的计时器只有一个在运行,
@@ -74,7 +70,6 @@ declare function promiseOnPending<P extends PromiseLike<any>>(
74
70
  * and returns a function to clear the timer so it can be terminated at any time
75
71
  * */
76
72
  declare function runInterval(id: ID, createFn: Fn): () => void
77
-
78
73
  /**
79
74
  * @desc 保证传入的函数在程序的运行期间只运行一次
80
75
  *
@@ -82,4 +77,11 @@ declare function runInterval(id: ID, createFn: Fn): () => void
82
77
  * */
83
78
  declare function onceRun(fn: Fn, id?: any): void
84
79
 
85
- export { onceRun, promiseOnPending, runInterval, singleton, singletonObj }
80
+ export {
81
+ PromiseOnPendingOptions,
82
+ onceRun,
83
+ promiseOnPending,
84
+ runInterval,
85
+ singleton,
86
+ singletonObj,
87
+ }
package/lib/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Bundle of @livelybone/singleton
3
- * Generated: 2023-01-30
4
- * Version: 1.3.0
3
+ * Generated: 2023-08-10
4
+ * Version: 1.3.2
5
5
  * License: MIT
6
6
  * Author: 2631541504@qq.com
7
7
  */
@@ -51,6 +51,10 @@ function singleton(id, defaultValue) {
51
51
  value: ids.get(k),
52
52
  delete: function _delete() {
53
53
  return ids.delete(k);
54
+ },
55
+ update: function update(action) {
56
+ var v = typeof action === 'function' ? action(ids.get()) : action;
57
+ ids.set(k, v);
54
58
  }
55
59
  };
56
60
  }
package/lib/umd/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Bundle of @livelybone/singleton
3
- * Generated: 2023-01-30
4
- * Version: 1.3.0
3
+ * Generated: 2023-08-10
4
+ * Version: 1.3.2
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"),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(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(o)},update:function(e){e="function"==typeof e?e(t.get()):e;t.set(o,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.1",
3
+ "version": "1.3.2",
4
4
  "description": "A util of singleton wrapping",
5
5
  "main": "./lib/umd/index.js",
6
6
  "module": "./lib/es/index.js",
@@ -12,7 +12,10 @@
12
12
  "eslint": "eslint ./ --ext .ts,.js --fix",
13
13
  "test": "npm run build:test && cross-env NODE_ENV=test istanbul cover node_modules/mocha/bin/_mocha -- ./test",
14
14
  "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
15
- "commit": "git-cz"
15
+ "commit": "git-cz",
16
+ "prepublishOnly": "npm run build && npm run eslint",
17
+ "release": "npm publish --registry=https://registry.npmjs.org",
18
+ "release:alpha": "npm publish --tag alpha --registry=https://registry.npmjs.org"
16
19
  },
17
20
  "repository": {
18
21
  "type": "git",