forked from VotingVoices/chorus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'telemetryMiddleware' and a preliminary 'start' telemetry action
- Loading branch information
1 parent
174aaa9
commit 117f83e
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// TODO: Move to a different location? | ||
|
||
export const enum TelemetryActionType { | ||
START = 'TELEMETRY/START', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// TODO: Move to a better location? | ||
|
||
import { action } from 'typesafe-actions'; | ||
import { TelemetryActionType } from './TelemetryActionType'; | ||
|
||
export const start = () => | ||
action(TelemetryActionType.START); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ActionType } from 'typesafe-actions'; | ||
import { TelemetryActionType } from './TelemetryActionType'; | ||
import * as telemetryActions from './TelemetryActions'; | ||
|
||
// Courtesy of 'broofa's answer in https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript | ||
/*function uuidv4() { | ||
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => | ||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) | ||
) | ||
}*/ | ||
|
||
function uniqueId(): string { | ||
// TODO: This is almost certainly not a good way to generate IDs. | ||
return Math.random().toString(); | ||
} | ||
|
||
export class TelemetrySession { | ||
private sessionId: string; | ||
|
||
constructor() { | ||
this.sessionId = uniqueId(); | ||
} | ||
|
||
public recordStart() { | ||
// TODO: Post to the API endpoint | ||
} | ||
|
||
// TODO: Remove | ||
public getSessionId() { | ||
return this.sessionId; | ||
} | ||
} | ||
|
||
type TelemetryAction = ActionType<typeof telemetryActions>; | ||
|
||
export function createTelemetrySession(): TelemetrySession { | ||
return new TelemetrySession(); | ||
} | ||
|
||
export const telemetryMiddleware = (session: TelemetrySession) => () => (next: any) => (action: TelemetryAction) => { | ||
switch (action.type) { | ||
case TelemetryActionType.START: { | ||
session.recordStart(); | ||
break; | ||
} | ||
} | ||
} |