jacdac-ts
Advanced tools
Comparing version 1.0.31 to 1.0.32
@@ -1,2 +0,2 @@ | ||
export declare const enum NumberFormat { | ||
export declare enum NumberFormat { | ||
Int8LE = 1, | ||
@@ -3,0 +3,0 @@ UInt8LE = 2, |
@@ -6,7 +6,24 @@ import { Packet } from "./packet"; | ||
export interface BusOptions { | ||
sendPacketAsync: (p: Packet) => Promise<void>; | ||
sendPacketAsync?: (p: Packet) => Promise<void>; | ||
connectAsync?: () => Promise<void>; | ||
disconnectAsync?: () => Promise<void>; | ||
} | ||
export interface Error { | ||
context: string; | ||
exception: any; | ||
} | ||
export interface PacketEventEmitter { | ||
/** | ||
* Event emitted when the bus is connected | ||
* @param event | ||
* @param listener | ||
*/ | ||
on(event: 'connect', listener: () => void): boolean; | ||
/** | ||
* Event emitted when the bus is connecting | ||
* @param event | ||
* @param listener | ||
*/ | ||
on(event: 'connecting', listener: () => void): boolean; | ||
/** | ||
* Event emitted when the bus is disconnected | ||
@@ -53,2 +70,8 @@ * @param event | ||
on(event: 'deviceannounce', listener: (device: Device) => void): boolean; | ||
/** | ||
* Event emitted when an exception occurs | ||
* @param event | ||
* @param listener | ||
*/ | ||
on(event: 'error', listener: (error: Error) => void): void; | ||
} | ||
@@ -59,3 +82,5 @@ /** | ||
export declare class Bus extends EventEmitter implements PacketEventEmitter { | ||
options: BusOptions; | ||
options?: BusOptions; | ||
private _connected; | ||
private _connectPromise; | ||
private _devices; | ||
@@ -70,3 +95,4 @@ private _deviceNames; | ||
*/ | ||
constructor(options: BusOptions); | ||
constructor(options?: BusOptions); | ||
private resetTime; | ||
get timestamp(): number; | ||
@@ -77,2 +103,6 @@ get minConsolePriority(): ConsolePriority; | ||
sendPacketAsync(p: Packet): Promise<void>; | ||
get connecting(): boolean; | ||
get connected(): boolean; | ||
errorHandler(context: string, exception: any): void; | ||
connectAsync(): Promise<void>; | ||
disconnectAsync(): Promise<void>; | ||
@@ -79,0 +109,0 @@ /** |
@@ -12,3 +12,4 @@ import { SMap } from "./utils"; | ||
on(type: string, handler: EventHandler): boolean; | ||
off(type: string, handler: EventHandler): boolean; | ||
emit(type: string, evt?: any): void; | ||
} |
/// <reference types="w3c-web-usb" /> | ||
import * as U from "./utils"; | ||
import { Bus } from "./bus"; | ||
export declare const HF2_DEVICE_MAJOR = 42; | ||
@@ -70,2 +69,1 @@ export declare const HF2_CMD_BININFO = 1; | ||
} | ||
export declare function requestUSBBus(requestDevice?: (options: USBDeviceRequestOptions) => Promise<USBDevice>): Promise<Bus>; |
@@ -11,4 +11,5 @@ export * from './constants'; | ||
export * from './hf2'; | ||
export * from './webusb'; | ||
export * from './sensor'; | ||
export * from './logparser'; | ||
export * from './pretty'; |
{ | ||
"name": "jacdac-ts", | ||
"version": "1.0.31", | ||
"version": "1.0.32", | ||
"description": "", | ||
@@ -28,3 +28,5 @@ "keywords": [], | ||
"predist": "rm -rf dist", | ||
"dist": "node node_modules/rollup/bin/rollup -c rollup.config.ts && cd docs && node node_modules/gatsby/cli.js build --prefix-paths", | ||
"dist": "node node_modules/rollup/bin/rollup -c rollup.config.ts", | ||
"predistdocs": "cd docs && node node_modules/gatsby/cli.js clean", | ||
"distdocs": "cd docs && node node_modules/gatsby/cli.js build --prefix-paths", | ||
"watch": "node node_modules/rollup/bin/rollup -c rollup.config.ts -w", | ||
@@ -88,2 +90,8 @@ "test": "node node_modules/jest/bin/jest.js", | ||
[ | ||
"@qiwi/semantic-release-gh-pages-plugin", | ||
{ | ||
"src": "docs/public" | ||
} | ||
], | ||
[ | ||
"@semantic-release/git", | ||
@@ -112,2 +120,3 @@ { | ||
"devDependencies": { | ||
"@qiwi/semantic-release-gh-pages-plugin": "^3.0.0", | ||
"@semantic-release/exec": "^5.0.0", | ||
@@ -114,0 +123,0 @@ "@semantic-release/git": "^9.0.0", |
import { read16, read32 } from "./utils"; | ||
export const enum NumberFormat { | ||
export enum NumberFormat { | ||
Int8LE = 1, | ||
@@ -5,0 +5,0 @@ UInt8LE = 2, |
102
src/bus.ts
@@ -8,8 +8,28 @@ import { Packet } from "./packet"; | ||
export interface BusOptions { | ||
sendPacketAsync: (p: Packet) => Promise<void>; | ||
sendPacketAsync?: (p: Packet) => Promise<void>; | ||
connectAsync?: () => Promise<void>; | ||
disconnectAsync?: () => Promise<void>; | ||
} | ||
export interface Error { | ||
context: string; | ||
exception: any; | ||
} | ||
export interface PacketEventEmitter { | ||
/** | ||
* Event emitted when the bus is connected | ||
* @param event | ||
* @param listener | ||
*/ | ||
on(event: 'connect', listener: () => void): boolean; | ||
/** | ||
* Event emitted when the bus is connecting | ||
* @param event | ||
* @param listener | ||
*/ | ||
on(event: 'connecting', listener: () => void): boolean; | ||
/** | ||
* Event emitted when the bus is disconnected | ||
@@ -62,2 +82,9 @@ * @param event | ||
on(event: 'deviceannounce', listener: (device: Device) => void): boolean; | ||
/** | ||
* Event emitted when an exception occurs | ||
* @param event | ||
* @param listener | ||
*/ | ||
on(event: 'error', listener: (error: Error) => void): void; | ||
} | ||
@@ -69,2 +96,5 @@ | ||
export class Bus extends EventEmitter implements PacketEventEmitter { | ||
private _connected = false; | ||
private _connectPromise: Promise<void>; | ||
private _devices: Device[] = []; | ||
@@ -80,9 +110,13 @@ private _deviceNames: SMap<string> = {}; | ||
*/ | ||
constructor(public options: BusOptions) { | ||
constructor(public options?: BusOptions) { | ||
super(); | ||
this._startTime = Date.now(); | ||
this.options = this.options || {}; | ||
this.resetTime(); | ||
this.on('deviceannounce', () => this.pingLoggers()); | ||
} | ||
private resetTime() { | ||
this._startTime = Date.now(); | ||
} | ||
get timestamp() { | ||
@@ -111,6 +145,49 @@ return Date.now() - this._startTime; | ||
this.emit('packetsend', p); | ||
return this.options.sendPacketAsync(p) | ||
return this.options?.sendPacketAsync(p) || Promise.resolve(); | ||
} | ||
disconnectAsync(): Promise<void> { | ||
get connecting() { | ||
return !!this._connectPromise; | ||
} | ||
get connected() { | ||
return this._connected; | ||
} | ||
errorHandler(context: string, exception: any) { | ||
this.emit("error", { context, exception }) | ||
} | ||
connectAsync(): Promise<void> { | ||
// already connected | ||
if (this._connected) | ||
return Promise.resolve(); | ||
// connecting | ||
if (!this._connectPromise) { | ||
this._connected = false; | ||
this._connectPromise = Promise.resolve(); | ||
this.emit("connecting"); | ||
const connectAsyncPromise = this.options?.connectAsync() || Promise.resolve(); | ||
this._connectPromise = connectAsyncPromise | ||
.then(() => { | ||
this._connectPromise = undefined; | ||
this._connected = true; | ||
this.emit("connect"); | ||
}) | ||
.catch(e => { | ||
this.errorHandler("connect", e); | ||
this._connected = false; | ||
this._connectPromise = undefined; | ||
this.emit("disconnect"); | ||
}) | ||
} | ||
return this._connectPromise; | ||
} | ||
async disconnectAsync(): Promise<void> { | ||
if (!this._connected) return Promise.resolve(); | ||
if (this._connectPromise) | ||
throw new Error("trying to disconnect while connecting"); | ||
this._connected = false; | ||
if (this._gcInterval) { | ||
@@ -120,5 +197,10 @@ clearInterval(this._gcInterval); | ||
} | ||
return (this.options?.disconnectAsync() || Promise.resolve()) | ||
.then(() => { this.emit("disconnect") }) | ||
try { | ||
this.options?.disconnectAsync(); | ||
} catch (e) { | ||
this.errorHandler("disconnect", e) | ||
} | ||
finally { | ||
this.emit("disconnet"); | ||
} | ||
} | ||
@@ -142,3 +224,3 @@ | ||
if (!this._gcInterval) | ||
if (!this._gcInterval && this.connected) | ||
this._gcInterval = setInterval(() => this.gcDevices(), 2000); | ||
@@ -145,0 +227,0 @@ } |
@@ -25,2 +25,13 @@ import { SMap } from "./utils"; | ||
off(type: string, handler: EventHandler) { | ||
if (!type || !handler) return false; | ||
const hs = this.handlers[type]; | ||
const index = hs.indexOf(handler); | ||
if (index > -1) return false; | ||
hs.splice(index, -1); | ||
return true; | ||
} | ||
emit(type: string, evt?: any) { | ||
@@ -27,0 +38,0 @@ const hs = this.handlers[type]; |
@@ -427,25 +427,1 @@ import * as U from "./utils" | ||
} | ||
export async function requestUSBBus(requestDevice?: (options: USBDeviceRequestOptions) => Promise<USBDevice>): Promise<Bus> { | ||
if (!requestDevice && typeof navigator !== "undefined" && navigator.usb && navigator.usb.requestDevice) { | ||
requestDevice = options => navigator.usb.requestDevice(options); | ||
} | ||
const transport = new Transport(requestDevice); | ||
const hf2 = new Proto(transport); | ||
await hf2.init() | ||
const bus = new Bus({ | ||
sendPacketAsync: p => { | ||
const buf = p.toBuffer(); | ||
return hf2.sendJDMessageAsync(buf) | ||
.then(() => { }, err => console.log(err)); | ||
}, | ||
disconnectAsync: () => hf2.disconnectAsync() | ||
}); | ||
hf2.onJDMessage(buf => { | ||
const pkts = Packet.fromFrame(buf, bus.timestamp) | ||
for (const pkt of pkts) | ||
bus.processPacket(pkt); | ||
}); | ||
return bus; | ||
} |
@@ -11,4 +11,5 @@ export * from './constants' | ||
export * from './hf2' | ||
export * from './webusb' | ||
export * from './sensor' | ||
export * from './logparser' | ||
export * from './pretty' |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
532180
39
7094
30