Skip to content

Commit

Permalink
fix #12 frontend cache for config, meta // rename PackageService to D…
Browse files Browse the repository at this point in the history
…ataStore
  • Loading branch information
dmstern committed Sep 2, 2018
1 parent 1f56b12 commit caeb03c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import PackagesService from '@/services/PackageService';
import DataStore from '@/services/DataStore';
import Package from '@/model/Package';
import { SearchItem } from '@/model/SearchItem';
import router from '@/router';
Expand Down Expand Up @@ -204,10 +204,10 @@ export default class App extends Vue {
}
private loadPackages(): void {
PackagesService.Instance.getPackages().then((packages: Package[]) => {
DataStore.Instance.getPackages().then((packages: Package[]) => {
this.searchItems =
PackagesService.Instance.searchItems
.concat(PackagesService.Instance.crafters)
DataStore.Instance.searchItems
.concat(DataStore.Instance.crafters)
.concat(packages);
this.filterSearchItems();
});
Expand Down
10 changes: 5 additions & 5 deletions src/components/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import BackendApi from '@/services/BackendApi';
import DataStore from '@/services/DataStore';
import Config from '@/model/Config';
import { IPackageJSON } from '@/model/package-json';
Expand Down Expand Up @@ -107,14 +107,14 @@ export default class About extends Vue {
}
private loadMetaInfo() {
BackendApi.Instance.getMetaInfo().then((response) => {
this.data.meta = response.data;
DataStore.Instance.getMetaInfo().then((response) => {
this.data.meta = response;
});
}
private loadConfig() {
BackendApi.Instance.getConfig().then((response) => {
this.data.config = response.data;
DataStore.Instance.getConfig().then((response) => {
this.data.config = response;
});
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/components/PackageList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import PackageService from '@/services/PackageService';
import DataStore from '@/services/DataStore';
import { PackagesResponse } from '@/model/PackageResponse';
import PackagesService from '@/services/PackageService';
import Package from '@/model/Package';
import { EventBus, Events } from '@/services/event-bus';
import BackendApi from '@/services/BackendApi';
import LoadingSpinner from '@/components/LoadingSpinner.vue';
import CrafterAvatar from '@/components/CrafterAvatar.vue';
Expand Down Expand Up @@ -115,13 +113,13 @@ export default class Packages extends Vue {
}
private loadConfig() {
BackendApi.Instance.getConfig().then((config) => {
this.artifactoryUrl = config.data.artifactory.host;
DataStore.Instance.getConfig().then((config) => {
this.artifactoryUrl = config.artifactory.host;
});
}
private async loadPackages(): Promise<Package[]> {
return PackagesService.Instance.getPackages().then((packages) => {
return DataStore.Instance.getPackages().then((packages) => {
return packages;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/BackendApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class BackendApi {
return this.get(`packageDetail/${scope ? `${scope}/` : ''}${packageName}`);
}

public getMetaInfo(): AxiosPromise<IPackageJSON> { // TODO: #2 add service with frontend cache
public getMetaInfo(): AxiosPromise<IPackageJSON> {
return this.get(`meta`);
}

Expand Down
34 changes: 31 additions & 3 deletions src/services/PackageService.ts → src/services/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { SearchItem } from '@/model/SearchItem';
import { PackageMetaDataDTO } from '@/model/package-meta-data';
import Crafter from '@/model/Crafter';
import SearchComparable from '@/model/SearchComparable';
import { IPackageJSON } from '@/model/package-json';
import Config from '@/model/Config';

export default class PackagesService {
public static get Instance(): PackagesService {
export default class DataStore {
public static get Instance(): DataStore {
return this.instance || (this.instance = new this());
}

Expand All @@ -19,7 +21,7 @@ export default class PackagesService {
return this.crafterList;
}

private static instance: PackagesService;
private static instance: DataStore;

private request!: Promise<Package[]>;
private packages!: Package[];
Expand All @@ -31,6 +33,8 @@ export default class PackagesService {
currentPackage?: Package,
},
};
private metaInfo!: IPackageJSON;
private config!: Config;

private constructor() {
this.packages = [];
Expand Down Expand Up @@ -115,4 +119,28 @@ export default class PackagesService {
}));
}

public getConfig(): Promise<Config> {
if (this.config) {
return new Promise((resolve, reject) => {
resolve(this.config);
});
}
return BackendApi.Instance.getConfig().then((response) => {
this.config = response.data;
return this.config;
});
}

public getMetaInfo(): Promise<IPackageJSON> {
if (this.metaInfo) {
return new Promise((resolve, reject) => {
resolve(this.metaInfo);
});
}
return BackendApi.Instance.getMetaInfo().then((response) => {
this.metaInfo = response.data;
return this.metaInfo;
});
}

}
9 changes: 4 additions & 5 deletions src/views/PackageDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,10 @@
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import Package from '@/model/Package';
import PackagesService from '@/services/PackageService';
import DataStore from '@/services/DataStore';
import Router from '@/router';
import LoadingSpinner from '@/components/LoadingSpinner.vue';
import { PackageMetaDataDTO, IVersions } from '@/model/package-meta-data';
import BackendApi from '@/services/BackendApi';
import router from '@/router';
import Config from '@/model/Config';
import PackageDetailItem from '@/components/PackageDetailItem.vue';
Expand Down Expand Up @@ -238,13 +237,13 @@ export default class PackageDetail extends Vue {
}
private loadConfig() {
return BackendApi.Instance.getConfig().then((config) => {
this.data.config = config.data;
return DataStore.Instance.getConfig().then((config) => {
this.data.config = config;
});
}
private getPackageDetails() {
return PackagesService.Instance.getPackageDetail({
return DataStore.Instance.getPackageDetail({
scope: Router.currentRoute.params.scope,
packageName: Router.currentRoute.params.packageName,
}).then((response) => {
Expand Down

0 comments on commit caeb03c

Please sign in to comment.