/**
 * Copyright (c) 2020 Bytedance Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import createHash from '@/utils/createHash' ; const globalHeapKey = '__MAGIC_HEAP__' ; type DataMapType = Record < string , unknown > ; declare global { interface Window { __MAGIC_HEAP__ : DataMapType ; } } type PropTypes = typeof Number | typeof Boolean | typeof String | typeof Array | typeof Function | typeof Object ; export type PropTypesMap < Props extends { } = Record < string , unknown > > = Record < keyof Props , PropTypes > ; export type ValueOf < Props > = Props [ keyof Props ] ; class Heap { dataMap : DataMapType = { } ; constructor ( ) { if ( ! window [ globalHeapKey ] ) { window [ globalHeapKey ] = { } ; } this . dataMap = window [ globalHeapKey ] ; } useProps = < T > ( value ) : string => { let propId = createHash ( 12 ) ; while ( Object . prototype . hasOwnProperty . call ( this . dataMap , propId ) ) { propId = createHash ( 12 ) ; } this . dataMap [ propId ] = value ; return propId ; } ; getPropsValue = < Props extends { } = Record < string , unknown > > ( name , value , propTypes = { } as PropTypesMap < Props > ) : ValueOf < Props > => { if ( propTypes [ name ] === Boolean ) { if ( value === 'false' ) { return ( false as unknown ) as ValueOf < Props > ; } return ( true as unknown ) as ValueOf < Props > ; } else if ( propTypes [ name ] === Number ) { return ( Number ( value ) as unknown ) as ValueOf < Props > ; } if ( this . dataMap [ value ] ) { const heapValue = this . dataMap [ value ] ; delete this . dataMap [ value ] ; return heapValue as ValueOf < Props > ; } return ( value as unknown ) as ValueOf < Props > ; } ; } export default new Heap ( ) ;