fib-pool 1.7.0 → 1.7.1

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/Readme.md CHANGED
@@ -12,7 +12,7 @@ npm install fib-pool [--save]
12
12
 
13
13
  ```sh
14
14
  npm test # fibjs test
15
- npm run typecheck # fibjs --check
15
+ npm run typecheck # fibjs --check, including test/types.ts
16
16
  ```
17
17
 
18
18
  ## Creating a pool
@@ -113,6 +113,19 @@ var res = p("test", (conn) => {
113
113
  | `retry` | positive integer | `1` | how many times `create` is attempted before giving up |
114
114
  | `strict` | boolean | `true` | see below |
115
115
 
116
+ The pooled item is unconstrained: `create` may return an object, a class
117
+ instance, a function, or a primitive. The default `destroy` calls whichever of
118
+ `close` / `destroy` / `dispose` the item provides and ignores items that have
119
+ none of them.
120
+
121
+ ```js
122
+ var p = Pool(() => 10); // a primitive item
123
+ p((v) => v + 1);
124
+
125
+ var q = Pool(() => ({ value: 1 })); // a plain object, nothing to destroy
126
+ q((o) => o.value);
127
+ ```
128
+
116
129
  Invalid `maxsize`, `timeout` and `retry` values throw a `TypeError` when the
117
130
  pool is created rather than being silently coerced:
118
131
 
package/package.json CHANGED
@@ -1,9 +1,22 @@
1
1
  {
2
2
  "name": "fib-pool",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "Generic resource pooling for fibjs",
5
- "main": "./src/index.ts",
5
+ "main": "./src/index.cts",
6
+ "types": "./src/index.ts",
6
7
  "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./src/index.ts",
12
+ "default": "./src/index.ts"
13
+ },
14
+ "require": {
15
+ "types": "./src/index.d.cts",
16
+ "default": "./src/index.cts"
17
+ }
18
+ }
19
+ },
7
20
  "repository": "git://github.com/fibjs/fib-pool.git",
8
21
  "author": "Liu Hu <lion@9465.net>",
9
22
  "homepage": "https://github.com/fibjs/fib-pool",
package/src/index.cts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * CommonJS entry point.
3
+ *
4
+ * The pool is implemented in `index.ts` as an ES module, so requiring that
5
+ * file yields the module namespace object (`{ default, __esModule }`) rather
6
+ * than the factory. Callers of `require('fib-pool')` expect the export to *be*
7
+ * the factory function, so this bridge unwraps the default export.
8
+ *
9
+ * A static `import` is not an option here: this file is CommonJS, so it must
10
+ * load the implementation through `require` to get the namespace object.
11
+ *
12
+ * `module.exports =` is used instead of `export =` because fibjs runs
13
+ * TypeScript in strip-only mode, which rejects export assignments. The type of
14
+ * this file is described by the sibling `index.d.cts`.
15
+ */
16
+ const impl = require('./index.ts');
17
+
18
+ module.exports = impl.default;
@@ -0,0 +1,3 @@
1
+ import Pool from './index.ts';
2
+
3
+ export = Pool;
package/src/index.ts CHANGED
@@ -3,9 +3,17 @@ import * as util from 'util';
3
3
 
4
4
  declare namespace FibPoolNS {
5
5
  /**
6
- * The shape `create` must return. All three members are optional: the
7
- * default destroy calls whichever of them the item actually provides, and
8
- * a non-function value (a plain property) is ignored.
6
+ * The shape the default destroy looks for. All three members are optional:
7
+ * it calls whichever of them the item actually provides, and a non-function
8
+ * value (a plain property) is ignored.
9
+ *
10
+ * This is deliberately *not* a constraint on the pooled item. Every member
11
+ * is optional, which makes it a weak type, and TypeScript rejects any type
12
+ * without a property in common with a weak type. Constraining the item to
13
+ * it would reject the perfectly valid `Pool(() => 10)` and
14
+ * `Pool(() => ({ value: 1 }))`, so the constraint is absent and the cast to
15
+ * this shape happens only inside the default destroy, where a primitive
16
+ * simply has no such member to call.
9
17
  */
10
18
  interface DippedItemStruct {
11
19
  close?: unknown;
@@ -13,7 +21,11 @@ declare namespace FibPoolNS {
13
21
  dispose?: unknown;
14
22
  }
15
23
 
16
- interface FibPoolOptionArgs<T extends DippedItemStruct = {}> {
24
+ /**
25
+ * `T` is the pooled item and may be anything `create` returns, including a
26
+ * primitive. It is only inferred, never constrained.
27
+ */
28
+ interface FibPoolOptionArgs<T = any> {
17
29
  create: (name: string) => T;
18
30
  destroy?: (item?: T) => any;
19
31
  maxsize?: number;
@@ -147,8 +159,8 @@ function wrapStrict<T extends object>(item: () => T | undefined): T {
147
159
  }
148
160
 
149
161
  export default function GetPool<
150
- T_CREATED_ITEM extends FibPoolNS.DippedItemStruct,
151
- T_POOL_FUNC_RETURN
162
+ T_CREATED_ITEM,
163
+ T_POOL_FUNC_RETURN = any
152
164
  > (
153
165
  _opt: FibPoolNS.FibPoolOptionArgs<T_CREATED_ITEM> | FibPoolNS.FibPoolOptionArgs<T_CREATED_ITEM>['create'],
154
166
  maxsize?: number,
@@ -168,15 +180,20 @@ export default function GetPool<
168
180
  }
169
181
 
170
182
  const create = opt.create;
171
- const destroy = opt.destroy || ((o: FibPoolNS.DippedItemStruct | undefined) => {
172
- /* create is allowed to return null/undefined: there is nothing to
173
- destroy then */
183
+ const destroy = opt.destroy || ((o: unknown) => {
184
+ /* create may return null/undefined or any primitive: there is nothing
185
+ to destroy then. Functions are kept, since they can carry members. */
174
186
  if (o === null || o === undefined)
175
187
  return;
176
188
 
177
- callIfFunction(o.close);
178
- callIfFunction(o.destroy);
179
- callIfFunction(o.dispose);
189
+ if (typeof o !== 'object' && typeof o !== 'function')
190
+ return;
191
+
192
+ const item = o as FibPoolNS.DippedItemStruct;
193
+
194
+ callIfFunction(item.close);
195
+ callIfFunction(item.destroy);
196
+ callIfFunction(item.dispose);
180
197
  });
181
198
 
182
199
  const maxsizeValue = opt.maxsize === undefined ? 10 : toPositiveInteger(opt.maxsize, 'maxsize');