Skip to content

Commit 2b8771d

Browse files
authored
Always attach a TelemetryDeck SDK instance to window (#8)
* Prefer named exports * Always attach a TelemetryDeck SDK instance to window
1 parent 94a11da commit 2b8771d

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/telemetrydeck.mjs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { version } from '../package.json';
2-
import sha256 from './utils/sha256.mjs';
3-
import assertKeyValue from './utils/assert-key-value.mjs';
4-
import transformPayload from './utils/transform-payload.mjs';
2+
import { sha256 } from './utils/sha256.mjs';
3+
import { assertKeyValue } from './utils/assert-key-value.mjs';
4+
import { transformPayload } from './utils/transform-payload.mjs';
55

66
const APP = 'app';
77
const USER = 'user';
@@ -91,8 +91,14 @@ export class TelemetryDeck {
9191
}
9292
}
9393

94-
if (window && window.td) {
94+
// Automatically attach a TelemetryDeck instance to the window object once the SDK loads
95+
if (window) {
9596
const td = new TelemetryDeck({});
96-
td.ingest(window.td);
97+
98+
// Ingest messages which where pushed to an array on the window object
99+
if (window.td) {
100+
td.ingest(window.td);
101+
}
102+
97103
window.td = td;
98104
}

src/utils/assert-key-value.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
const assertKeyValue = (key, value) => {
1+
export const assertKeyValue = (key, value) => {
22
if (!value) {
33
throw new Error(`TelemetryDeck: "${key}" is not set`);
44
}
55
};
6-
7-
export default assertKeyValue;

src/utils/sha256.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://stackoverflow.com/a/48161723/54547
2-
export default async function sha256(message) {
2+
export async function sha256(message) {
33
// encode as UTF-8
44
const messageBuffer = new TextEncoder().encode(message);
55

@@ -12,4 +12,4 @@ export default async function sha256(message) {
1212
// convert bytes to hex string
1313
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
1414
return hashHex;
15-
}
15+
}

src/utils/transform-payload.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
const transformPayload = (payload) => Object.entries(payload).map((entry) => entry.join(':'));
2-
3-
export default transformPayload;
1+
export const transformPayload = (payload) =>
2+
Object.entries(payload).map((entry) => entry.join(':'));

0 commit comments

Comments
 (0)