Skip to content

Commit

Permalink
Fix initial time (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
roderickhsiao authored Apr 27, 2021
1 parent 9b0e317 commit 59738f2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 112 deletions.
19 changes: 15 additions & 4 deletions src/core/setupI13n.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

import React, { useMemo, useEffect } from 'react';
import React, { useMemo, useEffect, useRef } from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';

import getDisplayName from '../utils/getDisplayName';
Expand Down Expand Up @@ -48,23 +48,34 @@ function setupI13n(Component, options = {}, plugins = []) {
...restProps
} = props;

const isPluginsInitialized = useRef(false);
const {
i13nInstance: reactI13n,
executeI13nEvent: executeEvent
} = useReactI13nRoot(options);

useEffect(() => {
if (reactI13n) {
const initializeReactI13n = () => {
if (reactI13n && !isPluginsInitialized.current) {
plugins.forEach((plugin) => {
reactI13n.plug(plugin);
});
reactI13n.createRootI13nNode();
isPluginsInitialized.current = true;
}
};
// initial
initializeReactI13n();

// Update plugins if reactI13n instance changes
useEffect(() => {
isPluginsInitialized.current = false;
initializeReactI13n();

return () => {
isPluginsInitialized.current = false;
reactI13n?.cleanUpPlugins();
};
}, [reactI13n, plugins]);
}, [reactI13n]);

const parentI13nNode = reactI13n?.getRootI13nNode();

Expand Down
106 changes: 0 additions & 106 deletions src/libs/ViewportDetector.js

This file was deleted.

7 changes: 5 additions & 2 deletions tests/functional/i13n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ describe('React I13n test', () => {
expect(events[currentEventCount - 1].position).to.eql(1);
});

it('should fire a enterViewport', () => {
it('should fire a enterViewport', (done) => {
const events = window.firedEvents;
expect(events.filter(({ name }) => name === 'enterViewport').length).to.greaterThan(1);
setTimeout(() => {
expect(events.filter(({ name }) => name === 'enterViewport').length).to.greaterThan(1);
done();
});
});
});

0 comments on commit 59738f2

Please sign in to comment.