-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
94 additions
and
65 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/signals/browser-signals/src/core/analytics-service/index.ts
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,26 @@ | ||
import { AnyAnalytics } from '../../types' | ||
import { EmitSignal } from '../emitter' | ||
|
||
/** | ||
* Helper / facade that wraps the analytics, and abstracts away the details of the analytics instance. | ||
*/ | ||
export class AnalyticsService { | ||
private instance: AnyAnalytics | ||
private signalEmitter: EmitSignal | ||
constructor(analyticsInstance: AnyAnalytics, signalEmitter: EmitSignal) { | ||
this.instance = analyticsInstance | ||
this.signalEmitter = signalEmitter | ||
} | ||
get writeKey() { | ||
return this.instance.settings.writeKey | ||
} | ||
addSegmentEventEmitter() { | ||
this.instance.addSourceMiddleware(({ payload, next }) => { | ||
this.signalEmitter.emit({ | ||
type: 'instrumentation', | ||
data: payload.obj, | ||
}) | ||
next(payload) | ||
}) | ||
} | ||
} |
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
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
19 changes: 19 additions & 0 deletions
19
packages/signals/browser-signals/src/core/signal-generators/register.ts
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,19 @@ | ||
import { isClass } from '../../utils/is-class' | ||
import { SignalEmitter } from '../emitter' | ||
import { SignalGeneratorClass, SignalGenerator } from './types' | ||
|
||
export const registerGenerator = ( | ||
emitter: SignalEmitter, | ||
signalGenerators: (SignalGeneratorClass | SignalGenerator)[] | ||
): VoidFunction => { | ||
const cleanupFns = signalGenerators.map((gen) => { | ||
if (isClass(gen)) { | ||
// Check if Gen is a function and has a constructor | ||
return new gen().register(emitter) | ||
} else { | ||
return gen.register(emitter) | ||
} | ||
}) | ||
// Return a cleanup function that calls all the cleanup functions (e.g unsubscribes from event listeners) | ||
return () => cleanupFns.forEach((fn) => fn()) | ||
} |
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
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