mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
/*!
|
|
* @license
|
|
* Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { Injectable } from '@angular/core';
|
|
import { Agent, AgentsApi } from '@alfresco/js-api';
|
|
import { BehaviorSubject, from, Observable, of } from 'rxjs';
|
|
import { map, switchMap } from 'rxjs/operators';
|
|
import { AlfrescoApiService } from '../../services';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class AgentService {
|
|
private _agentsApi: AgentsApi;
|
|
private agents = new BehaviorSubject<Agent[]>([]);
|
|
|
|
get agentsApi(): AgentsApi {
|
|
this._agentsApi = this._agentsApi ?? new AgentsApi(this.apiService.getInstance());
|
|
return this._agentsApi;
|
|
}
|
|
|
|
agents$ = this.agents.asObservable();
|
|
|
|
constructor(private apiService: AlfrescoApiService) {}
|
|
|
|
/**
|
|
* Gets all agents from cache. If cache is empty, fetches agents from backend.
|
|
*
|
|
* @returns Agent[] list containing agents.
|
|
*/
|
|
getAgents(): Observable<Agent[]> {
|
|
return this.agents$.pipe(
|
|
switchMap((agentsList) => {
|
|
if (agentsList.length) {
|
|
return of(agentsList);
|
|
}
|
|
return from(this.agentsApi.getAgents()).pipe(
|
|
map((paging) => {
|
|
const agentEntries = paging.list.entries.map((agentEntry) => agentEntry.entry);
|
|
this.agents.next(agentEntries);
|
|
return agentEntries;
|
|
})
|
|
);
|
|
})
|
|
);
|
|
}
|
|
}
|