Skip to content

Commit 5ef1909

Browse files
committed
feat: add scopeExit
1 parent ab377cf commit 5ef1909

File tree

8 files changed

+2356
-2573
lines changed

8 files changed

+2356
-2573
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ jobs:
4747
lfs: true
4848
- uses: actions/setup-node@v4
4949
with:
50-
node-version: "20"
50+
node-version: "22"
51+
- name: Update npm
52+
run: npm install -g npm@latest
5153
- name: Install modules
5254
run: npm ci
5355
- uses: actions/download-artifact@v4
@@ -59,7 +61,6 @@ jobs:
5961
- name: Release
6062
env:
6163
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
6364
run: npx semantic-release
6465
- name: Set npm package url to GITHUB_OUTPUT
6566
id: set-npm-url

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,47 @@ const parts = splitText("Hello <and> world [then] !", ["<and>", "[then]"]);
460460
console.log(parts); // ["Hello ", new Separator("<and>"), " world ", new Separator("[then]"), " !"]
461461
```
462462

463+
### `scopeExit`
464+
Create a scope exit handle that will call the provided callback when disposed, to be used with `using` or `await using`.
465+
466+
For example, this code:
467+
```typescript
468+
import {scopeExit} from "lifecycle-utils";
469+
470+
function example() {
471+
using exitHandle = scopeExit(() => {
472+
console.log("exiting scope");
473+
});
474+
console.log("inside scope");
475+
}
476+
477+
async function asyncExample() {
478+
await using exitHandle = scopeExit(async () => {
479+
await new Promise((resolve) => setTimeout(resolve, 100));
480+
console.log("exiting async scope");
481+
});
482+
console.log("inside async scope");
483+
}
484+
485+
example();
486+
console.log("example done");
487+
console.log()
488+
489+
await asyncExample();
490+
console.log("asyncExample done");
491+
```
492+
493+
Will print this:
494+
```
495+
inside scope
496+
exiting scope
497+
example done
498+
499+
inside async scope
500+
exiting async scope
501+
asyncExample done
502+
```
503+
463504
## Contributing
464505
To contribute to `lifecycle-utils` see [CONTRIBUTING.md](https://github.com/giladgd/lifecycle-utils/blob/master/CONTRIBUTING.md).
465506

eslint.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ export default tseslint.config({
153153
args: "none",
154154
ignoreRestSiblings: true,
155155
varsIgnorePattern: "^set",
156-
caughtErrors: "none"
156+
caughtErrors: "none",
157+
ignoreUsingDeclarations: true
157158
}],
158159
"@typescript-eslint/no-empty-object-type": ["off"],
159160
"@typescript-eslint/member-ordering": ["warn", {

0 commit comments

Comments
 (0)