This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Lifecycle.mjs
291 lines (257 loc) · 8.68 KB
/
Lifecycle.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import EventTarget from './shims/EventTarget.mjs';
import StateChangeEvent from './StateChangeEvent.mjs';
const ACTIVE = 'active';
const PASSIVE = 'passive';
const HIDDEN = 'hidden';
const FROZEN = 'frozen';
// const DISCARDED = 'discarded'; Not used but show to completeness.
const TERMINATED = 'terminated';
// Detect Safari to work around Safari-specific bugs.
const IS_SAFARI = typeof safari === 'object' && safari.pushNotification;
const SUPPORTS_PAGE_TRANSITION_EVENTS = 'onpageshow' in self;
const EVENTS = [
'focus',
'blur',
'visibilitychange',
'freeze',
'resume',
'pageshow',
// IE9-10 do not support the pagehide event, so we fall back to unload
// Note: unload *MUST ONLY* be added conditionally, otherwise it will
// prevent page navigation caching (a.k.a bfcache).
SUPPORTS_PAGE_TRANSITION_EVENTS ? 'pagehide' : 'unload',
];
/**
* @param {!Event} evt
* @return {string}
*/
const onbeforeunload = (evt) => {
evt.preventDefault();
return evt.returnValue = 'Are you sure?';
};
/**
* Converts an array of states into an object where the state is the key
* and the value is the index.
* @param {!Array<string>} arr
* @return {!Object}
*/
const toIndexedObject = (arr) => arr.reduce((acc, val, idx) => {
acc[val] = idx;
return acc;
}, {});
/**
* @type {!Array<!Object>}
*/
const LEGAL_STATE_TRANSITIONS = [
// The normal unload process (bfcache process is addressed above).
[ACTIVE, PASSIVE, HIDDEN, TERMINATED],
// An active page transitioning to frozen,
// or an unloading page going into the bfcache.
[ACTIVE, PASSIVE, HIDDEN, FROZEN],
// A hidden page transitioning back to active.
[HIDDEN, PASSIVE, ACTIVE],
// A frozen page being resumed
[FROZEN, HIDDEN],
// A frozen (bfcached) page navigated back to
// Note: [FROZEN, HIDDEN] can happen here, but it's already covered above.
[FROZEN, ACTIVE],
[FROZEN, PASSIVE],
].map(toIndexedObject);
/**
* Accepts a current state and a future state and returns an array of legal
* state transition paths. This is needed to normalize behavior across browsers
* since some browsers do not fire events in certain cases and thus skip
* states.
* @param {string} oldState
* @param {string} newState
* @return {!Array<string>}
*/
const getLegalStateTransitionPath = (oldState, newState) => {
// We're intentionally not using for...of here so when we transpile to ES5
// we don't need to include the Symbol polyfills.
for (let order, i = 0; order = LEGAL_STATE_TRANSITIONS[i]; ++i) {
const oldIndex = order[oldState];
const newIndex = order[newState];
if (oldIndex >= 0 &&
newIndex >= 0 &&
newIndex > oldIndex) {
// Differences greater than one should be reported
// because it means a state was skipped.
return Object.keys(order).slice(oldIndex, newIndex + 1);
}
}
return [];
// TODO(philipwalton): it shouldn't be possible to get here, but
// consider some kind of warning or call to action if it happens.
// console.warn(`Invalid state change detected: ${oldState} > ${newState}`);
};
/**
* Returns the current state based on the document's visibility and
* in input focus states. Note this method is only used to determine
* active vs passive vs hidden states, as other states require listening
* for events.
* @return {string}
*/
const getCurrentState = () => {
if (document.visibilityState === HIDDEN) {
return HIDDEN;
}
if (document.hasFocus()) {
return ACTIVE;
}
return PASSIVE;
};
/**
* Class definition for the exported, singleton lifecycle instance.
*/
export default class Lifecycle extends EventTarget {
/**
* Initializes state, state history, and adds event listeners to monitor
* state changes.
*/
constructor() {
super();
const state = getCurrentState();
this._state = state;
this._unsavedChanges = [];
// Bind the callback and add event listeners.
this._handleEvents = this._handleEvents.bind(this);
// Add capturing events on window so they run immediately.
EVENTS.forEach((evt) => addEventListener(evt, this._handleEvents, true));
// Safari does not reliably fire the `pagehide` or `visibilitychange`
// events when closing a tab, so we have to use `beforeunload` with a
// timeout to check whether the default action was prevented.
// - https://bugs.webkit.org/show_bug.cgi?id=151610
// - https://bugs.webkit.org/show_bug.cgi?id=151234
// NOTE: we only add this to Safari because adding it to Firefox would
// prevent the page from being eligible for bfcache.
if (IS_SAFARI) {
addEventListener('beforeunload', (evt) => {
this._safariBeforeUnloadTimeout = setTimeout(() => {
if (!(evt.defaultPrevented || evt.returnValue.length > 0)) {
this._dispatchChangesIfNeeded(evt, HIDDEN);
}
}, 0);
});
}
}
/**
* @return {string}
*/
get state() {
return this._state;
}
/**
* Returns the value of document.wasDiscarded. This is arguably unnecessary
* but I think there's value in having the entire API in one place and
* consistent across browsers.
* @return {boolean}
*/
get pageWasDiscarded() {
return document.wasDiscarded || false;
}
/**
* @param {Symbol|Object} id A unique symbol or object identifying the
*. pending state. This ID is required when removing the state later.
*/
addUnsavedChanges(id) {
// Don't add duplicate state. Note: ideall this would be a set, but for
// better browser compatibility we're using an array.
if (!this._unsavedChanges.indexOf(id) > -1) {
// If this is the first state being added,
// also add a beforeunload listener.
if (this._unsavedChanges.length === 0) {
addEventListener('beforeunload', onbeforeunload);
}
this._unsavedChanges.push(id);
}
}
/**
* @param {Symbol|Object} id A unique symbol or object identifying the
*. pending state. This ID is required when removing the state later.
*/
removeUnsavedChanges(id) {
const idIndex = this._unsavedChanges.indexOf(id);
if (idIndex > -1) {
this._unsavedChanges.splice(idIndex, 1);
// If there's no more pending state, remove the event listener.
if (this._unsavedChanges.length === 0) {
removeEventListener('beforeunload', onbeforeunload);
}
}
}
/**
* @private
* @param {!Event} originalEvent
* @param {string} newState
*/
_dispatchChangesIfNeeded(originalEvent, newState) {
if (newState !== this._state) {
const oldState = this._state;
const path = getLegalStateTransitionPath(oldState, newState);
for (let i = 0; i < path.length - 1; ++i) {
const oldState = path[i];
const newState = path[i + 1];
this._state = newState;
this.dispatchEvent(new StateChangeEvent('statechange', {
oldState,
newState,
originalEvent,
}));
}
}
}
/**
* @private
* @param {!Event} evt
*/
_handleEvents(evt) {
if (IS_SAFARI) {
clearTimeout(this._safariBeforeUnloadTimeout);
}
switch (evt.type) {
case 'pageshow':
case 'resume':
this._dispatchChangesIfNeeded(evt, getCurrentState());
break;
case 'focus':
this._dispatchChangesIfNeeded(evt, ACTIVE);
break;
case 'blur':
// The `blur` event can fire while the page is being unloaded, so we
// only need to update the state if the current state is "active".
if (this._state === ACTIVE) {
this._dispatchChangesIfNeeded(evt, getCurrentState());
}
break;
case 'pagehide':
case 'unload':
this._dispatchChangesIfNeeded(evt, evt.persisted ? FROZEN : TERMINATED);
break;
case 'visibilitychange':
// The document's `visibilityState` will change to hidden as the page
// is being unloaded, but in such cases the lifecycle state shouldn't
// change.
if (this._state !== FROZEN &&
this._state !== TERMINATED) {
this._dispatchChangesIfNeeded(evt, getCurrentState());
}
break;
case 'freeze':
this._dispatchChangesIfNeeded(evt, FROZEN);
break;
}
}
}