Skip to content

feat: add end method to flush policies for cleanup #753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ describe('CountFlushPolicy', () => {
it('triggers a flush when reaching limit', () => {
const policy = new CountFlushPolicy(3);

const observer = jest.fn().mockImplementation((value) => {
console.log(`new value: ${value}`);
});
const observer = jest.fn();

policy.shouldFlush.onChange(observer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ describe('TimerFlushPolicy', () => {
const policy = new TimerFlushPolicy(time);
policy.start();

const observer = jest.fn().mockImplementation((value) => {
console.log(`new value: ${value}`);
});
const observer = jest.fn();

policy.shouldFlush.onChange(observer);

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/flushPolicies/flush-policy-executer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class FlushPolicyExecuter {
}

remove(policy: FlushPolicy) {
policy.end();
let i = this.policies.findIndex((p) => p === policy);
return this.removeIndex(i);
}
Expand Down Expand Up @@ -80,6 +81,9 @@ export class FlushPolicyExecuter {
unsubscribe();
}
}
for (const policy of this.policies) {
policy.end();
}
}

private startPolicy(policy: FlushPolicy) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/flushPolicies/timer-flush-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class TimerFlushPolicy extends FlushPolicyBase {
this.startTimer();
}

end(): void {
clearTimeout(this.flushTimeout);
}

onEvent(_event: SegmentEvent): void {
// Reset interval
this.startTimer();
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/flushPolicies/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export interface FlushPolicy {
* Called when the flush has been completed.
*/
reset(): void;

/**
* Ends the execution of the flush policy.
* All cleanup methods should be contained here
*/
end(): void;
}

/**
Expand All @@ -77,6 +83,10 @@ export abstract class FlushPolicyBase implements FlushPolicy {
this.shouldFlush.value = false;
}

end(): void {
// Nothing to cleanup
}

abstract start(): void;

abstract onEvent(event: SegmentEvent): void;
Expand Down