1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
[quickjs-emscripten](../README.md) / [Exports](../modules.md) / Scope
# Class: Scope
Scope helps reduce the burden of manually tracking and disposing of
Lifetimes. See [withScope](Scope.md#withscope). and [withScopeAsync](Scope.md#withscopeasync).
## Implements
- [`Disposable`](../interfaces/Disposable.md)
## Table of contents
### Constructors
- [constructor](Scope.md#constructor)
### Accessors
- [alive](Scope.md#alive)
### Methods
- [dispose](Scope.md#dispose)
- [manage](Scope.md#manage)
- [withScope](Scope.md#withscope)
- [withScopeAsync](Scope.md#withscopeasync)
- [withScopeMaybeAsync](Scope.md#withscopemaybeasync)
## Constructors
### constructor
• **new Scope**()
## Accessors
### alive
• `get` **alive**(): `boolean`
#### Returns
`boolean`
#### Implementation of
[Disposable](../interfaces/Disposable.md).[alive](../interfaces/Disposable.md#alive)
#### Defined in
[ts/lifetime.ts:265](https://github.com/justjake/quickjs-emscripten/blob/main/ts/lifetime.ts#L265)
## Methods
### dispose
▸ **dispose**(): `void`
Dispose of the underlying resources used by this object.
#### Returns
`void`
#### Implementation of
[Disposable](../interfaces/Disposable.md).[dispose](../interfaces/Disposable.md#dispose)
#### Defined in
[ts/lifetime.ts:269](https://github.com/justjake/quickjs-emscripten/blob/main/ts/lifetime.ts#L269)
___
### manage
▸ **manage**<`T`\>(`lifetime`): `T`
Track `lifetime` so that it is disposed when this scope is disposed.
#### Type parameters
| Name | Type |
| :------ | :------ |
| `T` | extends [`Disposable`](../interfaces/Disposable.md) |
#### Parameters
| Name | Type |
| :------ | :------ |
| `lifetime` | `T` |
#### Returns
`T`
#### Defined in
[ts/lifetime.ts:260](https://github.com/justjake/quickjs-emscripten/blob/main/ts/lifetime.ts#L260)
___
### withScope
▸ `Static` **withScope**<`R`\>(`block`): `R`
Run `block` with a new Scope instance that will be disposed after the block returns.
Inside `block`, call `scope.manage` on each lifetime you create to have the lifetime
automatically disposed after the block returns.
**`warning`** Do not use with async functions. Instead, use [withScopeAsync](Scope.md#withscopeasync).
#### Type parameters
| Name |
| :------ |
| `R` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `block` | (`scope`: [`Scope`](Scope.md)) => `R` |
#### Returns
`R`
#### Defined in
[ts/lifetime.ts:205](https://github.com/justjake/quickjs-emscripten/blob/main/ts/lifetime.ts#L205)
___
### withScopeAsync
▸ `Static` **withScopeAsync**<`R`\>(`block`): `Promise`<`R`\>
Run `block` with a new Scope instance that will be disposed after the
block's returned promise settles. Inside `block`, call `scope.manage` on each
lifetime you create to have the lifetime automatically disposed after the
block returns.
#### Type parameters
| Name |
| :------ |
| `R` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `block` | (`scope`: [`Scope`](Scope.md)) => `Promise`<`R`\> |
#### Returns
`Promise`<`R`\>
#### Defined in
[ts/lifetime.ts:242](https://github.com/justjake/quickjs-emscripten/blob/main/ts/lifetime.ts#L242)
___
### withScopeMaybeAsync
▸ `Static` **withScopeMaybeAsync**<`Return`, `This`, `Yielded`\>(`_this`, `block`): `Return` \| `Promise`<`Return`\>
#### Type parameters
| Name |
| :------ |
| `Return` |
| `This` |
| `Yielded` |
#### Parameters
| Name | Type |
| :------ | :------ |
| `_this` | `This` |
| `block` | `MaybeAsyncBlock`<`Return`, `This`, `Yielded`, [[`Scope`](Scope.md)]\> |
#### Returns
`Return` \| `Promise`<`Return`\>
#### Defined in
[ts/lifetime.ts:218](https://github.com/justjake/quickjs-emscripten/blob/main/ts/lifetime.ts#L218)
|