-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[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
Conversation
/** | ||
* 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; |
There was a problem hiding this comment.
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
d6f1001
to
d7a49d7
Compare
Hey @AbhiPrasad I am messing around with something, and I would like to get the output of |
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 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 4 AM, time to sleep ... |
@@ -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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
packages/hub/src/hub.ts
Outdated
export function getStack(hub: Hub): Layer[] { | ||
return hub.stack; | ||
} |
There was a problem hiding this comment.
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.
packages/hub/src/hub.ts
Outdated
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)); |
There was a problem hiding this comment.
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.
packages/hub/src/scope.ts
Outdated
|
||
/** Session */ | ||
protected _session?: Session; | ||
public _session?: Session; |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
Interesting, |
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 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 |
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 "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
No 😭 I am gonna continue breaking things 😭 |
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 "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
kekBYE PR was fun thou! |
If you wanna chat further - recently just opened up #4660 |
Was an amazing opportunity to hang out with y'all, really fun. 👋🏻 PR |
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:
yarn lint
) & (yarn test
).