8000 Prepare moving to context API by roderickhsiao · Pull Request #259 · yahoo/react-i13n · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Prepare moving to context API #259

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

Merged
merged 44 commits into from
Apr 26, 2021
Merged

Prepare moving to context API #259

merged 44 commits into from
Apr 26, 2021

Conversation

roderickhsiao
Copy link
Collaborator
@roderickhsiao roderickhsiao commented May 28, 2020

Implementation

  1. Remove augmentComponent and componentSpec, several issues for the current implementation
  • We use the same spec file to for root and child/leave components but just cherrypick some of the prototype/static method for root, which doesn't make much sense to share the logic as most of the logic just for child node, and it increase the complexity greatly
  • We never really document some API such as this.shouldFollowLink, but in our code logic we are assuming the method exists (black magic).
  • We heavily relies on assigning global (window instance) and access cross function or passing via context/props, which makes the logic pretty complicate (as we need to fallback check the context or props)
  1. Move to use React context. Centralized some method such as executeEvent, or the reactI13nInstance, each component we wrapped with another Context.Provider to overrides the I13nNode and ParentNode but share those common function
  2. Migrate componentSpec to hooks so the lifecycle will be much cleaner, and we can separate hooks like plugin (useScanLinks, useViewportDetect` ...) and each hooks handle its own logic, the main I13nComponent file will be much cleaner to read
  3. For external facing API, we will create both HOC and hook for user to access the method, so it could be used in both functional component/class
  4. Replace viewport detect logic using intersection observer

Breaking changes

  1. If you need to support browser not supporting intersection observer, you must require polyfill
  2. If you are using functional component or class for createI13nNode, we will pass innerProps to the wrapped component, and you must attach to your render function
  3. No longer expose ReactI13n.getInstance, you can directly import { getInstance } from 'react-i13n
  4. Remove legacy context this.context.i13n, we will use directly react context import { I13nContext } from 'react-i13n which expose executeEvent

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.


};

export default I13nComponent;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This component will be the main class handling different functions (replacing ComponentSpecs)

// getI13nNode: this.getI13nNode.bind(this),
// reactI13nInstance: this._reactI13nInstance
// };
// }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We probably don't need to pass props anymore for accessing these function

let href = '';

// return and do nothing if the handler is append on a component without I13nMixin
if (!self.executeI13nEvent) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

make no sense for a handler to assume caller method

* @method getInstance
* @return the ReactI13n instance
*/
static getInstance() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is for people accessing outside of React tree, moving to another export method and change not to pollute window

@@ -47,7 +26,7 @@ const staticSpecs = {
// remove propTypes for production build
propTypes: {
component: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
model: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
// model: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

legacy props, planned to remove it

}
// if (self.props.scanLinks && self.props.scanLinks.enable) {
// self._scanLinks();
// }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

slowly migrate logic to hooks and the new component entry point, comment out logic I already migrated

// });
// self._getReactI13n().execute('created', { i13nNode });
// });
// },

/**
* _shouldFollowLink, provide a hook to check followLink.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We never expose shouldFollowLink to any documentation, probably only use internally. This should either be a props or something else if we want to support it. (functional component won't work anyway)

const self = this;
if (self._subI13nComponents && self._subI13nComponents.length > 0) {
self._subI13nComponents.forEach((subI13nComponent) => {
subI13nComponent.componentClickListener.remove();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fun fact, this should be unsubscribe instead of remove
https://github.com/yahoo/subscribe-ui-event

const result = window._reactI13nInstance.getRootI13nNode().getMergedModel();
expect(result).to.eql({ sec: 'default-section-name', page: 'test-page' });
});

it('should fire a pageview', () => {
it.only('should fire a pageview', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will enable test one by one to make sure all cases are covered (except if we have breaking changes)

@roderickhsiao roderickhsiao mentioned this pull request Jul 21, 2020
17 tasks
});

it('should not follow it if follow is set to false', (done) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

duplicated test

mockComponent = {
props: {
href: 'foo'
},
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is actually a bug which hides BUTTON logic.

// Do not trigger navigate action. Let browser handle it natively.
if (!href || (href && href[0] === '#')) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is actually an existing bug, when a button-ish type tag is used, we will never have href, and we always set the value to false even there is a form. the callback below will never be executed.

@roderickhsiao roderickhsiao marked this pull request as ready for review April 23, 2021 16:57
@roderickhsiao roderickhsiao merged commit 01106be into master Apr 26, 2021
@roderickhsiao roderickhsiao deleted the context branch April 26, 2021 19:25
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.

1 participant
0