Skip to content

Commit

Permalink
fixed circular and null reference
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Nov 4, 2017
1 parent def1fbc commit 7e5d1e4
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 32 deletions.
4 changes: 2 additions & 2 deletions build/lib/accessory.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TradfriClient } from "../tradfri-client";
import { DeviceInfo } from "./deviceInfo";
import { IPSODevice } from "./ipsoDevice";
import { Light } from "./light";
import { OperationProvider } from "./operation-provider";
import { Plug } from "./plug";
import { Sensor } from "./sensor";
export declare enum AccessoryTypes {
Expand All @@ -24,5 +24,5 @@ export declare class Accessory extends IPSODevice {
* INTERNAL USE ONLY!
* @param client The client instance to link this object to
*/
link(client: TradfriClient): this;
link(client: OperationProvider): this;
}
24 changes: 16 additions & 8 deletions build/lib/accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,25 @@ class Accessory extends ipsoDevice_1.IPSODevice {
*/
link(client) {
super.link(client);
for (const light of this.lightList) {
light.link(client);
if (this.lightList != null) {
for (const light of this.lightList) {
light.link(client);
}
}
for (const plug of this.plugList) {
plug.link(client);
if (this.plugList != null) {
for (const plug of this.plugList) {
plug.link(client);
}
}
for (const sensor of this.sensorList) {
sensor.link(client);
if (this.sensorList != null) {
for (const sensor of this.sensorList) {
sensor.link(client);
}
}
for (const swtch of this.switchList) {
swtch.link(client);
if (this.switchList != null) {
for (const swtch of this.switchList) {
swtch.link(client);
}
}
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions build/lib/ipsoObject.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TradfriClient } from "../tradfri-client";
import { DictionaryLike } from "./object-polyfill";
import { OperationProvider } from "./operation-provider";
export declare type PropertyTransform = (value: any, parent?: IPSOObject) => any;
export declare type RequiredPredicate = (me: IPSOObject, reference: IPSOObject) => boolean;
/**
Expand Down Expand Up @@ -55,11 +55,11 @@ export declare class IPSOObject {
* @param set Custom setter trap (optional). This is called after mandatory traps are in place and before default behavior
*/
createProxy(get?: (me: this, key: PropertyKey) => any, set?: (me: this, key: PropertyKey, value, receiver) => boolean): this;
protected client: TradfriClient;
protected client: OperationProvider;
/**
* Link this object to a TradfriClient for a simplified API.
* INTERNAL USE ONLY!
* @param client The client instance to link this object to
*/
link(client: TradfriClient): this;
link(client: OperationProvider): this;
}
3 changes: 1 addition & 2 deletions build/lib/ipsoObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const tradfri_client_1 = require("../tradfri-client");
const logger_1 = require("./logger");
const object_polyfill_1 = require("./object-polyfill");
// ===========================================================
Expand Down Expand Up @@ -486,6 +485,6 @@ __decorate([
], IPSOObject.prototype, "isProxy", void 0);
__decorate([
exports.doNotSerialize,
__metadata("design:type", tradfri_client_1.TradfriClient)
__metadata("design:type", Object)
], IPSOObject.prototype, "client", void 0);
exports.IPSOObject = IPSOObject;
3 changes: 1 addition & 2 deletions build/lib/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const tradfri_client_1 = require("../tradfri-client");
const accessory_1 = require("./accessory");
const conversions_1 = require("./conversions");
const ipsoDevice_1 = require("./ipsoDevice");
Expand Down Expand Up @@ -100,7 +99,7 @@ class Light extends ipsoDevice_1.IPSODevice {
* @throws Throws an error if it isn't
*/
ensureLink() {
if (!(this.client instanceof tradfri_client_1.TradfriClient)) {
if (this.client == null) {
throw new Error("Cannot use the simplified API on devices which aren't linked to a client instance.");
}
if (!(this._accessory instanceof accessory_1.Accessory)) {
Expand Down
7 changes: 7 additions & 0 deletions build/lib/operation-provider.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Accessory } from "./accessory";
import { Group, GroupOperation } from "./group";
import { LightOperation } from "./light";
export interface OperationProvider {
operateGroup(group: Group, operation: GroupOperation): Promise<boolean>;
operateLight(accessory: Accessory, operation: LightOperation): Promise<boolean>;
}
2 changes: 2 additions & 0 deletions build/lib/operation-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Empty file.
Empty file added build/lib/operationProvider.js
Empty file.
3 changes: 2 additions & 1 deletion build/tradfri-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Group, GroupInfo, GroupOperation } from "./lib/group";
import { LightOperation } from "./lib/light";
import { LoggerFunction } from "./lib/logger";
import { DictionaryLike } from "./lib/object-polyfill";
import { OperationProvider } from "./lib/operation-provider";
import { Scene } from "./lib/scene";
export declare type ObserveResourceCallback = (resp: CoapResponse) => void;
export declare type ObserveDevicesCallback = (addedDevices: Accessory[], removedDevices: Accessory[]) => void;
Expand Down Expand Up @@ -34,7 +35,7 @@ export interface TradfriClient {
removeListener(event: "error", callback: ErrorCallback): this;
removeAllListeners(event?: ObservableEvents): this;
}
export declare class TradfriClient extends EventEmitter {
export declare class TradfriClient extends EventEmitter implements OperationProvider {
readonly hostname: string;
/** dictionary of CoAP observers */
observedPaths: string[];
Expand Down
27 changes: 18 additions & 9 deletions src/lib/accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DeviceInfo } from "./deviceInfo";
import { IPSODevice } from "./ipsoDevice";
import { deserializeWith, ipsoKey, IPSOObject, PropertyTransform, required, serializeWith } from "./ipsoObject";
import { Light } from "./light";
import { OperationProvider } from "./operation-provider";
import { Plug } from "./plug";
import { Sensor } from "./sensor";

Expand Down Expand Up @@ -53,19 +54,27 @@ export class Accessory extends IPSODevice {
* INTERNAL USE ONLY!
* @param client The client instance to link this object to
*/
public link(client: TradfriClient): this {
public link(client: OperationProvider): this {
super.link(client);
for (const light of this.lightList) {
light.link(client);
if (this.lightList != null) {
for (const light of this.lightList) {
light.link(client);
}
}
for (const plug of this.plugList) {
plug.link(client);
if (this.plugList != null) {
for (const plug of this.plugList) {
plug.link(client);
}
}
for (const sensor of this.sensorList) {
sensor.link(client);
if (this.sensorList != null) {
for (const sensor of this.sensorList) {
sensor.link(client);
}
}
for (const swtch of this.switchList) {
swtch.link(client);
if (this.switchList != null) {
for (const swtch of this.switchList) {
swtch.link(client);
}
}
return this;
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/ipsoObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TradfriClient } from "../tradfri-client";
import { log } from "./logger";
import { DictionaryLike, entries } from "./object-polyfill";
import { OperationProvider } from "./operation-provider";

// ===========================================================
// define decorators so we can define all properties type-safe
Expand Down Expand Up @@ -499,13 +500,13 @@ export class IPSOObject {
});
}

@doNotSerialize protected client: TradfriClient;
@doNotSerialize protected client: OperationProvider;
/**
* Link this object to a TradfriClient for a simplified API.
* INTERNAL USE ONLY!
* @param client The client instance to link this object to
*/
public link(client: TradfriClient): this {
public link(client: OperationProvider): this {
this.client = client;
return this;
}
Expand Down
3 changes: 1 addition & 2 deletions src/lib/light.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TradfriClient } from "../tradfri-client";
import { Accessory } from "./accessory";
import { conversions, deserializers, serializers } from "./conversions";
import { IPSODevice } from "./ipsoDevice";
Expand Down Expand Up @@ -152,7 +151,7 @@ export class Light extends IPSODevice {
* @throws Throws an error if it isn't
*/
private ensureLink() {
if (!(this.client instanceof TradfriClient)) {
if (this.client == null) {
throw new Error("Cannot use the simplified API on devices which aren't linked to a client instance.");
}
if (!(this._accessory instanceof Accessory)) {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/operation-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Accessory } from "./accessory";
import { Group, GroupOperation } from "./group";
import { LightOperation } from "./light";

export interface OperationProvider {
operateGroup(group: Group, operation: GroupOperation): Promise<boolean>;
operateLight(accessory: Accessory, operation: LightOperation): Promise<boolean>;
}
3 changes: 2 additions & 1 deletion src/tradfri-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IPSOObject } from "./lib/ipsoObject";
import { Light, LightOperation, Spectrum } from "./lib/light";
import { log, LoggerFunction, setCustomLogger } from "./lib/logger";
import { DictionaryLike } from "./lib/object-polyfill";
import { OperationProvider } from "./lib/operation-provider";
import { wait } from "./lib/promises";
import { Scene } from "./lib/scene";
import { TradfriError, TradfriErrorCodes } from "./lib/tradfri-error";
Expand Down Expand Up @@ -56,7 +57,7 @@ export declare interface TradfriClient {
removeAllListeners(event?: ObservableEvents): this;
}

export class TradfriClient extends EventEmitter {
export class TradfriClient extends EventEmitter implements OperationProvider {

/** dictionary of CoAP observers */
public observedPaths: string[] = [];
Expand Down

0 comments on commit 7e5d1e4

Please sign in to comment.