or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

async-operations.mddisposables.mdindex.mdobservable-aggregation.mdobservable-combination.mdobservable-creation.mdobservable-filtering.mdobservable-transformation.mdschedulers.mdsubjects.mdtesting.mdtime-operations.md

disposables.mddocs/

0

# Disposables

1

2

Resource management objects for cleaning up subscriptions and managing the lifetime of observables and observers.

3

4

## Capabilities

5

6

### Disposable

7

8

Basic disposable interface for resource cleanup.

9

10

```javascript { .api }

11

/**

12

* Creates a disposable with the given cleanup action

13

* @param {function} action - Action to run during disposal

14

* @returns {Disposable} Disposable object

15

*/

16

Rx.Disposable.create = function(action);

17

18

/**

19

* Gets an empty disposable that does nothing when disposed

20

* @returns {Disposable} Empty disposable

21

*/

22

Rx.Disposable.empty;

23

24

/**

25

* Checks if object is a disposable

26

* @param {*} obj - Object to test

27

* @returns {boolean} True if object is disposable

28

*/

29

Rx.Disposable.isDisposable = function(obj);

30

31

/**

32

* Disposes the resource

33

*/

34

dispose: function();

35

36

/**

37

* Gets a value that indicates whether the object is disposed

38

* @returns {boolean} True if disposed

39

*/

40

isDisposed: function();

41

```

42

43

**Usage Example:**

44

45

```javascript

46

var disposable = Rx.Disposable.create(function() {

47

console.log('Cleanup performed');

48

});

49

50

// Later...

51

disposable.dispose(); // Logs: 'Cleanup performed'

52

console.log(disposable.isDisposed()); // true

53

```

54

55

### CompositeDisposable

56

57

Manages multiple disposables as a single disposable.

58

59

```javascript { .api }

60

/**

61

* Creates a composite disposable with initial disposables

62

* @param {...Disposable} disposables - Initial disposables to manage

63

* @returns {CompositeDisposable} Composite disposable

64

*/

65

function CompositeDisposable(...disposables);

66

67

/**

68

* Adds a disposable to the group

69

* @param {Disposable} item - Disposable to add

70

*/

71

add: function(item);

72

73

/**

74

* Removes and disposes a disposable from the group

75

* @param {Disposable} item - Disposable to remove

76

* @returns {boolean} True if item was found and removed

77

*/

78

remove: function(item);

79

80

/**

81

* Disposes all disposables in the group and prohibits adding more

82

*/

83

dispose: function();

84

85

/**

86

* Clears all disposables from group without disposing them

87

*/

88

clear: function();

89

90

/**

91

* Converts to array of disposables

92

* @returns {Array<Disposable>} Array of disposables

93

*/

94

toArray: function();

95

96

/**

97

* Gets the number of disposables in the group

98

* @returns {number} Count of disposables

99

*/

100

length: number;

101

102

/**

103

* Gets whether the group is disposed

104

* @returns {boolean} True if disposed

105

*/

106

isDisposed: function();

107

```

108

109

**Usage Example:**

110

111

```javascript

112

var composite = new Rx.CompositeDisposable();

113

114

var subscription1 = someObservable.subscribe(observer1);

115

var subscription2 = someObservable.subscribe(observer2);

116

117

composite.add(subscription1);

118

composite.add(subscription2);

119

120

// Dispose all subscriptions at once

121

composite.dispose();

122

```

123

124

### SerialDisposable

125

126

Holds a single disposable, replacing it with new ones over time.

127

128

```javascript { .api }

129

/**

130

* Creates a serial disposable

131

* @returns {SerialDisposable} Serial disposable

132

*/

133

function SerialDisposable();

134

135

/**

136

* Gets or sets the current disposable

137

* @param {Disposable} [value] - New disposable to set

138

* @returns {Disposable} Current disposable if getting

139

*/

140

getDisposable: function();

141

setDisposable: function(value);

142

143

/**

144

* Disposes the current disposable and prevents future assignments

145

*/

146

dispose: function();

147

148

/**

149

* Gets whether the serial disposable is disposed

150

* @returns {boolean} True if disposed

151

*/

152

isDisposed: function();

153

```

154

155

**Usage Example:**

156

157

```javascript

158

var serialDisposable = new Rx.SerialDisposable();

159

160

// Set initial subscription

161

serialDisposable.setDisposable(observable1.subscribe(observer));

162

163

// Replace with new subscription (previous one is disposed)

164

serialDisposable.setDisposable(observable2.subscribe(observer));

165

166

// Dispose current subscription

167

serialDisposable.dispose();

168

```

169

170

### SingleAssignmentDisposable

171

172

Holds a single disposable that can only be assigned once.

173

174

```javascript { .api }

175

/**

176

* Creates a single assignment disposable

177

* @returns {SingleAssignmentDisposable} Single assignment disposable

178

*/

179

function SingleAssignmentDisposable();

180

181

/**

182

* Gets the current disposable

183

* @returns {Disposable} Current disposable

184

*/

185

getDisposable: function();

186

187

/**

188

* Sets the disposable (can only be called once)

189

* @param {Disposable} value - Disposable to set

190

*/

191

setDisposable: function(value);

192

193

/**

194

* Disposes the current disposable

195

*/

196

dispose: function();

197

198

/**

199

* Gets whether the disposable is disposed

200

* @returns {boolean} True if disposed

201

*/

202

isDisposed: function();

203

```

204

205

### RefCountDisposable

206

207

Provides a disposable that can be disposed multiple times with reference counting.

208

209

```javascript { .api }

210

/**

211

* Creates a reference counted disposable

212

* @param {Disposable} disposable - Underlying disposable to manage

213

* @returns {RefCountDisposable} Reference counted disposable

214

*/

215

function RefCountDisposable(disposable);

216

217

/**

218

* Disposes the underlying disposable

219

*/

220

dispose: function();

221

222

/**

223

* Gets whether the disposable is disposed

224

* @returns {boolean} True if disposed

225

*/

226

isDisposed: function();

227

228

/**

229

* Returns a disposable that increments the reference count

230

* @returns {Disposable} Disposable that decrements count when disposed

231

*/

232

getDisposable: function();

233

```

234

235

### BinaryDisposable

236

237

Manages two disposables as a single disposable.

238

239

```javascript { .api }

240

/**

241

* Creates a binary disposable

242

* @param {Disposable} first - First disposable

243

* @param {Disposable} second - Second disposable

244

* @returns {BinaryDisposable} Binary disposable

245

*/

246

function BinaryDisposable(first, second);

247

248

/**

249

* Disposes both disposables

250

*/

251

dispose: function();

252

253

/**

254

* Gets whether both disposables are disposed

255

* @returns {boolean} True if disposed

256

*/

257

isDisposed: function();

258

```

259

260

### ScheduledDisposable

261

262

Schedules disposal of another disposable on a specific scheduler.

263

264

```javascript { .api }

265

/**

266

* Creates a scheduled disposable

267

* @param {Scheduler} scheduler - Scheduler to dispose on

268

* @param {Disposable} disposable - Disposable to schedule for disposal

269

* @returns {ScheduledDisposable} Scheduled disposable

270

*/

271

function ScheduledDisposable(scheduler, disposable);

272

273

/**

274

* Schedules disposal of the wrapped disposable

275

*/

276

dispose: function();

277

278

/**

279

* Gets whether the disposable is disposed

280

* @returns {boolean} True if disposed

281

*/

282

isDisposed: function();

283

```

284

285

## Types

286

287

```javascript { .api }

288

// Core disposable interface

289

interface Disposable {

290

dispose(): void;

291

isDisposed(): boolean;

292

}

293

294

// Composite disposable interface

295

interface CompositeDisposable extends Disposable {

296

add(item: Disposable): void;

297

remove(item: Disposable): boolean;

298

clear(): void;

299

toArray(): Disposable[];

300

length: number;

301

}

302

303

// Serial disposable interface

304

interface SerialDisposable extends Disposable {

305

getDisposable(): Disposable;

306

setDisposable(value: Disposable): void;

307

}

308

309

// Single assignment disposable interface

310

interface SingleAssignmentDisposable extends Disposable {

311

getDisposable(): Disposable;

312

setDisposable(value: Disposable): void;

313

}

314

315

// Reference counted disposable interface

316

interface RefCountDisposable extends Disposable {

317

getDisposable(): Disposable;

318

}

319

```