@@ -187,6 +187,92 @@ disposeAggregator.add(async () => {
187187disposeAggregator .dispose ();
188188```
189189
190+ ### ` DisposableHandle `
191+ An object that provides a ` .dispose() ` method that can called only once.
192+
193+ Calling ` .dispose() ` will call the provided ` onDispose ` function only once.
194+ Any subsequent calls to ` .dispose() ` will do nothing.
195+
196+ ``` typescript
197+ import {DisposableHandle } from " lifecycle-utils" ;
198+
199+ function createHandle() {
200+ console .log (" allocating resources" );
201+
202+ return new DisposableHandle (() => {
203+ console .log (" resources disposed" );
204+ });
205+ }
206+
207+ const handle = createHandle ();
208+ handle .dispose ();
209+ ```
210+
211+ Using the ` using ` feature of TypeScript is also supported:
212+ ``` typescript
213+ import {DisposableHandle } from " lifecycle-utils" ;
214+
215+ function createHandle() {
216+ console .log (" allocating resources" );
217+
218+ return new DisposableHandle (() => {
219+ console .log (" resources disposed" );
220+ });
221+ }
222+
223+ function doWork() {
224+ using handle = createHandle ();
225+ }
226+
227+ doWork ();
228+ // resources disposed
229+ // the dispose function was called since the scope of the `doWork` function ended
230+ ```
231+
232+ ### ` AsyncDisposableHandle `
233+ An object that provides an async ` .dispose() ` method that can called only once.
234+
235+ Calling ` .dispose() ` will call the provided ` onDispose ` function only once.
236+ Any subsequent calls to ` .dispose() ` will do nothing.
237+
238+ ``` typescript
239+ import {AsyncDisposableHandle } from " lifecycle-utils" ;
240+
241+ function createHandle() {
242+ console .log (" allocating resources" );
243+
244+ return new AsyncDisposableHandle (async () => {
245+ await new Promise (resolve => setTimeout (resolve , 1000 ));
246+ console .log (" resources disposed" );
247+ });
248+ }
249+
250+ const handle = createHandle ();
251+ await handle .dispose ();
252+ ```
253+
254+ Using the ` await using ` feature of TypeScript is also supported:
255+ ``` typescript
256+ import {AsyncDisposableHandle } from " lifecycle-utils" ;
257+
258+ function createHandle() {
259+ console .log (" allocating resources" );
260+
261+ return new AsyncDisposableHandle (async () => {
262+ await new Promise (resolve => setTimeout (resolve , 1000 ));
263+ console .log (" resources disposed" );
264+ });
265+ }
266+
267+ async function doWork() {
268+ await using handle = createHandle ();
269+ }
270+
271+ await doWork ();
272+ // resources disposed
273+ // the dispose function was called since the scope of the `doWork` function ended
274+ ```
275+
190276### ` MultiKeyMap `
191277` MultiKeyMap ` is a utility class that works like a ` Map ` , but accepts multiple values as the key for each value.
192278
0 commit comments