Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/berendsliedrecht/dtor


https://github.com/berendsliedrecht/dtor

Last synced: 22 days ago
JSON representation

Awesome Lists containing this project

README

        

# Experimental TypeScript Deconstructor

Requires TypeScript 0.5.2 or newer

## Example tsconfig.json

```json
{
"compilerOptions": {
"lib": ["esnext", "dom"],
"target": "es2015",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"outDir": "build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true
}
}
```

## Example using with async deconstructors

```typescript
import type { AsyncDeconstructable } from 'dtor'
import { Deconstructor } from "dtor";

class SomeExample {
public constructor() {
console.log("constructed");
}

@Deconstructor()
public async asyncDeconstructor() {
await new Promise((r) => setTimeout(r, 2000));
console.log("deconstructed");
}
}

void (async () => {
await using cls = new SomeExample() as AsyncDeconstructable;
})()

```

## Example using with sync deconstructors

```typescript
import type { Deconstructable } from 'dtor'
import { Deconstructor } from "dtor";

class SomeExample {
public constructor() {
console.log("constructed");
}

@Deconstructor()
public deconstructor() {
console.log("deconstructed");
}
}

using cls = new SomeExample() as Deconstructable;
```

## Without this library

```typescript
class SomeExample {
public constructor() {
console.log("constructed");
}

[Symbol.dispose]() {
console.log("deconstructed");
}
}

using cls = new SomeExample() as Deconstructable;
```