Skip to content
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

Add new buffered methods, improve types #561

Merged
merged 6 commits into from
Aug 3, 2022

Conversation

silesky
Copy link
Contributor

@silesky silesky commented Aug 1, 2022

This PR:

  • Adds 'screen', 'register', 'deregister', 'user' method and 'VERSION' property on AnalyticsBrowser. 🎸
  • Allows buffering of 'screen', 'register', 'deregister' methods for snippet users. 🎹
  • Improves typing -- e.g better names, add overloads 🎷

Outstanding todo: update the actual UI snippet (the one that shows up on app.segment.com?) so that the copypasta from includes 'screen', 'register', and 'deregister' ⛑️

😢 Because of some differences in return types between AnalyticsBrowser and Analytics (AnalyticsBrowser always being async, while Analytics is inconsistent in that regard) the dream of a unified AnalyticsCore interface between Analytics and AnalyticsBrowser is on hold.

It’s IMO advisable to not try to obsess over strict sharing of a public interfaces, as that common abstraction does not seem possible, and it can in addition become hard to maintain either for the legacy behavior of one system or due to subtle differences between platforms. Ultimately, our API is just a bags of methods and we want to evolve and be nimble, and not hamstring ourselves.

We can continue to share models and types where appropriate.

@changeset-bot
Copy link

changeset-bot bot commented Aug 1, 2022

🦋 Changeset detected

Latest commit: 822b057

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@segment/analytics-next Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@silesky silesky requested review from zikaari and chrisradek August 1, 2022 21:12
@silesky silesky force-pushed the chore/refactor-analytics-core-improve-types branch 2 times, most recently from 1d0b9d6 to 63cda2b Compare August 1, 2022 21:24
@silesky silesky changed the title Chore/refactor analytics core improve types add new buffered methods, improve types Aug 1, 2022
@silesky silesky changed the title add new buffered methods, improve types Add new buffered methods, improve types Aug 1, 2022
@silesky silesky force-pushed the chore/refactor-analytics-core-improve-types branch from b964248 to 8e8e76f Compare August 1, 2022 21:54
@silesky silesky requested a review from pooyaj August 1, 2022 21:56
packages/browser/src/core/events/interfaces.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@chrisradek chrisradek left a comment

Choose a reason for hiding this comment

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

Love seeing the addition of more methods on the buffered Analytics class! Had some pretty minor comments.

packages/browser/src/core/analytics/index.ts Outdated Show resolved Hide resolved
packages/browser/src/core/events/interfaces.ts Outdated Show resolved Hide resolved
properties?: object & {
[k: string]: JSONValue
}
properties?: EventProperties
Copy link
Contributor

Choose a reason for hiding this comment

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

This might be too strict - we have some users passing in an instance of a class as event properties and TypeScript will complain that the class doesn't have the index signature.

Copy link
Contributor Author

@silesky silesky Aug 3, 2022

Choose a reason for hiding this comment

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

I don’t think the official typescript interface should support people passing “any class”, since it’s AFAIK not possible in TS to distinguish between an intentionally passed class instance and an accidentally-passed function or array — the problem with the “object” type is that so much in JS is an object.

I think we should write the typescript API to encourage people to do things idiomatically, i.e create a toJSON() on their class or construct an object literal using class properties via {…new MyClass()} — but accepting any class seems like smell to me — and, as a consumer, the looseness of object would be a source of confusion (and uncaught typescript bugs).

Since this isn’t a runtime change, and is a non breaking change (in the Typescript “correctness” sense) that affects a super minority of npm typescript users, I feel good about this. They are always free to keep using vanilla JS and ignore or unsafely cast in their own calling code, but I want us to lead them into the light — and also do a better job of using Typescript to clarify and document the API (e.g using overloads).

/my two cents

@silesky silesky force-pushed the chore/refactor-analytics-core-improve-types branch from 8e8e76f to d27f0f5 Compare August 3, 2022 18:05
@silesky silesky merged commit 1af2a9f into master Aug 3, 2022
@silesky silesky deleted the chore/refactor-analytics-core-improve-types branch August 3, 2022 20:20
@github-actions github-actions bot mentioned this pull request Aug 3, 2022
@chambo-e chambo-e mentioned this pull request Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants