From 9d47dee858c53318d7f41eb2f9d99d24e9217c8c Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 5 Apr 2023 14:38:22 -0400 Subject: [PATCH] feat: Introduce PlatformSpecific as a new wrapper type for injectables Signed-off-by: Sebastian Malton --- .../common/utils/platform-specific-version.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 packages/core/src/common/utils/platform-specific-version.ts diff --git a/packages/core/src/common/utils/platform-specific-version.ts b/packages/core/src/common/utils/platform-specific-version.ts new file mode 100644 index 0000000000..adbc9971cc --- /dev/null +++ b/packages/core/src/common/utils/platform-specific-version.ts @@ -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 { + instantiate: () => T; + readonly platform: NodeJS.Platform | ""; +} + +export const getPlatformSpecificFor = (targetPlatform: NodeJS.Platform, id: string) => (specificImplementations: PlatformSpecific[]): T => { + const impl = specificImplementations.find(impl => impl.platform === targetPlatform) + ?? specificImplementations.find(impl => impl.platform === ""); + + if (!impl) { + throw new Error(`No platform specific implementation of "${id}" found`); + } + + return impl.instantiate(); +}