Commits
Lotte Hofstede authored d932b44a770
7 7 | import { NormalizedBundle } from "./normalized-bundle.model"; |
8 8 | import { ListRemoteDataBuilder, SingleRemoteDataBuilder } from "./remote-data-builder"; |
9 9 | import { Request } from "../../data/request.models"; |
10 10 | import { hasValue } from "../../../shared/empty.util"; |
11 11 | import { RequestConfigureAction, RequestExecuteAction } from "../../data/request.actions"; |
12 12 | import { BitstreamRDBuilder } from "./bitstream-builder"; |
13 13 | import { NormalizedBitstream } from "./normalized-bitstream.model"; |
14 14 | |
15 15 | export class BundleBuilder { |
16 16 | |
17 - | constructor( |
18 - | protected objectCache: ObjectCacheService, |
19 - | protected responseCache: ResponseCacheService, |
20 - | protected requestService: RequestService, |
21 - | protected store: Store<CoreState>, |
22 - | protected href: string, |
23 - | protected normalized: NormalizedBundle |
24 - | ) { |
25 - | } |
17 + | constructor(protected objectCache: ObjectCacheService, |
18 + | protected responseCache: ResponseCacheService, |
19 + | protected requestService: RequestService, |
20 + | protected store: Store<CoreState>, |
21 + | protected href: string, |
22 + | protected normalized: NormalizedBundle) { |
23 + | } |
24 + | |
25 + | build(): Bundle { |
26 + | let links: any = {}; |
26 27 | |
27 - | build(): Bundle { |
28 - | let links: any = {}; |
28 + | if (hasValue(this.normalized.bitstreams)) { |
29 + | //for some reason the dispatches in the forEach don't |
30 + | //fire without this timeout. A zone issue? |
31 + | setTimeout(() => { |
32 + | this.normalized.bitstreams.forEach((href: string) => { |
33 + | const isCached = this.objectCache.hasBySelfLink(href); |
34 + | const isPending = this.requestService.isPending(href); |
29 35 | |
30 - | if (hasValue(this.normalized.bitstreams)) { |
31 - | //for some reason the dispatches in the forEach don't |
32 - | //fire without this timeout. A zone issue? |
33 - | setTimeout(() => { |
34 - | this.normalized.bitstreams.forEach((href: string) => { |
35 - | const isCached = this.objectCache.hasBySelfLink(href); |
36 - | const isPending = this.requestService.isPending(href); |
36 + | if (!(isCached || isPending)) { |
37 + | const request = new Request(href, NormalizedBitstream); |
38 + | this.store.dispatch(new RequestConfigureAction(request)); |
39 + | this.store.dispatch(new RequestExecuteAction(href)); |
40 + | } |
41 + | }); |
42 + | }, 0); |
37 43 | |
38 - | if (!(isCached || isPending)) { |
39 - | const request = new Request(href, NormalizedBitstream); |
40 - | this.store.dispatch(new RequestConfigureAction(request)); |
41 - | this.store.dispatch(new RequestExecuteAction(href)); |
42 - | } |
43 - | }); |
44 - | }, 0); |
44 + | links.bitstreams = this.normalized.bitstreams.map((href: string) => { |
45 + | return new BitstreamRDBuilder( |
46 + | this.objectCache, |
47 + | this.responseCache, |
48 + | this.requestService, |
49 + | this.store, |
50 + | href |
51 + | ).build(); |
52 + | }); |
53 + | } |
45 54 | |
46 - | links.bitstreams = this.normalized.bitstreams.map((href: string) => { |
47 - | return new BitstreamRDBuilder( |
48 - | this.objectCache, |
49 - | this.responseCache, |
50 - | this.requestService, |
51 - | this.store, |
52 - | href |
53 - | ).build(); |
54 - | }); |
55 + | if (hasValue(this.normalized.primaryBitstream)) { |
56 + | const href = this.normalized.primaryBitstream; |
57 + | //for some reason the dispatches in the forEach don't |
58 + | //fire without this timeout. A zone issue? |
59 + | setTimeout(() => { |
60 + | const isCached = this.objectCache.hasBySelfLink(href); |
61 + | const isPending = this.requestService.isPending(href); |
62 + | |
63 + | if (!(isCached || isPending)) { |
64 + | const request = new Request(href, NormalizedBitstream); |
65 + | this.store.dispatch(new RequestConfigureAction(request)); |
66 + | this.store.dispatch(new RequestExecuteAction(href)); |
67 + | } |
68 + | }, 0); |
69 + | links.primaryBitstream = |
70 + | new BitstreamRDBuilder( |
71 + | this.objectCache, |
72 + | this.responseCache, |
73 + | this.requestService, |
74 + | this.store, |
75 + | href |
76 + | ).build(); |
77 + | } |
78 + | return Object.assign(new Bundle(), this.normalized, links); |
55 79 | } |
56 - | return Object.assign(new Bundle(), this.normalized, links); |
57 - | } |
58 80 | } |
59 81 | |
60 82 | export class BundleRDBuilder extends SingleRemoteDataBuilder<Bundle, NormalizedBundle> { |
61 83 | |
62 - | constructor( |
63 - | objectCache: ObjectCacheService, |
64 - | responseCache: ResponseCacheService, |
65 - | requestService: RequestService, |
66 - | store: Store<CoreState>, |
67 - | href: string |
68 - | ) { |
69 - | super(objectCache, responseCache, requestService, store, href, NormalizedBundle); |
70 - | } |
84 + | constructor(objectCache: ObjectCacheService, |
85 + | responseCache: ResponseCacheService, |
86 + | requestService: RequestService, |
87 + | store: Store<CoreState>, |
88 + | href: string) { |
89 + | super(objectCache, responseCache, requestService, store, href, NormalizedBundle); |
90 + | } |
71 91 | |
72 - | protected normalizedToDomain(normalized: NormalizedBundle): Bundle { |
73 - | return new BundleBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build(); |
74 - | } |
92 + | protected normalizedToDomain(normalized: NormalizedBundle): Bundle { |
93 + | return new BundleBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build(); |
94 + | } |
75 95 | |
76 96 | } |
77 97 | |
78 98 | export class BundleListRDBuilder extends ListRemoteDataBuilder<Bundle, NormalizedBundle> { |
79 - | constructor( |
80 - | objectCache: ObjectCacheService, |
81 - | responseCache: ResponseCacheService, |
82 - | requestService: RequestService, |
83 - | store: Store<CoreState>, |
84 - | href: string |
85 - | ) { |
86 - | super(objectCache, responseCache, requestService, store, href, NormalizedBundle); |
87 - | } |
99 + | constructor(objectCache: ObjectCacheService, |
100 + | responseCache: ResponseCacheService, |
101 + | requestService: RequestService, |
102 + | store: Store<CoreState>, |
103 + | href: string) { |
104 + | super(objectCache, responseCache, requestService, store, href, NormalizedBundle); |
105 + | } |
88 106 | |
89 - | protected normalizedToDomain(normalized: NormalizedBundle): Bundle { |
90 - | return new BundleBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build(); |
91 - | } |
107 + | protected normalizedToDomain(normalized: NormalizedBundle): Bundle { |
108 + | return new BundleBuilder(this.objectCache, this.responseCache, this.requestService, this.store, this.href, normalized).build(); |
109 + | } |
92 110 | |
93 111 | } |