Skip to content

Instantly share code, notes, and snippets.

@zapkub
Created September 14, 2021 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zapkub/30def5f2a6eff81d3ba70f2fd5dac525 to your computer and use it in GitHub Desktop.
Save zapkub/30def5f2a6eff81d3ba70f2fd5dac525 to your computer and use it in GitHub Desktop.
decoupling everlong things
type Message = Uint8Array;
class BluetoothControl {
collectScanResult() {
}
private _onDiscoverCallback: (device: BLEDevice) => void
onDiscoverDevice(callback: (device: BLEDevice) => void) {
this._onDiscoverCallback = callback;
}
}
class BLEDevice {
constructor() {}
private _onMessage: (data: Message) => void
public setOnMessage(callback: (data: Message) => void) {
this._onMessage = callback;
}
private _listenTo() {
// listen to bluetooth
this._onMessage?.(new Uint8Array());
}
public toogleHost() {}
}
class Classroom {
devices: BLEDevice[]
constructor() { }
addDevice(device: BLEDevice) {
device.setOnMessage(this.sendLocal);
this.devices.push(device);
}
sendLocal(uint8: Uint8Array) {
// forward
}
}
class Online {
devices: BLEDevice[]
addDevice(device: BLEDevice) {
device.setOnMessage(this.sendLocal);
this.devices.push(device);
}
sendLocal(uint8: Uint8Array) {
// forward
}
}
class ModeController {
constructor(
private classroom: Classroom,
private online: Online,
private bluetoothControl: BluetoothControl,
) {
}
onModeChange(mode: 'Online' | 'Classroom') {
if (mode === 'Online') {
this.bluetoothControl.onDiscoverDevice(this.online.addDevice);
} else {
this.bluetoothControl.onDiscoverDevice(this.classroom.addDevice);
}
}
}
function init() {
const bluetoothControl = new BluetoothControl();
const classroom = new Classroom();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment