项目作者: xaliphostes

项目描述 :
Signal-Slot "à la" Qt
高级语言: TypeScript
项目地址: git://github.com/xaliphostes/signal-slot-js.git
创建时间: 2020-12-22T11:52:27Z
项目社区:https://github.com/xaliphostes/signal-slot-js

开源协议:

下载


Signal-Slot library “à la” Qt

Signal/Slot pattern implemented in TypeScript.

  • No dependency, small (only 4ko in mignified mode) and efficient library.

  • We try to follow the Qt syntax for the connection, i.e., connect(sender, signal, receiver, slot).

  • Slot can be any method from a class, a setter, an arrow function or any function with any number of arguments.

  • Allows to connect/disconnect and lock/unlock connections

Installation

  1. npm i signal-slot-js

or from the sources

  1. git checkout git@github.com:xaliphostes/signal-slot-js.git
  2. npm install
  3. npm run build

Testing

Require node version > 11

  1. npm run test

Running examples

All examples are in the examples directory. Run them using ts-node

Usage

The following example shows how to implement the combineLatest from RxJS.

It will create the following:

  1. task1 ---\
  2. ---> CombineLatest --> task3
  3. rask2 ---/

task3 is launched only when task1 AND task2 are done.

  1. import {create, emit, connect} from 'signal-slot'
  2. // ----------------------------------------------
  3. class Task {
  4. constructor(public name: string, public ms: number) {
  5. // Create a signal for this class
  6. create(this, 'finished')
  7. }
  8. run() {
  9. console.log(`Task ${this.name} is running for ${this.ms}ms...`)
  10. // Emit a signal
  11. setTimeout( () => emit(this, 'finished'), this.ms)
  12. }
  13. }
  14. // ----------------------------------------------
  15. // Will be executed when task1 AND task2 are done
  16. class CombineLatest {
  17. private doneTask1 = false
  18. private doneTask2 = false
  19. constructor(private task1: Task, private task2: Task) {
  20. // Create a signal for this class
  21. create(this, 'done')
  22. // Perform two connections
  23. connect({sender: task1, signal: 'finished', receiver: () => this.doneTask(1)})
  24. connect({sender: task2, signal: 'finished', receiver: () => this.doneTask(2)})
  25. }
  26. private doneTask(n: number) {
  27. console.log('done doing task', n)
  28. switch(n) {
  29. case 1: this.doneTask1 = true; break
  30. case 2: this.doneTask2 = true; break
  31. }
  32. if (this.doneTask1 && this.doneTask2) {
  33. console.log('doing task combine')
  34. this.doneTask1 = false
  35. this.doneTask2 = false
  36. // Emit a signal
  37. emit(this, 'done')
  38. }
  39. }
  40. }
  41. // ----------------------------------------------
  42. const task1 = new Task('task 1', 500)
  43. const task2 = new Task('task 2', 3000)
  44. const combine = new CombineLatest(task1, task2)
  45. // Perform a connection
  46. connect({
  47. sender: combine,
  48. signal: 'done',
  49. receiver: () => console.log('All done!')
  50. })
  51. // Run the 2 tasks in parallel and trigger combine
  52. // when both are done
  53. task1.run()
  54. task2.run()

License

MIT License