-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathactivity-context.js
More file actions
64 lines (56 loc) · 2.07 KB
/
activity-context.js
File metadata and controls
64 lines (56 loc) · 2.07 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
/*
* ActivityContext
*
* Single authority for accessing the runtime Activity instance.
*
* NOTE: This repo uses RequireJS shims + globals for browser builds.
* This module supports AMD (RequireJS), CommonJS (Jest), and a browser global
* fallback (ActivityContext) without exporting the Activity instance onto
* window.activity.
*/
(function (root, factory) {
// Lazy singleton: factory runs once, under the loader's control when
// possible (AMD), but always exposes a global for non-AMD consumers.
let _mod;
function getModule() {
if (!_mod) _mod = factory();
return _mod;
}
if (typeof define === "function" && define.amd) {
define([], function () {
return getModule();
});
} else if (typeof module !== "undefined" && module.exports) {
module.exports = getModule();
}
// Ensure a global reference exists so non-AMD code (activity.js, synthutils.js)
// can access it immediately.
try {
root.ActivityContext = getModule();
} catch (e) {
// ignore if root is not writable in some hostile environments
}
})(typeof globalThis !== "undefined" ? globalThis : this, function () {
"use strict";
let _activity = null;
function setActivity(activityInstance) {
if (!activityInstance) {
throw new Error("Cannot set ActivityContext with a falsy value");
}
_activity = activityInstance;
}
function getActivity() {
if (!_activity) {
throw new Error(
"Activity not initialized yet. Use dependency injection or wait for initialization."
);
}
return _activity;
}
// Freeze the public interface so no runtime code can accidentally (or
// maliciously) override setActivity / getActivity after the module loads.
// Object.freeze is shallow, which is sufficient here: we only need to
// protect the two method references, not their internal closures.
const ActivityContextAPI = Object.freeze({ setActivity, getActivity });
return ActivityContextAPI;
});