Skip to content

Commit 1e15d02

Browse files
committed
feat: avoid babel-runtime
1 parent 34afbd6 commit 1e15d02

File tree

5 files changed

+102
-78
lines changed

5 files changed

+102
-78
lines changed

.babelrc

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
{
2-
"plugins": [
3-
"transform-object-rest-spread",
4-
"transform-flow-strip-types",
5-
"transform-runtime",
6-
"add-module-exports"
7-
],
82
"presets": [
93
[
104
"env",
115
{
126
"targets": {
13-
"browsers": [
14-
"last 2 versions"
15-
]
7+
"node": "current"
168
},
179
"useBuiltIns": true
1810
}

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
]
1212
},
1313
"dependencies": {
14-
"babel-runtime": "^6.23.0",
1514
"debug": "^2.6.6",
1615
"load-script": "^1.0.0",
1716
"sister": "^3.0.0"
@@ -23,7 +22,6 @@
2322
"babel-plugin-add-module-exports": "^0.2.1",
2423
"babel-plugin-transform-flow-strip-types": "^6.22.0",
2524
"babel-plugin-transform-object-rest-spread": "^6.23.0",
26-
"babel-plugin-transform-runtime": "^6.23.0",
2725
"babel-preset-env": "1.4.0",
2826
"babel-register": "^6.24.1",
2927
"chai": "^3.5.0",

src/.babelrc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"plugins": [
3+
"transform-object-rest-spread",
4+
"transform-flow-strip-types",
5+
"add-module-exports"
6+
],
7+
"presets": [
8+
[
9+
"env",
10+
{
11+
"debug": true,
12+
"targets": {
13+
"browsers": [
14+
"last 2 version",
15+
"not last 2 ie version",
16+
">5%"
17+
]
18+
},
19+
"useBuiltIns": true
20+
}
21+
]
22+
]
23+
}

src/YouTubePlayer.js

+68-61
Original file line numberDiff line numberDiff line change
@@ -55,71 +55,78 @@ YouTubePlayer.promisifyPlayer = (playerAPIReady: Promise<YouTubePlayerType>, str
5555

5656
for (const functionName of functionNames) {
5757
if (strictState && FunctionStateMap[functionName]) {
58-
functions[functionName] = async (...args) => {
59-
const stateInfo = FunctionStateMap[functionName];
60-
const player = await playerAPIReady;
61-
const playerState = player.getPlayerState();
62-
63-
// eslint-disable-next-line no-warning-comments
64-
// TODO: Just spread the args into the function once Babel is fixed:
65-
// https://github.com/babel/babel/issues/4270
66-
//
67-
// eslint-disable-next-line prefer-spread
68-
const value = player[functionName].apply(player, args);
69-
70-
// TRICKY: For functions like `seekTo`, a change in state must be
71-
// triggered given that the resulting state could match the initial
72-
// state.
73-
if (
74-
stateInfo.stateChangeRequired ||
75-
76-
// eslint-disable-next-line no-extra-parens
77-
(
78-
Array.isArray(stateInfo.acceptableStates) &&
79-
stateInfo.acceptableStates.indexOf(playerState) === -1
80-
)
81-
) {
82-
await new Promise((resolve) => {
83-
const onPlayerStateChange = () => {
84-
const playerStateAfterChange = player.getPlayerState();
85-
86-
let timeout;
87-
88-
if (typeof stateInfo.timeout === 'number') {
89-
timeout = setTimeout(() => {
90-
player.removeEventListener('onStateChange', onPlayerStateChange);
91-
92-
resolve();
93-
}, stateInfo.timeout);
94-
}
95-
96-
if (
58+
functions[functionName] = (...args) => {
59+
return playerAPIReady
60+
.then((player) => {
61+
const stateInfo = FunctionStateMap[functionName];
62+
const playerState = player.getPlayerState();
63+
64+
// eslint-disable-next-line no-warning-comments
65+
// TODO: Just spread the args into the function once Babel is fixed:
66+
// https://github.com/babel/babel/issues/4270
67+
//
68+
// eslint-disable-next-line prefer-spread
69+
const value = player[functionName].apply(player, args);
70+
71+
// TRICKY: For functions like `seekTo`, a change in state must be
72+
// triggered given that the resulting state could match the initial
73+
// state.
74+
if (
75+
stateInfo.stateChangeRequired ||
76+
77+
// eslint-disable-next-line no-extra-parens
78+
(
9779
Array.isArray(stateInfo.acceptableStates) &&
98-
stateInfo.acceptableStates.indexOf(playerStateAfterChange) !== -1
99-
) {
100-
player.removeEventListener('onStateChange', onPlayerStateChange);
101-
102-
clearTimeout(timeout);
103-
resolve();
104-
}
105-
};
106-
107-
player.addEventListener('onStateChange', onPlayerStateChange);
80+
stateInfo.acceptableStates.indexOf(playerState) === -1
81+
)
82+
) {
83+
return new Promise((resolve) => {
84+
const onPlayerStateChange = () => {
85+
const playerStateAfterChange = player.getPlayerState();
86+
87+
let timeout;
88+
89+
if (typeof stateInfo.timeout === 'number') {
90+
timeout = setTimeout(() => {
91+
player.removeEventListener('onStateChange', onPlayerStateChange);
92+
93+
resolve();
94+
}, stateInfo.timeout);
95+
}
96+
97+
if (
98+
Array.isArray(stateInfo.acceptableStates) &&
99+
stateInfo.acceptableStates.indexOf(playerStateAfterChange) !== -1
100+
) {
101+
player.removeEventListener('onStateChange', onPlayerStateChange);
102+
103+
clearTimeout(timeout);
104+
105+
resolve();
106+
}
107+
};
108+
109+
player.addEventListener('onStateChange', onPlayerStateChange);
110+
})
111+
.then(() => {
112+
return value;
113+
});
114+
}
115+
116+
return value;
108117
});
109-
}
110-
111-
return value;
112118
};
113119
} else {
114-
functions[functionName] = async (...args) => {
115-
const player = await playerAPIReady;
116-
117-
// eslint-disable-next-line no-warning-comments
118-
// TODO: Just spread the args into the function once Babel is fixed:
119-
// https://github.com/babel/babel/issues/4270
120-
//
121-
// eslint-disable-next-line prefer-spread
122-
return player[functionName].apply(player, args);
120+
functions[functionName] = (...args) => {
121+
return playerAPIReady
122+
.then((player) => {
123+
// eslint-disable-next-line no-warning-comments
124+
// TODO: Just spread the args into the function once Babel is fixed:
125+
// https://github.com/babel/babel/issues/4270
126+
//
127+
// eslint-disable-next-line prefer-spread
128+
return player[functionName].apply(player, args);
129+
});
123130
};
124131
}
125132
}

src/index.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,19 @@ export default (maybeElementId: YouTubePlayerType | HTMLElement | string, option
5252

5353
options.events = YouTubePlayer.proxyEvents(emitter);
5454

55-
const playerAPIReady = new Promise(async (resolve: (result: YouTubePlayerType) => void) => {
55+
const playerAPIReady = new Promise((resolve: (result: YouTubePlayerType) => void) => {
5656
if (typeof maybeElementId === 'string' || maybeElementId instanceof HTMLElement) {
57-
const YT = await youtubeIframeAPI;
57+
// eslint-disable-next-line promise/catch-or-return
58+
youtubeIframeAPI
59+
.then((YT) => {
60+
const player: YouTubePlayerType = new YT.Player(maybeElementId, options);
5861

59-
const player: YouTubePlayerType = new YT.Player(maybeElementId, options);
62+
emitter.on('ready', () => {
63+
resolve(player);
64+
});
6065

61-
emitter.on('ready', () => {
62-
resolve(player);
63-
});
66+
return null;
67+
});
6468
} else if (typeof maybeElementId === 'object' && maybeElementId.playVideo instanceof Function) {
6569
const player: YouTubePlayerType = maybeElementId;
6670

0 commit comments

Comments
 (0)