Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
fix: config props
Browse files Browse the repository at this point in the history
  • Loading branch information
fikryfahrezy committed Nov 27, 2023
1 parent e790352 commit 263e648
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/lib/PlayerMedia.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Player,
PlayerRef,
PlayerDispatcher,
PlayerConfigObject,
PlayerConfigProps,
PlayerGetPlayerKey,
PlayerInternalPlayer
} from './players/types';
Expand All @@ -27,7 +27,7 @@
export let playsinline: boolean;
export let pip: boolean;
export let stopOnUnmount: boolean;
export let config: PlayerConfigObject;
export let config: PlayerConfigProps;
export let progressFrequency: number | undefined = undefined;
export let disableDeferredLoading: boolean | undefined = undefined;
Expand Down
11 changes: 7 additions & 4 deletions src/lib/SveltePlayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import type {
PlayerUrl,
PlayerConfig,
PlayerConfigKey,
PlayerConfigProps,
PlayerGetPlayerKey,
PlayerInternalPlayer
} from './players/types';
Expand Down Expand Up @@ -128,9 +128,12 @@
dispatch('ready');
}
function getConfig<T extends PlayerConfigKey>(configUrl: PlayerUrl, configKey: T) {
const memoized = memoize<(url: typeof configUrl, key: T) => PlayerConfig[T]>((_, key) => {
return merge<PlayerConfig[T]>(defaultConfig[key] || {}, config[key] || {});
function getConfig<TUrl extends PlayerUrl, TKey extends string>(
configUrl: TUrl,
configKey: TKey
) {
const memoized = memoize<(url: TUrl, key: TKey) => PlayerConfigProps>((_, key) => {
return merge<PlayerConfigProps>(defaultConfig[key] || {}, config[key] || {});
});
return memoized(configUrl, configKey);
Expand Down
29 changes: 19 additions & 10 deletions src/lib/players/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import type {
FileErrorSDKGlobal,
FileInternalPlayer
} from './file.types';
import type { NotImplementedConfig } from './notimplemented.types';

export type PlayerInstance =
| YTPlayer
Expand All @@ -52,20 +51,32 @@ export type PlayerInternalPlayer = Omit<FileInternalPlayer, 'player'> & {

export type PlayerGetPlayerKey = keyof PlayerInternalPlayer;

export type PlayerConfig = {
export type PlayerConfigObject = {
youtube: YouTubeConfig;
soundcloud: SoundCloudConfig;
vimeo: ViemoConfig;
facebook: FacebookConfig;
streamable: undefined;
wistia: WistiaConfig;
twitch: TwitchConfig;
dailymotion: DailyMotionConfig;
mixcloud: MixcloudConfig;
vidyard: VidyardConfig;
kaltura: undefined;
file: FileConfig;
'not-implemented': NotImplementedConfig;
};

export type PlayerConfigKey = keyof PlayerConfigObject;

export type PlayerConfigValue = PlayerConfigObject[PlayerConfigKey];

/* eslint-disable-next-line @typescript-eslint/ban-types
-- This is hacky trick to preserve auto-complete the possible union string
meanwhile still able to assign string value outside the unioin string */
export type PlayerKey = PlayerConfigKey | (string & {});

export type PlayerConfig = {
[Property in PlayerKey]: Property extends PlayerConfigKey
? PlayerConfigObject[Property]
: unknown;
};

export type PlayerUrl = string | YouTubeUrl | FileUrl;
Expand Down Expand Up @@ -112,9 +123,7 @@ export type PlayerDispatcher = {
loaded: undefined;
};

export type PlayerConfigKey = keyof PlayerConfig;

export type PlayerConfigObject = PlayerConfig[PlayerConfigKey];
export type PlayerConfigProps = RecursivePartial<PlayerConfigValue>;

export type PlayerProps = {
url: PlayerUrl;
Expand All @@ -132,7 +141,7 @@ export type PlayerProps = {
pip: boolean;
stopOnUnmount: boolean;
previewTabIndex: number;
config: RecursivePartial<PlayerConfigObject>;
config: PlayerConfigProps;
oEmbedUrl: string;
display: string;
};
Expand Down Expand Up @@ -160,7 +169,7 @@ export type PlayerRef = {
export type PlayerComponent = Constructor<SvelteComponent<Partial<PlayerProps>> & PlayerRef>;

export type Player = {
key: PlayerConfigKey;
key: PlayerKey;
name: string;
loopOnEnded?: boolean;
forceLoad?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/players/utility.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export type Constructor<T> = new (...args: any[]) => T;

export type Prettify<T> = {
[K in keyof T]: T[K];
/* eslint-disable-next-line @typescript-eslint/ban-types -- TODO: Comment this*/
/* eslint-disable-next-line @typescript-eslint/ban-types -- this is fine*/
} & {};

// https://stackoverflow.com/a/70428144/12976234
export type PickMatching<T, V> = { [K in keyof T as T[K] extends V ? K : never]: T[K] };
/* eslint-disable-next-line @typescript-eslint/ban-types -- TODO: Comment this*/
/* eslint-disable-next-line @typescript-eslint/ban-types -- this is fine*/
export type ObjectMethods<T> = PickMatching<T, Function>;

/* eslint-disable-next-line @typescript-eslint/no-explicit-any -- this is fine*/
Expand Down
5 changes: 1 addition & 4 deletions src/lib/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const defaultConfig: PlayerConfig = {
show_playcount: false
}
},
streamable: undefined,
youtube: {
playerVars: {
playsinline: 1,
Expand Down Expand Up @@ -43,7 +42,6 @@ export const defaultConfig: PlayerConfig = {
},
title: null
},
kaltura: undefined,
file: {
attributes: {},
tracks: [],
Expand Down Expand Up @@ -74,6 +72,5 @@ export const defaultConfig: PlayerConfig = {
},
vidyard: {
options: {}
},
'not-implemented': {}
}
};
2 changes: 1 addition & 1 deletion src/test/players/DailyMotion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test('load()', async function (t) {
return Promise.resolve();
});

// TODO: Add test for this, this behaciour cannot
// TODO: Add test for this, this behaviour cannot
// be tested yet because we can't set container
// programmatically
test('load() - no container', async function (t) {
Expand Down

0 comments on commit 263e648

Please sign in to comment.