-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add currentPdt getter and getStartDate() method (#661)
- Loading branch information
Showing
10 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { PlaybackEngine } from './types'; | ||
|
||
type MediaWithPDT = HTMLMediaElement & { getStartDate?: () => Date }; | ||
|
||
export function getStartDate(mediaEl: MediaWithPDT, hls: PlaybackEngine | undefined) { | ||
if (hls) { | ||
const playingDate = hls.playingDate; | ||
|
||
if (playingDate != null) { | ||
// If the video is very long and the currentTime will transition day boundaries, | ||
// this may end up not being accurate | ||
return new Date(playingDate.getTime() - mediaEl.currentTime * 1000); | ||
} | ||
} | ||
|
||
if (typeof mediaEl.getStartDate === 'function') { | ||
return mediaEl.getStartDate(); | ||
} | ||
|
||
return new Date(NaN); | ||
} | ||
|
||
export function getCurrentPdt(mediaEl: MediaWithPDT, hls: PlaybackEngine | undefined) { | ||
if (hls && hls.playingDate) { | ||
return hls.playingDate; | ||
} | ||
|
||
if (typeof mediaEl.getStartDate === 'function') { | ||
const startDate = mediaEl.getStartDate(); | ||
|
||
// If the video is very long and the currentTime will transition day boundaries, | ||
// this may end up not being accurate | ||
return new Date(startDate.getTime() + mediaEl.currentTime * 1000); | ||
} | ||
|
||
return new Date(NaN); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { assert, aTimeout } from '@open-wc/testing'; | ||
import { getStartDate, getCurrentPdt } from '../src/pdt'; | ||
|
||
describe('getCurrentPdt', function () { | ||
describe('with hls.js', function () { | ||
it('will return playingDate, if not null', function () { | ||
const time = Date.now(); | ||
const currentPdt = getCurrentPdt( | ||
{}, | ||
{ | ||
playingDate: new Date(time), | ||
} | ||
); | ||
|
||
assert.equal(currentPdt.getTime(), time, 'the returned date matches the expected date'); | ||
}); | ||
|
||
it('will return an invalid date if playingDate is null', function () { | ||
const currentPdt = getCurrentPdt( | ||
{}, | ||
{ | ||
playingDate: null, | ||
} | ||
); | ||
|
||
assert.isNaN(currentPdt.getTime(), "the currentPdt's getTime() is NaN, which is an invalid time"); | ||
}); | ||
}); | ||
|
||
describe('with native video', function () { | ||
it('will return invalid date if no getStartDate method', function () { | ||
const currentPdt = getCurrentPdt({}, {}); | ||
|
||
assert.isNaN(currentPdt.getTime(), "the currentPdt's getTime() is NaN, which is an invalid time"); | ||
}); | ||
|
||
it('will return getStartDate plus currentTime', function () { | ||
const time = Date.now(); | ||
let currentPdt = getCurrentPdt( | ||
{ | ||
currentTime: 0, | ||
getStartDate() { | ||
return new Date(time); | ||
}, | ||
}, | ||
{} | ||
); | ||
|
||
assert.equal( | ||
currentPdt.getTime(), | ||
time, | ||
'with a currentTime of 0, getCurrentPdt is equivalent to getStartDate time' | ||
); | ||
|
||
currentPdt = getCurrentPdt( | ||
{ | ||
currentTime: 60, | ||
getStartDate() { | ||
return new Date(time); | ||
}, | ||
}, | ||
{} | ||
); | ||
|
||
assert.equal( | ||
currentPdt.getTime(), | ||
time + 60 * 1000, | ||
'with a currentTime of 0, getCurrentPdt is equivalent to getStartDate time plus 60 seconds' | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getStartDate', function () { | ||
describe('with hls.js', function () { | ||
it('will return a Date that is currentTime before playingDate', function () { | ||
const time = Date.now(); | ||
let startDate = getStartDate( | ||
{ | ||
currentTime: 0, | ||
}, | ||
{ | ||
playingDate: new Date(time), | ||
} | ||
); | ||
|
||
assert.equal( | ||
startDate.getTime(), | ||
time, | ||
'with a currentTime of zero, the returned date is equivalent to playingDate' | ||
); | ||
|
||
startDate = getStartDate( | ||
{ | ||
currentTime: 60, | ||
}, | ||
{ | ||
playingDate: new Date(time), | ||
} | ||
); | ||
|
||
assert.equal(startDate.getTime(), time - 60 * 1000, 'the returned date should be 60 seconds before playingDate'); | ||
}); | ||
|
||
it('will return an invalid Date if playingDate is null', function () { | ||
const startDate = getStartDate( | ||
{ | ||
currentTime: 60, | ||
}, | ||
{ | ||
playingDate: null, | ||
} | ||
); | ||
|
||
assert.isNaN(startDate.getTime(), 'NaN is an invalid date, when playingDate is null'); | ||
}); | ||
}); | ||
|
||
describe('with native video', function () { | ||
it('will return invalid date if no getStartDate method', function () { | ||
const currentPdt = getCurrentPdt({}, {}); | ||
|
||
assert.isNaN(currentPdt.getTime(), "the currentPdt's getTime() is NaN, which is an invalid time"); | ||
}); | ||
|
||
it('will return getStartDate', function () { | ||
const time = Date.now(); | ||
const startDate = getStartDate( | ||
{ | ||
currentTime: 60, | ||
getStartDate() { | ||
return new Date(time); | ||
}, | ||
}, | ||
{} | ||
); | ||
|
||
assert.equal( | ||
startDate.getTime(), | ||
time, | ||
'getCurrentPdt is equivalent to getStartDate time, regardless of currentTime' | ||
); | ||
}); | ||
}); | ||
}); |
530170b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
elements-demo-create-react-app – ./examples/create-react-app-with-typescript
elements-demo-create-react-app.vercel.app
elements-demo-create-react-app-git-main-mux.vercel.app
elements-demo-create-react-app-mux.vercel.app
530170b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
elements-demo-svelte-kit – ./examples/svelte-kit
elements-demo-svelte-kit.vercel.app
elements-demo-svelte-kit-git-main-mux.vercel.app
elements-demo-svelte-kit-mux.vercel.app
530170b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
elements-demo-vanilla – ./examples/vanilla-ts-esm
elements-demo-vanilla-git-main-mux.vercel.app
elements-demo-vanilla-mux.vercel.app
elements-demo-vanilla.vercel.app
530170b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
elements-demo-vue – ./examples/vue-with-typescript
elements-demo-vue.vercel.app
elements-demo-vue-mux.vercel.app
elements-demo-vue-git-main-mux.vercel.app
530170b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
elements-demo-nextjs – ./examples/nextjs-with-typescript
elements-demo-nextjs-mux.vercel.app
elements-demo-nextjs.vercel.app
elements-demo-nextjs-git-main-mux.vercel.app