Skip to content

Commit

Permalink
Fix API dependency injection, RX handling, fs.list()
Browse files Browse the repository at this point in the history
  • Loading branch information
Pwuts committed Mar 30, 2023
1 parent fd4fd4b commit b4597b0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/api/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ export class BadgeFileSystemApi {
textEncoder = new TextEncoder();
textDecoder = new TextDecoder();

/** Lists entries in the folder given by `path` */
async list(path: string): Promise<FileListing[]> {
/**
* Lists entries in the folder given by `path`
* @param path default: `/internal`
*/
async list(path: string = '/internal'): Promise<FileListing[]> {
if (path == '') {
throw Error('Path must not be empty');
}
let pathEncoded = this.textEncoder.encode(path);
let data: ArrayBuffer = await this.transaction(BadgeUSB.PROTOCOL_COMMAND_FILESYSTEM_LIST, pathEncoded, 4000);

Expand Down
12 changes: 8 additions & 4 deletions src/badge-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* @author Reinier van der Leer
*/

import { BadgeUSB, TransactionArgs } from "./badge-usb";
import { BadgeFileSystemApi } from "./api/filesystem";
import { BadgeAppFSApi } from "./api/appfs";
import { BadgeNVSApi } from "./api/nvs";
import { BadgeUSB } from "./badge-usb";

export type ProgressCallback = (status: string, progressPercent: number) => void;

Expand Down Expand Up @@ -54,15 +54,19 @@ export class BadgeAPI {


/*** Filesystem API ***/
public fileSystem = new BadgeFileSystemApi(this.transaction);
public fileSystem = new BadgeFileSystemApi(
(...args: TransactionArgs) => this.transaction(...args),
);

/*** AppFS API ***/
public appFS = new BadgeAppFSApi(
this.fileSystem,
this.disconnect,
this.transaction,
(...args: TransactionArgs) => this.transaction(...args),
);

/*** NVS API */
public nvs = new BadgeNVSApi(this.transaction);
public nvs = new BadgeNVSApi(
(...args: TransactionArgs) => this.transaction(...args),
);
}
4 changes: 3 additions & 1 deletion src/badge-usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export class BadgeUSB {
dataView, identifier, magic,
type: responseType,
payload: {
buffer,
buffer: payload,
crc: payloadCRC,
declaredLength: payloadLength,
},
Expand All @@ -485,6 +485,8 @@ export class BadgeUSB {
}
}

export type TransactionArgs = Parameters<BadgeUSB['transaction']>;

class TransactionPromise extends Promise<TransactionResponse> {
resolve: (value: TransactionResponse | PromiseLike<TransactionResponse>) => void;
reject: (reason: TransactionResponse | Error) => void;
Expand Down

0 comments on commit b4597b0

Please sign in to comment.