Commit dde134ca authored by David Burke's avatar David Burke

Update properties observable only when it actually changes

parent 1041959e
Pipeline #69632106 passed with stages
in 5 minutes and 42 seconds
import { Injectable } from "@angular/core";
import { BehaviorSubject } from "rxjs";
import { IAuth, ISignInResp } from "./api.interfaces";
import { IAuth } from "./api.interfaces";
import { map } from "rxjs/operators";
const initialState: IAuth = {
......
......@@ -5,8 +5,7 @@ import {
forkJoin,
combineLatest,
Observable,
EMPTY,
of
EMPTY
} from "rxjs";
import { map, tap, exhaustMap, withLatestFrom, delay } from "rxjs/operators";
import { AuthService } from "./auth.service";
......@@ -51,6 +50,7 @@ export class DevicesService {
private _devices: IDevice[];
getDevices = this.devices.asObservable();
private properties = new BehaviorSubject<IProperty[]>([]);
private _properties: IProperty[];
getProperties = this.properties.asObservable();
getDevicesWithProperties: Observable<IDeviceWithProperties[]> = combineLatest(
this.getDevices,
......@@ -82,6 +82,7 @@ export class DevicesService {
constructor(private http: HttpClient, private auth: AuthService) {
this.auth.getAuthToken.subscribe(token => (this.accessToken = token));
this.devices.subscribe(devices => (this._devices = devices));
this.properties.subscribe(properties => (this._properties = properties));
const devicesData = localStorage.getItem(storageDevicesKey);
if (devicesData) {
const devices: IDevice[] = JSON.parse(devicesData);
......@@ -205,11 +206,25 @@ export class DevicesService {
}
setProperties(data: IProperty[]) {
this.properties.next(data);
localStorage.setItem(storagePropertiesKey, JSON.stringify(data));
// Check if changed
if (!this.deepEqual(data, this._properties)) {
this.properties.next(data);
localStorage.setItem(storagePropertiesKey, JSON.stringify(data));
}
}
clearDevices() {
this.devices.next([]);
}
/** Simplistic deep equal implementation */
private deepEqual(x, y) {
const ok = Object.keys,
tx = typeof x,
ty = typeof y;
return x && y && tx === "object" && tx === ty
? ok(x).length === ok(y).length &&
ok(x).every(key => this.deepEqual(x[key], y[key]))
: x === y;
}
}
import { Routes } from "@angular/router";
export const componentDeclarations: any[] = [];
export const providerDeclarations: any[] = [];
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom",
"es6",
"es2015.iterable"
],
"baseUrl": ".",
"paths": {
"~/*": [
"src/*"
]
}
},
"exclude": [
"**/*.tns.ts",
"**/*.android.ts",
"**/*.ios.ts",
"**/*.spec.ts",
"wearable"
]
}
\ No newline at end of file
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noUnusedLocals": true,
"target": "es2015",
"typeRoots": ["node_modules/@types"],
"lib": ["es2017", "dom", "es6", "es2015.iterable"],
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
},
"exclude": [
"**/*.tns.ts",
"**/*.android.ts",
"**/*.ios.ts",
"**/*.spec.ts",
"wearable"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment