Skip to content

Lightweight reactive extensions for modern JavaScript

License

Notifications You must be signed in to change notification settings

machester4/rxjs-lite

Repository files navigation

Lightweight reactive extensions for modern JavaScript (rxjs-lite)

npm package version number Actions Status License

sample code

import { Observable } from 'rxjs-lite'

class ChatModelObservable extends Observable {
  private messages: string[] = []

  constructor() {
    super()
  }

  sendMessage(userID: number, message: string) {
    this.messages.push(message)
    this._next({ userID, message })
  }
}

type ChatMessageDto = {
  userID: number
  message: string
}

const testObservable = new ChatModelObservable()
const subscriptionA = testObservable.subscribe<ChatMessageDto>((message) =>
  console.log('message received subscription A: ', message)
)
const subscriptionB = testObservable.subscribe<ChatMessageDto>((message) =>
  console.log('message received subscription B: ', message)
)

testObservable.sendMessage(1, 'Hello')

subscriptionA.unsubscribe()

testObservable.sendMessage(2, 'World')