run npm install to generate a package lock
This commit is contained in:
10
node_modules/promise-make-counter/.editorconfig
generated
vendored
Normal file
10
node_modules/promise-make-counter/.editorconfig
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
4
node_modules/promise-make-counter/dist/index.d.ts
generated
vendored
Normal file
4
node_modules/promise-make-counter/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
import type { Result } from './types.js';
|
||||
declare const makeCounterPromise: () => Result;
|
||||
export default makeCounterPromise;
|
||||
export type { Result };
|
||||
24
node_modules/promise-make-counter/dist/index.js
generated
vendored
Normal file
24
node_modules/promise-make-counter/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
/* IMPORT */
|
||||
import makeNakedPromise from 'promise-make-naked';
|
||||
/* MAIN */
|
||||
const makeCounterPromise = () => {
|
||||
const { promise, resolve, isPending } = makeNakedPromise();
|
||||
let counter = 0;
|
||||
const increment = () => {
|
||||
counter += 1;
|
||||
};
|
||||
const decrement = () => {
|
||||
counter -= 1;
|
||||
if (counter)
|
||||
return;
|
||||
resolve();
|
||||
};
|
||||
const init = () => {
|
||||
increment();
|
||||
queueMicrotask(decrement);
|
||||
};
|
||||
init();
|
||||
return { promise, isPending, increment, decrement };
|
||||
};
|
||||
/* EXPORT */
|
||||
export default makeCounterPromise;
|
||||
7
node_modules/promise-make-counter/dist/types.d.ts
generated
vendored
Normal file
7
node_modules/promise-make-counter/dist/types.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
type Result = {
|
||||
promise: Promise<void>;
|
||||
isPending: () => boolean;
|
||||
increment: () => void;
|
||||
decrement: () => void;
|
||||
};
|
||||
export type { Result };
|
||||
2
node_modules/promise-make-counter/dist/types.js
generated
vendored
Normal file
2
node_modules/promise-make-counter/dist/types.js
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/* MAIN */
|
||||
export {};
|
||||
21
node_modules/promise-make-counter/license
generated
vendored
Normal file
21
node_modules/promise-make-counter/license
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2024-present Fabio Spampinato
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
32
node_modules/promise-make-counter/package.json
generated
vendored
Normal file
32
node_modules/promise-make-counter/package.json
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "promise-make-counter",
|
||||
"repository": "github:fabiospampinato/promise-make-counter",
|
||||
"description": "A simple function that makes a counter-based promise, which can be incremented and decremented, and it resolves once its counter reaches zero.",
|
||||
"version": "1.0.1",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"exports": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"clean": "tsex clean",
|
||||
"compile": "tsex compile",
|
||||
"compile:watch": "tsex compile --watch",
|
||||
"test": "tsex test",
|
||||
"test:watch": "tsex test --watch",
|
||||
"prepublishOnly": "tsex prepare"
|
||||
},
|
||||
"keywords": [
|
||||
"promise",
|
||||
"make",
|
||||
"counter",
|
||||
"reference"
|
||||
],
|
||||
"dependencies": {
|
||||
"promise-make-naked": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"fava": "^0.3.4",
|
||||
"tsex": "^4.0.2",
|
||||
"typescript": "^5.5.0-beta"
|
||||
}
|
||||
}
|
||||
48
node_modules/promise-make-counter/readme.md
generated
vendored
Normal file
48
node_modules/promise-make-counter/readme.md
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
# Promise Make Counter
|
||||
|
||||
A simple function that makes a counter-based promise, which can be incremented and decremented, and it resolves once its counter reaches zero.
|
||||
|
||||
This is an alternative to creating a promise that waits on many other promises to resolve. You can do that, but creating many promises can be surprisingly slow, instead you could use this package, switch internally to using callbacks-style APIs rather than async/await-style APIs, and just increment the counter at the start of each callback, and decrement it at its end, once its job is done.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install --save promise-make-counter
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import fs from 'node:fs';
|
||||
import makeCounterPromise from 'promise-make-counter';
|
||||
|
||||
// Let's create a counter-based promise
|
||||
|
||||
const {promise, isPending, increment, decrement} = makeCounterPromise ();
|
||||
|
||||
// Now let's do some work that's tracked by our counter-based promise
|
||||
// It's important to always increment at the start, and decrement at the end
|
||||
|
||||
for ( const filePath of filePaths ) {
|
||||
|
||||
increment ();
|
||||
|
||||
fs.readFile ( filePath, 'utf8', ( error, data ) => {
|
||||
|
||||
// Do something here...
|
||||
|
||||
decrement ();
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Let's wait for the counter-based promise to resolve
|
||||
// This will happen once its internal counter will reach zero
|
||||
|
||||
await promise;
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT © Fabio Spampinato
|
||||
48
node_modules/promise-make-counter/src/index.ts
generated
vendored
Normal file
48
node_modules/promise-make-counter/src/index.ts
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
/* IMPORT */
|
||||
|
||||
import makeNakedPromise from 'promise-make-naked';
|
||||
import type {Result} from './types';
|
||||
|
||||
/* MAIN */
|
||||
|
||||
const makeCounterPromise = (): Result => {
|
||||
|
||||
const {promise, resolve, isPending} = makeNakedPromise<void> ();
|
||||
|
||||
let counter = 0;
|
||||
|
||||
const increment = (): void => {
|
||||
|
||||
counter += 1;
|
||||
|
||||
};
|
||||
|
||||
const decrement = (): void => {
|
||||
|
||||
counter -= 1;
|
||||
|
||||
if ( counter ) return;
|
||||
|
||||
resolve ();
|
||||
|
||||
};
|
||||
|
||||
const init = (): void => { // Accounting for no increment/decrement calls
|
||||
|
||||
increment ();
|
||||
|
||||
queueMicrotask ( decrement );
|
||||
|
||||
};
|
||||
|
||||
init ();
|
||||
|
||||
return { promise, isPending, increment, decrement };
|
||||
|
||||
};
|
||||
|
||||
/* EXPORT */
|
||||
|
||||
export default makeCounterPromise;
|
||||
export type {Result};
|
||||
13
node_modules/promise-make-counter/src/types.ts
generated
vendored
Normal file
13
node_modules/promise-make-counter/src/types.ts
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
/* MAIN */
|
||||
|
||||
type Result = {
|
||||
promise: Promise<void>,
|
||||
isPending: () => boolean,
|
||||
increment: () => void,
|
||||
decrement: () => void
|
||||
};
|
||||
|
||||
/* EXPORT */
|
||||
|
||||
export type {Result};
|
||||
52
node_modules/promise-make-counter/test/index.js
generated
vendored
Normal file
52
node_modules/promise-make-counter/test/index.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
/* IMPORT */
|
||||
|
||||
import {describe} from 'fava';
|
||||
import makeCounterPromise from '../dist/index.js';
|
||||
|
||||
/* MAIN */
|
||||
|
||||
describe ( 'Promise Make Counter', it => {
|
||||
|
||||
it ( 'resolves without increments', async t => {
|
||||
|
||||
const {isPending} = makeCounterPromise ();
|
||||
|
||||
t.true ( isPending () );
|
||||
|
||||
await t.wait ( 0 );
|
||||
|
||||
t.false ( isPending () );
|
||||
|
||||
});
|
||||
|
||||
it ( 'resolves after equal increments and decrements', async t => {
|
||||
|
||||
const {isPending, increment, decrement} = makeCounterPromise ();
|
||||
|
||||
t.true ( isPending () );
|
||||
|
||||
increment ();
|
||||
increment ();
|
||||
increment ();
|
||||
|
||||
await t.wait ( 0 );
|
||||
|
||||
t.true ( isPending () );
|
||||
|
||||
decrement ();
|
||||
decrement ();
|
||||
|
||||
await t.wait ( 0 );
|
||||
|
||||
t.true ( isPending () );
|
||||
|
||||
decrement ();
|
||||
|
||||
await t.wait ( 0 );
|
||||
|
||||
t.false ( isPending () );
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
3
node_modules/promise-make-counter/tsconfig.json
generated
vendored
Normal file
3
node_modules/promise-make-counter/tsconfig.json
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "tsex/tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user