Skip to content

[IGNORE WIP] Use FP to decrease bundle size #4381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 38 commits into from

Conversation

yordis
Copy link
Contributor

@yordis yordis commented Jan 11, 2022

Note: I have no context of things, so take things with a grain of salt, I am just messing around in public.

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

Comment on lines -19 to -27
/**
* Checks if this hub's version is older than the given version.
*
* @param version A version number to compare to.
* @return True if the given version is newer; otherwise false.
*
* @hidden
*/
isOlderThan(version: number): boolean;
Copy link
Contributor Author

@yordis yordis Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear to me why such function would be in the Hub interface since nowhere in the codebase we call isOlderThan and only from inside the Hub class is where such function is defined, which leads me to believe that was added for testing purpose, but still, don't think it is needed.

Among with other functions, especially that the interface was marked as internal stuff

@yordis yordis force-pushed the testing-fp-things branch from d6f1001 to d7a49d7 Compare January 11, 2022 05:01
@yordis
Copy link
Contributor Author

yordis commented Jan 11, 2022

Hey @AbhiPrasad I am messing around with something, and I would like to get the output of size-limit job in the mid-time, would you mind running the jobs for the first time? 🙏🏻

@yordis yordis mentioned this pull request Jan 11, 2022
@yordis
Copy link
Contributor Author

yordis commented Jan 11, 2022

Well, PoC proven (probably maybe, the correctness is wrong, but not that far off)

Changing the programming style to be more functional allows the minified to compress even more the code. Probably due to the fact that they cant minify things like method names and so on.

Also, probably easier to test, all you have are functions and data (hub data), at the cost of having to import functions instead using dot notations but I think it is beneficial still (bias)

Also, if you continue with this Hub could become just a type since all you care is a contract of the data structure behind, being said, meh.

Want to see how much I can improve it by doing the same strategy. Also really interesting to learn from doing this pattern some issues with missing higher-order functions that could be more specialized or some issue with classes exposing methods as protected but yet in the code, they are used as if they were public ...

4 AM, time to sleep ...

Screen Shot 2022-01-11 at 4 36 30 AM

@@ -48,7 +49,7 @@ export class LinkedErrors implements Integration {
*/
public setupOnce(): void {
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
const self = getCurrentHub().getIntegration(LinkedErrors);
const self = getIntegration(getCurrentHub(), LinkedErrors);
return self ? _handler(self._key, self._limit, event, hint) : event;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self is no longer this ... therefore, _key and _limit should be public ... thinking later how to continue to do more FP tuff in another part of the code base

if (!hub.captureSession) {
return;
}
// TODO: Follow up on this
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get it but I dont ... probably need to circle back on this one

const clientOptions = client ? client.getOptions() : {};
// This checks prevents most of the occurrences of the bug linked below:
// https://github.com/getsentry/sentry-javascript/issues/2622
// The bug is caused by multiple SDK instances, where one is minified and one is using non-mangled code.
// Unfortunatelly we cannot fix it reliably (thus reserved property in rollup's terser config),
// as we cannot force people using multiple instances in their apps to sync SDK versions.
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same deal here, self is no longer this so such keys are public ....

return self._shouldDropEvent(event, options) ? null : event;
}
return event;
});
}

/** JSDoc */
private _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean {
if (this._isSentryError(event, options)) {
public _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved all these functions since they could be static there fore, they could be a function outside the class

*/
export class Hub implements HubInterface {
export class Hub {
Copy link
Contributor Author

@yordis yordis Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle back on removing the class altogether and make a simpler factory function.

Comment on lines 124 to 126
export function getStack(hub: Hub): Layer[] {
return hub.stack;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little trick, ultimately in an FP style, having function getters helps since only one-time the stack will be present since such key cant be minified type of thing, I think.

const registryHubTopStack = getHubFromCarrier(registry).getStackTop();
setHubOnCarrier(activeDomain, new Hub(registryHubTopStack.client, Scope.clone(registryHubTopStack.scope)));
if (!hasHubOnCarrier(activeDomain) || isOlderThan(getHubFromCarrier(activeDomain), API_VERSION)) {
const registryHubTopStack = getStackTop(getHubFromCarrier(registry));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, moved all the methods outside the class, and pass hub as the first parameter.


/** Session */
protected _session?: Session;
public _session?: Session;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same deal here, static clone is static so weird that it was marked as protected since newScope down below is actually accessing the value as it they were public

@@ -64,7 +65,7 @@ export class Angular implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub | any): void {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up on this, any should be remove, temp hack

@yordis
Copy link
Contributor Author

yordis commented Jan 13, 2022

Interesting, @sentry/minimal is just @sentry/hub but using the global Hub. Not sure what would be the benefits over it, especially having spread out things and dealing with compilers.

@yordis
Copy link
Contributor Author

yordis commented Jan 13, 2022

I am back to 🍏 land 😄

After getting familiar with some parts of the codebase, I am biased. I think it simplifies some parts of the code, at the cost of importing functions 🤷🏻

I could probably continue applying some "improvements" in areas like Client and removing the need of minimal and move such code to hub since it is just a wrapper around it that uses the global instance of the Hub.

Also, keep removing a lot of magical strings that can't be minified that ended up calling functions. Since you don't need to serialize most abstractions, we are better off doing it the functional programming way as I did in part of the code already.

I haven't gotten into Integrations, but I think the same deal doesn't need to be an interface. a simple factory function should do it

function makeRewriteFrames(options: { root?: string; prefix?: string; iteratee?: StackFrameIteratee } = {}) {
  // ...

  return ['RewriteFrames', function RewriteFramesProcessor(...) {
    // ....
  }]
}

Something like that.

Or the concept of the integration could be removed as it is right now and be closer to how Sentry work and call them `EventProcessor's instead. Instead of hiding things from people, they can register the processors directly.

But, I have no context of where the Integration concept is heading. So far, a simple function would do the job and may simplify a few things and makes it friendly for minification.

Screen Shot 2022-01-13 at 5 41 26 AM

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2022

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@yordis
Copy link
Contributor Author

yordis commented Feb 3, 2022

No 😭 I am gonna continue breaking things 😭

@github-actions
Copy link
Contributor

github-actions bot commented Mar 1, 2022

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@yordis
Copy link
Contributor Author

yordis commented Mar 1, 2022

kekBYE PR was fun thou!

@AbhiPrasad
Copy link
Member

If you wanna chat further - recently just opened up #4660

@yordis
Copy link
Contributor Author

yordis commented Jul 1, 2022

Was an amazing opportunity to hang out with y'all, really fun. 👋🏻 PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants