timingsrc 1.2.5 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/README.md +0 -1
  2. package/build/es2019/factories/compute-velocity.d.ts +2 -0
  3. package/build/es2019/factories/compute-velocity.d.ts.map +1 -0
  4. package/build/es2019/factories/compute-velocity.js +2 -0
  5. package/build/es2019/factories/compute-velocity.js.map +1 -0
  6. package/build/es2019/factories/default-set-timingsrc.d.ts +9 -2
  7. package/build/es2019/factories/default-set-timingsrc.d.ts.map +1 -1
  8. package/build/es2019/factories/default-set-timingsrc.js +6 -3
  9. package/build/es2019/factories/default-set-timingsrc.js.map +1 -1
  10. package/build/es2019/factories/set-current-time.d.ts +1 -2
  11. package/build/es2019/factories/set-current-time.d.ts.map +1 -1
  12. package/build/es2019/factories/set-current-time.js.map +1 -1
  13. package/build/es2019/factories/set-playback-rate.d.ts +1 -2
  14. package/build/es2019/factories/set-playback-rate.d.ts.map +1 -1
  15. package/build/es2019/factories/set-playback-rate.js.map +1 -1
  16. package/build/es2019/factories/set-timingsrc-with-custom-update-function.d.ts +6 -2
  17. package/build/es2019/factories/set-timingsrc-with-custom-update-function.d.ts.map +1 -1
  18. package/build/es2019/factories/set-timingsrc-with-custom-update-function.js +3 -1
  19. package/build/es2019/factories/set-timingsrc-with-custom-update-function.js.map +1 -1
  20. package/build/es2019/factories/set-timingsrc.d.ts +5 -2
  21. package/build/es2019/factories/set-timingsrc.d.ts.map +1 -1
  22. package/build/es2019/factories/set-timingsrc.js +1 -3
  23. package/build/es2019/factories/set-timingsrc.js.map +1 -1
  24. package/build/es2019/factories/update-gradually.d.ts +7 -2
  25. package/build/es2019/factories/update-gradually.d.ts.map +1 -1
  26. package/build/es2019/factories/update-gradually.js +18 -7
  27. package/build/es2019/factories/update-gradually.js.map +1 -1
  28. package/build/es2019/factories/update-media-element.d.ts +5 -2
  29. package/build/es2019/factories/update-media-element.d.ts.map +1 -1
  30. package/build/es2019/factories/update-media-element.js.map +1 -1
  31. package/build/es2019/factories/update-stepwise-factory.d.ts +12 -2
  32. package/build/es2019/factories/update-stepwise-factory.d.ts.map +1 -1
  33. package/build/es2019/factories/update-stepwise-factory.js +98 -25
  34. package/build/es2019/factories/update-stepwise-factory.js.map +1 -1
  35. package/build/es2019/factories/window.d.ts +1 -2
  36. package/build/es2019/factories/window.d.ts.map +1 -1
  37. package/build/es2019/factories/window.js.map +1 -1
  38. package/build/es2019/functions/determine-supported-playback-rate-values.d.ts +2 -1
  39. package/build/es2019/functions/determine-supported-playback-rate-values.d.ts.map +1 -1
  40. package/build/es2019/functions/determine-supported-playback-rate-values.js.map +1 -1
  41. package/build/es2019/functions/update-vector-with-new-position.d.ts +6 -0
  42. package/build/es2019/functions/update-vector-with-new-position.d.ts.map +1 -0
  43. package/build/es2019/functions/update-vector-with-new-position.js +5 -0
  44. package/build/es2019/functions/update-vector-with-new-position.js.map +1 -0
  45. package/build/es2019/module.d.ts +11 -3
  46. package/build/es2019/module.d.ts.map +1 -1
  47. package/build/es2019/module.js +3 -1
  48. package/build/es2019/module.js.map +1 -1
  49. package/build/es2019/types/index.d.ts +1 -17
  50. package/build/es2019/types/index.d.ts.map +1 -1
  51. package/build/es2019/types/index.js +1 -17
  52. package/build/es2019/types/index.js.map +1 -1
  53. package/build/es2019/types/update-function.d.ts +1 -1
  54. package/build/es2019/types/update-function.d.ts.map +1 -1
  55. package/build/es2019/types/update-vector-with-custom-state.d.ts +3 -0
  56. package/build/es2019/types/update-vector-with-custom-state.d.ts.map +1 -0
  57. package/build/es2019/types/update-vector-with-custom-state.js +2 -0
  58. package/build/es2019/types/update-vector-with-custom-state.js.map +1 -0
  59. package/build/es5/bundle.js +167 -63
  60. package/package.json +16 -16
  61. package/src/factories/compute-velocity.ts +2 -0
  62. package/src/factories/default-set-timingsrc.ts +30 -19
  63. package/src/factories/set-current-time.ts +2 -4
  64. package/src/factories/set-playback-rate.ts +2 -4
  65. package/src/factories/set-timingsrc-with-custom-update-function.ts +24 -9
  66. package/src/factories/set-timingsrc.ts +14 -4
  67. package/src/factories/update-gradually.ts +37 -8
  68. package/src/factories/update-media-element.ts +18 -3
  69. package/src/factories/update-stepwise-factory.ts +133 -38
  70. package/src/factories/window.ts +1 -3
  71. package/src/functions/determine-supported-playback-rate-values.ts +3 -1
  72. package/src/functions/update-vector-with-new-position.ts +9 -0
  73. package/src/module.ts +4 -0
  74. package/src/types/index.ts +1 -17
  75. package/src/types/update-function.ts +5 -1
  76. package/src/types/update-vector-with-custom-state.ts +5 -0
  77. package/build/es2019/types/default-timingsrc-factory.d.ts +0 -8
  78. package/build/es2019/types/default-timingsrc-factory.d.ts.map +0 -1
  79. package/build/es2019/types/default-timingsrc-factory.js +0 -2
  80. package/build/es2019/types/default-timingsrc-factory.js.map +0 -1
  81. package/build/es2019/types/determine-supported-playback-rate-values-function.d.ts +0 -2
  82. package/build/es2019/types/determine-supported-playback-rate-values-function.d.ts.map +0 -1
  83. package/build/es2019/types/determine-supported-playback-rate-values-function.js +0 -2
  84. package/build/es2019/types/determine-supported-playback-rate-values-function.js.map +0 -1
  85. package/build/es2019/types/pause-function.d.ts +0 -2
  86. package/build/es2019/types/pause-function.d.ts.map +0 -1
  87. package/build/es2019/types/pause-function.js +0 -2
  88. package/build/es2019/types/pause-function.js.map +0 -1
  89. package/build/es2019/types/play-function.d.ts +0 -2
  90. package/build/es2019/types/play-function.d.ts.map +0 -1
  91. package/build/es2019/types/play-function.js +0 -2
  92. package/build/es2019/types/play-function.js.map +0 -1
  93. package/build/es2019/types/set-current-time-factory.d.ts +0 -3
  94. package/build/es2019/types/set-current-time-factory.d.ts.map +0 -1
  95. package/build/es2019/types/set-current-time-factory.js +0 -2
  96. package/build/es2019/types/set-current-time-factory.js.map +0 -1
  97. package/build/es2019/types/set-current-time-function.d.ts +0 -2
  98. package/build/es2019/types/set-current-time-function.d.ts.map +0 -1
  99. package/build/es2019/types/set-current-time-function.js +0 -2
  100. package/build/es2019/types/set-current-time-function.js.map +0 -1
  101. package/build/es2019/types/set-playback-rate-factory.d.ts +0 -3
  102. package/build/es2019/types/set-playback-rate-factory.d.ts.map +0 -1
  103. package/build/es2019/types/set-playback-rate-factory.js +0 -2
  104. package/build/es2019/types/set-playback-rate-factory.js.map +0 -1
  105. package/build/es2019/types/set-playback-rate-function.d.ts +0 -2
  106. package/build/es2019/types/set-playback-rate-function.d.ts.map +0 -1
  107. package/build/es2019/types/set-playback-rate-function.js +0 -2
  108. package/build/es2019/types/set-playback-rate-function.js.map +0 -1
  109. package/build/es2019/types/set-timingsrc-factory.d.ts +0 -5
  110. package/build/es2019/types/set-timingsrc-factory.d.ts.map +0 -1
  111. package/build/es2019/types/set-timingsrc-factory.js +0 -2
  112. package/build/es2019/types/set-timingsrc-factory.js.map +0 -1
  113. package/build/es2019/types/set-timingsrc-function.d.ts +0 -4
  114. package/build/es2019/types/set-timingsrc-function.d.ts.map +0 -1
  115. package/build/es2019/types/set-timingsrc-function.js +0 -2
  116. package/build/es2019/types/set-timingsrc-function.js.map +0 -1
  117. package/build/es2019/types/set-timingsrc-with-custom-update-function-factory.d.ts +0 -5
  118. package/build/es2019/types/set-timingsrc-with-custom-update-function-factory.d.ts.map +0 -1
  119. package/build/es2019/types/set-timingsrc-with-custom-update-function-factory.js +0 -2
  120. package/build/es2019/types/set-timingsrc-with-custom-update-function-factory.js.map +0 -1
  121. package/build/es2019/types/set-timingsrc-with-custom-update-function-function.d.ts +0 -5
  122. package/build/es2019/types/set-timingsrc-with-custom-update-function-function.d.ts.map +0 -1
  123. package/build/es2019/types/set-timingsrc-with-custom-update-function-function.js +0 -2
  124. package/build/es2019/types/set-timingsrc-with-custom-update-function-function.js.map +0 -1
  125. package/build/es2019/types/update-gradually-factory.d.ts +0 -4
  126. package/build/es2019/types/update-gradually-factory.d.ts.map +0 -1
  127. package/build/es2019/types/update-gradually-factory.js +0 -2
  128. package/build/es2019/types/update-gradually-factory.js.map +0 -1
  129. package/build/es2019/types/update-media-element-factory.d.ts +0 -7
  130. package/build/es2019/types/update-media-element-factory.d.ts.map +0 -1
  131. package/build/es2019/types/update-media-element-factory.js +0 -2
  132. package/build/es2019/types/update-media-element-factory.js.map +0 -1
  133. package/build/es2019/types/update-media-element-function.d.ts +0 -2
  134. package/build/es2019/types/update-media-element-function.d.ts.map +0 -1
  135. package/build/es2019/types/update-media-element-function.js +0 -2
  136. package/build/es2019/types/update-media-element-function.js.map +0 -1
  137. package/build/es2019/types/update-stepwise-factory-factory.d.ts +0 -4
  138. package/build/es2019/types/update-stepwise-factory-factory.d.ts.map +0 -1
  139. package/build/es2019/types/update-stepwise-factory-factory.js +0 -2
  140. package/build/es2019/types/update-stepwise-factory-factory.js.map +0 -1
  141. package/build/es2019/types/update-stepwise-factory.d.ts +0 -3
  142. package/build/es2019/types/update-stepwise-factory.d.ts.map +0 -1
  143. package/build/es2019/types/update-stepwise-factory.js +0 -2
  144. package/build/es2019/types/update-stepwise-factory.js.map +0 -1
  145. package/build/es2019/types/window-factory.d.ts +0 -2
  146. package/build/es2019/types/window-factory.d.ts.map +0 -1
  147. package/build/es2019/types/window-factory.js +0 -2
  148. package/build/es2019/types/window-factory.js.map +0 -1
  149. package/src/types/default-timingsrc-factory.ts +0 -15
  150. package/src/types/determine-supported-playback-rate-values-function.ts +0 -1
  151. package/src/types/pause-function.ts +0 -1
  152. package/src/types/play-function.ts +0 -1
  153. package/src/types/set-current-time-factory.ts +0 -3
  154. package/src/types/set-current-time-function.ts +0 -1
  155. package/src/types/set-playback-rate-factory.ts +0 -3
  156. package/src/types/set-playback-rate-function.ts +0 -1
  157. package/src/types/set-timingsrc-factory.ts +0 -8
  158. package/src/types/set-timingsrc-function.ts +0 -8
  159. package/src/types/set-timingsrc-with-custom-update-function-factory.ts +0 -10
  160. package/src/types/set-timingsrc-with-custom-update-function-function.ts +0 -10
  161. package/src/types/update-gradually-factory.ts +0 -9
  162. package/src/types/update-media-element-factory.ts +0 -12
  163. package/src/types/update-media-element-function.ts +0 -8
  164. package/src/types/update-stepwise-factory-factory.ts +0 -4
  165. package/src/types/update-stepwise-factory.ts +0 -3
  166. package/src/types/window-factory.ts +0 -1
@@ -1,6 +1,16 @@
1
- import { TSetTimingsrcFactory } from '../types';
1
+ import type { ITimingObject } from 'timing-object';
2
+ import type { createSetTimingsrcWithCustomUpdateFunction } from '../factories/set-timingsrc-with-custom-update-function';
3
+ import { IUpdateVector } from '../interfaces';
4
+ import { TPrepareTimingStateVectorFunction, TUpdateFunction } from '../types';
2
5
 
3
- export const createSetTimingsrc: TSetTimingsrcFactory = (setTimingsrcWithCustomUpdateFunction, update) => {
4
- return (mediaElement, timingObject, prepareTimingStateVector = null) =>
6
+ export const createSetTimingsrc =
7
+ <UpdateVectorWithCustomState extends IUpdateVector>(
8
+ setTimingsrcWithCustomUpdateFunction: ReturnType<typeof createSetTimingsrcWithCustomUpdateFunction>,
9
+ update: TUpdateFunction<UpdateVectorWithCustomState>
10
+ ) =>
11
+ (
12
+ mediaElement: HTMLMediaElement,
13
+ timingObject: ITimingObject,
14
+ prepareTimingStateVector: null | TPrepareTimingStateVectorFunction = null
15
+ ) =>
5
16
  setTimingsrcWithCustomUpdateFunction(mediaElement, timingObject, update, prepareTimingStateVector);
6
- };
@@ -1,29 +1,58 @@
1
- import { TUpdateGraduallyFactory } from '../types';
1
+ import type { determineSupportedPlaybackRateValues } from '../functions/determine-supported-playback-rate-values';
2
+ import type { updateVectorWithNewPosition as updateVectorWithNewPositionFunction } from '../functions/update-vector-with-new-position';
3
+ import { IUpdateVector } from '../interfaces';
4
+ import { TUpdateFunction } from '../types';
5
+ import type { createComputeVelocity } from './compute-velocity';
6
+
7
+ export const createUpdateGradually = (
8
+ computeVelocity: ReturnType<typeof createComputeVelocity>,
9
+ [minValue, maxValue]: ReturnType<typeof determineSupportedPlaybackRateValues>,
10
+ threshold: number,
11
+ tolerance: number,
12
+ updateVectorWithNewPosition: typeof updateVectorWithNewPositionFunction
13
+ ): TUpdateFunction<IUpdateVector & { mediaElementDelay: number }> => {
14
+ return ({ position, velocity }, currentTime, previousUpdateVectorWithCustomState) => {
15
+ let { mediaElementDelay } = previousUpdateVectorWithCustomState ?? { mediaElementDelay: 0 };
2
16
 
3
- export const createUpdateGradually: TUpdateGraduallyFactory = ([minValue, maxValue], timeConstant, threshold, tolerance) => {
4
- return ({ position, velocity }, currentTime) => {
5
17
  if (velocity < minValue || velocity > maxValue) {
6
- return { position, velocity: 0 };
18
+ return { mediaElementDelay, position, velocity: 0 };
7
19
  }
8
20
 
9
21
  if (position < 0 || velocity === 0) {
10
- return { position, velocity };
22
+ return { mediaElementDelay, position, velocity };
11
23
  }
12
24
 
13
25
  const positionDifference = currentTime - position;
14
26
  const absolutePositionDifference = Math.abs(positionDifference);
15
27
 
16
28
  if (absolutePositionDifference > threshold) {
17
- return { position, velocity };
29
+ const { position: lastPosition } = previousUpdateVectorWithCustomState ?? { position: null };
30
+
31
+ if (positionDifference < 0 || positionDifference > mediaElementDelay) {
32
+ if (lastPosition === currentTime) {
33
+ mediaElementDelay += absolutePositionDifference;
34
+ }
35
+
36
+ return updateVectorWithNewPosition(mediaElementDelay, position, (nextPosition) =>
37
+ computeVelocity(nextPosition - position, minValue, maxValue, velocity)
38
+ );
39
+ }
40
+
41
+ if (lastPosition !== currentTime) {
42
+ return updateVectorWithNewPosition(mediaElementDelay - absolutePositionDifference, position, (nextPosition) =>
43
+ computeVelocity(nextPosition - position, minValue, maxValue, velocity)
44
+ );
45
+ }
18
46
  }
19
47
 
20
48
  if (absolutePositionDifference > tolerance) {
21
49
  return {
50
+ mediaElementDelay,
22
51
  position: currentTime,
23
- velocity: Math.max(minValue, Math.min(maxValue, ((timeConstant - positionDifference) / timeConstant) * velocity))
52
+ velocity: computeVelocity(positionDifference, minValue, maxValue, velocity)
24
53
  };
25
54
  }
26
55
 
27
- return { position: currentTime, velocity };
56
+ return { mediaElementDelay, position: currentTime, velocity };
28
57
  };
29
58
  };
@@ -1,7 +1,22 @@
1
- import { TUpdateMediaElementFactory } from '../types';
1
+ import type { pause as pauseFunction } from '../functions/pause';
2
+ import type { play as playFunction } from '../functions/play';
3
+ import type { createSetCurrentTime } from './set-current-time';
4
+ import type { createSetPlaybackRate } from './set-playback-rate';
2
5
 
3
- export const createUpdateMediaElement: TUpdateMediaElementFactory = (pause, play, setCurrentTime, setPlaybackRate) => {
4
- return (currentTime, duration, mediaElement, playbackRate, position, velocity) => {
6
+ export const createUpdateMediaElement = (
7
+ pause: typeof pauseFunction,
8
+ play: typeof playFunction,
9
+ setCurrentTime: ReturnType<typeof createSetCurrentTime>,
10
+ setPlaybackRate: ReturnType<typeof createSetPlaybackRate>
11
+ ) => {
12
+ return (
13
+ currentTime: number,
14
+ duration: number,
15
+ mediaElement: HTMLMediaElement,
16
+ playbackRate: number,
17
+ position: number,
18
+ velocity: number
19
+ ) => {
5
20
  if (position < 0) {
6
21
  if (currentTime > 0) {
7
22
  setCurrentTime(mediaElement, currentTime, 0);
@@ -1,57 +1,152 @@
1
- import { TUpdateStepwiseFactoryFactory } from '../types';
1
+ import type { TTranslateTimingStateVectorFunction } from 'timing-object';
2
+ import { IUpdateVector } from '../interfaces';
3
+ import { TUpdateFunction } from '../types';
4
+
5
+ const MAXIMUM_PLAYHEAD_DIFFERENCE = 0.5;
6
+
7
+ export const createUpdateStepwiseFactory = (translateTimingStateVector: TTranslateTimingStateVectorFunction) => {
8
+ return (
9
+ tolerance: number
10
+ ): TUpdateFunction<
11
+ IUpdateVector & {
12
+ lastAppliedPostion: number;
13
+ lastAppliedTimestamp: number;
14
+ lastAppliedVelocity: number;
15
+ lastPlayheadDifference: number;
16
+ mediaElementDelay: number;
17
+ numberOfDetectedResets: number;
18
+ numberOfExpectedResets: number;
19
+ }
20
+ > => {
21
+ return (timingStateVector, currentTime, previousUpdateVectorWithCustomState) => {
22
+ let {
23
+ lastAppliedPostion,
24
+ lastAppliedTimestamp,
25
+ lastAppliedVelocity,
26
+ lastPlayheadDifference,
27
+ mediaElementDelay,
28
+ numberOfDetectedResets,
29
+ numberOfExpectedResets
30
+ } = previousUpdateVectorWithCustomState ?? {
31
+ lastAppliedPostion: 0,
32
+ lastAppliedTimestamp: 0,
33
+ lastAppliedVelocity: 0,
34
+ lastPlayheadDifference: 0,
35
+ mediaElementDelay: 0,
36
+ numberOfDetectedResets: 0,
37
+ numberOfExpectedResets: 1
38
+ };
2
39
 
3
- export const createUpdateStepwiseFactory: TUpdateStepwiseFactoryFactory = (translateTimingStateVector) => {
4
- return (tolerance) => {
5
- let lastMotionUpdate: null | { position: number; timestamp: number; velocity: number } = null;
6
- let lastPlayheadDifference = 0;
7
- let mediaElementDelay = 0;
8
-
9
- return (timingStateVector, currentTime) => {
10
40
  if (timingStateVector.position < 0 || timingStateVector.velocity === 0) {
11
- lastMotionUpdate = null;
12
-
13
- return { position: timingStateVector.position, velocity: timingStateVector.velocity };
41
+ lastAppliedPostion = timingStateVector.position;
42
+ lastAppliedVelocity = timingStateVector.velocity;
43
+
44
+ return {
45
+ lastAppliedPostion,
46
+ lastAppliedTimestamp: 0,
47
+ lastAppliedVelocity,
48
+ lastPlayheadDifference,
49
+ mediaElementDelay,
50
+ numberOfDetectedResets,
51
+ numberOfExpectedResets,
52
+ position: lastAppliedPostion,
53
+ velocity: lastAppliedVelocity
54
+ };
14
55
  }
15
56
 
16
- if (lastMotionUpdate !== null) {
17
- const playheadDifference = Math.abs(currentTime - lastMotionUpdate.position);
18
-
19
- // Bug #4: Safari decreases currentTime after playing for about 200 milliseconds.
20
- if (lastPlayheadDifference - 0.01 < playheadDifference && lastPlayheadDifference < 0.5) {
57
+ // Bug #4: Safari decreases currentTime after playing for about 200 milliseconds.
58
+ if (lastAppliedVelocity === timingStateVector.velocity && lastPlayheadDifference < MAXIMUM_PLAYHEAD_DIFFERENCE) {
59
+ const playheadDifference = Math.abs(currentTime - lastAppliedPostion) * lastAppliedVelocity;
60
+
61
+ if (playheadDifference < MAXIMUM_PLAYHEAD_DIFFERENCE) {
62
+ if (playheadDifference + 0.001 > lastPlayheadDifference) {
63
+ lastPlayheadDifference = playheadDifference;
64
+
65
+ if (numberOfDetectedResets < numberOfExpectedResets) {
66
+ return {
67
+ lastAppliedPostion,
68
+ lastAppliedTimestamp,
69
+ lastAppliedVelocity,
70
+ lastPlayheadDifference,
71
+ mediaElementDelay,
72
+ numberOfDetectedResets,
73
+ numberOfExpectedResets,
74
+ position: currentTime,
75
+ velocity: lastAppliedVelocity
76
+ };
77
+ }
78
+ } else {
79
+ lastPlayheadDifference = playheadDifference;
80
+ numberOfDetectedResets += 1;
81
+
82
+ if (numberOfDetectedResets <= numberOfExpectedResets) {
83
+ return {
84
+ lastAppliedPostion,
85
+ lastAppliedTimestamp,
86
+ lastAppliedVelocity,
87
+ lastPlayheadDifference,
88
+ mediaElementDelay,
89
+ numberOfDetectedResets,
90
+ numberOfExpectedResets,
91
+ position: currentTime,
92
+ velocity: lastAppliedVelocity
93
+ };
94
+ }
95
+
96
+ numberOfExpectedResets += 1;
97
+ }
98
+ } else {
21
99
  lastPlayheadDifference = playheadDifference;
22
-
23
- return { position: currentTime, velocity: lastMotionUpdate.velocity };
100
+ numberOfExpectedResets = Math.max(numberOfDetectedResets, 1);
24
101
  }
25
-
26
- lastPlayheadDifference = Number.POSITIVE_INFINITY;
102
+ } else {
103
+ lastAppliedTimestamp = 0;
27
104
  }
28
105
 
29
106
  const positionDifference = Math.abs(currentTime - timingStateVector.position);
30
-
31
- if (positionDifference > tolerance) {
32
- if (lastMotionUpdate !== null) {
33
- const elapsedTime = timingStateVector.timestamp - lastMotionUpdate.timestamp;
34
-
35
- const { position } = translateTimingStateVector({ acceleration: 0, ...lastMotionUpdate }, elapsedTime);
107
+ const velocityHasChanged =
108
+ lastAppliedVelocity === 0 ||
109
+ (lastAppliedVelocity < 0 && timingStateVector.velocity > 0) ||
110
+ (lastAppliedVelocity > 0 && timingStateVector.velocity < 0);
111
+
112
+ if (positionDifference > tolerance || velocityHasChanged) {
113
+ if (lastAppliedTimestamp > 0) {
114
+ const elapsedTime = timingStateVector.timestamp - lastAppliedTimestamp;
115
+ const { position } = translateTimingStateVector(
116
+ { acceleration: 0, position: lastAppliedPostion, timestamp: lastAppliedTimestamp, velocity: lastAppliedVelocity },
117
+ elapsedTime
118
+ );
36
119
 
37
120
  mediaElementDelay = position - currentTime;
38
121
  }
39
122
 
40
- const positioWithDelay = timingStateVector.position + mediaElementDelay;
41
-
42
- lastMotionUpdate = {
43
- position: positioWithDelay,
44
- timestamp: timingStateVector.timestamp,
45
- velocity: timingStateVector.velocity
123
+ lastAppliedPostion = timingStateVector.position + mediaElementDelay;
124
+ lastAppliedVelocity = timingStateVector.velocity;
125
+
126
+ return {
127
+ lastAppliedPostion,
128
+ lastAppliedTimestamp: timingStateVector.timestamp,
129
+ lastAppliedVelocity,
130
+ lastPlayheadDifference: 0,
131
+ mediaElementDelay,
132
+ numberOfDetectedResets: 0,
133
+ numberOfExpectedResets,
134
+ position: lastAppliedPostion,
135
+ velocity: lastAppliedVelocity
46
136
  };
47
- lastPlayheadDifference = 0;
48
-
49
- return { position: positioWithDelay, velocity: timingStateVector.velocity };
50
137
  }
51
138
 
52
- lastMotionUpdate = null;
53
-
54
- return { position: currentTime, velocity: timingStateVector.velocity };
139
+ return {
140
+ lastAppliedPostion,
141
+ lastAppliedTimestamp,
142
+ lastAppliedVelocity,
143
+ lastPlayheadDifference,
144
+ mediaElementDelay,
145
+ numberOfDetectedResets,
146
+ numberOfExpectedResets,
147
+ position: currentTime,
148
+ velocity: timingStateVector.velocity
149
+ };
55
150
  };
56
151
  };
57
152
  };
@@ -1,3 +1 @@
1
- import { TWindowFactory } from '../types';
2
-
3
- export const createWindow: TWindowFactory = () => (typeof window === 'undefined' ? null : window);
1
+ export const createWindow = () => (typeof window === 'undefined' ? null : window);
@@ -1,6 +1,8 @@
1
+ import type { createWindow } from '../factories/window';
2
+
1
3
  const DEFAULT_VALUES = [Number.MIN_VALUE, Number.MAX_VALUE] as const;
2
4
 
3
- export const determineSupportedPlaybackRateValues = (window: null | (Window & typeof globalThis)) => {
5
+ export const determineSupportedPlaybackRateValues = (window: ReturnType<typeof createWindow>): readonly [number, number] => {
4
6
  if (window === null) {
5
7
  return DEFAULT_VALUES;
6
8
  }
@@ -0,0 +1,9 @@
1
+ export const updateVectorWithNewPosition = (
2
+ mediaElementDelay: number,
3
+ position: number,
4
+ updateVelocity: (nextPosition: number) => number
5
+ ) => {
6
+ const nextPosition = position + mediaElementDelay;
7
+
8
+ return { mediaElementDelay, position: nextPosition, velocity: updateVelocity(nextPosition) };
9
+ };
package/src/module.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { animationFrame, on } from 'subscribable-things';
2
2
  import { translateTimingStateVector } from 'timing-object';
3
+ import { createComputeVelocity } from './factories/compute-velocity';
3
4
  import { createDefaultSetTimingsrc } from './factories/default-set-timingsrc';
4
5
  import { createSetCurrentTime } from './factories/set-current-time';
5
6
  import { createSetPlaybackRate } from './factories/set-playback-rate';
@@ -12,6 +13,7 @@ import { createWindow } from './factories/window';
12
13
  import { determineSupportedPlaybackRateValues } from './functions/determine-supported-playback-rate-values';
13
14
  import { pause } from './functions/pause';
14
15
  import { play } from './functions/play';
16
+ import { updateVectorWithNewPosition } from './functions/update-vector-with-new-position';
15
17
 
16
18
  export { createSetTimingsrc };
17
19
 
@@ -29,10 +31,12 @@ export const setTimingsrcWithCustomUpdateFunction = createSetTimingsrcWithCustom
29
31
  );
30
32
 
31
33
  export const setTimingsrc = createDefaultSetTimingsrc(
34
+ createComputeVelocity,
32
35
  createSetTimingsrc,
33
36
  createUpdateGradually,
34
37
  createUpdateStepwise,
35
38
  determineSupportedPlaybackRateValues,
36
39
  setTimingsrcWithCustomUpdateFunction,
40
+ updateVectorWithNewPosition,
37
41
  createWindow()
38
42
  );
@@ -1,19 +1,3 @@
1
- export * from './default-timingsrc-factory';
2
- export * from './determine-supported-playback-rate-values-function';
3
- export * from './pause-function';
4
- export * from './play-function';
5
1
  export * from './prepare-timing-state-vector-function';
6
- export * from './set-current-time-factory';
7
- export * from './set-current-time-function';
8
- export * from './set-playback-rate-factory';
9
- export * from './set-playback-rate-function';
10
- export * from './set-timingsrc-factory';
11
- export * from './set-timingsrc-function';
12
- export * from './set-timingsrc-with-custom-update-function-factory';
13
- export * from './set-timingsrc-with-custom-update-function-function';
14
2
  export * from './update-function';
15
- export * from './update-gradually-factory';
16
- export * from './update-media-element-factory';
17
- export * from './update-stepwise-factory';
18
- export * from './update-stepwise-factory-factory';
19
- export * from './window-factory';
3
+ export * from './update-vector-with-custom-state';
@@ -1,4 +1,8 @@
1
1
  import { ITimingStateVector } from 'timing-object';
2
2
  import { IUpdateVector } from '../interfaces';
3
3
 
4
- export type TUpdateFunction = (timingStateVector: ITimingStateVector, currentTime: number) => IUpdateVector;
4
+ export type TUpdateFunction<UpdateVectorWithCustomState extends IUpdateVector> = (
5
+ timingStateVector: ITimingStateVector,
6
+ currentTime: number,
7
+ previousUpdateVectorWithCustomState: null | UpdateVectorWithCustomState
8
+ ) => UpdateVectorWithCustomState;
@@ -0,0 +1,5 @@
1
+ import { TUpdateFunction } from './update-function';
2
+
3
+ export type TUpdateVectorWithCustomState<UpdateFunction> = UpdateFunction extends TUpdateFunction<infer UpdateVectorWithCustomState>
4
+ ? UpdateVectorWithCustomState
5
+ : never;
@@ -1,8 +0,0 @@
1
- import { TDetermineSupportedPlaybackRateValuesFunction } from './determine-supported-playback-rate-values-function';
2
- import { TSetTimingsrcFactory } from './set-timingsrc-factory';
3
- import { TSetTimingsrcFunction } from './set-timingsrc-function';
4
- import { TSetTimingsrcWithCustomUpdateFunctionFunction } from './set-timingsrc-with-custom-update-function-function';
5
- import { TUpdateGraduallyFactory } from './update-gradually-factory';
6
- import { TUpdateStepwiseFactory } from './update-stepwise-factory';
7
- export declare type TDefaultSetTimingsrcFactory = (createSetTimingsrc: TSetTimingsrcFactory, createUpdateGradually: TUpdateGraduallyFactory, createUpdateStepwise: TUpdateStepwiseFactory, determineSupportedPlaybackRateValues: TDetermineSupportedPlaybackRateValuesFunction, setTimingsrcWithCustomUpdateFunction: TSetTimingsrcWithCustomUpdateFunctionFunction, window: null | (Window & typeof globalThis)) => TSetTimingsrcFunction;
8
- //# sourceMappingURL=default-timingsrc-factory.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"default-timingsrc-factory.d.ts","sourceRoot":"","sources":["../../../src/types/default-timingsrc-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6CAA6C,EAAE,MAAM,qDAAqD,CAAC;AACpH,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAAE,6CAA6C,EAAE,MAAM,sDAAsD,CAAC;AACrH,OAAO,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AACrE,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAEnE,oBAAY,2BAA2B,GAAG,CACtC,kBAAkB,EAAE,oBAAoB,EACxC,qBAAqB,EAAE,uBAAuB,EAC9C,oBAAoB,EAAE,sBAAsB,EAC5C,oCAAoC,EAAE,6CAA6C,EACnF,oCAAoC,EAAE,6CAA6C,EACnF,MAAM,EAAE,IAAI,GAAG,CAAC,MAAM,GAAG,OAAO,UAAU,CAAC,KAC1C,qBAAqB,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=default-timingsrc-factory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"default-timingsrc-factory.js","sourceRoot":"","sources":["../../../src/types/default-timingsrc-factory.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export declare type TDetermineSupportedPlaybackRateValuesFunction = (window: null | (Window & typeof globalThis)) => readonly [number, number];
2
- //# sourceMappingURL=determine-supported-playback-rate-values-function.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"determine-supported-playback-rate-values-function.d.ts","sourceRoot":"","sources":["../../../src/types/determine-supported-playback-rate-values-function.ts"],"names":[],"mappings":"AAAA,oBAAY,6CAA6C,GAAG,CAAC,MAAM,EAAE,IAAI,GAAG,CAAC,MAAM,GAAG,OAAO,UAAU,CAAC,KAAK,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=determine-supported-playback-rate-values-function.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"determine-supported-playback-rate-values-function.js","sourceRoot":"","sources":["../../../src/types/determine-supported-playback-rate-values-function.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export declare type TPauseFunction = (mediaElement: HTMLMediaElement) => void;
2
- //# sourceMappingURL=pause-function.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pause-function.d.ts","sourceRoot":"","sources":["../../../src/types/pause-function.ts"],"names":[],"mappings":"AAAA,oBAAY,cAAc,GAAG,CAAC,YAAY,EAAE,gBAAgB,KAAK,IAAI,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=pause-function.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pause-function.js","sourceRoot":"","sources":["../../../src/types/pause-function.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export declare type TPlayFunction = (mediaElement: HTMLMediaElement) => void;
2
- //# sourceMappingURL=play-function.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"play-function.d.ts","sourceRoot":"","sources":["../../../src/types/play-function.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa,GAAG,CAAC,YAAY,EAAE,gBAAgB,KAAK,IAAI,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=play-function.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"play-function.js","sourceRoot":"","sources":["../../../src/types/play-function.ts"],"names":[],"mappings":""}
@@ -1,3 +0,0 @@
1
- import { TSetCurrentTimeFunction } from './set-current-time-function';
2
- export declare type TSetCurrentTimeFactory = (currentTimeAssignments: WeakMap<HTMLMediaElement, [number, number]>) => TSetCurrentTimeFunction;
3
- //# sourceMappingURL=set-current-time-factory.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-current-time-factory.d.ts","sourceRoot":"","sources":["../../../src/types/set-current-time-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AAEtE,oBAAY,sBAAsB,GAAG,CAAC,sBAAsB,EAAE,OAAO,CAAC,gBAAgB,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,KAAK,uBAAuB,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=set-current-time-factory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-current-time-factory.js","sourceRoot":"","sources":["../../../src/types/set-current-time-factory.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export declare type TSetCurrentTimeFunction = (mediaElement: HTMLMediaElement, currentValue: number, nextValue: number) => void;
2
- //# sourceMappingURL=set-current-time-function.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-current-time-function.d.ts","sourceRoot":"","sources":["../../../src/types/set-current-time-function.ts"],"names":[],"mappings":"AAAA,oBAAY,uBAAuB,GAAG,CAAC,YAAY,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=set-current-time-function.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-current-time-function.js","sourceRoot":"","sources":["../../../src/types/set-current-time-function.ts"],"names":[],"mappings":""}
@@ -1,3 +0,0 @@
1
- import { TSetPlaybackRateFunction } from './set-playback-rate-function';
2
- export declare type TSetPlaybackRateFactory = (playbackRateAssignments: WeakMap<HTMLMediaElement, [number, number]>) => TSetPlaybackRateFunction;
3
- //# sourceMappingURL=set-playback-rate-factory.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-playback-rate-factory.d.ts","sourceRoot":"","sources":["../../../src/types/set-playback-rate-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAExE,oBAAY,uBAAuB,GAAG,CAAC,uBAAuB,EAAE,OAAO,CAAC,gBAAgB,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,KAAK,wBAAwB,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=set-playback-rate-factory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-playback-rate-factory.js","sourceRoot":"","sources":["../../../src/types/set-playback-rate-factory.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export declare type TSetPlaybackRateFunction = (mediaElement: HTMLMediaElement, currentValue: number, nextValue: number) => void;
2
- //# sourceMappingURL=set-playback-rate-function.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-playback-rate-function.d.ts","sourceRoot":"","sources":["../../../src/types/set-playback-rate-function.ts"],"names":[],"mappings":"AAAA,oBAAY,wBAAwB,GAAG,CAAC,YAAY,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=set-playback-rate-function.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-playback-rate-function.js","sourceRoot":"","sources":["../../../src/types/set-playback-rate-function.ts"],"names":[],"mappings":""}
@@ -1,5 +0,0 @@
1
- import { TSetTimingsrcFunction } from './set-timingsrc-function';
2
- import { TSetTimingsrcWithCustomUpdateFunctionFunction } from './set-timingsrc-with-custom-update-function-function';
3
- import { TUpdateFunction } from './update-function';
4
- export declare type TSetTimingsrcFactory = (setTimingsrcWithCustomUpdateFunction: TSetTimingsrcWithCustomUpdateFunctionFunction, update: TUpdateFunction) => TSetTimingsrcFunction;
5
- //# sourceMappingURL=set-timingsrc-factory.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-timingsrc-factory.d.ts","sourceRoot":"","sources":["../../../src/types/set-timingsrc-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAAE,6CAA6C,EAAE,MAAM,sDAAsD,CAAC;AACrH,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,oBAAY,oBAAoB,GAAG,CAC/B,oCAAoC,EAAE,6CAA6C,EACnF,MAAM,EAAE,eAAe,KACtB,qBAAqB,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=set-timingsrc-factory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-timingsrc-factory.js","sourceRoot":"","sources":["../../../src/types/set-timingsrc-factory.ts"],"names":[],"mappings":""}
@@ -1,4 +0,0 @@
1
- import { ITimingObject } from 'timing-object';
2
- import { TPrepareTimingStateVectorFunction } from './prepare-timing-state-vector-function';
3
- export declare type TSetTimingsrcFunction = (mediaElement: HTMLMediaElement, timingObject: ITimingObject, prepareTimingStateVector?: null | TPrepareTimingStateVectorFunction) => () => void;
4
- //# sourceMappingURL=set-timingsrc-function.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-timingsrc-function.d.ts","sourceRoot":"","sources":["../../../src/types/set-timingsrc-function.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,iCAAiC,EAAE,MAAM,wCAAwC,CAAC;AAE3F,oBAAY,qBAAqB,GAAG,CAChC,YAAY,EAAE,gBAAgB,EAC9B,YAAY,EAAE,aAAa,EAC3B,wBAAwB,CAAC,EAAE,IAAI,GAAG,iCAAiC,KAClE,MAAM,IAAI,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=set-timingsrc-function.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-timingsrc-function.js","sourceRoot":"","sources":["../../../src/types/set-timingsrc-function.ts"],"names":[],"mappings":""}
@@ -1,5 +0,0 @@
1
- import { TAnimationFrameFunction, TOnFunction } from 'subscribable-things';
2
- import { TSetTimingsrcWithCustomUpdateFunctionFunction } from './set-timingsrc-with-custom-update-function-function';
3
- import { TUpdateMediaElementFunction } from './update-media-element-function';
4
- export declare type TSetTimingsrcWithCustomUpdateFunctionFactory = (animationFrame: TAnimationFrameFunction, document: Document, on: TOnFunction, updateMediaElement: TUpdateMediaElementFunction) => TSetTimingsrcWithCustomUpdateFunctionFunction;
5
- //# sourceMappingURL=set-timingsrc-with-custom-update-function-factory.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-timingsrc-with-custom-update-function-factory.d.ts","sourceRoot":"","sources":["../../../src/types/set-timingsrc-with-custom-update-function-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAC3E,OAAO,EAAE,6CAA6C,EAAE,MAAM,sDAAsD,CAAC;AACrH,OAAO,EAAE,2BAA2B,EAAE,MAAM,iCAAiC,CAAC;AAE9E,oBAAY,4CAA4C,GAAG,CACvD,cAAc,EAAE,uBAAuB,EACvC,QAAQ,EAAE,QAAQ,EAClB,EAAE,EAAE,WAAW,EACf,kBAAkB,EAAE,2BAA2B,KAC9C,6CAA6C,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=set-timingsrc-with-custom-update-function-factory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-timingsrc-with-custom-update-function-factory.js","sourceRoot":"","sources":["../../../src/types/set-timingsrc-with-custom-update-function-factory.ts"],"names":[],"mappings":""}
@@ -1,5 +0,0 @@
1
- import { ITimingObject } from 'timing-object';
2
- import { TPrepareTimingStateVectorFunction } from './prepare-timing-state-vector-function';
3
- import { TUpdateFunction } from './update-function';
4
- export declare type TSetTimingsrcWithCustomUpdateFunctionFunction = (mediaElement: HTMLMediaElement, timingObject: ITimingObject, updateFunction: TUpdateFunction, prepareTimingStateVector?: null | TPrepareTimingStateVectorFunction) => () => void;
5
- //# sourceMappingURL=set-timingsrc-with-custom-update-function-function.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"set-timingsrc-with-custom-update-function-function.d.ts","sourceRoot":"","sources":["../../../src/types/set-timingsrc-with-custom-update-function-function.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,iCAAiC,EAAE,MAAM,wCAAwC,CAAC;AAC3F,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,oBAAY,6CAA6C,GAAG,CACxD,YAAY,EAAE,gBAAgB,EAC9B,YAAY,EAAE,aAAa,EAC3B,cAAc,EAAE,eAAe,EAC/B,wBAAwB,CAAC,EAAE,IAAI,GAAG,iCAAiC,KAClE,MAAM,IAAI,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=set-timingsrc-with-custom-update-function-function.js.map