-
-
Notifications
You must be signed in to change notification settings - Fork 197
feat(preview): add public API for deviceFound and deviceLost for preview devices #4083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5ce6012
feat(preview): add api for deviceFound and deviceLost for preview dev…
Fatme 569b498
test(preview): add unit tests for previewDevicesService
Fatme aeec0ac
fix(preview-unit-tests): inject previewDevicesService in order to fix…
Fatme 8a5f64f
chore(preview): handle PR comments
Fatme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -12,7 +12,6 @@ declare global { | |
|
||
interface IPreviewSdkService extends EventEmitter { | ||
getQrCodeUrl(options: IHasUseHotModuleReloadOption): string; | ||
connectedDevices: Device[]; | ||
initialize(getInitialFiles: (device: Device) => Promise<FilesPayload>): void; | ||
applyChanges(filesPayload: FilesPayload): Promise<void>; | ||
stop(): void; | ||
|
@@ -34,4 +33,11 @@ declare global { | |
*/ | ||
link: boolean; | ||
} | ||
|
||
interface IPreviewDevicesService extends EventEmitter { | ||
connectedDevices: Device[]; | ||
onDevicesPresence(devices: Device[]): void; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the context of this service, this should be something like set/update ConnectedDevices. |
||
getDeviceById(id: string): Device; | ||
getDevicesForPlatform(platform: string): Device[]; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
lib/services/livesync/playground/devices/preview-devices-service.ts
This file contains hidden or 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,36 @@ | ||
import { Device } from "nativescript-preview-sdk"; | ||
import { EventEmitter } from "events"; | ||
import { DeviceDiscoveryEventNames } from "../../../../common/constants"; | ||
|
||
export class PreviewDevicesService extends EventEmitter implements IPreviewDevicesService { | ||
public connectedDevices: Device[] = []; | ||
|
||
public onDevicesPresence(devices: Device[]): void { | ||
_(devices) | ||
.reject(d => _.find(this.connectedDevices, device => d.id === device.id)) | ||
.each(device => this.raiseDeviceFound(device)); | ||
|
||
_(this.connectedDevices) | ||
.reject(d => _.find(devices, device => d.id === device.id)) | ||
.each(device => this.raiseDeviceLost(device)); | ||
} | ||
|
||
public getDeviceById(id: string): Device { | ||
return _.find(this.connectedDevices, { id }); | ||
} | ||
|
||
public getDevicesForPlatform(platform: string): Device[] { | ||
return _.filter(this.connectedDevices, { platform: platform.toLowerCase() }); | ||
} | ||
|
||
private raiseDeviceFound(device: Device) { | ||
this.emit(DeviceDiscoveryEventNames.DEVICE_FOUND, device); | ||
this.connectedDevices.push(device); | ||
} | ||
|
||
private raiseDeviceLost(device: Device) { | ||
this.emit(DeviceDiscoveryEventNames.DEVICE_LOST, device); | ||
_.remove(this.connectedDevices, d => d.id === device.id); | ||
} | ||
} | ||
$injector.register("previewDevicesService", PreviewDevicesService); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
import { Yok } from "../../lib/common/yok"; | ||
import { PreviewDevicesService } from "../../lib/services/livesync/playground/devices/preview-devices-service"; | ||
import { Device } from "nativescript-preview-sdk"; | ||
import { assert } from "chai"; | ||
import { DeviceDiscoveryEventNames } from "../../lib/common/constants"; | ||
import { LoggerStub } from "../stubs"; | ||
|
||
let foundDevices: Device[] = []; | ||
let lostDevices: Device[] = []; | ||
|
||
function createTestInjector(): IInjector { | ||
const injector = new Yok(); | ||
injector.register("previewDevicesService", PreviewDevicesService); | ||
injector.register("logger", LoggerStub); | ||
return injector; | ||
} | ||
|
||
function createDevice(id: string): Device { | ||
return { | ||
id, | ||
platform: "ios", | ||
model: "my test model", | ||
name: "my test name", | ||
osVersion: "10.0.0", | ||
previewAppVersion: "19.0.0", | ||
runtimeVersion: "5.0.0" | ||
}; | ||
} | ||
|
||
describe("PreviewDevicesService", () => { | ||
describe("onDevicesPresence", () => { | ||
let previewDevicesService: IPreviewDevicesService = null; | ||
beforeEach(() => { | ||
const injector = createTestInjector(); | ||
previewDevicesService = injector.resolve("previewDevicesService"); | ||
previewDevicesService.on(DeviceDiscoveryEventNames.DEVICE_FOUND, device => { | ||
foundDevices.push(device); | ||
}); | ||
previewDevicesService.on(DeviceDiscoveryEventNames.DEVICE_LOST, device => { | ||
lostDevices.push(device); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
previewDevicesService.removeAllListeners(); | ||
foundDevices = []; | ||
lostDevices = []; | ||
}); | ||
|
||
it("should add new device", () => { | ||
const device = createDevice("device1"); | ||
|
||
previewDevicesService.onDevicesPresence([device]); | ||
|
||
assert.deepEqual(previewDevicesService.connectedDevices, [device]); | ||
assert.deepEqual(foundDevices, [device]); | ||
assert.deepEqual(lostDevices, []); | ||
}); | ||
it("should add new device when there are already connected devices", () => { | ||
const device1 = createDevice("device1"); | ||
const device2 = createDevice("device2"); | ||
previewDevicesService.connectedDevices = [device1]; | ||
|
||
previewDevicesService.onDevicesPresence([device1, device2]); | ||
|
||
assert.deepEqual(previewDevicesService.connectedDevices, [device1, device2]); | ||
assert.deepEqual(foundDevices, [device2]); | ||
assert.deepEqual(lostDevices, []); | ||
}); | ||
it("should add more than one new device", () => { | ||
const device1 = createDevice("device1"); | ||
const device2 = createDevice("device2"); | ||
const device3 = createDevice("device3"); | ||
|
||
previewDevicesService.onDevicesPresence([device1, device2, device3]); | ||
|
||
assert.deepEqual(previewDevicesService.connectedDevices, [device1, device2, device3]); | ||
assert.deepEqual(foundDevices, [device1, device2, device3]); | ||
assert.deepEqual(lostDevices, []); | ||
}); | ||
it("should remove device", () => { | ||
const device1 = createDevice("device1"); | ||
previewDevicesService.connectedDevices = [device1]; | ||
|
||
previewDevicesService.onDevicesPresence([]); | ||
|
||
assert.deepEqual(foundDevices, []); | ||
assert.deepEqual(lostDevices, [device1]); | ||
}); | ||
it("should add and remove devices in the same time", () => { | ||
const device1 = createDevice("device1"); | ||
const device2 = createDevice("device2"); | ||
previewDevicesService.connectedDevices = [device1]; | ||
|
||
previewDevicesService.onDevicesPresence([device2]); | ||
|
||
assert.deepEqual(previewDevicesService.connectedDevices, [device2]); | ||
assert.deepEqual(foundDevices, [device2]); | ||
assert.deepEqual(lostDevices, [device1]); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to expose a public setter? We could expose just a getConnectedDevices method.