thegecko / webbluetooth

Node.js implementation of the Web Bluetooth Specification
https://thegecko.github.io/webbluetooth/
MIT License
150 stars 23 forks source link

feat: use EventTarget #205

Open Symbitic opened 5 months ago

Symbitic commented 5 months ago

I extracted the first part of my bigger PR: this just replaces all usage of EventDispatcher with EventTarget.

I tried a few experiments with how to document events. I recommend running npm run docs and seeing the documentation for the Bluetooth class.

thegecko commented 5 months ago

Thanks, can you fix up the linting? I'm also keen to keep the noble adapter around for now.

Symbitic commented 5 months ago

I ran eslint with --fix and re-added the noble-adapter file.

For documentation, I experimented with two styles: an "Events" category in the description of every class, and a Markdown table in the override of addEventListener.

image image

Do you have a preference?

thegecko commented 5 months ago

Thanks for the update!

For documentation, I experimented with two styles: an "Events" category in the description of every class, and a Markdown table in the override of addEventListener.

I think tying the events to the class directly makes it more readable and discoverable. It's not a strong preference though!

Symbitic commented 5 months ago

I think tying the events to the class directly makes it more readable and discoverable. It's not a strong preference though!

I added an "Events" category to every class. Should be ready for merging.

thegecko commented 4 months ago

@Symbitic I tried this locally and see many of the tests are failing. The expected output looks like:

> yarn test          
yarn run v1.22.22
$ mocha --timeout 10000 test/*.test.js

  bluetooth module
    ✔ should describe basic constants

  device
    ✔ should return a device with requestDevice
    ✔ should have device properties
    ✔ should have gatt properties
    ✔ should connect and disconnect (2355ms)
    ✔ should have disconnect event (2270ms)

  devices
    ✔ should return no devices with getDevices (2058ms)
    ✔ should return one device with getDevices after a request (2215ms)
    ✔ should forget devices (4225ms)

  all devices
    ✔ should return all devices with getDevices (2045ms)

  services
    ✔ should get services
    ✔ should get primary service

  characteristics
    ✔ should get characteristics
    ✔ should get characteristic
    ✔ should read characteristic value (64ms)
    ✔ should write characteristic value (63ms)

--- PRESS A BUTTON ---

    ✔ should emit characteristic value changed event (1974ms)

  descriptors
    ✔ should get descriptors
    ✔ should get descriptor

  bluetoothuuid
    ✔ should get service
    ✔ should get characteristic
    ✔ should get descriptor
    ✔ should get canonical uuid

  23 passing (41s)

But this is the output from this PR:

> yarn test
yarn run v1.22.22
$ mocha --timeout 10000 test/*.test.js

  bluetooth module
    ✔ should describe basic constants

  device
    ✔ should return a device with requestDevice
    ✔ should have device properties
    ✔ should have gatt properties
    ✔ should connect and disconnect (2497ms)
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.
    1) should have disconnect event

  devices
    ✔ should return no devices with getDevices (2057ms)
    ✔ should return one device with getDevices after a request (2180ms)
    ✔ should forget devices (4242ms)

  all devices
    ✔ should return all devices with getDevices (2059ms)

  services
    2) should get services
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.
    3) should get primary service
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.

  characteristics
    4) should get characteristics
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.
    5) should get characteristic
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.
    6) should read characteristic value
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.
    7) should write characteristic value
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.
    8) should emit characteristic value changed event
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.

  descriptors
    9) "before each" hook for "should get descriptors"
(node:29473) [DEP0168] DeprecationWarning: Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.

  bluetoothuuid
    ✔ should get service
    ✔ should get characteristic
    ✔ should get descriptor
    ✔ should get canonical uuid

  13 passing (56s)
  9 failing

  1) device
       should have disconnect event:
     Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/robmor01/Projects/webbluetooth/test/bluetooth.test.js)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7)

  2) services
       should get services:
     TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of DOMEvent
      at BluetoothRemoteGATTService.dispatchEvent (node:internal/event_target:755:13)
      at new BluetoothRemoteGATTService (dist/service.js:123:14)
      at /Users/robmor01/Projects/webbluetooth/dist/server.js:129:28
      at Array.map (<anonymous>)
      at BluetoothRemoteGATTServer.<anonymous> (dist/server.js:125:42)
      at Generator.next (<anonymous>)
      at fulfilled (dist/server.js:29:58)

  3) services
       should get primary service:
     TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of DOMEvent
      at BluetoothRemoteGATTService.dispatchEvent (node:internal/event_target:755:13)
      at new BluetoothRemoteGATTService (dist/service.js:123:14)
      at /Users/robmor01/Projects/webbluetooth/dist/server.js:129:28
      at Array.map (<anonymous>)
      at BluetoothRemoteGATTServer.<anonymous> (dist/server.js:125:42)
      at Generator.next (<anonymous>)
      at fulfilled (dist/server.js:29:58)

  4) characteristics
       should get characteristics:
     TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of DOMEvent
      at BluetoothRemoteGATTService.dispatchEvent (node:internal/event_target:755:13)
      at new BluetoothRemoteGATTService (dist/service.js:123:14)
      at /Users/robmor01/Projects/webbluetooth/dist/server.js:129:28
      at Array.map (<anonymous>)
      at BluetoothRemoteGATTServer.<anonymous> (dist/server.js:125:42)
      at Generator.next (<anonymous>)
      at fulfilled (dist/server.js:29:58)

  5) characteristics
       should get characteristic:
     TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of DOMEvent
      at BluetoothRemoteGATTService.dispatchEvent (node:internal/event_target:755:13)
      at new BluetoothRemoteGATTService (dist/service.js:123:14)
      at /Users/robmor01/Projects/webbluetooth/dist/server.js:129:28
      at Array.map (<anonymous>)
      at BluetoothRemoteGATTServer.<anonymous> (dist/server.js:125:42)
      at Generator.next (<anonymous>)
      at fulfilled (dist/server.js:29:58)

  6) characteristics
       should read characteristic value:
     TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of DOMEvent
      at BluetoothRemoteGATTService.dispatchEvent (node:internal/event_target:755:13)
      at new BluetoothRemoteGATTService (dist/service.js:123:14)
      at /Users/robmor01/Projects/webbluetooth/dist/server.js:129:28
      at Array.map (<anonymous>)
      at BluetoothRemoteGATTServer.<anonymous> (dist/server.js:125:42)
      at Generator.next (<anonymous>)
      at fulfilled (dist/server.js:29:58)

  7) characteristics
       should write characteristic value:
     TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of DOMEvent
      at BluetoothRemoteGATTService.dispatchEvent (node:internal/event_target:755:13)
      at new BluetoothRemoteGATTService (dist/service.js:123:14)
      at /Users/robmor01/Projects/webbluetooth/dist/server.js:129:28
      at Array.map (<anonymous>)
      at BluetoothRemoteGATTServer.<anonymous> (dist/server.js:125:42)
      at Generator.next (<anonymous>)
      at fulfilled (dist/server.js:29:58)

  8) characteristics
       should emit characteristic value changed event:
     Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/robmor01/Projects/webbluetooth/test/bluetooth.test.js)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7)

  9) descriptors
       "before each" hook for "should get descriptors":
     TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of DOMEvent
      at BluetoothRemoteGATTService.dispatchEvent (node:internal/event_target:755:13)
      at new BluetoothRemoteGATTService (dist/service.js:123:14)
      at /Users/robmor01/Projects/webbluetooth/dist/server.js:129:28
      at Array.map (<anonymous>)
      at BluetoothRemoteGATTServer.<anonymous> (dist/server.js:125:42)
      at Generator.next (<anonymous>)
      at fulfilled (dist/server.js:29:58)

error Command failed with signal "SIGABRT".