From fbb64a6031e1813a7a715d1ad5b80d11bc4dfb3c Mon Sep 17 00:00:00 2001 From: Art Lowel Date: Mon, 29 May 2017 13:42:11 +0200 Subject: [PATCH] adapted remote data build method to aggregate relation remotedata arrays --- .../builders/remote-data-build.service.ts | 47 +++++++++++-------- src/app/core/shared/bitstream.model.ts | 2 +- src/app/core/shared/bundle.model.ts | 4 +- src/app/core/shared/collection.model.ts | 4 +- src/app/core/shared/community.model.ts | 4 +- src/app/core/shared/dspace-object.model.ts | 2 +- src/app/core/shared/item.model.ts | 27 ++++------- .../collections/collections.component.html | 4 +- .../collections/collections.component.ts | 4 +- .../file-section/file-section.component.html | 6 +-- .../file-section/file-section.component.ts | 2 +- src/server.ts | 29 +++++++----- 12 files changed, 67 insertions(+), 68 deletions(-) diff --git a/src/app/core/cache/builders/remote-data-build.service.ts b/src/app/core/cache/builders/remote-data-build.service.ts index 274b72e102..4a70f05166 100644 --- a/src/app/core/cache/builders/remote-data-build.service.ts +++ b/src/app/core/cache/builders/remote-data-build.service.ts @@ -134,9 +134,12 @@ export class RemoteDataBuildService { }); }, 0); - links[relationship] = normalized[relationship].map((href: string) => { - return this.buildSingle(href, resourceConstructor); + let rdArr = []; + normalized[relationship].forEach((href: string) => { + rdArr.push(this.buildSingle(href, resourceConstructor)); }); + + links[relationship] = this.aggregate(rdArr); } else { // without the setTimeout, the actions inside requestService.configure @@ -155,29 +158,33 @@ export class RemoteDataBuildService { } aggregate(input: RemoteData[]): RemoteData { - const requestPending: Observable = > Observable.combineLatest( + const requestPending = Observable.combineLatest( ...input.map(rd => rd.isRequestPending), - (...pendingArray) => pendingArray.every(e => e === true) - ).distinctUntilChanged(); - const responsePending: Observable = > Observable.combineLatest( + ).map((...pendingArray) => pendingArray.every(e => e === true)) + .distinctUntilChanged(); + + const responsePending = Observable.combineLatest( ...input.map(rd => rd.isResponsePending), - (...pendingArray) => pendingArray.every(e => e === true) - ).distinctUntilChanged(); - const isSuccessFul: Observable = > Observable.combineLatest( + ).map((...pendingArray) => pendingArray.every(e => e === true)) + .distinctUntilChanged(); + + const isSuccessFul = Observable.combineLatest( ...input.map(rd => rd.hasSucceeded), - (...successArray) => successArray.every(e => e === true) - ).distinctUntilChanged(); - const errorMessage: Observable = > Observable.combineLatest( + ).map((...successArray) => successArray.every(e => e === true)) + .distinctUntilChanged(); + + const errorMessage = Observable.combineLatest( ...input.map(rd => rd.errorMessage), - (...errors) => errors - .map((e, idx) => { - if (hasValue(e)) { - return `[${idx}]: ${e}`; - } - }) - .filter(e => hasValue(e)) - .join(", ") + ).map((...errors) => errors + .map((e, idx) => { + if (hasValue(e)) { + return `[${idx}]: ${e}`; + } + }) + .filter(e => hasValue(e)) + .join(", ") ); + const payload = > Observable.combineLatest( ...input.map(rd => rd.payload) ); diff --git a/src/app/core/shared/bitstream.model.ts b/src/app/core/shared/bitstream.model.ts index 5325e395d8..06bdf41f30 100644 --- a/src/app/core/shared/bitstream.model.ts +++ b/src/app/core/shared/bitstream.model.ts @@ -27,7 +27,7 @@ export class Bitstream extends DSpaceObject { /** * An array of Bundles that are direct parents of this Bitstream */ - parents: Array>; + parents: RemoteData; /** * The Bundle that owns this Bitstream diff --git a/src/app/core/shared/bundle.model.ts b/src/app/core/shared/bundle.model.ts index 7c2f6b05d4..8f3a284fb6 100644 --- a/src/app/core/shared/bundle.model.ts +++ b/src/app/core/shared/bundle.model.ts @@ -12,13 +12,13 @@ export class Bundle extends DSpaceObject { /** * An array of Items that are direct parents of this Bundle */ - parents: Array>; + parents: RemoteData; /** * The Item that owns this Bundle */ owner: Item; - bitstreams: Array> + bitstreams: RemoteData } diff --git a/src/app/core/shared/collection.model.ts b/src/app/core/shared/collection.model.ts index 4287eff63c..30814726b8 100644 --- a/src/app/core/shared/collection.model.ts +++ b/src/app/core/shared/collection.model.ts @@ -58,13 +58,13 @@ export class Collection extends DSpaceObject { /** * An array of Collections that are direct parents of this Collection */ - parents: Array>; + parents: RemoteData; /** * The Collection that owns this Collection */ owner: Collection; - items: Array>; + items: RemoteData; } diff --git a/src/app/core/shared/community.model.ts b/src/app/core/shared/community.model.ts index 9639abd258..67c8211fd2 100644 --- a/src/app/core/shared/community.model.ts +++ b/src/app/core/shared/community.model.ts @@ -50,13 +50,13 @@ export class Community extends DSpaceObject { /** * An array of Communities that are direct parents of this Community */ - parents: Array>; + parents: RemoteData; /** * The Community that owns this Community */ owner: Community; - collections: Array>; + collections: RemoteData; } diff --git a/src/app/core/shared/dspace-object.model.ts b/src/app/core/shared/dspace-object.model.ts index 7f6e0f5ee2..2005a250e7 100644 --- a/src/app/core/shared/dspace-object.model.ts +++ b/src/app/core/shared/dspace-object.model.ts @@ -38,7 +38,7 @@ export abstract class DSpaceObject implements CacheableObject { /** * An array of DSpaceObjects that are direct parents of this DSpaceObject */ - parents: Array>; + parents: RemoteData; /** * The DSpaceObject that owns this DSpaceObject diff --git a/src/app/core/shared/item.model.ts b/src/app/core/shared/item.model.ts index 8a589822d1..424ead03e7 100644 --- a/src/app/core/shared/item.model.ts +++ b/src/app/core/shared/item.model.ts @@ -31,14 +31,14 @@ export class Item extends DSpaceObject { /** * An array of Collections that are direct parents of this Item */ - parents: Array>; + parents: RemoteData; /** * The Collection that owns this Item */ owner: Collection; - bundles: Array>; + bundles: RemoteData; /** @@ -63,13 +63,11 @@ export class Item extends DSpaceObject { * Retrieves all files that should be displayed on the item page of this item * @returns {Observable>>} an array of all Bitstreams in the "ORIGINAL" bundle */ - getFiles(): Observable>> { + getFiles(): Observable { const bundle: Observable = this.getBundle("ORIGINAL"); - return bundle.map(bundle => { - if (hasValue(bundle) && Array.isArray(bundle.bitstreams)) { - return bundle.bitstreams.map(bitstream => bitstream.payload) - } - }); + return bundle + .filter(bundle => hasValue(bundle)) + .flatMap(bundle => bundle.bitstreams.payload); } /** @@ -78,9 +76,8 @@ export class Item extends DSpaceObject { * @returns {Observable} the Bundle that belongs to this item with the given name */ getBundle(name: String): Observable { - return Observable.combineLatest( - ...this.bundles.map(b => b.payload), - (...bundles: Array) => bundles) + return this.bundles.payload + .filter(bundles => hasValue(bundles)) .map(bundles => { return bundles.find((bundle: Bundle) => { return bundle.name === name @@ -88,12 +85,4 @@ export class Item extends DSpaceObject { }); } - /** - * Retrieves all direct parent collections of this item - * @returns {Array>} an array of all Collections that contain this item - */ - getCollections(): Array> { - return this.parents.map(collection => collection.payload.map(parent => parent)); - } - } diff --git a/src/app/item-page/simple/collections/collections.component.html b/src/app/item-page/simple/collections/collections.component.html index bf764ae182..8f184817c2 100644 --- a/src/app/item-page/simple/collections/collections.component.html +++ b/src/app/item-page/simple/collections/collections.component.html @@ -1,7 +1,7 @@ diff --git a/src/app/item-page/simple/collections/collections.component.ts b/src/app/item-page/simple/collections/collections.component.ts index 0ae810ca86..199fb2b773 100644 --- a/src/app/item-page/simple/collections/collections.component.ts +++ b/src/app/item-page/simple/collections/collections.component.ts @@ -20,7 +20,7 @@ export class CollectionsComponent implements OnInit { separator: string = "
" - collections: Array>; + collections: Observable; constructor() { this.universalInit(); @@ -31,7 +31,7 @@ export class CollectionsComponent implements OnInit { } ngOnInit(): void { - this.collections = this.item.getCollections(); + this.collections = this.item.parents.payload; } diff --git a/src/app/item-page/simple/file-section/file-section.component.html b/src/app/item-page/simple/file-section/file-section.component.html index 0cc15e090e..149a1b2017 100644 --- a/src/app/item-page/simple/file-section/file-section.component.html +++ b/src/app/item-page/simple/file-section/file-section.component.html @@ -1,8 +1,8 @@ diff --git a/src/app/item-page/simple/file-section/file-section.component.ts b/src/app/item-page/simple/file-section/file-section.component.ts index 359728ac70..d434c62b9e 100644 --- a/src/app/item-page/simple/file-section/file-section.component.ts +++ b/src/app/item-page/simple/file-section/file-section.component.ts @@ -20,7 +20,7 @@ export class FileSectionComponent implements OnInit { separator: string = "
" - files: Observable>>; + files: Observable; constructor() { this.universalInit(); diff --git a/src/server.ts b/src/server.ts index 13837821d0..42f8d9bd5e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -82,22 +82,25 @@ function ngApp(req, res) { function onHandleError(parentZoneDelegate, currentZone, targetZone, error) { console.warn('Error in SSR, serving for direct CSR'); res.sendFile('index.html', { root: './src' }); - return false; } - Zone.current.fork({ name: 'CSR fallback', onHandleError }).run(() => { - res.render('index', { - req, - res, - // time: true, // use this to determine what part of your app is slow only in development - async: EnvConfig.universal.async, - preboot: EnvConfig.universal.preboot, - baseUrl: EnvConfig.ui.nameSpace, - requestUrl: req.originalUrl, - originUrl: EnvConfig.ui.baseUrl + if (EnvConfig.universal.preboot) { + Zone.current.fork({ name: 'CSR fallback', onHandleError }).run(() => { + res.render('index', { + req, + res, + // time: true, // use this to determine what part of your app is slow only in development + async: EnvConfig.universal.async, + preboot: EnvConfig.universal.preboot, + baseUrl: EnvConfig.ui.nameSpace, + requestUrl: req.originalUrl, + originUrl: EnvConfig.ui.baseUrl + }); }); - }); - + } + else { + res.sendFile('index.html', { root: './src' }); + } } /**