Skip to content

Commit

Permalink
Remove last traces of custom EventEmitter type
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Oct 5, 2020
1 parent f908a9e commit 89ad4ac
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 141 deletions.
7 changes: 5 additions & 2 deletions src/lib/Accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ import {
SerializedService,
Service,
ServiceCharacteristicChange,
ServiceConfigurationChange,
ServiceEventTypes,
ServiceId
} from './Service';
Expand Down Expand Up @@ -244,6 +243,10 @@ export type AccessoryCharacteristicChange = ServiceCharacteristicChange & {
service: Service;
};

export interface ServiceConfigurationChange {
service: Service;
}

const enum WriteRequestState {
REGULAR_REQUEST,
TIMED_WRITE_AUTHENTICATED,
Expand Down Expand Up @@ -419,7 +422,7 @@ export class Accessory extends EventEmitter {
this.emit(AccessoryEventTypes.SERVICE_CONFIGURATION_CHANGE, { service: service });
}

service.on(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE, (change: ServiceConfigurationChange) => {
service.on(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE, () => {
if (!service.isPrimaryService && service === this.primaryService) {
// service changed form primary to non primary service
this.primaryService = undefined;
Expand Down
48 changes: 0 additions & 48 deletions src/lib/EventEmitter.ts

This file was deleted.

65 changes: 29 additions & 36 deletions src/lib/Service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { EventEmitter } from "events";
import { CharacteristicValue, HapService, Nullable, ToHAPOptions, WithUUID, } from '../types';
import {
Characteristic,
CharacteristicChange,
CharacteristicEventTypes,
SerializedCharacteristic
} from './Characteristic';
import { EventEmitter } from './EventEmitter';
import * as HomeKitTypes from './gen';
import { IdentifierCache } from './model/IdentifierCache';
import { clone } from './util/clone';
import { toShortForm } from './util/uuid';

/**
* HAP spec allows a maximum of 100 characteristics per service!
*/
const MAX_CHARACTERISTICS = 100;

export interface SerializedService {
Expand All @@ -27,28 +29,27 @@ export interface SerializedService {

export type ServiceId = string; // string with the format: UUID + (subtype | "")

export const enum ServiceEventTypes {
CHARACTERISTIC_CHANGE = "characteristic-change",
SERVICE_CONFIGURATION_CHANGE = "service-configurationChange",
}

export type ServiceConfigurationChange = {
service: Service;
};

export type ServiceCharacteristicChange = CharacteristicChange & { characteristic: Characteristic };

type Events = {
[ServiceEventTypes.CHARACTERISTIC_CHANGE]: (change: ServiceCharacteristicChange) => void;
[ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE]: (change: ServiceConfigurationChange) => void; // TODO remove service: this
}

// noinspection JSUnusedGlobalSymbols
/**
* @deprecated Use ServiceEventTypes instead
*/
export type EventService = ServiceEventTypes.CHARACTERISTIC_CHANGE | ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE;

export const enum ServiceEventTypes {
CHARACTERISTIC_CHANGE = "characteristic-change",
SERVICE_CONFIGURATION_CHANGE = "service-configurationChange",
}

export declare interface Service {
on(event: "characteristic-change", listener: (change: ServiceCharacteristicChange) => void): this;
on(event: "service-configurationChange", listener: () => void): this;

emit(event: "characteristic-change", change: ServiceCharacteristicChange): boolean;
emit(event: "service-configurationChange"): boolean;
}

/**
* Service represents a set of grouped values necessary to provide a logical function. For instance, a
* "Door Lock Mechanism" service might contain two values, one for the "desired lock state" and one for the
Expand All @@ -68,11 +69,8 @@ export type EventService = ServiceEventTypes.CHARACTERISTIC_CHANGE | ServiceEven
* You can also define custom Services by providing your own UUID for the type that you generate yourself.
* Custom Services can contain an arbitrary set of Characteristics, but Siri will likely not be able to
* work with these.
*
* @event 'characteristic-change' => function({characteristic, oldValue, newValue, context}) { }
* Emitted after a change in the value of one of our Characteristics has occurred.
*/
export class Service extends EventEmitter<Events> {
export class Service extends EventEmitter {

static AccessControl: typeof HomeKitTypes.Generated.AccessControl;
static AccessoryInformation: typeof HomeKitTypes.Generated.AccessoryInformation;
Expand Down Expand Up @@ -100,10 +98,6 @@ export class Service extends EventEmitter<Events> {
static HumiditySensor: typeof HomeKitTypes.Generated.HumiditySensor;
static InputSource: typeof HomeKitTypes.TV.InputSource;
static IrrigationSystem: typeof HomeKitTypes.Generated.IrrigationSystem;
/**
* @deprecated Removed in iOS 11. Use ServiceLabel instead.
*/
static Label: typeof HomeKitTypes.Generated.ServiceLabel;
static LeakSensor: typeof HomeKitTypes.Generated.LeakSensor;
static LightSensor: typeof HomeKitTypes.Generated.LightSensor;
static Lightbulb: typeof HomeKitTypes.Generated.Lightbulb;
Expand Down Expand Up @@ -185,12 +179,11 @@ export class Service extends EventEmitter<Events> {
return this.UUID + (this.subtype || "");
}

addCharacteristic = (characteristic: Characteristic | {new (...args: any[]): Characteristic}, ...constructorArgs: any[]) => {
// characteristic might be a constructor like `Characteristic.Brightness` instead of an instance
// of Characteristic. Coerce if necessary.
if (typeof characteristic === 'function') {
characteristic = new characteristic(...constructorArgs) as Characteristic;
}
public addCharacteristic(input: Characteristic | {new (...args: any[]): Characteristic}, ...constructorArgs: any[]): Characteristic {
// characteristic might be a constructor like `Characteristic.Brightness` instead of an instance of Characteristic. Coerce if necessary.

let characteristic = typeof input === "function"? new input(...constructorArgs): input;

// check for UUID conflict
for (let index in this.characteristics) {
const existing = this.characteristics[index];
Expand All @@ -214,7 +207,7 @@ export class Service extends EventEmitter<Events> {

this.characteristics.push(characteristic);

this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE, clone({ service: this }));
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE);

return characteristic;
}
Expand All @@ -229,7 +222,7 @@ export class Service extends EventEmitter<Events> {
*/
setPrimaryService = (isPrimary: boolean = true) => {
this.isPrimaryService = isPrimary;
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE, clone({ service: this }));
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE);
};

/**
Expand All @@ -239,23 +232,23 @@ export class Service extends EventEmitter<Events> {
*/
setHiddenService = (isHidden: boolean = true) => {
this.isHiddenService = isHidden;
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE, clone({ service: this }));
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE);
}

//Allows setting other services that link to this one.
addLinkedService = (newLinkedService: Service) => {
//TODO: Add a check if the service is on the same accessory.
if (!this.linkedServices.includes(newLinkedService))
this.linkedServices.push(newLinkedService);
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE, clone({ service: this }));
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE);
}

removeLinkedService = (oldLinkedService: Service) => {
//TODO: Add a check if the service is on the same accessory.
const index = this.linkedServices.indexOf(oldLinkedService);
if (index !== -1)
this.linkedServices.splice(index, 1);
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE, clone({ service: this }));
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE);
}

removeCharacteristic = (characteristic: Characteristic) => {
Expand All @@ -274,7 +267,7 @@ export class Service extends EventEmitter<Events> {
this.characteristics.splice(Number.parseInt(targetCharacteristicIndex), 1);
characteristic.removeAllListeners();

this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE, clone({ service: this }));
this.emit(ServiceEventTypes.SERVICE_CONFIGURATION_CHANGE);
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/lib/controller/CameraController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from 'crypto';
import createDebug from "debug";
import { EventEmitter } from "events";
import {
CameraStreamingOptions,
Characteristic,
Expand All @@ -17,7 +18,6 @@ import {
StreamingRequest
} from "../..";
import { SessionIdentifier } from "../../types";
import { EventEmitter } from "../EventEmitter";
import { Doorbell, Microphone, Speaker } from "../gen/HomeKit";
import { Controller, ControllerServiceMap, ControllerType, DefaultControllerType } from "./Controller";
import Timeout = NodeJS.Timeout;
Expand Down Expand Up @@ -90,9 +90,12 @@ export const enum CameraControllerEvents {
SPEAKER_PROPERTIES_CHANGED = "speaker-change",
}

export type CameraControllerEventMap = {
[CameraControllerEvents.MICROPHONE_PROPERTIES_CHANGED]: (muted: boolean, volume: number) => void;
[CameraControllerEvents.SPEAKER_PROPERTIES_CHANGED]: (muted: boolean, volume: number) => void;
export declare interface CameraController {
on(event: "microphone-change", listener: (muted: boolean, volume: number) => void): this;
on(event: "speaker-change", listener: (muted: boolean, volume: number) => void): this;

emit(event: "microphone-change", muted: boolean, volume: number): boolean;
emit(event: "speaker-change", muted: boolean, volume: number): boolean;
}

/**
Expand All @@ -106,7 +109,7 @@ export type CameraControllerEventMap = {
* Emitted when the mute state or the volume changed. The Apple Home App typically does not set those values
* except the mute state. When you unmute the device microphone it will reset the mute state if it was set previously.
*/
export class CameraController extends EventEmitter<CameraControllerEventMap> implements Controller<CameraControllerServiceMap> {
export class CameraController extends EventEmitter implements Controller<CameraControllerServiceMap> {

private static readonly STREAM_MANAGEMENT = "streamManagement"; // key to index all RTPStreamManagement services

Expand Down
Loading

0 comments on commit 89ad4ac

Please sign in to comment.