Unignored dist/ so yarn/npm can install from git URL without needing to build locally. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
195 lines
4.3 KiB
JavaScript
195 lines
4.3 KiB
JavaScript
import { generateEventId } from './config';
|
|
/**
|
|
* Represents an active journey tracking session
|
|
*/
|
|
export class Journey {
|
|
constructor(name) {
|
|
this.metadata = {};
|
|
this.steps = [];
|
|
this._completed = false;
|
|
this._failed = false;
|
|
this.id = generateEventId();
|
|
this.name = name;
|
|
this.startedAt = new Date();
|
|
}
|
|
/**
|
|
* Set user context for this journey
|
|
*/
|
|
setUser(id, email, data) {
|
|
this.user = { id, email, data };
|
|
return this;
|
|
}
|
|
/**
|
|
* Set metadata for this journey
|
|
*/
|
|
setMetadata(key, value) {
|
|
this.metadata[key] = value;
|
|
return this;
|
|
}
|
|
/**
|
|
* Start a new step in this journey
|
|
*/
|
|
startStep(name, category) {
|
|
// Complete any existing step
|
|
if (this.currentStep && this.currentStep.status === 'in_progress') {
|
|
this.currentStep.status = 'completed';
|
|
this.currentStep.endedAt = new Date();
|
|
}
|
|
const step = {
|
|
name,
|
|
category,
|
|
startedAt: new Date(),
|
|
status: 'in_progress',
|
|
data: {},
|
|
};
|
|
this.steps.push(step);
|
|
this.currentStep = step;
|
|
return new Step(step, this);
|
|
}
|
|
/**
|
|
* Mark the journey as completed
|
|
*/
|
|
complete() {
|
|
if (this.currentStep && this.currentStep.status === 'in_progress') {
|
|
this.currentStep.status = 'completed';
|
|
this.currentStep.endedAt = new Date();
|
|
}
|
|
this._completed = true;
|
|
}
|
|
/**
|
|
* Mark the journey as failed
|
|
*/
|
|
fail() {
|
|
if (this.currentStep && this.currentStep.status === 'in_progress') {
|
|
this.currentStep.status = 'failed';
|
|
this.currentStep.endedAt = new Date();
|
|
}
|
|
this._failed = true;
|
|
}
|
|
/**
|
|
* Get the journey context for an event
|
|
*/
|
|
getContext() {
|
|
return {
|
|
journeyId: this.id,
|
|
name: this.name,
|
|
currentStep: this.currentStep?.name,
|
|
startedAt: this.startedAt,
|
|
metadata: this.metadata,
|
|
};
|
|
}
|
|
/**
|
|
* Get the user context for this journey
|
|
*/
|
|
getUser() {
|
|
return this.user;
|
|
}
|
|
/**
|
|
* Check if the journey is complete
|
|
*/
|
|
get isComplete() {
|
|
return this._completed || this._failed;
|
|
}
|
|
/**
|
|
* Get journey ID
|
|
*/
|
|
get journeyId() {
|
|
return this.id;
|
|
}
|
|
}
|
|
/**
|
|
* Represents a step within a journey
|
|
*/
|
|
export class Step {
|
|
constructor(step, journey) {
|
|
this.step = step;
|
|
this.journey = journey;
|
|
}
|
|
/**
|
|
* Set data for this step
|
|
*/
|
|
setData(key, value) {
|
|
this.step.data[key] = value;
|
|
return this;
|
|
}
|
|
/**
|
|
* Mark the step as completed
|
|
*/
|
|
complete() {
|
|
this.step.status = 'completed';
|
|
this.step.endedAt = new Date();
|
|
}
|
|
/**
|
|
* Mark the step as failed
|
|
*/
|
|
fail() {
|
|
this.step.status = 'failed';
|
|
this.step.endedAt = new Date();
|
|
}
|
|
/**
|
|
* Get the step name
|
|
*/
|
|
get name() {
|
|
return this.step.name;
|
|
}
|
|
/**
|
|
* Get the parent journey
|
|
*/
|
|
getJourney() {
|
|
return this.journey;
|
|
}
|
|
}
|
|
/**
|
|
* Journey scope that auto-completes on disposal
|
|
*/
|
|
export class JourneyScope {
|
|
constructor(journey, onComplete) {
|
|
this.journey = journey;
|
|
this.onComplete = onComplete;
|
|
}
|
|
/**
|
|
* Get the underlying journey
|
|
*/
|
|
getJourney() {
|
|
return this.journey;
|
|
}
|
|
/**
|
|
* Dispose of the journey scope
|
|
*/
|
|
[Symbol.dispose]() {
|
|
if (!this.journey.isComplete) {
|
|
this.journey.complete();
|
|
}
|
|
this.onComplete?.();
|
|
}
|
|
}
|
|
/**
|
|
* Step scope that auto-completes on disposal
|
|
*/
|
|
export class StepScope {
|
|
constructor(step) {
|
|
this.step = step;
|
|
}
|
|
/**
|
|
* Get the underlying step
|
|
*/
|
|
getStep() {
|
|
return this.step;
|
|
}
|
|
/**
|
|
* Set data on the step
|
|
*/
|
|
setData(key, value) {
|
|
this.step.setData(key, value);
|
|
return this;
|
|
}
|
|
/**
|
|
* Dispose of the step scope
|
|
*/
|
|
[Symbol.dispose]() {
|
|
if (this.step['step'].status === 'in_progress') {
|
|
this.step.complete();
|
|
}
|
|
}
|
|
}
|
|
//# sourceMappingURL=journey.js.map
|