Skip to content

Commit 733ca08

Browse files
committed
Pausing
1 parent af32f92 commit 733ca08

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

injected/src/features/duck-player-native.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class DuckPlayerNativeFeature extends ContentFeature {
1616
/** @type {DuckPlayerNative} */
1717
current;
1818

19-
init(args) {
19+
async init(args) {
2020
console.log('DUCK PLAYER NATIVE LOADING', args);
2121

2222
// TODO: May depend on page type
@@ -49,6 +49,20 @@ export class DuckPlayerNativeFeature extends ContentFeature {
4949
const comms = new DuckPlayerNativeMessages(this.messaging);
5050
const settings = { selectors };
5151

52+
53+
/** @type {InitialSettings} */
54+
let initialSetup;
55+
56+
// TODO: This seems to get initted twice. Check with Daniel
57+
try {
58+
initialSetup = await comms.initialSetup();
59+
} catch (e) {
60+
console.error(e);
61+
return;
62+
}
63+
64+
console.log('INITIAL SETUP', initialSetup);
65+
5266
comms.subscribeToURLChange(({ pageType }) => {
5367
console.log('GOT PAGE TYPE', pageType);
5468
let next;
@@ -77,9 +91,6 @@ export class DuckPlayerNativeFeature extends ContentFeature {
7791
this.current = next;
7892
}
7993
});
80-
81-
/** Fire onReady event */
82-
comms.notifyFeatureIsReady();
8394
}
8495
}
8596

injected/src/features/duckplayer-native/duckplayer-native.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,7 @@ export class DuckPlayerNative {
7676
});
7777
}
7878

79-
async init() {
80-
/** @type {InitialSettings} */
81-
let initialSetup;
82-
83-
// TODO: This seems to get initted twice. Check with Daniel
84-
try {
85-
initialSetup = await this.messages.initialSetup();
86-
} catch (e) {
87-
this.logger.error(e);
88-
return;
89-
}
90-
91-
this.logger.log('INITIAL SETUP', initialSetup);
92-
79+
init() {
9380
this.logger.log('Running init handlers');
9481
this.onInit(this.sideEffects, this.logger);
9582

@@ -108,6 +95,8 @@ export class DuckPlayerNative {
10895
this.logger.log('Running load handlers immediately');
10996
this.onLoad(this.sideEffects, this.logger);
11097
}
98+
99+
this.messages.notifyFeatureIsReady();
111100
}
112101
}
113102

@@ -140,12 +129,19 @@ export function setupDuckPlayerForYouTube(settings, environment, messages) {
140129

141130
const targetElement = document.querySelector(videoElementContainer);
142131
if (targetElement) {
143-
sideEffects.add('stopping video from playing', () => stopVideoFromPlaying(videoElement));
144-
sideEffects.add('appending thumbnail', () =>
145-
appendThumbnailOverlay(/** @type {HTMLElement} */ (targetElement), this.environment),
146-
);
132+
133+
if (pause) {
134+
sideEffects.add('stopping video from playing', () => stopVideoFromPlaying(videoElement));
135+
sideEffects.add('appending thumbnail', () =>
136+
appendThumbnailOverlay(/** @type {HTMLElement} */ (targetElement), environment),
137+
);
138+
} else {
139+
sideEffects.destroy('stopping video from playing');
140+
sideEffects.destroy('appending thumbnail');
141+
}
147142
}
148143
});
144+
149145
messages.subscribeToMuteAudio(({ mute }) => {
150146
logger.log('Running mute audio handler. Mute:', mute);
151147
muteAudio(mute);

injected/src/features/duckplayer-native/mock-transport.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class TestTransport {
7171
console.log('PAGE TYPE', response);
7272
timeout = 100;
7373
break;
74-
}
74+
}
7575

7676
const callbackTimeout = setTimeout(() => {
7777
logger.log('Calling handler for', _msg.subscriptionName);

injected/src/features/duckplayer-native/util.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ export class SideEffects {
118118

119119
/**
120120
* Remove elements, event listeners etc
121+
* @param {string} [name]
121122
*/
122-
destroy() {
123-
for (const cleanup of this._cleanups) {
123+
destroy(name) {
124+
const cleanups = name ? this._cleanups.filter(c => c.name === name) : this._cleanups;
125+
for (const cleanup of cleanups) {
124126
if (typeof cleanup.fn === 'function') {
125127
try {
126128
if (this.debug) {
@@ -134,7 +136,11 @@ export class SideEffects {
134136
throw new Error('invalid cleanup');
135137
}
136138
}
137-
this._cleanups = [];
139+
if (name) {
140+
this._cleanups = this._cleanups.filter(c => c.name !== name);
141+
} else {
142+
this._cleanups = [];
143+
}
138144
}
139145
}
140146

0 commit comments

Comments
 (0)