Skip to content

Commit

Permalink
feat: add alternative way to inform the new property
Browse files Browse the repository at this point in the history
  • Loading branch information
franpeza committed Jan 9, 2024
1 parent de7b505 commit 87f4dbb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/lib/main/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function snippet(
// remove any previously patched functions
if (top == win) {
(config!.forward || []).map(function (forwardProps) {
const { property } = resolvePartytownForwardProperty(forwardProps);
const [property] = resolvePartytownForwardProperty(forwardProps);
delete win[property.split('.')[0] as any];
});
}
Expand Down Expand Up @@ -141,7 +141,7 @@ export function snippet(
// this is the top window
// patch the functions that'll be forwarded to the worker
(config.forward || []).map(function (forwardProps) {
const { property, preserveBehavior } = resolvePartytownForwardProperty(forwardProps);
const [property, { preserveBehavior }] = resolvePartytownForwardProperty(forwardProps);
mainForwardFn = win;
property.split('.').map(function (_, i, forwardPropsArr) {
mainForwardFn = mainForwardFn[forwardPropsArr[i]] =
Expand Down
2 changes: 1 addition & 1 deletion src/lib/sandbox/main-forward-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const mainForwardTrigger = (worker: PartytownWebWorker, $winId$: WinId, w
win._ptf = undefined;

forwards.map((forwardProps) => {
const { property, preserveBehavior } = resolvePartytownForwardProperty(forwardProps);
const [property, { preserveBehavior }] = resolvePartytownForwardProperty(forwardProps);
mainForwardFn = win;
property.split('.').map((_, i, arr) => {
mainForwardFn = mainForwardFn[arr[i]] =
Expand Down
8 changes: 6 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,11 @@ export interface PartytownConfig {
nonce?: string;
}

export type PartytownForwardSettingsProperty = { property: string; preserveBehavior: boolean };
export type PartytownForwardPropertySettings = {
preserveBehavior?: boolean;
};

export type PartytownForwardPropertyWithSettings = [string, PartytownForwardPropertySettings?];

/**
* A forward property to patch on `window`. The forward config property is an string,
Expand All @@ -532,7 +536,7 @@ export type PartytownForwardSettingsProperty = { property: string; preserveBehav
*
* @public
*/
export type PartytownForwardProperty = string | PartytownForwardSettingsProperty;
export type PartytownForwardProperty = string | PartytownForwardPropertyWithSettings;

/**
* @public
Expand Down
22 changes: 13 additions & 9 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type {
ApplyPath,
MainWindow,
PartytownForwardProperty,
PartytownForwardSettingsProperty,
PartytownForwardPropertySettings,
PartytownForwardPropertyWithSettings,
RandomId,
StringIndexable,
} from './types';
Expand Down Expand Up @@ -145,16 +146,19 @@ export const isValidUrl = (url: any): boolean => {
}
};

const defaultPartytownForwardPropertySettings: Required<PartytownForwardPropertySettings> = {
preserveBehavior: false,
};

export const resolvePartytownForwardProperty = (
property: PartytownForwardProperty
): PartytownForwardSettingsProperty => {
if (typeof property === 'string') {
return {
property,
preserveBehavior: false,
};
propertyOrPropertyWithSettings: PartytownForwardProperty
): Required<PartytownForwardPropertyWithSettings> => {
if (typeof propertyOrPropertyWithSettings === 'string') {
return [propertyOrPropertyWithSettings, defaultPartytownForwardPropertySettings];
}
return property;
const [property, settings = defaultPartytownForwardPropertySettings] =
propertyOrPropertyWithSettings;
return [property, { ...defaultPartytownForwardPropertySettings, ...settings }];
};

type GetOriginalBehaviorReturn = {
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/event-forwarding/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'superDuperFunction',
'superArray.push',
'KiwiSizing',
{ property: 'superPreservedArray.unshift', preserveBehavior: true },
['superPreservedArray.unshift', { preserveBehavior: true }],
],
logCalls: true,
logGetters: true,
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/gtm/preserve-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

return url;
},
forward: [{ property: 'dataLayer.push', preserveBehavior: true }],
forward: [['dataLayer.push', { preserveBehavior: true }]],
logCalls: true,
logGetters: true,
logSetters: true,
Expand Down

0 comments on commit 87f4dbb

Please sign in to comment.