Skip to content

Commit

Permalink
PackageDetail / PackageService: refactor layers
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Aug 29, 2018
1 parent 05da993 commit 80fb59f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
50 changes: 31 additions & 19 deletions src/services/PackageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import BackendApi from '@/services/BackendApi';
import { PackagesResponse } from '@/model/PackageResponse';
import Package from '@/model/Package';
import { SearchItem, SearchKey } from '@/model/SearchItem';
import { PackageMetaDataDTO } from '@/model/package-meta-data';

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

public get packageNames(): string[] {
return this.packageNamesList;
}

public get searchItems(): Array<SearchItem|Package> {
return this.searchItemList;
}
Expand All @@ -20,41 +17,61 @@ export default class PackagesService {

private request!: Promise<Package[]>;
private packages!: Package[];
private packageNamesList!: string[];
private searchItemList!: SearchItem[];
private packageDetails!: {
[packageName: string]: Package,
[packageName: string]: {
packageDetail: Package,
currentPackage?: Package,
},
};

private constructor() {
this.packages = [];
this.packageNamesList = [];
this.searchItemList = [];
this.packageDetails = {};
}

public async getPackageDetail(packageId: {scope: string, packageName: string}): Promise<Package> {
public async getPackageDetail(packageId: {scope: string, packageName: string}): Promise<{
packageDetail: Package,
currentPackage?: Package,
}> {
const scope = packageId.scope;
const packageName = packageId.packageName;
const key = scope ? `${scope}/${packageName}` : packageName;
const cachedPackageDetails = this.packageDetails[key];
if (cachedPackageDetails) {
return new Promise<Package>((fulfill, reject) => {
return new Promise<{
packageDetail: Package,
currentPackage?: Package,
}>((fulfill) => {
fulfill(cachedPackageDetails);
});
}

return new Promise<Package>((fulfill, reject) => {
return new Promise<{
packageDetail: Package,
currentPackage?: Package,
}>((fulfill) => {
BackendApi.Instance.getPackageDetail({scope, packageName}).then((response) => {
this.packageDetails[key] = new Package(response.data);
const packageDetail: Package = new Package(response.data);
let currentPackage: Package | undefined;
const currentVersionObject: string | PackageMetaDataDTO =
packageDetail.versions[packageDetail['dist-tags'].latest];
if (typeof currentVersionObject !== 'string') {
currentPackage = new Package(currentVersionObject);
}
this.packageDetails[key] = {
packageDetail,
currentPackage,
};
return fulfill(this.packageDetails[key]);
});
});
}

public async getPackages(): Promise<Package[]> {
if (this.packages.length) {
return new Promise<Package[]>((fulfill, reject) => {
return new Promise<Package[]>((fulfill) => {
fulfill(this.packages);
});
}
Expand All @@ -65,14 +82,9 @@ export default class PackagesService {
return (this.request = new Promise<Package[]>((fulfill, reject) => {
BackendApi.Instance.getPackages().then((response) => {
const packagesResponse: PackagesResponse = response.data;
this.packageNamesList = Object.keys(packagesResponse).filter(
(key) => !key.startsWith('_'),
);

for (const packageName of this.packageNamesList) {
const modifiedPackage: Package = new Package(
packagesResponse[packageName],
);
for (const packageName of Object.keys(packagesResponse).filter((key) => !key.startsWith('_'))) {
const modifiedPackage: Package = new Package(packagesResponse[packageName]);
this.packages.push(modifiedPackage);

if (modifiedPackage.keywords) {
Expand Down
20 changes: 8 additions & 12 deletions src/views/PackageDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ export default class PackageDetail extends Vue {
@Prop() private dataProp!: {
packageDetail: Package | null,
currentPackage: Package | null,
currentPackage?: Package,
currentTags: IVersions,
versionsHistory: IVersions,
config: Config | undefined,
};
private data: {
packageDetail: Package | null,
currentPackage: Package | null,
currentPackage?: Package,
currentTags: IVersions,
versionsHistory: IVersions,
config: Config | undefined,
Expand All @@ -202,7 +202,7 @@ export default class PackageDetail extends Vue {
this.activeTab = 0;
this.data = {
packageDetail: null,
currentPackage: null,
currentPackage: undefined,
currentTags: {},
versionsHistory: {},
config: undefined,
Expand All @@ -220,7 +220,7 @@ export default class PackageDetail extends Vue {
this.activeTab = 0;
this.data = {
packageDetail: null,
currentPackage: null,
currentPackage: undefined,
currentTags: {},
versionsHistory: {},
config: undefined,
Expand All @@ -245,14 +245,10 @@ export default class PackageDetail extends Vue {
scope: Router.currentRoute.params.scope,
packageName: Router.currentRoute.params.packageName,
}).then((response) => {
this.data.packageDetail = response;
const currentVersionObject: string | PackageMetaDataDTO =
this.data.packageDetail.versions[this.data.packageDetail['dist-tags'].latest];
if (typeof currentVersionObject !== 'string') {
this.data.currentPackage = new Package(currentVersionObject);
this.data.currentTags = response['dist-tags'];
this.data.versionsHistory = response.versions;
}
this.data.packageDetail = response.packageDetail;
this.data.currentTags = response.packageDetail['dist-tags'];
this.data.versionsHistory = response.packageDetail.versions;
this.data.currentPackage = response.currentPackage;
});
}
Expand Down

0 comments on commit 80fb59f

Please sign in to comment.