/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ const IS_PLATFORM_OBJECT_KEY = Symbol('isPlatformObject') const CLONE_PLATFORM_OBJECT_KEY = Symbol('clonePlatformObject') type CloneFn = (value: T) => T /** * Marks the given object or instances of the given class as platform objects. * * Optionally, it sets the clone function for that platform object, which is a * simplification of the serializable attribute of the Web interface. */ export function setPlatformObject( obj: { prototype: unknown }, options?: { clone: CloneFn } ): void export function setPlatformObject(obj: T, options?: { clone: CloneFn }): void export function setPlatformObject(obj: any, options?: { clone: (value: any) => any }): void { if (typeof obj === 'function') { ;(obj.prototype as any)[IS_PLATFORM_OBJECT_KEY] = true if (options) { ;(obj.prototype as any)[CLONE_PLATFORM_OBJECT_KEY] = options.clone } } else { ;(obj as any)[IS_PLATFORM_OBJECT_KEY] = true if (options) { ;(obj as any)[CLONE_PLATFORM_OBJECT_KEY] = options.clone } } } /** * Indicates if the given object is a platform object. */ export function isPlatformObject(obj: T): boolean { return IS_PLATFORM_OBJECT_KEY in (obj as any) } /** * Returns the clone function for the given platform object, if it was set. */ export function getPlatformObjectClone(obj: T): ((value: T) => T) | undefined { return (obj as any)[CLONE_PLATFORM_OBJECT_KEY] }