Skip to content

Commit

Permalink
format getter index lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Oct 3, 2021
1 parent 4965587 commit 839622a
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {
AccessType,
InterfaceType,
MainWindowContext,
NodeName,
PlatformInstanceId,
} from './types';
import {
InstanceIdKey,
InterfaceTypeKey,
NodeNameKey,
webWorkerCtx,
WinIdKey,
} from './web-worker/worker-constants';
import { InterfaceType, MainWindowContext, NodeName, PlatformInstanceId } from './types';

export const debug = (globalThis as any).partytownDebug;

Expand Down Expand Up @@ -63,9 +69,10 @@ export const logWorkerGetter = (
return;
}
logWorker(
`Get ${logTargetProp(target, memberPath)}, returned: ${logValue(memberPath, rtn)}${
isCached ? ' (cached)' : ''
}`
`Get ${logTargetProp(target, AccessType.Get, memberPath)}, returned: ${logValue(
memberPath,
rtn
)}${isCached ? ' (cached)' : ''}`
);
} catch (e) {}
}
Expand All @@ -77,7 +84,12 @@ export const logWorkerSetter = (target: any, memberPath: string[], value: any) =
if (target && target[WinIdKey] !== webWorkerCtx.$winId$) {
return;
}
logWorker(`Set ${logTargetProp(target, memberPath)}, value: ${logValue(memberPath, value)}`);
logWorker(
`Set ${logTargetProp(target, AccessType.Set, memberPath)}, value: ${logValue(
memberPath,
value
)}`
);
} catch (e) {}
}
};
Expand All @@ -89,15 +101,15 @@ export const logWorkerCall = (target: any, memberPath: string[], args: any[], rt
return;
}
logWorker(
`Call ${logTargetProp(target, memberPath)}(${args
`Call ${logTargetProp(target, AccessType.CallMethod, memberPath)}(${args
.map((v) => logValue(memberPath, v))
.join(', ')}), returned: ${logValue(memberPath, rtn)}`
);
} catch (e) {}
}
};

const logTargetProp = (target: any, memberPath: string[]) => {
const logTargetProp = (target: any, accessType: AccessType, memberPath: string[]) => {
let n = '';
if (target) {
const instanceId = target[InstanceIdKey];
Expand Down Expand Up @@ -136,6 +148,13 @@ const logTargetProp = (target: any, memberPath: string[]) => {
console.warn('¯\\_(ツ)_/¯ TARGET', target);
}
}
if (AccessType.Get === accessType && memberPath.length > 1) {
const first = memberPath.slice(0, memberPath.length - 1);
const last = memberPath[memberPath.length - 1];
if (!isNaN(last as any)) {
return (n += `${first.join('.')}[${last}]`);
}
}
return (n += memberPath.join('.'));
};

Expand Down

0 comments on commit 839622a

Please sign in to comment.