mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
feat: Introduce PlatformSpecific as a new wrapper type for injectables
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
7d5b6018a5
commit
9d47dee858
20
packages/core/src/common/utils/platform-specific-version.ts
Normal file
20
packages/core/src/common/utils/platform-specific-version.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
export interface PlatformSpecific<T> {
|
||||
instantiate: () => T;
|
||||
readonly platform: NodeJS.Platform | "<default>";
|
||||
}
|
||||
|
||||
export const getPlatformSpecificFor = (targetPlatform: NodeJS.Platform, id: string) => <T>(specificImplementations: PlatformSpecific<T>[]): T => {
|
||||
const impl = specificImplementations.find(impl => impl.platform === targetPlatform)
|
||||
?? specificImplementations.find(impl => impl.platform === "<default>");
|
||||
|
||||
if (!impl) {
|
||||
throw new Error(`No platform specific implementation of "${id}" found`);
|
||||
}
|
||||
|
||||
return impl.instantiate();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user