this repo has no description
1import * as models from "../../api/models";
2import * as serverData from "../../foundation/json-parsing/server-data";
3import { ResponseMetadata } from "../../foundation/network/network";
4/**
5 * Creates a new `PageRefreshController` with the provided information.
6 * @param response the response from the network request.
7 * @param refreshWhileVisibleForNextPreferredContentRefreshDate indicates that the page supports refreshing while visible, but only where the nextPreferredContentRefreshDate is used as the refresh date.
8 * @returns a `PageRefreshController`.
9 */
10export function newPageRefreshControllerFromResponse(response, refreshWhileVisibleForNextPreferredContentRefreshDate = false) {
11 return {
12 timeToLive: timeToLiveFromResponse(response),
13 nextPreferredContentRefreshDate: null,
14 refreshWhileVisibleForNextPreferredContentRefreshDate: refreshWhileVisibleForNextPreferredContentRefreshDate,
15 };
16}
17/**
18 * Creates a new `PageRefreshController`.
19 * This is primarily used for shelf hydration where we don't have a full page response to use for TTL data.
20 * @returns an empty `PageRefreshController`.
21 */
22export function newPageRefreshController() {
23 return {
24 timeToLive: null,
25 nextPreferredContentRefreshDate: null,
26 refreshWhileVisibleForNextPreferredContentRefreshDate: false,
27 };
28}
29/**
30 * Adds a preferredContentRefreshDate to the refreshController if it's sooner than the currently known nextPreferredContentRefreshDate.
31 * Provided date must be in the future.
32 * @param nextPreferredContentRefreshDate the new date to check and add.
33 * @param refreshController the refreshController to add the new date to
34 */
35export function addNextPreferredContentRefreshDate(newPreferredContentRefreshDate, refreshController) {
36 if (serverData.isNull(refreshController) || serverData.isNull(newPreferredContentRefreshDate)) {
37 return;
38 }
39 if ((serverData.isNull(refreshController.nextPreferredContentRefreshDate) ||
40 newPreferredContentRefreshDate.getTime() < refreshController.nextPreferredContentRefreshDate.getTime()) &&
41 newPreferredContentRefreshDate.getTime() > new Date().getTime()) {
42 refreshController.nextPreferredContentRefreshDate = newPreferredContentRefreshDate;
43 }
44}
45/**
46 * Generates a `PageRefreshPolicy` that can be attached to a page to define the refresh logic of that page.
47 * @param refreshController the object that has collected data to create a suitable refresh policy
48 * @returns the refresh policy, if a content TTL or nextPreferredContentRefreshDate has been provided.
49 */
50export function pageRefreshPolicyForController(objectGraph, refreshController) {
51 if (!isAutomaticPageRefreshEnabled(objectGraph)) {
52 return null;
53 }
54 if (serverData.isNull(refreshController)) {
55 return null;
56 }
57 let date;
58 let usingNextPreferredContentRefreshDate = false;
59 if (serverData.isDefinedNonNull(refreshController.timeToLive) &&
60 serverData.isDefinedNonNull(refreshController.nextPreferredContentRefreshDate)) {
61 const timeToLiveDate = dateByAddingTimeToNow(refreshController.timeToLive);
62 if (timeToLiveDate.getTime() < refreshController.nextPreferredContentRefreshDate.getTime() &&
63 timeToLiveDate.getTime() > new Date().getTime()) {
64 date = timeToLiveDate;
65 }
66 else {
67 date = refreshController.nextPreferredContentRefreshDate;
68 usingNextPreferredContentRefreshDate = true;
69 }
70 }
71 else if (serverData.isDefinedNonNull(refreshController.timeToLive)) {
72 date = dateByAddingTimeToNow(refreshController.timeToLive);
73 }
74 else if (serverData.isDefinedNonNull(refreshController.nextPreferredContentRefreshDate)) {
75 date = refreshController.nextPreferredContentRefreshDate;
76 usingNextPreferredContentRefreshDate = true;
77 }
78 else {
79 return null;
80 }
81 if (date.getTime() <= new Date().getTime()) {
82 return null;
83 }
84 const refreshWhileVisible = usingNextPreferredContentRefreshDate && refreshController.refreshWhileVisibleForNextPreferredContentRefreshDate;
85 return new models.PageRefreshPolicy("timeToLive", 0.0, null, date, refreshWhileVisible);
86}
87/**
88 * Returns the nextPreferredContentRefreshDate for the provided refresh controller.
89 * Primarily used when fetching individual shelves so we can attach a refresh date, rather
90 * than a `PageRefreshPolicy`, which sits up at a page level.
91 * @param refreshController the object that has collected refresh data.
92 * @returns the nextPreferredContentRefreshDate, if it has been provided.
93 */
94export function nextPreferredContentRefreshDateForController(refreshController) {
95 return refreshController === null || refreshController === void 0 ? void 0 : refreshController.nextPreferredContentRefreshDate;
96}
97/**
98 * Returns the time to live value from the response data.
99 *
100 * @param dataContainer The response data container from which to extract the content TTL
101 * @returns a content TTL, if any, or null.
102 */
103function timeToLiveFromResponse(response) {
104 return response[ResponseMetadata.contentMaxAge];
105}
106/**
107 * Returns a new date, adjusted by the number of seconds.
108 *
109 * @param seconds The number of seconds to add to now
110 * @returns The adjusted Date
111 */
112function dateByAddingTimeToNow(seconds) {
113 const date = new Date();
114 date.setSeconds(date.getSeconds() + seconds);
115 return date;
116}
117/**
118 * Returns whether refreshing pages automatically is enabled in the bag.
119 */
120function isAutomaticPageRefreshEnabled(objectGraph) {
121 return objectGraph.bag.enableAutomaticPageRefresh;
122}
123//# sourceMappingURL=page-refresh-controller.js.map