From 852a7d37b46be75eaeece28cc91967d5f75a79f0 Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Thu, 25 May 2023 13:45:30 -0500 Subject: [PATCH 1/3] Refresh method --- packages/ring-client-api/api.ts | 40 +++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/ring-client-api/api.ts b/packages/ring-client-api/api.ts index 54e8f6e2..9d946660 100644 --- a/packages/ring-client-api/api.ts +++ b/packages/ring-client-api/api.ts @@ -47,6 +47,7 @@ export interface RingApiOptions extends SessionOptions { export class RingApi extends Subscribed { public readonly restClient public readonly onRefreshTokenUpdated + onNewNotification = new Subject<{ device:RingCamera | RingIntercom, notification:PushNotification }>() constructor(public readonly options: RingApiOptions & RefreshTokenAuth) { super() @@ -212,27 +213,41 @@ export class RingApi extends Subscribed { } } + pushReceiver : PushReceiver | undefined; + devicesById: { [id: number]: RingCamera | RingIntercom | undefined } = {}; + private async registerPushReceiver( cameras: RingCamera[], intercoms: RingIntercom[] ) { - const pushReceiver = new PushReceiver({ - logLevel: 'NONE', - senderId: '876313859327', // for Ring android app. 703521446232 for ring-site - }), - devicesById: { [id: number]: RingCamera | RingIntercom | undefined } = {}, - sendToDevice = (id: number, notification: PushNotification) => { - devicesById[id]?.processPushNotification(notification) + const sendToDevice = (id: number, notification: PushNotification) => { + const device = this.devicesById[id]; + if (device) { + this.onNewNotification.next({ device, notification }); + device.processPushNotification(notification); + } }, onPushNotificationToken = new Subject() + this.devicesById = {}; for (const camera of cameras) { - devicesById[camera.id] = camera + this.devicesById[camera.id] = camera } for (const intercom of intercoms) { - devicesById[intercom.id] = intercom + this.devicesById[intercom.id] = intercom } + if (this.pushReceiver) { + return; + } + + const pushReceiver = new PushReceiver({ + logLevel: 'NONE', + senderId: '876313859327', // for Ring android app. 703521446232 for ring-site + }); + + this.pushReceiver = pushReceiver; + pushReceiver.onCredentialsChanged( ({ newCredentials: { @@ -423,4 +438,11 @@ export class RingApi extends Subscribed { this.restClient.clearTimeouts() clearTimeouts() } + + refresh() { + this.disconnect(); + if (this.locationsPromise) { + this.locationsPromise = this.fetchAndBuildLocations(); + } + } } From d2dd913c86e423a77be26fcba466dedb240a2ea6 Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Thu, 25 May 2023 13:46:48 -0500 Subject: [PATCH 2/3] Lint fix --- packages/ring-client-api/api.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/ring-client-api/api.ts b/packages/ring-client-api/api.ts index 9d946660..b2a81a37 100644 --- a/packages/ring-client-api/api.ts +++ b/packages/ring-client-api/api.ts @@ -47,7 +47,10 @@ export interface RingApiOptions extends SessionOptions { export class RingApi extends Subscribed { public readonly restClient public readonly onRefreshTokenUpdated - onNewNotification = new Subject<{ device:RingCamera | RingIntercom, notification:PushNotification }>() + onNewNotification = new Subject<{ + device: RingCamera | RingIntercom + notification: PushNotification + }>() constructor(public readonly options: RingApiOptions & RefreshTokenAuth) { super() @@ -213,23 +216,23 @@ export class RingApi extends Subscribed { } } - pushReceiver : PushReceiver | undefined; - devicesById: { [id: number]: RingCamera | RingIntercom | undefined } = {}; + pushReceiver: PushReceiver | undefined + devicesById: { [id: number]: RingCamera | RingIntercom | undefined } = {} private async registerPushReceiver( cameras: RingCamera[], intercoms: RingIntercom[] ) { const sendToDevice = (id: number, notification: PushNotification) => { - const device = this.devicesById[id]; + const device = this.devicesById[id] if (device) { - this.onNewNotification.next({ device, notification }); - device.processPushNotification(notification); + this.onNewNotification.next({ device, notification }) + device.processPushNotification(notification) } }, onPushNotificationToken = new Subject() - this.devicesById = {}; + this.devicesById = {} for (const camera of cameras) { this.devicesById[camera.id] = camera } @@ -238,15 +241,15 @@ export class RingApi extends Subscribed { } if (this.pushReceiver) { - return; + return } const pushReceiver = new PushReceiver({ logLevel: 'NONE', senderId: '876313859327', // for Ring android app. 703521446232 for ring-site - }); + }) - this.pushReceiver = pushReceiver; + this.pushReceiver = pushReceiver pushReceiver.onCredentialsChanged( ({ @@ -440,9 +443,9 @@ export class RingApi extends Subscribed { } refresh() { - this.disconnect(); + this.disconnect() if (this.locationsPromise) { - this.locationsPromise = this.fetchAndBuildLocations(); + this.locationsPromise = this.fetchAndBuildLocations() } } } From dbe553d0318a4e0432bea2b4a1511d94e9066728 Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Thu, 25 May 2023 13:59:05 -0500 Subject: [PATCH 3/3] Add changeset --- .changeset/thick-bananas-appear.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/thick-bananas-appear.md diff --git a/.changeset/thick-bananas-appear.md b/.changeset/thick-bananas-appear.md new file mode 100644 index 00000000..ea1cc60e --- /dev/null +++ b/.changeset/thick-bananas-appear.md @@ -0,0 +1,5 @@ +--- +'ring-client-api': patch +--- + +Adds the `refresh()` method to the RingApi class, which allows locations and devices to be reloaded cleanly. Additionally adds `onNewNotification` subject to the `RingApi` allowing a client to set up a single subscription for all push notifications without needing to resubscribe after a refresh.