import { unwrap , isArray , isUndefined , ProxyPropertyKey } from './shared' ; import { BaseProxyHandler , ShadowTarget } from './base-handler' ; const getterMap = new WeakMap < ( ) => any , ( ) => any > ( ) ; const setterMap = new WeakMap < ( v : any ) => void , ( v : any ) => void > ( ) ; export class ReadOnlyHandler extends BaseProxyHandler { wrapValue ( value ) { return this . membrane . getReadOnlyProxy ( value ) ; } wrapGetter ( originalGet ) { const wrappedGetter = getterMap . get ( originalGet ) ; if ( ! isUndefined ( wrappedGetter ) ) { return wrappedGetter ; } const handler = this ; const get = function ( this ) : any { return handler . wrapValue ( originalGet . call ( unwrap ( this ) ) ) ; } ; getterMap . set ( originalGet , get ) ; return get ; } wrapSetter ( originalSet ) { const wrappedSetter = setterMap . get ( originalSet ) ; if ( ! isUndefined ( wrappedSetter ) ) { return wrappedSetter ; } const handler = this ; const set = function ( this , v ) { if ( process . env . NODE_ENV !== 'production' ) { const { originalTarget } = handler ; throw new Error ( `Invalid mutation: Cannot invoke a setter on "${ originalTarget }". "${ originalTarget }" is read-only.` ) ; } } ; setterMap . set ( originalSet , set ) ; return set ; } set ( shadowTarget , key , value ) { if ( process . env . NODE_ENV !== 'production' ) { const { originalTarget } = this ; const msg = isArray ( originalTarget ) ? `Invalid mutation: Cannot mutate array at index ${ key . toString ( ) }. Array is read-only.` : `Invalid mutation: Cannot set "${ key . toString ( ) }" on "${ originalTarget }". "${ originalTarget }" is read-only.` ; throw new Error ( msg ) ; } return false ; } deleteProperty ( shadowTarget , key ) { if ( process . env . NODE_ENV !== 'production' ) { const { originalTarget } = this ; throw new Error ( `Invalid mutation: Cannot delete "${ key . toString ( ) }" on "${ originalTarget }". "${ originalTarget }" is read-only.` ) ; } return false ; } setPrototypeOf ( shadowTarget , prototype ) { if ( process . env . NODE_ENV !== 'production' ) { const { originalTarget } = this ; throw new Error ( `Invalid prototype mutation: Cannot set prototype on "${ originalTarget }". "${ originalTarget }" prototype is read-only.` ) ; } } preventExtensions ( shadowTarget ) { if ( process . env . NODE_ENV !== 'production' ) { const { originalTarget } = this ; throw new Error ( `Invalid mutation: Cannot preventExtensions on ${ originalTarget }". "${ originalTarget } is read-only.` ) ; } return false ; } defineProperty ( shadowTarget , key , descriptor ) { if ( process . env . NODE_ENV !== 'production' ) { const { originalTarget } = this ; throw new Error ( `Invalid mutation: Cannot defineProperty "${ key . toString ( ) }" on "${ originalTarget }". "${ originalTarget }" is read-only.` ) ; } return false ; } }