Commit fb4b6951 authored by Fabien Amarger's avatar Fabien Amarger
Browse files

feat(ViewWrapper): Add the ViewWrapper class to libview

parent 3a3651a61eb9
Pipeline #64096 failed with stages
in 47 seconds
import {ViewDescription, View} from '.';
export class ViewWrapper {
DESCRIPTOR: ViewDescription;
_VIEW_ENTRYPOINT?: View;
constructor(descriptor: ViewDescription) {
this.DESCRIPTOR = descriptor;
}
getViewEntrypoint(): Promise<View> {
const fetchViewData = async () => {
// eslint-disable-next-line no-eval
const viewData = eval(
await fetch(this.viewUrl).then((data) => data.text())
);
return viewData;
};
return new Promise((resolve, reject) => {
if (this._VIEW_ENTRYPOINT === undefined) {
fetchViewData()
.then((viewData) => {
if (viewData.VIEW_ENTRYPOINT === undefined) {
reject(`View entrypoint is not defined in ${this.DESCRIPTOR.name}`);
}
this._VIEW_ENTRYPOINT = viewData.VIEW_ENTRYPOINT;
resolve(viewData.VIEW_ENTRYPOINT);
})
.catch((e) => {
console.error('Error fetching view data : ', e);
reject(e);
});
} else {
resolve(this._VIEW_ENTRYPOINT);
}
});
}
isApplicable(uri: string, endpoint: string): Promise<boolean> {
return new Promise((resolve, reject) => {
this.getViewEntrypoint()
.then((view) => {
resolve(view.isApplicable(uri, endpoint));
})
.catch((e) => {
console.error('Error fetching view data : ', e);
reject(e);
});
});
}
get name(): string {
return this.DESCRIPTOR.name;
}
get id(): string {
return this.DESCRIPTOR.name;
}
get viewUrl(): string {
return this.DESCRIPTOR.url;
}
}
...@@ -17,3 +17,5 @@ export interface ViewDescription { ...@@ -17,3 +17,5 @@ export interface ViewDescription {
description: string; description: string;
url: string; url: string;
} }
export {ViewWrapper} from './ViewWrapper';
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