Skip to content

Commit 6f4a06a

Browse files
authored
Merge pull request #55 from diffusionstudio/konstantin/fix/remove-listeners-from-detached-clip
Konstantin/fix/remove listeners from detached clip
2 parents 4993c5d + a9110d7 commit 6f4a06a

File tree

14 files changed

+87
-17
lines changed

14 files changed

+87
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@diffusionstudio/core",
33
"private": false,
4-
"version": "1.4.1",
4+
"version": "1.4.3",
55
"type": "module",
66
"description": "Build bleeding edge video processing applications",
77
"files": [

src/clips/clip/clip.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,40 @@ describe('The Clip Object', () => {
8484
expect(clip.track?.id).toBe(track.id);
8585
});
8686

87+
it('should remove event listeners on detach', async () => {
88+
const clip0 = new Clip({ start: 0, stop: 20 });
89+
const clip1 = new Clip({ stop: 60, start: 30 });
90+
91+
const composition = new Composition();
92+
const track0 = composition.createTrack('base');
93+
const track1 = composition.createTrack('base');
94+
95+
await track0.add(clip0);
96+
await track0.add(clip1);
97+
98+
expect(track0.clips.length).toBe(2);
99+
expect(track0.clips[0].start.frames).toBe(0);
100+
expect(track0.clips[0].stop.frames).toBe(20);
101+
102+
expect(track0.clips[1].start.frames).toBe(30);
103+
expect(track0.clips[1].stop.frames).toBe(60);
104+
105+
clip1.detach();
106+
107+
expect(track0.clips.length).toBe(1);
108+
109+
await track1.add(clip1);
110+
111+
expect(track1.clips.length).toBe(1);
112+
expect(track1.clips[0].start.frames).toBe(30);
113+
expect(track1.clips[0].stop.frames).toBe(60);
114+
115+
clip1.start = 10;
116+
117+
expect(track1.clips[0].start.frames).toBe(10);
118+
expect(track1.clips[0].stop.frames).toBe(60);
119+
});
120+
87121
it('should be removable from the track', async () => {
88122
const clip1 = new Clip({ stop: 900, start: 570 });
89123

src/composition/composition.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ export class Composition extends EventEmitterMixin<CompositionEvents, typeof Ser
490490
if (index != undefined && index >= 0) {
491491
this.tracks.splice(index, 1);
492492
this.trigger('detach', undefined);
493+
track.off('*');
493494
return track;
494495
}
495496
}

src/mixins/event.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ export function EventEmitterMixin<Events = {}, T extends Constructor = Construct
3636
return id;
3737
}
3838

39-
public off(id?: string, ...ids: string[]) {
39+
public off(id?: string | '*', ...ids: string[]) {
4040
if (!id) return;
4141

42+
if (id === '*') {
43+
this._handlers = {};
44+
return;
45+
}
46+
4247
for (const obj of Object.values(this._handlers)) {
4348
if (id in obj) {
4449
delete obj[id];

src/tracks/caption/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export * from './preset.interface';
1515
export * from './preset.solar';
1616
export * from './preset.whisper';
1717
export * from './preset.verdant';
18+
export * from './preset.types';

src/tracks/caption/preset.cascade.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { GeneratorOptions } from '../../models';
1414
import type { DefaultCaptionPresetConfig } from './preset.types';
1515
import type { CaptionTrack } from './caption';
1616
import type { CaptionPresetStrategy } from './preset.interface';
17+
import type { Position } from '../../types';
1718

1819
export class CascadeCaptionPreset extends Serializer implements CaptionPresetStrategy {
1920
@serializable()
@@ -22,10 +23,14 @@ export class CascadeCaptionPreset extends Serializer implements CaptionPresetStr
2223
@serializable()
2324
public readonly type = 'CASCADE';
2425

26+
@serializable()
27+
public position: Position;
28+
2529
public constructor(config: Partial<DefaultCaptionPresetConfig> = {}) {
2630
super();
2731

2832
this.generatorOptions = config.generatorOptions ?? { duration: [1.4] };
33+
this.position = config.position ?? { x: '12%', y: '44%' };
2934
}
3035

3136
public async applyTo(track: CaptionTrack): Promise<void> {
@@ -70,10 +75,7 @@ export class CascadeCaptionPreset extends Serializer implements CaptionPresetStr
7075
angle: Math.PI / 4,
7176
distance: 2,
7277
},
73-
position: {
74-
x: '12%',
75-
y: '44%',
76-
},
78+
position: this.position,
7779
stop: sequence.words[i].stop.add(offset),
7880
start: sequence.words[i].start.add(offset),
7981
})

src/tracks/caption/preset.classic.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { CaptionPresetType, DefaultCaptionPresetConfig } from './preset.typ
1414
import type { GeneratorOptions } from '../../models';
1515
import type { CaptionTrack } from './caption';
1616
import type { CaptionPresetStrategy } from './preset.interface';
17+
import type { Position } from '../../types';
1718

1819
export class ClassicCaptionPreset extends Serializer implements CaptionPresetStrategy {
1920
@serializable()
@@ -22,11 +23,14 @@ export class ClassicCaptionPreset extends Serializer implements CaptionPresetStr
2223
@serializable()
2324
public readonly type: CaptionPresetType = 'CLASSIC';
2425

26+
@serializable()
27+
public position: Position;
2528

2629
public constructor(config: Partial<DefaultCaptionPresetConfig> = {}) {
2730
super();
2831

2932
this.generatorOptions = config.generatorOptions ?? { duration: [0.2] };
33+
this.position = config.position ?? { x: '50%', y: '50%' };
3034
}
3135

3236
public async applyTo(track: CaptionTrack): Promise<void> {
@@ -63,7 +67,7 @@ export class ClassicCaptionPreset extends Serializer implements CaptionPresetStr
6367
angle: Math.PI * 0.40,
6468
alpha: 1,
6569
},
66-
position: 'center',
70+
position: this.position,
6771
stop: sequence.stop.add(offset),
6872
start: sequence.start.add(offset),
6973
scale: new Keyframe([0, 8], [0.96, 1], { easing: 'easeOut' }),

src/tracks/caption/preset.guinea.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type { MultiColorCaptionPresetConfig, CaptionPresetType } from './preset.
1515
import type { CaptionPresetStrategy } from './preset.interface';
1616
import type { WordGroup, Word } from '../../models';
1717
import type { CaptionTrack } from './caption';
18-
import type { hex } from '../../types';
18+
import type { hex, Position } from '../../types';
1919

2020
export class GuineaCaptionPreset extends Serializer implements CaptionPresetStrategy {
2121
@serializable()
@@ -24,10 +24,14 @@ export class GuineaCaptionPreset extends Serializer implements CaptionPresetStra
2424
@serializable()
2525
public colors: hex[];
2626

27+
@serializable()
28+
public position: Position;
29+
2730
public constructor(config: Partial<MultiColorCaptionPresetConfig> = {}) {
2831
super();
2932

3033
this.colors = config.colors ?? ['#1BD724', '#FFEE0C', '#FF2E17'];
34+
this.position = config.position ?? { x: '50%', y: '50%' };
3135
}
3236

3337
public async applyTo(track: CaptionTrack): Promise<void> {
@@ -73,7 +77,7 @@ export class GuineaCaptionPreset extends Serializer implements CaptionPresetStra
7377
leading: 1.3,
7478
font,
7579
textCase: 'upper',
76-
position: 'center',
80+
position: this.position,
7781
stop: stop.add(offset),
7882
start: start.add(offset),
7983
styles: [

src/tracks/caption/preset.solar.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { CaptionPresetType, DefaultCaptionPresetConfig } from './preset.typ
1515
import type { CaptionTrack } from './caption';
1616
import type { GeneratorOptions } from '../../models';
1717
import type { CaptionPresetStrategy } from './preset.interface';
18+
import type { Position } from '../../types';
1819

1920
export class SolarCaptionPreset extends Serializer implements CaptionPresetStrategy {
2021
@serializable()
@@ -23,10 +24,14 @@ export class SolarCaptionPreset extends Serializer implements CaptionPresetStrat
2324
@serializable()
2425
public readonly type: CaptionPresetType = 'SOLAR';
2526

27+
@serializable()
28+
public position: Position;
29+
2630
public constructor(config: Partial<DefaultCaptionPresetConfig> = {}) {
2731
super();
2832

2933
this.generatorOptions = config.generatorOptions ?? { duration: [0.2] };
34+
this.position = config.position ?? { x: '50%', y: '50%' };
3035
}
3136

3237
public async applyTo(track: CaptionTrack): Promise<void> {
@@ -65,7 +70,7 @@ export class SolarCaptionPreset extends Serializer implements CaptionPresetStrat
6570
angle: Math.PI / 2.5,
6671
alpha: 1,
6772
},
68-
position: 'center',
73+
position: this.position,
6974
stop: sequence.stop.add(offset),
7075
start: sequence.start.add(offset),
7176
scale: new Keyframe([0, 8], [0.96, 1], { easing: 'easeOut' }),

src/tracks/caption/preset.spotlight.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { SingleColorCaptionPresetConfig } from './preset.types';
1414
import type { CaptionPresetStrategy } from './preset.interface';
1515
import type { GeneratorOptions } from '../../models';
1616
import type { CaptionTrack } from './caption';
17-
import type { hex } from '../../types';
17+
import type { hex, Position } from '../../types';
1818

1919
export class SpotlightCaptionPreset extends Serializer implements CaptionPresetStrategy {
2020
@serializable()
@@ -26,11 +26,15 @@ export class SpotlightCaptionPreset extends Serializer implements CaptionPresetS
2626
@serializable()
2727
public color: hex;
2828

29+
@serializable()
30+
public position: Position;
31+
2932
public constructor(config: Partial<SingleColorCaptionPresetConfig> = {}) {
3033
super();
3134

3235
this.generatorOptions = config.generatorOptions ?? { duration: [0.2] };
3336
this.color = config.color ?? '#00FF4C';
37+
this.position = config.position ?? { x: '50%', y: '50%' };
3438
}
3539

3640
public async applyTo(track: CaptionTrack): Promise<void> {
@@ -69,7 +73,7 @@ export class SpotlightCaptionPreset extends Serializer implements CaptionPresetS
6973
angle: Math.PI / 4,
7074
distance: 2,
7175
},
72-
position: 'center',
76+
position: this.position,
7377
styles: [{
7478
fillStyle: this.color,
7579
}],

0 commit comments

Comments
 (0)