@streamscloud/kit 0.0.1-1771006168898 → 0.0.1-1771072688121
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.
- package/dist/ui/button/resources/button-theme.svelte +8 -11
- package/dist/ui/dialog/cmp.dialog-container.svelte +1 -1
- package/dist/ui/player/buttons/cmp.mobile-player-buttons.svelte +37 -0
- package/dist/ui/player/buttons/cmp.mobile-player-buttons.svelte.d.ts +7 -0
- package/dist/ui/player/buttons/cmp.player-buttons.svelte +127 -0
- package/dist/ui/player/buttons/cmp.player-buttons.svelte.d.ts +9 -0
- package/dist/ui/player/buttons/cmp.responsive-player-buttons.svelte +32 -0
- package/dist/ui/player/buttons/cmp.responsive-player-buttons.svelte.d.ts +8 -0
- package/dist/ui/player/buttons/index.d.ts +4 -0
- package/dist/ui/player/buttons/index.js +3 -0
- package/dist/ui/player/buttons/types.d.ts +7 -0
- package/dist/ui/player/buttons/types.js +1 -0
- package/dist/ui/player/carousel/carousel-localization.d.ts +3 -0
- package/dist/ui/player/carousel/carousel-localization.js +12 -0
- package/dist/ui/player/carousel/cmp.carousel.svelte +418 -0
- package/dist/ui/player/carousel/cmp.carousel.svelte.d.ts +38 -0
- package/dist/ui/player/carousel/index.d.ts +2 -0
- package/dist/ui/player/carousel/index.js +1 -0
- package/dist/ui/player/carousel/types.d.ts +1 -0
- package/dist/ui/player/carousel/types.js +1 -0
- package/dist/ui/player/feed-slider/cmp.feed-slider.svelte +206 -0
- package/dist/ui/player/feed-slider/cmp.feed-slider.svelte.d.ts +43 -0
- package/dist/ui/player/feed-slider/index.d.ts +3 -0
- package/dist/ui/player/feed-slider/index.js +2 -0
- package/dist/ui/player/feed-slider/prevent-feed-scroll.d.ts +5 -0
- package/dist/ui/player/feed-slider/prevent-feed-scroll.js +11 -0
- package/dist/ui/player/feed-slider/types.d.ts +16 -0
- package/dist/ui/player/feed-slider/types.js +1 -0
- package/dist/ui/player/feed-slider/wheel-gestures-adapter.d.ts +17 -0
- package/dist/ui/player/feed-slider/wheel-gestures-adapter.js +79 -0
- package/dist/ui/player/providers/chunks-player-buffer/index.d.ts +2 -0
- package/dist/ui/player/providers/chunks-player-buffer/index.js +2 -0
- package/dist/ui/player/providers/chunks-player-buffer/player-chunk-item.svelte.d.ts +9 -0
- package/dist/ui/player/providers/chunks-player-buffer/player-chunk-item.svelte.js +9 -0
- package/dist/ui/player/providers/chunks-player-buffer/player-chunk.svelte.d.ts +30 -0
- package/dist/ui/player/providers/chunks-player-buffer/player-chunk.svelte.js +72 -0
- package/dist/ui/player/providers/chunks-player-buffer/player-chunks-manager.svelte.d.ts +23 -0
- package/dist/ui/player/providers/chunks-player-buffer/player-chunks-manager.svelte.js +220 -0
- package/dist/ui/player/providers/default-chunks-player-buffer.svelte.d.ts +23 -0
- package/dist/ui/player/providers/default-chunks-player-buffer.svelte.js +71 -0
- package/dist/ui/player/providers/default-feed-player-buffer.svelte.d.ts +29 -0
- package/dist/ui/player/providers/default-feed-player-buffer.svelte.js +121 -0
- package/dist/ui/player/providers/index.d.ts +4 -0
- package/dist/ui/player/providers/index.js +3 -0
- package/dist/ui/player/providers/service.d.ts +2 -0
- package/dist/ui/player/providers/service.js +13 -0
- package/dist/ui/player/providers/types.d.ts +54 -0
- package/dist/ui/player/providers/types.js +1 -0
- package/dist/ui/player/utils/index.d.ts +1 -0
- package/dist/ui/player/utils/index.js +1 -0
- package/dist/ui/player/utils/touch-synchronizer.d.ts +7 -0
- package/dist/ui/player/utils/touch-synchronizer.js +21 -0
- package/dist/ui/progress/cmp.progress.svelte +1 -1
- package/package.json +19 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { FeedSliderBuffer, FeedSliderCallbacks } from './types';
|
|
2
|
+
import { type Snippet } from 'svelte';
|
|
3
|
+
declare function $$render<T extends {
|
|
4
|
+
id: string;
|
|
5
|
+
}>(): {
|
|
6
|
+
props: {
|
|
7
|
+
buffer: FeedSliderBuffer<T>;
|
|
8
|
+
on?: FeedSliderCallbacks;
|
|
9
|
+
children: Snippet<[{
|
|
10
|
+
item: T;
|
|
11
|
+
active?: boolean;
|
|
12
|
+
}]>;
|
|
13
|
+
};
|
|
14
|
+
exports: {};
|
|
15
|
+
bindings: "";
|
|
16
|
+
slots: {};
|
|
17
|
+
events: {};
|
|
18
|
+
};
|
|
19
|
+
declare class __sveltets_Render<T extends {
|
|
20
|
+
id: string;
|
|
21
|
+
}> {
|
|
22
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
23
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
24
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
25
|
+
bindings(): "";
|
|
26
|
+
exports(): {};
|
|
27
|
+
}
|
|
28
|
+
interface $$IsomorphicComponent {
|
|
29
|
+
new <T extends {
|
|
30
|
+
id: string;
|
|
31
|
+
}>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
32
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
33
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
34
|
+
<T extends {
|
|
35
|
+
id: string;
|
|
36
|
+
}>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
37
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
38
|
+
}
|
|
39
|
+
declare const Cmp: $$IsomorphicComponent;
|
|
40
|
+
type Cmp<T extends {
|
|
41
|
+
id: string;
|
|
42
|
+
}> = InstanceType<typeof Cmp<T>>;
|
|
43
|
+
export default Cmp;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** @type {import('svelte/action').Action} */
|
|
2
|
+
export const preventFeedScroll = (node, value = true) => {
|
|
3
|
+
node.dataset[preventFeedScrollingAttribute] = value.toString();
|
|
4
|
+
return {
|
|
5
|
+
destroy() {
|
|
6
|
+
// the node has been removed from the DOM
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export const isScrollingPrevented = (node) => node.dataset[preventFeedScrollingAttribute] === 'true';
|
|
11
|
+
const preventFeedScrollingAttribute = 'preventFeedScrolling';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type FeedSliderCallbacks = {
|
|
2
|
+
itemActivated?: (e: string) => void;
|
|
3
|
+
itemDeactivated?: (e: string) => void;
|
|
4
|
+
};
|
|
5
|
+
export interface FeedSliderBuffer<T extends {
|
|
6
|
+
id: string;
|
|
7
|
+
}> {
|
|
8
|
+
readonly current: T | null;
|
|
9
|
+
readonly loaded: T[];
|
|
10
|
+
readonly currentIndex: number;
|
|
11
|
+
readonly canLoadNext: boolean;
|
|
12
|
+
readonly canLoadPrevious: boolean;
|
|
13
|
+
readonly animationDuration: number;
|
|
14
|
+
loadNext: () => void;
|
|
15
|
+
loadPrevious: () => void;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type WheelAdapterCallbacks = {
|
|
2
|
+
canLoadNext: () => boolean;
|
|
3
|
+
canLoadPrevious: () => boolean;
|
|
4
|
+
onTrigger: (direction: number) => void;
|
|
5
|
+
getAnimationDurationMs: () => number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Minimal, robust wheel adapter:
|
|
9
|
+
* - EMA over axisVelocity[1] to smooth noisy streams (esp. on Windows)
|
|
10
|
+
* - Cooldown blocks triggers while animation runs
|
|
11
|
+
* - Mouse fallback: if velocity is near zero but delta is large, treat as a discrete "kick"
|
|
12
|
+
*/
|
|
13
|
+
export declare const createWheelAdapter: (target: HTMLElement, params: {
|
|
14
|
+
cbs: WheelAdapterCallbacks;
|
|
15
|
+
}) => {
|
|
16
|
+
destroy(): void;
|
|
17
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// wheel-gestures-adapter.ts
|
|
2
|
+
import { WheelGestures } from 'wheel-gestures';
|
|
3
|
+
/**
|
|
4
|
+
* Minimal, robust wheel adapter:
|
|
5
|
+
* - EMA over axisVelocity[1] to smooth noisy streams (esp. on Windows)
|
|
6
|
+
* - Cooldown blocks triggers while animation runs
|
|
7
|
+
* - Mouse fallback: if velocity is near zero but delta is large, treat as a discrete "kick"
|
|
8
|
+
*/
|
|
9
|
+
export const createWheelAdapter = (target, params) => {
|
|
10
|
+
const { cbs } = params;
|
|
11
|
+
// Tunables
|
|
12
|
+
const PEAK_THRESHOLD = 0.4; // EMA magnitude threshold to consider as a "peak"
|
|
13
|
+
const ACCEL_THRESHOLD = 0.02; // minimal directional EMA rise to count as real acceleration
|
|
14
|
+
const EMA_ALPHA = 0.35; // EMA smoothing factor (0..1); higher = snappier, noisier
|
|
15
|
+
const MOUSE_DELTA_KICK = 12; // mouse: large delta step (≈12 mac / ≈100 win)
|
|
16
|
+
const MOUSE_STEP_RATIO_KICK = 350; // dimensionless; tune 200–500 if needed
|
|
17
|
+
const wheelGestures = WheelGestures({ preventWheelAction: true, reverseSign: false });
|
|
18
|
+
wheelGestures.observe(target);
|
|
19
|
+
let isAnimating = false;
|
|
20
|
+
let cooldownTimer = null;
|
|
21
|
+
// EMA state
|
|
22
|
+
let emaVelocity = 0;
|
|
23
|
+
let previousEmaVelocity = 0;
|
|
24
|
+
const startCooldown = () => {
|
|
25
|
+
if (cooldownTimer) {
|
|
26
|
+
clearTimeout(cooldownTimer);
|
|
27
|
+
}
|
|
28
|
+
cooldownTimer = window.setTimeout(() => {
|
|
29
|
+
isAnimating = false;
|
|
30
|
+
}, cbs.getAnimationDurationMs() + 100);
|
|
31
|
+
};
|
|
32
|
+
const fire = (direction) => {
|
|
33
|
+
// Respect external guards (e.g., paging boundaries)
|
|
34
|
+
if ((direction > 0 && !cbs.canLoadNext()) || (direction < 0 && !cbs.canLoadPrevious())) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
isAnimating = true;
|
|
38
|
+
cbs.onTrigger(direction);
|
|
39
|
+
startCooldown();
|
|
40
|
+
};
|
|
41
|
+
wheelGestures.on('wheel', ({ axisDelta: [, axisDeltaY], axisVelocity: [, axisVelocityY] }) => {
|
|
42
|
+
const velocityY = axisVelocityY || 0;
|
|
43
|
+
const deltaY = axisDeltaY || 0;
|
|
44
|
+
// Tracking only: always update EMA and compute signs/acceleration
|
|
45
|
+
previousEmaVelocity = emaVelocity;
|
|
46
|
+
emaVelocity += (velocityY - emaVelocity) * EMA_ALPHA;
|
|
47
|
+
const emaMagnitude = Math.abs(emaVelocity);
|
|
48
|
+
const velocitySign = Math.sign(emaVelocity) || Math.sign(velocityY);
|
|
49
|
+
const emaAcceleration = emaVelocity - previousEmaVelocity;
|
|
50
|
+
// During animation we only track; no arming, no triggering
|
|
51
|
+
if (isAnimating) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// Path 1: mouse-like discrete kick (platform-agnostic via delta/velocity ratio)
|
|
55
|
+
const absDelta = Math.abs(deltaY);
|
|
56
|
+
const absVel = Math.abs(velocityY);
|
|
57
|
+
const stepRatio = absDelta / Math.max(1e-6, absVel);
|
|
58
|
+
const isMouseLikeKick = absDelta >= MOUSE_DELTA_KICK && stepRatio >= MOUSE_STEP_RATIO_KICK;
|
|
59
|
+
if (isMouseLikeKick) {
|
|
60
|
+
const direction = deltaY > 0 ? 1 : -1; // 1 = next/down, -1 = previous/up
|
|
61
|
+
fire(direction);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
// Path 2: trackpad/inertia via EMA acceleration and peak gating
|
|
65
|
+
const isAcceleratingInDirection = velocitySign !== 0 && emaAcceleration * velocitySign > ACCEL_THRESHOLD;
|
|
66
|
+
if (isAcceleratingInDirection && emaMagnitude > PEAK_THRESHOLD) {
|
|
67
|
+
const direction = velocitySign > 0 ? 1 : -1; // 1 = next/down, -1 = previous/up
|
|
68
|
+
fire(direction);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
destroy() {
|
|
73
|
+
wheelGestures.unobserve(target);
|
|
74
|
+
if (cooldownTimer) {
|
|
75
|
+
clearTimeout(cooldownTimer);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PlayerChunkItem } from './player-chunk-item.svelte';
|
|
2
|
+
import type { WithId } from '../types';
|
|
3
|
+
export declare class PlayerChunk<TItem extends WithId, TChunk extends WithId> {
|
|
4
|
+
readonly model: TChunk;
|
|
5
|
+
readonly items: TItem[];
|
|
6
|
+
readonly chunkItems: PlayerChunkItem<TItem>[];
|
|
7
|
+
readonly activeChunkItem: PlayerChunkItem<TItem>;
|
|
8
|
+
private _chunkItems;
|
|
9
|
+
private _activeItemIndex;
|
|
10
|
+
private _fetchDeferred;
|
|
11
|
+
private _itemsLoader;
|
|
12
|
+
constructor(data: {
|
|
13
|
+
chunk: TChunk;
|
|
14
|
+
provider: {
|
|
15
|
+
loadChunkItems: (chunkId: string, continuationToken: string | null | undefined) => Promise<{
|
|
16
|
+
items: TItem[];
|
|
17
|
+
continuationToken: string | null;
|
|
18
|
+
}>;
|
|
19
|
+
};
|
|
20
|
+
callbacks?: {
|
|
21
|
+
onChunkFullyLoaded: (chunk: PlayerChunk<TItem, TChunk>) => void;
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
get isLoading(): boolean;
|
|
25
|
+
get canLoadMore(): boolean;
|
|
26
|
+
get activeItemIndex(): number;
|
|
27
|
+
loadMore: () => Promise<TItem[]>;
|
|
28
|
+
setActiveItemIndex: (index: number) => Promise<void>;
|
|
29
|
+
mutateChunkItems: (items: PlayerChunkItem<TItem>[]) => void;
|
|
30
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ContinuationToken, Deferred } from '../../../../core';
|
|
2
|
+
import { CursorDataLoader } from '../../../../core/data-loaders';
|
|
3
|
+
import { PlayerChunkItem } from './player-chunk-item.svelte';
|
|
4
|
+
export class PlayerChunk {
|
|
5
|
+
model;
|
|
6
|
+
items = $derived.by(() => this._chunkItems.map((i) => i.model));
|
|
7
|
+
chunkItems = $derived.by(() => this._chunkItems);
|
|
8
|
+
activeChunkItem = $derived.by(() => this._chunkItems[this._activeItemIndex] ?? null);
|
|
9
|
+
_chunkItems = $state.raw([]);
|
|
10
|
+
_activeItemIndex = $state(0);
|
|
11
|
+
_fetchDeferred = $state.raw(null);
|
|
12
|
+
_itemsLoader;
|
|
13
|
+
constructor(data) {
|
|
14
|
+
const { chunk, provider, callbacks } = data;
|
|
15
|
+
this.model = chunk;
|
|
16
|
+
this._itemsLoader = new CursorDataLoader({
|
|
17
|
+
loadPage: async (continuationToken) => {
|
|
18
|
+
if (this._fetchDeferred) {
|
|
19
|
+
return await this._fetchDeferred.promise;
|
|
20
|
+
}
|
|
21
|
+
if (!this.canLoadMore) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
this._fetchDeferred = new Deferred();
|
|
26
|
+
const itemsResult = await provider.loadChunkItems(this.model.id, continuationToken.toNextChunkString());
|
|
27
|
+
const newItems = itemsResult.items;
|
|
28
|
+
this._chunkItems = [
|
|
29
|
+
...this._chunkItems,
|
|
30
|
+
...newItems.map((item) => new PlayerChunkItem({
|
|
31
|
+
model: item,
|
|
32
|
+
chunkId: this.model.id
|
|
33
|
+
}))
|
|
34
|
+
];
|
|
35
|
+
const continuationTokenResult = ContinuationToken.fromPayload(itemsResult.continuationToken);
|
|
36
|
+
if (!continuationTokenResult.canLoadMore) {
|
|
37
|
+
callbacks?.onChunkFullyLoaded(this);
|
|
38
|
+
}
|
|
39
|
+
const result = {
|
|
40
|
+
items: newItems,
|
|
41
|
+
continuationToken: continuationTokenResult
|
|
42
|
+
};
|
|
43
|
+
this._fetchDeferred.resolve(result);
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
this._fetchDeferred?.resolve(null);
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
this._fetchDeferred = null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
get isLoading() {
|
|
57
|
+
return !!this._fetchDeferred;
|
|
58
|
+
}
|
|
59
|
+
get canLoadMore() {
|
|
60
|
+
return this._itemsLoader.continuationToken.canLoadMore;
|
|
61
|
+
}
|
|
62
|
+
get activeItemIndex() {
|
|
63
|
+
return this._activeItemIndex;
|
|
64
|
+
}
|
|
65
|
+
loadMore = () => this._itemsLoader.loadMore();
|
|
66
|
+
setActiveItemIndex = async (index) => {
|
|
67
|
+
this._activeItemIndex = index;
|
|
68
|
+
};
|
|
69
|
+
mutateChunkItems = (items) => {
|
|
70
|
+
this._chunkItems = items;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PlayerChunk } from './player-chunk.svelte';
|
|
2
|
+
import type { IChunksPlayerDataProvider, WithId } from '../types';
|
|
3
|
+
export declare class PlayerChunksManager<TItem extends WithId, TChunk extends WithId> {
|
|
4
|
+
private provider;
|
|
5
|
+
private _activeChunkIndex;
|
|
6
|
+
private _loadedChunks;
|
|
7
|
+
private _warmUpDeferred;
|
|
8
|
+
constructor(provider: IChunksPlayerDataProvider<TItem, TChunk>);
|
|
9
|
+
get activeChunk(): PlayerChunk<TItem, TChunk>;
|
|
10
|
+
get loadedChunks(): PlayerChunk<TItem, TChunk>[];
|
|
11
|
+
get isLoading(): boolean;
|
|
12
|
+
get flattenedChunkItems(): TItem[];
|
|
13
|
+
get flattenedActiveChunkItemIndex(): number;
|
|
14
|
+
tryActivateItemById: (id: string) => boolean;
|
|
15
|
+
removeItemById: (id: string) => boolean;
|
|
16
|
+
removeChunkById: (id: string) => boolean | undefined;
|
|
17
|
+
initialize: () => Promise<void>;
|
|
18
|
+
setActiveChunkIndex: (index: number, chunkItemIndex: number) => Promise<void>;
|
|
19
|
+
activateItemAtFlattenedIndex: (index: number) => Promise<void>;
|
|
20
|
+
warmUp: () => Promise<void>;
|
|
21
|
+
reset: () => Promise<void>;
|
|
22
|
+
private warmUpSequentially;
|
|
23
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { Deferred } from '../../../../core';
|
|
2
|
+
import { PlayerChunk } from './player-chunk.svelte';
|
|
3
|
+
const ITEMS_BUFFER_SIZE = 10;
|
|
4
|
+
// Configuration: if true, always start from first item when switching chunks
|
|
5
|
+
// if false, activate the exact item at its position in the new chunk
|
|
6
|
+
const START_FROM_FIRST_ITEM_ON_CHUNK_SWITCH = true;
|
|
7
|
+
export class PlayerChunksManager {
|
|
8
|
+
provider;
|
|
9
|
+
_activeChunkIndex = $state(-1);
|
|
10
|
+
_loadedChunks = $state.raw([]);
|
|
11
|
+
_warmUpDeferred = $state.raw(null);
|
|
12
|
+
constructor(provider) {
|
|
13
|
+
this.provider = provider;
|
|
14
|
+
}
|
|
15
|
+
get activeChunk() {
|
|
16
|
+
return this._loadedChunks[this._activeChunkIndex] ?? null;
|
|
17
|
+
}
|
|
18
|
+
get loadedChunks() {
|
|
19
|
+
return this._loadedChunks;
|
|
20
|
+
}
|
|
21
|
+
get isLoading() {
|
|
22
|
+
return this._warmUpDeferred !== null || this._loadedChunks.some((c) => c.isLoading);
|
|
23
|
+
}
|
|
24
|
+
get flattenedChunkItems() {
|
|
25
|
+
return this._loadedChunks.reduce((acc, chunk) => {
|
|
26
|
+
acc.push(...chunk.items);
|
|
27
|
+
return acc;
|
|
28
|
+
}, []);
|
|
29
|
+
}
|
|
30
|
+
get flattenedActiveChunkItemIndex() {
|
|
31
|
+
if (!this.activeChunk || !this.activeChunk.activeChunkItem) {
|
|
32
|
+
return -1;
|
|
33
|
+
}
|
|
34
|
+
let itemsBeforeActiveChunk = 0;
|
|
35
|
+
for (let i = 0; i < this._activeChunkIndex; i++) {
|
|
36
|
+
itemsBeforeActiveChunk += this._loadedChunks[i].items.length;
|
|
37
|
+
}
|
|
38
|
+
return itemsBeforeActiveChunk + this.activeChunk.activeItemIndex;
|
|
39
|
+
}
|
|
40
|
+
tryActivateItemById = (id) => {
|
|
41
|
+
const chunkWithItemIndex = this._loadedChunks.findIndex((chunk) => chunk.items.some((item) => item.id === id));
|
|
42
|
+
const chunkWithItem = this._loadedChunks[chunkWithItemIndex];
|
|
43
|
+
if (!chunkWithItem) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const itemIndex = chunkWithItem.items.findIndex((item) => item.id === id);
|
|
47
|
+
this.setActiveChunkIndex(chunkWithItemIndex, itemIndex);
|
|
48
|
+
return true;
|
|
49
|
+
};
|
|
50
|
+
removeItemById = (id) => {
|
|
51
|
+
const itemIndex = this.flattenedChunkItems.findIndex((item) => item.id === id);
|
|
52
|
+
if (itemIndex === -1) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const currentItem = this.flattenedChunkItems[this.flattenedActiveChunkItemIndex];
|
|
56
|
+
// Find next item to activate BEFORE removing (if removing current item)
|
|
57
|
+
let nextItemId = currentItem?.id;
|
|
58
|
+
if (this.flattenedActiveChunkItemIndex === itemIndex) {
|
|
59
|
+
// Try next item
|
|
60
|
+
if (itemIndex + 1 < this.flattenedChunkItems.length) {
|
|
61
|
+
nextItemId = this.flattenedChunkItems[itemIndex + 1].id;
|
|
62
|
+
}
|
|
63
|
+
// Try previous item
|
|
64
|
+
else if (itemIndex > 0) {
|
|
65
|
+
nextItemId = this.flattenedChunkItems[itemIndex - 1].id;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// No items left
|
|
69
|
+
nextItemId = null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Remove item from chunks
|
|
73
|
+
this._loadedChunks.forEach((chunk) => {
|
|
74
|
+
if (chunk.chunkItems.some((i) => i.model.id === id)) {
|
|
75
|
+
chunk.mutateChunkItems(chunk.chunkItems.filter((i) => i.model.id !== id));
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
if (nextItemId) {
|
|
79
|
+
this.tryActivateItemById(nextItemId);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this._activeChunkIndex = -1;
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
};
|
|
86
|
+
removeChunkById = (id) => {
|
|
87
|
+
const chunkIndex = this._loadedChunks.findIndex((chunk) => chunk.model.id === id);
|
|
88
|
+
if (chunkIndex === -1) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
const isActiveChunk = this._activeChunkIndex === chunkIndex;
|
|
92
|
+
let newActiveChunkIndex = this._activeChunkIndex;
|
|
93
|
+
this._loadedChunks = [...this._loadedChunks.slice(0, chunkIndex), ...this._loadedChunks.slice(chunkIndex + 1)];
|
|
94
|
+
if (this._loadedChunks.length === 0) {
|
|
95
|
+
this._activeChunkIndex = -1;
|
|
96
|
+
this.provider.onEndReached?.();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (isActiveChunk) {
|
|
100
|
+
if (chunkIndex < this._loadedChunks.length) {
|
|
101
|
+
newActiveChunkIndex = chunkIndex;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
newActiveChunkIndex = this._loadedChunks.length - 1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (this._activeChunkIndex > chunkIndex) {
|
|
108
|
+
newActiveChunkIndex--;
|
|
109
|
+
}
|
|
110
|
+
this.setActiveChunkIndex(newActiveChunkIndex, 0);
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
initialize = async () => {
|
|
114
|
+
this._loadedChunks = this.provider.initialData.prefetchedChunks.map((chunk) => new PlayerChunk({ chunk, provider: { loadChunkItems: this.provider.loadChunkItems } }));
|
|
115
|
+
await this.warmUp();
|
|
116
|
+
if (this._loadedChunks.length === 0 || this.flattenedChunkItems.length === 0) {
|
|
117
|
+
this.provider.onEndReached?.();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const firstFilledChunkIndex = this._loadedChunks.findIndex((c) => c.items.length > 0);
|
|
121
|
+
const initialStartItemIndex = firstFilledChunkIndex === 0 && this.provider.initialData.startItemIndex && this.provider.initialData.startItemIndex > 0
|
|
122
|
+
? this.provider.initialData.startItemIndex
|
|
123
|
+
: 0;
|
|
124
|
+
this.setActiveChunkIndex(firstFilledChunkIndex, initialStartItemIndex);
|
|
125
|
+
};
|
|
126
|
+
setActiveChunkIndex = async (index, chunkItemIndex) => {
|
|
127
|
+
this._activeChunkIndex = index;
|
|
128
|
+
this.activeChunk.setActiveItemIndex(chunkItemIndex);
|
|
129
|
+
// Don't wait for warm up to be finished, it runs in the background
|
|
130
|
+
this.warmUp();
|
|
131
|
+
};
|
|
132
|
+
activateItemAtFlattenedIndex = async (index) => {
|
|
133
|
+
const activeChunkId = this.activeChunk?.model.id;
|
|
134
|
+
const flattenedItems = this.loadedChunks.flatMap((x) => x.chunkItems);
|
|
135
|
+
const nextItem = flattenedItems[index];
|
|
136
|
+
if (!nextItem) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (nextItem.chunkId !== activeChunkId) {
|
|
140
|
+
const nextChunkIndex = this.loadedChunks.findIndex((c) => c.model.id === nextItem.chunkId);
|
|
141
|
+
if (nextChunkIndex === -1) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const itemIndexInChunk = START_FROM_FIRST_ITEM_ON_CHUNK_SWITCH ? 0 : this.loadedChunks[nextChunkIndex].chunkItems.indexOf(nextItem);
|
|
145
|
+
this.setActiveChunkIndex(nextChunkIndex, itemIndexInChunk);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
this.activeChunk.setActiveItemIndex(this.activeChunk.chunkItems.indexOf(nextItem));
|
|
149
|
+
// Don't wait for warm up to be finished, it runs in the background
|
|
150
|
+
this.warmUp();
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
warmUp = async () => {
|
|
154
|
+
if (this._warmUpDeferred) {
|
|
155
|
+
return this._warmUpDeferred.promise;
|
|
156
|
+
}
|
|
157
|
+
this._warmUpDeferred = new Deferred();
|
|
158
|
+
try {
|
|
159
|
+
await this.warmUpSequentially();
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
this._warmUpDeferred.resolve();
|
|
163
|
+
this._warmUpDeferred = null;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
reset = async () => {
|
|
167
|
+
this._activeChunkIndex = -1;
|
|
168
|
+
this._loadedChunks = [];
|
|
169
|
+
this._warmUpDeferred = null;
|
|
170
|
+
await this.warmUp();
|
|
171
|
+
};
|
|
172
|
+
warmUpSequentially = async () => {
|
|
173
|
+
const startChunkIndex = Math.max(0, this._activeChunkIndex);
|
|
174
|
+
// Calculate how many items we need ahead of current position
|
|
175
|
+
const getItemsAhead = () => {
|
|
176
|
+
const currentFlatIndex = this.flattenedActiveChunkItemIndex;
|
|
177
|
+
const totalItems = this.flattenedChunkItems.length;
|
|
178
|
+
return totalItems - currentFlatIndex - 1; // -1 because current item doesn't count
|
|
179
|
+
};
|
|
180
|
+
while (getItemsAhead() < ITEMS_BUFFER_SIZE) {
|
|
181
|
+
// Find first non-fully-loaded chunk starting from active
|
|
182
|
+
let targetChunkIndex = -1;
|
|
183
|
+
for (let i = startChunkIndex; i < this._loadedChunks.length; i++) {
|
|
184
|
+
if (this._loadedChunks[i].canLoadMore) {
|
|
185
|
+
targetChunkIndex = i;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// If all chunks are fully loaded, load more chunks
|
|
190
|
+
if (targetChunkIndex === -1) {
|
|
191
|
+
const result = await this.provider.loadMoreChunks();
|
|
192
|
+
if (result.length === 0) {
|
|
193
|
+
// No more chunks available
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
this._loadedChunks = [
|
|
197
|
+
...this._loadedChunks,
|
|
198
|
+
...result.map((chunk) => new PlayerChunk({
|
|
199
|
+
chunk,
|
|
200
|
+
provider: { loadChunkItems: this.provider.loadChunkItems }
|
|
201
|
+
}))
|
|
202
|
+
];
|
|
203
|
+
continue; // Re-check for chunks to fill
|
|
204
|
+
}
|
|
205
|
+
// Load one page from target chunk
|
|
206
|
+
const chunk = this._loadedChunks[targetChunkIndex];
|
|
207
|
+
const itemsBefore = chunk.items.length;
|
|
208
|
+
await chunk.loadMore();
|
|
209
|
+
const itemsAfter = chunk.items.length;
|
|
210
|
+
// If chunk became fully loaded but added no items, continue to next chunk
|
|
211
|
+
if (itemsAfter === itemsBefore && !chunk.canLoadMore) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
// If no progress made and chunk is not fully loaded, something went wrong
|
|
215
|
+
if (itemsAfter === itemsBefore && chunk.canLoadMore) {
|
|
216
|
+
break; // Avoid infinite loop
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { IChunksPlayerBuffer, IChunksPlayerDataProvider, WithId } from './types';
|
|
2
|
+
export declare class DefaultChunksPlayerBuffer<TItem extends WithId, TChunk extends WithId> implements IChunksPlayerBuffer<TItem> {
|
|
3
|
+
readonly kind = "chunks";
|
|
4
|
+
readonly loaded: TItem[];
|
|
5
|
+
readonly currentIndex: number;
|
|
6
|
+
readonly current: TItem | null;
|
|
7
|
+
readonly canLoadNext: boolean;
|
|
8
|
+
readonly canLoadPrevious: boolean;
|
|
9
|
+
readonly navigationDisabled: boolean;
|
|
10
|
+
readonly animationDuration = 500;
|
|
11
|
+
private _playerChunksManager;
|
|
12
|
+
private _onEndReachedFn;
|
|
13
|
+
constructor(provider: IChunksPlayerDataProvider<TItem, TChunk>);
|
|
14
|
+
get activeChunk(): import("./chunks-player-buffer").PlayerChunk<TItem, TChunk>;
|
|
15
|
+
setActiveChunkItemIndex: (index: number) => void;
|
|
16
|
+
tryActivateItemById: (id: string) => boolean;
|
|
17
|
+
removeChunkById: (id: string) => boolean | undefined;
|
|
18
|
+
removeItemById: (id: string) => boolean;
|
|
19
|
+
loadNext: () => Promise<void>;
|
|
20
|
+
loadPrevious: () => Promise<void>;
|
|
21
|
+
reset: () => void;
|
|
22
|
+
ensureWarmedUp: () => Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Utils } from '../../../core/utils';
|
|
2
|
+
import { PlayerChunksManager } from './chunks-player-buffer';
|
|
3
|
+
export class DefaultChunksPlayerBuffer {
|
|
4
|
+
kind = 'chunks';
|
|
5
|
+
loaded = $derived.by(() => this._playerChunksManager.flattenedChunkItems);
|
|
6
|
+
currentIndex = $derived.by(() => this._playerChunksManager.flattenedActiveChunkItemIndex);
|
|
7
|
+
current = $derived(this.currentIndex >= 0 ? this.loaded[this.currentIndex] : null);
|
|
8
|
+
canLoadNext = $derived.by(() => {
|
|
9
|
+
if (this.loaded.length && this.currentIndex < this.loaded.length - 1) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
// Check if manager is still loading
|
|
13
|
+
if (this._playerChunksManager.isLoading) {
|
|
14
|
+
return false; // Don't trigger onEndReached while loading
|
|
15
|
+
}
|
|
16
|
+
return !!this._onEndReachedFn;
|
|
17
|
+
});
|
|
18
|
+
canLoadPrevious = $derived(this.currentIndex > 0);
|
|
19
|
+
navigationDisabled = $derived(!this.canLoadNext && !this.canLoadPrevious);
|
|
20
|
+
animationDuration = 500;
|
|
21
|
+
_playerChunksManager;
|
|
22
|
+
_onEndReachedFn;
|
|
23
|
+
constructor(provider) {
|
|
24
|
+
this._onEndReachedFn = provider.onEndReached;
|
|
25
|
+
// Throttle navigation methods
|
|
26
|
+
this.loadNext = Utils.throttle(this.loadNext, this.animationDuration);
|
|
27
|
+
this.loadPrevious = Utils.throttle(this.loadPrevious, this.animationDuration);
|
|
28
|
+
this._playerChunksManager = new PlayerChunksManager(provider);
|
|
29
|
+
this._playerChunksManager.initialize();
|
|
30
|
+
}
|
|
31
|
+
get activeChunk() {
|
|
32
|
+
return this._playerChunksManager.activeChunk;
|
|
33
|
+
}
|
|
34
|
+
setActiveChunkItemIndex = (index) => {
|
|
35
|
+
if (!this.activeChunk) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
this.activeChunk.setActiveItemIndex(index);
|
|
39
|
+
};
|
|
40
|
+
tryActivateItemById = (id) => {
|
|
41
|
+
return this._playerChunksManager.tryActivateItemById(id);
|
|
42
|
+
};
|
|
43
|
+
removeChunkById = (id) => {
|
|
44
|
+
return this._playerChunksManager.removeChunkById(id);
|
|
45
|
+
};
|
|
46
|
+
removeItemById = (id) => {
|
|
47
|
+
return this._playerChunksManager.removeItemById(id);
|
|
48
|
+
};
|
|
49
|
+
loadNext = async () => {
|
|
50
|
+
if (!this.canLoadNext) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (this.currentIndex < this.loaded.length - 1) {
|
|
54
|
+
this._playerChunksManager.activateItemAtFlattenedIndex(this.currentIndex + 1);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
this._onEndReachedFn?.();
|
|
58
|
+
};
|
|
59
|
+
loadPrevious = async () => {
|
|
60
|
+
if (!this.canLoadPrevious) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
this._playerChunksManager.activateItemAtFlattenedIndex(this.currentIndex - 1);
|
|
64
|
+
};
|
|
65
|
+
reset = () => {
|
|
66
|
+
this._playerChunksManager.reset();
|
|
67
|
+
};
|
|
68
|
+
ensureWarmedUp = async () => {
|
|
69
|
+
await this._playerChunksManager.warmUp();
|
|
70
|
+
};
|
|
71
|
+
}
|