Skip to content

Commit 96b0893

Browse files
authored
feat: add end method to flush policies for cleanup (#753)
1 parent 6ebd65e commit 96b0893

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

packages/core/src/flushPolicies/__tests__/count-flush-policy.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ describe('CountFlushPolicy', () => {
55
it('triggers a flush when reaching limit', () => {
66
const policy = new CountFlushPolicy(3);
77

8-
const observer = jest.fn().mockImplementation((value) => {
9-
console.log(`new value: ${value}`);
10-
});
8+
const observer = jest.fn();
119

1210
policy.shouldFlush.onChange(observer);
1311

packages/core/src/flushPolicies/__tests__/timer-flush-policy.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ describe('TimerFlushPolicy', () => {
1010
const policy = new TimerFlushPolicy(time);
1111
policy.start();
1212

13-
const observer = jest.fn().mockImplementation((value) => {
14-
console.log(`new value: ${value}`);
15-
});
13+
const observer = jest.fn();
1614

1715
policy.shouldFlush.onChange(observer);
1816

packages/core/src/flushPolicies/flush-policy-executer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class FlushPolicyExecuter {
2323
}
2424

2525
remove(policy: FlushPolicy) {
26+
policy.end();
2627
let i = this.policies.findIndex((p) => p === policy);
2728
return this.removeIndex(i);
2829
}
@@ -80,6 +81,9 @@ export class FlushPolicyExecuter {
8081
unsubscribe();
8182
}
8283
}
84+
for (const policy of this.policies) {
85+
policy.end();
86+
}
8387
}
8488

8589
private startPolicy(policy: FlushPolicy) {

packages/core/src/flushPolicies/timer-flush-policy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export class TimerFlushPolicy extends FlushPolicyBase {
2929
this.startTimer();
3030
}
3131

32+
end(): void {
33+
clearTimeout(this.flushTimeout);
34+
}
35+
3236
onEvent(_event: SegmentEvent): void {
3337
// Reset interval
3438
this.startTimer();

packages/core/src/flushPolicies/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ export interface FlushPolicy {
6363
* Called when the flush has been completed.
6464
*/
6565
reset(): void;
66+
67+
/**
68+
* Ends the execution of the flush policy.
69+
* All cleanup methods should be contained here
70+
*/
71+
end(): void;
6672
}
6773

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

86+
end(): void {
87+
// Nothing to cleanup
88+
}
89+
8090
abstract start(): void;
8191

8292
abstract onEvent(event: SegmentEvent): void;

0 commit comments

Comments
 (0)