sdk: listener management

This commit is contained in:
Koushik Dutta
2021-12-05 00:48:24 -08:00
parent 64f44ab061
commit c018c5c19b
3 changed files with 19 additions and 2 deletions

4
sdk/index.d.ts vendored
View File

@@ -1,6 +1,6 @@
export * from './types';
import { DeviceBase } from './types';
import type { ScryptedNativeId } from './types';
import type { ScryptedNativeId, EventListenerRegister } from './types';
import type { ScryptedInterface, ScryptedStatic, Logger, DeviceState } from './types';
export declare class ScryptedDeviceBase extends DeviceBase {
nativeId?: string;
@@ -26,6 +26,7 @@ export declare class MixinDeviceBase<T> extends DeviceBase implements DeviceStat
private _log;
private _console;
private _deviceState;
private _listeners;
constructor(mixinDevice: T, mixinDeviceInterfaces: ScryptedInterface[], mixinDeviceState: DeviceState, mixinProviderNativeId: ScryptedNativeId);
get storage(): Storage;
get console(): Console;
@@ -34,6 +35,7 @@ export declare class MixinDeviceBase<T> extends DeviceBase implements DeviceStat
*/
onDeviceEvent(eventInterface: string, eventData: any): Promise<void>;
_lazyLoadDeviceState(): void;
manageListener(listener: EventListenerRegister): void;
release(): void;
}
declare let sdk: ScryptedStatic;

View File

@@ -60,6 +60,7 @@ class MixinDeviceBase extends types_1.DeviceBase {
this.mixinDevice = mixinDevice;
this.mixinDeviceInterfaces = mixinDeviceInterfaces;
this.mixinProviderNativeId = mixinProviderNativeId;
this._listeners = new Set();
this._deviceState = mixinDeviceState;
}
get storage() {
@@ -85,7 +86,13 @@ class MixinDeviceBase extends types_1.DeviceBase {
}
_lazyLoadDeviceState() {
}
manageListener(listener) {
this._listeners.add(listener);
}
release() {
for (const l of this._listeners) {
l.removeListener();
}
}
}
exports.MixinDeviceBase = MixinDeviceBase;

View File

@@ -1,6 +1,6 @@
export * from './types'
import { ScryptedInterfaceProperty, DeviceBase } from './types';
import type { ScryptedNativeId, DeviceManager, SystemManager, MediaManager, EndpointManager } from './types';
import type { ScryptedNativeId, DeviceManager, SystemManager, MediaManager, EndpointManager, EventListenerRegister } from './types';
import type { ScryptedInterface, ScryptedStatic, Logger, DeviceState } from './types';
export class ScryptedDeviceBase extends DeviceBase {
@@ -60,6 +60,7 @@ export class MixinDeviceBase<T> extends DeviceBase implements DeviceState {
private _log: Logger;
private _console: Console;
private _deviceState: DeviceState;
private _listeners = new Set<EventListenerRegister>();
constructor(public mixinDevice: T, public mixinDeviceInterfaces: ScryptedInterface[], mixinDeviceState: DeviceState, public mixinProviderNativeId: ScryptedNativeId) {
super();
@@ -95,7 +96,14 @@ export class MixinDeviceBase<T> extends DeviceBase implements DeviceState {
_lazyLoadDeviceState() {
}
manageListener(listener: EventListenerRegister) {
this._listeners.add(listener);
}
release() {
for (const l of this._listeners) {
l.removeListener();
}
}
}