-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
71 lines (65 loc) · 2.27 KB
/
utils.js
File metadata and controls
71 lines (65 loc) · 2.27 KB
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
/* --------------------------------------------------------------------------------
* Created on Wed Oct 16 2024
*
* Copyright (c) 2024 Colorado State University. All rights reserved. (1)
*
* Contributors:
* Mackenzie Grimes (1)
*
* --------------------------------------------------------------------------------
*/
import * as ThumbmarkJS from '@thumbmarkjs/thumbmarkjs';
// utility to simulate attempting a network call. To be deleted
const sleep = (ms) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
/**
* Fetch a pseudo-unique identifier for this browser, based on any device specs available to
* Javascript such as browser version, plugins, audio settings, video hardware, etc. Does not
* rely on cookies or localStorage to track users across sessions.
*
* @returns {Promise<string>} unique "fingerprint"/ID for this browser.
*/
const getDeviceFingerprint = async () => {
// Don't consider screen resolution or window permissions when generating unique fingerprint.
// This ensures that a user has the same fingerprint whether
// - NWS Connect apps are running as embeds/iframes or the parent window, and
// - user views NWS Connect apps across multiple displays
ThumbmarkJS.setOption('exclude', ['screen', 'permissions', 'system.cookieEnabled']);
return ThumbmarkJS.getFingerprint();
};
/**
*
* @param {Promise} promise A Promise of unknown state
* @returns True if Promise has already been resolved or rejected, False if 'pending'
*/
const isPromiseFinished = async (promise) =>
Promise.race([
new Promise((done) => {
setTimeout(() => done(false), 1);
}),
promise.then(
() => true,
() => true,
),
]);
/**
* Watch a currently-pending Promise, and if a given number of milliseconds pass without it
* resolving or rejecting, "abandon" it and run the given ```callback``` instead to notify.
*
* @param {Promise} promise
* @param {number} milliseconds
* @param {() => {}} callback
* @returns
*/
const subscribeToTimeout = (promise, milliseconds, callback) =>
new Promise(() => {
setTimeout(async () => {
const isFinished = await isPromiseFinished(promise);
if (!isFinished) {
callback();
}
}, milliseconds);
});
export { sleep, subscribeToTimeout, getDeviceFingerprint };