Skip to content

lifecycle hooks

awekrx edited this page May 29, 2026 · 1 revision

lifecycle-hooks

Import

import { lifecycleHooks } from '@dev-suite/decorators/lifecycle-hooks'

Category

  • class

Use Case

Run standard lifecycle methods (init/destroy) consistently for classes.

Replaces

  • Manual init/dispose invocation wrappers
  • Inconsistent lifecycle naming and call order

Example 1

Without decorator

class WorkerProcess {
  start() {
    this.connect();
    this.subscribe();
  }

  stop() {
    this.unsubscribe();
    this.disconnect();
  }

  private connect() {}
  private subscribe() {}
  private unsubscribe() {}
  private disconnect() {}
}

With decorator

import { lifecycleHooks } from '@dev-suite/decorators/lifecycle-hooks';

@lifecycleHooks({ onInit: 'connect', onDestroy: 'disconnect' })
class WorkerProcess {
  start() {
    this.subscribe();
  }

  stop() {
    this.unsubscribe();
  }

  private connect() {}
  private subscribe() {}
  private unsubscribe() {}
  private disconnect() {}
}

Why better

  • Lifecycle contract is explicit at class declaration
  • Reduces lifecycle orchestration boilerplate in app code

Example 2

Without decorator

class FileWatcher {
  boot() {
    this.openHandle();
    this.watch();
  }

  shutdown() {
    this.unwatch();
    this.closeHandle();
  }

  private openHandle() {}
  private watch() {}
  private unwatch() {}
  private closeHandle() {}
}

With decorator

import { lifecycleHooks } from '@dev-suite/decorators/lifecycle-hooks';

@lifecycleHooks({ onInit: 'openHandle', onDestroy: 'closeHandle' })
class FileWatcher {
  boot() {
    this.watch();
  }

  shutdown() {
    this.unwatch();
  }

  private openHandle() {}
  private watch() {}
  private unwatch() {}
  private closeHandle() {}
}

Why better

  • Works the same across different lifecycle method naming styles
  • Makes startup/shutdown behavior easier to audit

Clone this wiki locally