-
Notifications
You must be signed in to change notification settings - Fork 27
/
loaded-asset-state.js
125 lines (113 loc) · 3 KB
/
loaded-asset-state.js
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
import Ember from 'ember';
let cachedRequireEntries;
let cachedScriptTags;
let cachedLinkTags;
/**
* Determines whether an array contains the provided item.
*
* @param {Array} array
* @param {Any} item
* @return {Boolean}
*/
function has(array, item) {
return array.indexOf(item) !== -1;
}
/**
* Removes a DOM Node from the document.
*
* @param {Node} node
* @return {Void}
*/
function removeNode(node) {
node.parentNode.removeChild(node);
}
/**
* Converts an iterable object into an actual Array.
*
* @param {Iterable} iterable
* @return {Array}
*/
function toArray(iterable) {
return Array.prototype.slice.call(iterable);
}
/**
* Returns all of the HTML Elements matching a given selector as an array.
*
* @param {String} selector
* @return {Array<HTMLElement>}
*/
function getAll(selector) {
const htmlCollection = document.querySelectorAll(selector);
return toArray(htmlCollection);
}
/**
* Deletes an entry from require's list of modules.
*
* @param {String} entry
* @return {Void}
*/
function resetRequireEntry(entry) {
delete requirejs.entries[entry];
}
/**
* Compares two arrays, if they're different, invokes a callback for each
* entry that does not appear in the initial array.
*
* @param {Array} initial
* @param {Array} current
* @param {Function} diffHandler
* @return {Void}
*/
function compareAndIterate(initial, current, diffHandler) {
if (initial.length < current.length) {
for (let i = 0; i < current.length; i++) {
let entry = current[i];
if (!has(initial, entry)) {
diffHandler(entry);
}
}
}
}
/**
* Gets the current loaded asset state including scripts, links, and require
* modules.
*
* @return {Object}
*/
export function getLoadedAssetState() {
return {
requireEntries: Object.keys(requirejs.entries),
scripts: getAll('script'),
links: getAll('link')
};
}
/**
* Caches the current loaded asset state with regards to links, scripts, and JS
* modules currently present.
*
* @return {Void}
*/
export function cacheLoadedAssetState() {
({
requireEntries: cachedRequireEntries,
scripts: cachedScriptTags,
links: cachedLinkTags
} = getLoadedAssetState());
}
/**
* Restores the loaded asset state to the previous cached value with regards to
* links, scripts, and JS modules.
*
* @return {Void}
*/
export function resetLoadedAssetState() {
Ember.Logger.info('Resetting loaded asset state. This will attempt to restore the state of loaded assets to the last cached value. If an asset modified some global state, we cannot guarantee it will be reset. For more information see: https://github.com/trentmwillis/ember-asset-loader#resetting-test-state');
const {
requireEntries: currentRequireEntries,
scripts: currentScriptTags,
links: currentLinkTags
} = getLoadedAssetState();
compareAndIterate(cachedRequireEntries, currentRequireEntries, resetRequireEntry);
compareAndIterate(cachedScriptTags, currentScriptTags, removeNode);
compareAndIterate(cachedLinkTags, currentLinkTags, removeNode);
}