@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
172 lines (170 loc) • 8.21 kB
JavaScript
import { zip } from 'rxjs';
import { map, retry, share } from 'rxjs/operators';
import { QueryCache } from '../data/query-cache';
import { ClusterInventoryCache } from '../shared/cluster-inventory/cluster-inventory-cache';
import { GatewayInventoryCache } from '../shared/gateway-inventory/gateway-inventory-cache';
import { GatewayInventoryDetailCache } from '../shared/gateway-inventory/gateway-inventory-detail-cache';
import { ServerInventoryCache } from '../shared/server-inventory/server-inventory-cache';
import { ServerInventoryDetailCache } from '../shared/server-inventory/server-inventory-detail-cache';
export class InventoryQueryCaches {
appContext;
serverInventoryCache;
serverInventoryDetailCache;
gatewayInventoryCache;
gatewayInventoryDetailCache;
clusterInventoryCache;
serverInventorySharedContext = {
name: null,
server: false,
detail: false,
shared: null
};
serverInventoryCacheNodeName;
gatewayCached = false;
gatewayCombinedCached = false;
serverCache;
serverCombinedCache;
gatewayCache;
gatewayCombinedCache;
clusterCache;
constructor(appContext, options) {
this.appContext = appContext;
let cacheOptions;
if (options.serverCache) {
cacheOptions = options.serverCache.expiration ? { expiration: options.serverCache.expiration } : null;
this.serverInventoryCache = new ServerInventoryCache(this.appContext, cacheOptions);
this.serverCache = new QueryCache(params => this.createServerInventory(params), params => params.name);
}
if (options.serverCache && options.serverCombinedCache) {
cacheOptions = options.serverCombinedCache.expiration ? { expiration: options.serverCombinedCache.expiration } : null;
this.serverInventoryDetailCache = new ServerInventoryDetailCache(this.appContext, cacheOptions);
this.serverCombinedCache = new QueryCache(params => this.createServerInventoryCombined(params), params => params.name, () => this.serverInventoryCacheNodeName = null);
}
if (options.gatewayCache) {
cacheOptions = options.gatewayCache.expiration ? { expiration: options.gatewayCache.expiration } : null;
this.gatewayInventoryCache = new GatewayInventoryCache(this.appContext, cacheOptions);
this.gatewayCache = new QueryCache(params => this.createGatewayInventory(params), () => 'gateway', () => this.gatewayCached = false);
}
if (options.gatewayCache && options.gatewayCombinedCache) {
cacheOptions = options.gatewayCombinedCache.expiration ? { expiration: options.gatewayCombinedCache.expiration } : null;
this.gatewayInventoryDetailCache = new GatewayInventoryDetailCache(this.appContext, cacheOptions);
this.gatewayCombinedCache = new QueryCache(params => this.createGatewayInventoryCombined(params), () => 'gateway', () => this.gatewayCombinedCached = false);
}
if (options.clusterCache) {
cacheOptions = options.clusterCache.expiration ? { expiration: options.clusterCache.expiration } : null;
this.clusterInventoryCache = new ClusterInventoryCache(this.appContext, cacheOptions);
this.clusterCache = new QueryCache(params => this.createClusterInventory(params), params => params.name);
}
}
/**
* Clear all session storage.
*/
clear() {
if (this.serverInventoryCache) {
this.serverInventoryCache.clear();
}
if (this.serverInventoryDetailCache) {
this.serverInventoryDetailCache.clear();
}
if (this.gatewayInventoryCache) {
this.gatewayInventoryCache.clear();
}
if (this.gatewayInventoryDetailCache) {
this.gatewayInventoryDetailCache.clear();
}
if (this.clusterInventoryCache) {
this.clusterInventoryCache.clear();
}
}
/**
* Create server inventory query.
* - It shares the query with ServerInventory and ServerInventoryCombined. Keep track the request and re-create if asked twice.
*
* @param params the inventory query parameters.
* @param detail the state of server query or detail query.
* @return Observable<ServerInventory> Shared ServerInventory observable.
*/
createServerInventory(params, detail) {
// check
// - if not created yet.
// - if requested server name is changed.
// - if server query second time.
// - if detail query second time.
const createNew = !this.serverInventorySharedContext.shared
|| params.name !== this.serverInventorySharedContext.name
|| (this.serverInventorySharedContext.server && !detail)
|| (this.serverInventorySharedContext.detail && detail);
if (!createNew) {
if (detail) {
this.serverInventorySharedContext.detail = true;
}
else {
this.serverInventorySharedContext.server = true;
}
// return shared pre-created one.
return this.serverInventorySharedContext.shared;
}
// creates new shared observable.
let observable = this.serverInventoryCache.query({ name: params.name })
.pipe(map(inventory => inventory.instance));
if (params && params.retry) {
observable = observable.pipe(retry(params.retry));
}
this.serverInventorySharedContext = { name: params.name, server: !detail, detail: !!detail, shared: observable.pipe(share()) };
return this.serverInventorySharedContext.shared;
}
createServerInventoryCombined(params) {
let observable;
if (!this.serverInventoryCacheNodeName || this.serverInventoryCacheNodeName !== params.name) {
this.serverInventoryCacheNodeName = params.name;
observable = zip(this.createServerInventory(params, true), this.serverInventoryDetailCache.query({ name: params.name })
.pipe(map(inventory => inventory.instance)));
}
else {
observable = zip(this.serverInventoryCache.refresh({ name: params.name })
.pipe(map(inventory => inventory.instance)), this.serverInventoryDetailCache.refresh({ name: params.name })
.pipe(map(inventory => inventory.instance)));
}
if (params && params.retry) {
return observable.pipe(retry(params.retry));
}
return observable;
}
createGatewayInventory(params) {
let observable;
if (!this.gatewayCached) {
this.gatewayCached = true;
observable = this.gatewayInventoryCache.query({}).pipe(map(inventory => inventory.instance));
}
else {
observable = this.gatewayInventoryCache.refresh({}).pipe(map(inventory => inventory.instance));
}
if (params && params.retry) {
return observable.pipe(retry(params.retry));
}
return observable;
}
createGatewayInventoryCombined(params) {
let observable;
if (!this.gatewayCombinedCached) {
this.gatewayCombinedCached = true;
observable = zip(this.gatewayInventoryCache.query({}).pipe(map(inventory => inventory.instance)), this.gatewayInventoryDetailCache.query({}).pipe(map(inventory => inventory.instance)));
}
else {
observable = zip(this.gatewayInventoryCache.refresh({}).pipe(map(inventory => inventory.instance)), this.gatewayInventoryDetailCache.refresh({}).pipe(map(inventory => inventory.instance)));
}
if (params && params.retry) {
return observable.pipe(retry(params.retry));
}
return observable;
}
createClusterInventory(params) {
const observable = this.clusterInventoryCache.query({ name: params.name })
.pipe(map(inventory => inventory.instance));
if (params && params.retry) {
return observable.pipe(retry(params.retry));
}
return observable;
}
}
//# sourceMappingURL=inventory-query-caches.js.map