-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const discoveryDiTypes = { | ||
StartDiscoveryUseCase: Symbol.for("StartDiscoveryUseCase"), | ||
}; |
31 changes: 31 additions & 0 deletions
31
packages/core/src/internal/discovery/di/discoveryModule.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Container } from "inversify"; | ||
import { StartDiscoveryUseCase } from "@internal/discovery/use-case/UsbHidStartDiscoveryUseCase"; | ||
import { discoveryModuleFactory } from "./discoveryModule"; | ||
import { usbModuleFactory } from "@internal/usb/di/usbModule"; | ||
import { deviceModelModuleFactory } from "@internal/device-model/di/deviceModelModule"; | ||
import { discoveryDiTypes } from "./discoveryDiTypes"; | ||
|
||
|
||
describe("discoveryModuleFactory", () => { | ||
let container: Container; | ||
let mod: ReturnType<typeof discoveryModuleFactory>; | ||
beforeEach(() => { | ||
mod = discoveryModuleFactory(); | ||
container = new Container(); | ||
container.load( | ||
mod, | ||
// The following modules are injected into discovery module | ||
usbModuleFactory(), | ||
deviceModelModuleFactory(), | ||
); | ||
}); | ||
|
||
it("should return the device module", () => { | ||
expect(mod).toBeDefined(); | ||
}); | ||
|
||
it("should return none mocked use cases", () => { | ||
const startDiscoveryUseCase = container.get(discoveryDiTypes.StartDiscoveryUseCase); | ||
expect(startDiscoveryUseCase).toBeInstanceOf(StartDiscoveryUseCase); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/core/src/internal/discovery/di/discoveryModule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ContainerModule } from "inversify"; | ||
import { StartDiscoveryUseCase } from "@internal/discovery/use-case/UsbHidStartDiscoveryUseCase"; | ||
import { discoveryDiTypes } from "./discoveryDiTypes"; | ||
|
||
type FactoryProps = { | ||
stub: boolean; | ||
}; | ||
|
||
export const discoveryModuleFactory = ({ | ||
stub = false, | ||
}: Partial<FactoryProps> = {}) => | ||
new ContainerModule((bind, _unbind, _isBound, _rebind) => { | ||
bind(discoveryDiTypes.StartDiscoveryUseCase).to(StartDiscoveryUseCase); | ||
|
||
if (stub) { | ||
// We can rebind our interfaces to their mock implementations | ||
// rebind(...).to(....); | ||
} | ||
}); |
23 changes: 23 additions & 0 deletions
23
packages/core/src/internal/discovery/use-case/UsbHidStartDiscoveryUseCase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Observable } from "rxjs"; | ||
import { inject, injectable } from "inversify"; | ||
import { usbDiTypes } from "@internal/usb/di/usbDiTypes"; | ||
import type { UsbHidTransport } from "@internal/usb/transport/UsbHidTransport"; | ||
import { DiscoveredDevice } from "@internal/usb/model/DiscoveredDevice"; | ||
|
||
/** | ||
* Starts discovering devices connected via USB HID (and later BLE). | ||
* | ||
* For the WebHID implementation, this use-case needs to be called as a result of an user interaction (button "click" event for ex). | ||
*/ | ||
@injectable() | ||
export class StartDiscoveryUseCase { | ||
constructor( | ||
@inject(usbDiTypes.UsbHidTransport) private usbHidTransport: UsbHidTransport, | ||
// Later: @inject(usbDiTypes.BleTransport) private bleTransport: BleTransport, | ||
) { | ||
} | ||
|
||
execute(): Observable<DiscoveredDevice> { | ||
return this.usbHidTransport.startDiscovering(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters