1- import { debug , emptyObjectValue , resolvePartytownForwardProperty } from '../utils' ;
1+ import {
2+ debug ,
3+ emptyObjectValue ,
4+ getOriginalBehavior ,
5+ resolvePartytownForwardProperty ,
6+ } from '../utils' ;
27import type { MainWindow , PartytownConfig } from '../types' ;
38
49export function snippet (
@@ -13,8 +18,7 @@ export function snippet(
1318 scripts ?: NodeListOf < HTMLScriptElement > ,
1419 sandbox ?: HTMLIFrameElement | HTMLScriptElement ,
1520 mainForwardFn : typeof win = win ,
16- isReady ?: number ,
17- forwardPropertyRecords ?: [ typeof win , string , any , any , boolean ] [ ]
21+ isReady ?: number
1822) {
1923 // ES5 just so IE11 doesn't choke on arrow fns
2024 function ready ( ) {
@@ -104,17 +108,10 @@ export function snippet(
104108
105109 // remove any previously patched functions
106110 if ( top == win ) {
107- for ( i = ( forwardPropertyRecords || [ ] ) . length - 1 ; i >= 0 ; i -- ) {
108- const [ thisObject , property , originalValue , forwardValue , hadOwnProperty ] =
109- forwardPropertyRecords ! [ i ] ;
110- if ( thisObject [ property ] === forwardValue ) {
111- if ( hadOwnProperty ) {
112- thisObject [ property ] = originalValue ;
113- } else {
114- delete thisObject [ property ] ;
115- }
116- }
117- }
111+ ( config ! . forward || [ ] ) . map ( function ( forwardProps ) {
112+ const [ property ] = resolvePartytownForwardProperty ( forwardProps ) ;
113+ delete win [ property . split ( '.' ) [ 0 ] as any ] ;
114+ } ) ;
118115 }
119116
120117 for ( i = 0 ; i < scripts ! . length ; i ++ ) {
@@ -148,17 +145,20 @@ export function snippet(
148145 const [ property , { preserveBehavior } ] = resolvePartytownForwardProperty ( forwardProps ) ;
149146 mainForwardFn = win ;
150147 property . split ( '.' ) . map ( function ( _ , i , forwardPropsArr ) {
151- const forwardProperty = forwardPropsArr [ i ] ;
152- const thisObject = mainForwardFn ;
153- const hadOwnProperty = Object . prototype . hasOwnProperty . call ( thisObject , forwardProperty ) ;
154- const originalValue = thisObject [ forwardProperty ] ;
155- const forwardValue =
148+ mainForwardFn = mainForwardFn [ forwardPropsArr [ i ] ] =
156149 i + 1 < forwardPropsArr . length
157- ? originalValue || emptyObjectValue ( forwardPropsArr [ i + 1 ] )
150+ ? mainForwardFn [ forwardPropsArr [ i ] ] || emptyObjectValue ( forwardPropsArr [ i + 1 ] )
158151 : ( ( ) => {
159152 let originalFunction : ( ( ...args : any [ ] ) => any ) | null = null ;
160- if ( preserveBehavior && typeof originalValue === 'function' ) {
161- originalFunction = ( ...args : any [ ] ) => originalValue . apply ( thisObject , ...args ) ;
153+ if ( preserveBehavior ) {
154+ const { methodOrProperty, thisObject } = getOriginalBehavior (
155+ win ,
156+ forwardPropsArr
157+ ) ;
158+ if ( typeof methodOrProperty === 'function' ) {
159+ originalFunction = ( ...args : any [ ] ) =>
160+ methodOrProperty . apply ( thisObject , ...args ) ;
161+ }
162162 }
163163 return function ( ) {
164164 let returnValue : any ;
@@ -170,14 +170,6 @@ export function snippet(
170170 return returnValue ;
171171 } ;
172172 } ) ( ) ;
173- ( forwardPropertyRecords = forwardPropertyRecords || [ ] ) . push ( [
174- thisObject ,
175- forwardProperty ,
176- originalValue ,
177- forwardValue ,
178- hadOwnProperty ,
179- ] ) ;
180- mainForwardFn = thisObject [ forwardProperty ] = forwardValue ;
181173 } ) ;
182174 } ) ;
183175 }
0 commit comments