pxt-core 10.3.2 → 10.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/built/pxt.js CHANGED
@@ -120406,50 +120406,51 @@ var pxt;
120406
120406
  this.handleUSBDisconnected = this.handleUSBDisconnected.bind(this);
120407
120407
  }
120408
120408
  enable() {
120409
+ var _a, _b;
120409
120410
  if (this.enabled)
120410
120411
  return;
120411
120412
  this.enabled = true;
120412
120413
  this.log("registering webusb events");
120413
- navigator.usb.addEventListener('disconnect', this.handleUSBDisconnected, false);
120414
- navigator.usb.addEventListener('connect', this.handleUSBConnected, false);
120414
+ (_a = navigator.usb) === null || _a === void 0 ? void 0 : _a.addEventListener('disconnect', this.handleUSBDisconnected, false);
120415
+ (_b = navigator.usb) === null || _b === void 0 ? void 0 : _b.addEventListener('connect', this.handleUSBConnected, false);
120415
120416
  }
120416
120417
  disable() {
120418
+ var _a, _b;
120417
120419
  if (!this.enabled)
120418
120420
  return;
120419
120421
  this.enabled = false;
120420
120422
  this.log(`unregistering webusb events`);
120421
- navigator.usb.removeEventListener('disconnect', this.handleUSBDisconnected);
120422
- navigator.usb.removeEventListener('connect', this.handleUSBConnected);
120423
+ (_a = navigator.usb) === null || _a === void 0 ? void 0 : _a.removeEventListener('disconnect', this.handleUSBDisconnected);
120424
+ (_b = navigator.usb) === null || _b === void 0 ? void 0 : _b.removeEventListener('connect', this.handleUSBConnected);
120423
120425
  }
120424
- disposeAsync() {
120426
+ async disposeAsync() {
120425
120427
  this.disable();
120426
- return Promise.resolve();
120427
120428
  }
120428
120429
  handleUSBDisconnected(event) {
120430
+ var _a;
120429
120431
  this.log("device disconnected");
120430
120432
  if (event.device == this.dev) {
120431
120433
  this.log("clear device");
120432
120434
  this.clearDev();
120433
- if (this.onDeviceConnectionChanged)
120434
- this.onDeviceConnectionChanged(false);
120435
+ (_a = this.onDeviceConnectionChanged) === null || _a === void 0 ? void 0 : _a.call(this, false);
120435
120436
  }
120436
120437
  }
120437
120438
  handleUSBConnected(event) {
120439
+ var _a;
120438
120440
  const newdev = event.device;
120439
120441
  this.log(`device connected ${newdev.serialNumber}`);
120440
120442
  if (!this.dev && !this.connecting) {
120441
120443
  this.log("attach device");
120442
- if (this.onDeviceConnectionChanged)
120443
- this.onDeviceConnectionChanged(true);
120444
+ (_a = this.onDeviceConnectionChanged) === null || _a === void 0 ? void 0 : _a.call(this, true);
120444
120445
  }
120445
120446
  }
120446
120447
  clearDev() {
120448
+ var _a;
120447
120449
  if (this.dev) {
120448
120450
  this.dev = null;
120449
120451
  this.epIn = null;
120450
120452
  this.epOut = null;
120451
- if (this.onConnectionChanged)
120452
- this.onConnectionChanged();
120453
+ (_a = this.onConnectionChanged) === null || _a === void 0 ? void 0 : _a.call(this);
120453
120454
  }
120454
120455
  }
120455
120456
  error(msg) {
@@ -120458,27 +120459,28 @@ var pxt;
120458
120459
  log(msg) {
120459
120460
  pxt.debug("webusb: " + msg);
120460
120461
  }
120461
- disconnectAsync() {
120462
+ async disconnectAsync() {
120462
120463
  this.ready = false;
120463
120464
  if (!this.dev)
120464
- return Promise.resolve();
120465
+ return;
120465
120466
  this.log("close device");
120466
- return this.dev.close()
120467
- .catch(e => {
120467
+ try {
120468
+ await this.dev.close();
120469
+ }
120470
+ catch (e) {
120468
120471
  // just ignore errors closing, most likely device just disconnected
120469
- })
120470
- .then(() => {
120471
- this.clearDev();
120472
- return pxt.U.delay(500);
120473
- });
120472
+ }
120473
+ this.clearDev();
120474
+ await pxt.U.delay(500);
120474
120475
  }
120475
120476
  async forgetAsync() {
120476
120477
  var _a;
120477
120478
  if (!((_a = this.dev) === null || _a === void 0 ? void 0 : _a.forget))
120478
120479
  return false;
120479
120480
  try {
120481
+ const dev = this.dev;
120480
120482
  await this.disconnectAsync();
120481
- await this.dev.forget();
120483
+ await dev.forget();
120482
120484
  return true;
120483
120485
  // connection changed listener will handle disconnecting when access is revoked.
120484
120486
  }
@@ -120486,19 +120488,23 @@ var pxt;
120486
120488
  return false;
120487
120489
  }
120488
120490
  }
120489
- reconnectAsync() {
120491
+ async reconnectAsync() {
120490
120492
  this.log("reconnect");
120491
120493
  this.setConnecting(true);
120492
- return this.disconnectAsync()
120493
- .then(tryGetDevicesAsync)
120494
- .then(devs => this.connectAsync(devs))
120495
- .finally(() => this.setConnecting(false));
120494
+ try {
120495
+ await this.disconnectAsync();
120496
+ const devs = await tryGetDevicesAsync();
120497
+ await this.connectAsync(devs);
120498
+ }
120499
+ finally {
120500
+ this.setConnecting(false);
120501
+ }
120496
120502
  }
120497
120503
  setConnecting(v) {
120504
+ var _a;
120498
120505
  if (v != this.connecting) {
120499
120506
  this.connecting = v;
120500
- if (this.onConnectionChanged)
120501
- this.onConnectionChanged();
120507
+ (_a = this.onConnectionChanged) === null || _a === void 0 ? void 0 : _a.call(this);
120502
120508
  }
120503
120509
  }
120504
120510
  isConnecting() {
@@ -120558,61 +120564,60 @@ var pxt;
120558
120564
  this.setConnecting(false);
120559
120565
  }
120560
120566
  }
120561
- sendPacketAsync(pkt) {
120567
+ async sendPacketAsync(pkt) {
120562
120568
  if (!this.dev)
120563
- return Promise.reject(new Error("Disconnected"));
120569
+ throw new Error("Disconnected");
120564
120570
  pxt.Util.assert(pkt.length <= 64);
120565
120571
  if (!this.epOut) {
120566
- return this.dev.controlTransferOut({
120572
+ const res = await this.dev.controlTransferOut({
120567
120573
  requestType: "class",
120568
120574
  recipient: "interface",
120569
120575
  request: controlTransferSetReport,
120570
120576
  value: controlTransferOutReport,
120571
120577
  index: this.iface.interfaceNumber
120572
- }, pkt).then(res => {
120573
- if (res.status != "ok")
120574
- this.error("USB CTRL OUT transfer failed");
120575
- });
120578
+ }, pkt);
120579
+ if (res.status != "ok")
120580
+ this.error("USB CTRL OUT transfer failed");
120576
120581
  }
120577
- return this.dev.transferOut(this.epOut.endpointNumber, pkt)
120578
- .then(res => {
120582
+ else {
120583
+ const res = await this.dev.transferOut(this.epOut.endpointNumber, pkt);
120579
120584
  if (res.status != "ok")
120580
120585
  this.error("USB OUT transfer failed");
120581
- });
120586
+ }
120582
120587
  }
120583
- readLoop() {
120588
+ async readLoop() {
120584
120589
  if (this.readLoopStarted)
120585
120590
  return;
120586
120591
  this.readLoopStarted = true;
120587
120592
  this.log("start read loop");
120588
- let loop = () => {
120589
- if (!this.ready)
120590
- pxt.U.delay(300).then(loop);
120591
- else
120592
- this.recvPacketAsync()
120593
- .then(buf => {
120594
- if (buf[0]) {
120595
- // we've got data; retry reading immedietly after processing it
120596
- this.onData(buf);
120597
- loop();
120598
- }
120599
- else {
120600
- // throttle down if no data coming
120601
- pxt.U.delay(500).then(loop);
120602
- }
120603
- }, err => {
120604
- if (this.dev)
120605
- this.onError(err);
120606
- pxt.U.delay(300).then(loop);
120607
- });
120608
- };
120609
- loop();
120593
+ while (true) {
120594
+ if (!this.ready) {
120595
+ await pxt.U.delay(300);
120596
+ continue;
120597
+ }
120598
+ try {
120599
+ const buf = await this.recvPacketAsync();
120600
+ if (buf[0]) {
120601
+ // we've got data; retry reading immedietly after processing it
120602
+ this.onData(buf);
120603
+ }
120604
+ else {
120605
+ // throttle down if no data coming
120606
+ await pxt.U.delay(500);
120607
+ }
120608
+ }
120609
+ catch (e) {
120610
+ if (this.dev)
120611
+ this.onError(e);
120612
+ await pxt.U.delay(300);
120613
+ }
120614
+ }
120610
120615
  }
120611
120616
  async recvPacketAsync(timeoutMs) {
120612
120617
  const startTime = Date.now();
120613
120618
  while (!timeoutMs || Date.now() < startTime + timeoutMs) {
120614
120619
  if (!this.dev) {
120615
- return Promise.reject(new Error("Disconnected"));
120620
+ throw new Error("Disconnected");
120616
120621
  }
120617
120622
  const res = await (this.epIn ? this.dev.transferIn(this.epIn.endpointNumber, 64) : this.dev.controlTransferIn({
120618
120623
  requestType: "class",
@@ -120630,84 +120635,84 @@ var pxt;
120630
120635
  }
120631
120636
  this.error("USB IN timed out");
120632
120637
  }
120633
- initAsync() {
120638
+ async initAsync() {
120639
+ var _a;
120634
120640
  if (!this.dev)
120635
- return Promise.reject(new Error("Disconnected"));
120636
- let dev = this.dev;
120641
+ throw new Error("Disconnected");
120642
+ const dev = this.dev;
120637
120643
  this.log("open device");
120638
- return dev.open()
120639
- // assume one configuration; no one really does more
120640
- .then(() => {
120641
- this.log("select configuration");
120642
- return dev.selectConfiguration(1);
120643
- })
120644
- .then(() => {
120645
- let matchesFilters = (iface) => {
120646
- let a0 = iface.alternates[0];
120647
- for (let f of usb.filters) {
120648
- if (f.classCode == null || a0.interfaceClass === f.classCode) {
120649
- if (f.subclassCode == null || a0.interfaceSubclass === f.subclassCode) {
120650
- if (f.protocolCode == null || a0.interfaceProtocol === f.protocolCode) {
120651
- if (a0.endpoints.length == 0)
120652
- return true;
120653
- if (a0.endpoints.length == 2 &&
120654
- a0.endpoints.every(e => e.packetSize == 64))
120655
- return true;
120656
- }
120644
+ await dev.open();
120645
+ // assume one configuration; no one really does more
120646
+ this.log("select configuration");
120647
+ await dev.selectConfiguration(1);
120648
+ let matchesFilters = (iface) => {
120649
+ let a0 = iface.alternates[0];
120650
+ for (let f of usb.filters) {
120651
+ if (f.classCode == null || a0.interfaceClass === f.classCode) {
120652
+ if (f.subclassCode == null || a0.interfaceSubclass === f.subclassCode) {
120653
+ if (f.protocolCode == null || a0.interfaceProtocol === f.protocolCode) {
120654
+ if (a0.endpoints.length == 0)
120655
+ return true;
120656
+ if (a0.endpoints.length == 2 &&
120657
+ a0.endpoints.every(e => e.packetSize == 64))
120658
+ return true;
120657
120659
  }
120658
120660
  }
120659
120661
  }
120660
- return false;
120661
- };
120662
- this.log("got " + dev.configurations[0].interfaces.length + " interfaces");
120663
- const matching = dev.configurations[0].interfaces.filter(matchesFilters);
120664
- let iface = matching[matching.length - 1];
120665
- this.log(`${matching.length} matching interfaces; picking ${iface ? "#" + iface.interfaceNumber : "n/a"}`);
120666
- if (!iface)
120667
- this.error("cannot find supported USB interface");
120668
- this.altIface = iface.alternates[0];
120669
- this.iface = iface;
120670
- if (this.altIface.endpoints.length) {
120671
- this.log("using dedicated endpoints");
120672
- this.epIn = this.altIface.endpoints.filter(e => e.direction == "in")[0];
120673
- this.epOut = this.altIface.endpoints.filter(e => e.direction == "out")[0];
120674
- pxt.Util.assert(this.epIn.packetSize == 64);
120675
- pxt.Util.assert(this.epOut.packetSize == 64);
120676
120662
  }
120677
- else {
120678
- this.log("using ctrl pipe");
120679
- }
120680
- this.log("claim interface");
120681
- return dev.claimInterface(iface.interfaceNumber);
120682
- })
120683
- .then(() => {
120684
- this.log("device ready");
120685
- this.lastKnownDeviceSerialNumber = this.dev.serialNumber;
120686
- this.ready = true;
120687
- if (isHF2)
120688
- this.readLoop();
120689
- if (this.onConnectionChanged)
120690
- this.onConnectionChanged();
120691
- });
120663
+ return false;
120664
+ };
120665
+ this.log("got " + dev.configurations[0].interfaces.length + " interfaces");
120666
+ const matching = dev.configurations[0].interfaces.filter(matchesFilters);
120667
+ let iface = matching[matching.length - 1];
120668
+ this.log(`${matching.length} matching interfaces; picking ${iface ? "#" + iface.interfaceNumber : "n/a"}`);
120669
+ if (!iface)
120670
+ this.error("cannot find supported USB interface");
120671
+ this.altIface = iface.alternates[0];
120672
+ this.iface = iface;
120673
+ if (this.altIface.endpoints.length) {
120674
+ this.log("using dedicated endpoints");
120675
+ this.epIn = this.altIface.endpoints.filter(e => e.direction == "in")[0];
120676
+ this.epOut = this.altIface.endpoints.filter(e => e.direction == "out")[0];
120677
+ pxt.Util.assert(this.epIn.packetSize == 64);
120678
+ pxt.Util.assert(this.epOut.packetSize == 64);
120679
+ }
120680
+ else {
120681
+ this.log("using ctrl pipe");
120682
+ }
120683
+ this.log("claim interface");
120684
+ await dev.claimInterface(iface.interfaceNumber);
120685
+ this.log("device ready");
120686
+ this.lastKnownDeviceSerialNumber = this.dev.serialNumber;
120687
+ this.ready = true;
120688
+ if (isHF2) {
120689
+ // just starting, not waiting on it.
120690
+ /* await */ this.readLoop();
120691
+ }
120692
+ (_a = this.onConnectionChanged) === null || _a === void 0 ? void 0 : _a.call(this);
120693
+ }
120694
+ }
120695
+ async function pairAsync() {
120696
+ var _a;
120697
+ try {
120698
+ const dev = await ((_a = navigator.usb) === null || _a === void 0 ? void 0 : _a.requestDevice({
120699
+ filters: usb.filters
120700
+ }));
120701
+ return !!dev;
120692
120702
  }
120693
- }
120694
- function pairAsync() {
120695
- return navigator.usb.requestDevice({
120696
- filters: usb.filters
120697
- })
120698
- .then(dev => !!dev)
120699
- .catch(e => {
120703
+ catch (e) {
120700
120704
  // user cancelled
120701
120705
  if (e.name == "NotFoundError")
120702
120706
  return undefined;
120703
120707
  throw e;
120704
- });
120708
+ }
120705
120709
  }
120706
120710
  usb.pairAsync = pairAsync;
120707
120711
  async function tryGetDevicesAsync() {
120712
+ var _a;
120708
120713
  pxt.log(`webusb: get devices`);
120709
120714
  try {
120710
- const devs = await navigator.usb.getDevices();
120715
+ const devs = await ((_a = navigator.usb) === null || _a === void 0 ? void 0 : _a.getDevices());
120711
120716
  return devs || [];
120712
120717
  }
120713
120718
  catch (e) {
package/built/pxtlib.d.ts CHANGED
@@ -3308,6 +3308,14 @@ declare namespace pxt.webBluetooth {
3308
3308
  function pairAsync(): Promise<void>;
3309
3309
  function flashAsync(resp: pxtc.CompileResult, d?: pxt.commands.DeployOptions): Promise<void>;
3310
3310
  }
3311
+ interface Navigator {
3312
+ readonly usb?: {
3313
+ getDevices(): Promise<pxt.usb.USBDevice[]>;
3314
+ requestDevice(options?: pxt.usb.USBDeviceRequestOptions): Promise<pxt.usb.USBDevice>;
3315
+ addEventListener(type: "connect" | "disconnect", listener: (ev: pxt.usb.USBConnectionEvent) => any, useCapture?: boolean): void;
3316
+ removeEventListener(type: "connect" | "disconnect", callback: (ev: pxt.usb.USBConnectionEvent) => any, useCapture?: boolean): void;
3317
+ };
3318
+ }
3311
3319
  declare namespace pxt.usb {
3312
3320
  /**
3313
3321
  * For local testing of WebUSB, be sure to (temporarily)
@@ -3334,6 +3342,13 @@ declare namespace pxt.usb {
3334
3342
  protocolCode?: number;
3335
3343
  serialNumber?: string;
3336
3344
  }
3345
+ interface USBDeviceRequestOptions {
3346
+ filters: USBDeviceFilter[];
3347
+ exclusionFilters?: USBDeviceFilter[] | undefined;
3348
+ }
3349
+ interface USBConnectionEvent extends Event {
3350
+ device: USBDevice;
3351
+ }
3337
3352
  let filters: USBDeviceFilter[];
3338
3353
  function setFilters(f: USBDeviceFilter[]): void;
3339
3354
  type USBEndpointType = "bulk" | "interrupt" | "isochronous";