-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[lens] Native renderer #36165
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
flash1293
merged 16 commits into
elastic:feature/lens
from
flash1293:lens/native-renderer
May 12, 2019
Merged
[lens] Native renderer #36165
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6d6f304
add nativerenderer component
flash1293 44894df
use native renderer in app and editor frame
flash1293 943fca8
remove prop types
flash1293 731e264
add unit test and fix linting issues
flash1293 cc1a0db
rename test suite
flash1293 18f9d83
rename prop and adjust shallow comparison function
flash1293 60cd891
add documentation
flash1293 0eb1f31
move native renderer to top level directory
flash1293 0f652c3
Merge remote-tracking branch 'upstream/feature/lens' into lens/native…
flash1293 a21b0ea
add test, fix linting error and improve shallow equal comparison
flash1293 34e9328
Merge remote-tracking branch 'upstream/feature/lens' into lens/native…
flash1293 b124f88
Merge branch 'feature/lens' into lens/native-renderer
flash1293 7445372
Merge remote-tracking branch 'upstream/feature/lens' into lens/native…
flash1293 4baf6b1
fix native renderer tests
flash1293 dece47f
Merge remote-tracking branch 'upstream/feature/lens' into lens/native…
flash1293 5a21a43
Merge branch 'feature/lens' into lens/native-renderer
flash1293 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| export * from './native_renderer'; |
187 changes: 187 additions & 0 deletions
187
x-pack/plugins/lens/public/native_renderer/native_renderer.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { render } from 'react-dom'; | ||
| import { NativeRenderer } from './native_renderer'; | ||
| import { act } from 'react-dom/test-utils'; | ||
|
|
||
| function renderAndTriggerHooks(element: JSX.Element, mountpoint: Element) { | ||
| // act takes care of triggering state hooks | ||
| act(() => { | ||
| render(element, mountpoint); | ||
| }); | ||
| } | ||
|
|
||
| describe('native_renderer', () => { | ||
| let mountpoint: Element; | ||
|
|
||
| beforeEach(() => { | ||
| mountpoint = document.createElement('div'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| mountpoint.remove(); | ||
| }); | ||
|
|
||
| it('should render element in container', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = { a: 'abc' }; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| const containerElement = mountpoint.firstElementChild; | ||
| expect(renderSpy).toHaveBeenCalledWith(containerElement, testProps); | ||
| }); | ||
|
|
||
| it('should not render again if props do not change', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = { a: 'abc' }; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| expect(renderSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should not render again if props do not change shallowly', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = { a: 'abc' }; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={{ ...testProps }} />, | ||
| mountpoint | ||
| ); | ||
| expect(renderSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should not render again for unchanged callback functions', () => { | ||
flash1293 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const renderSpy = jest.fn(); | ||
| const testCallback = () => {}; | ||
| const testState = { a: 'abc' }; | ||
|
|
||
| render( | ||
| <NativeRenderer | ||
| render={renderSpy} | ||
| nativeProps={{ state: testState, setState: testCallback }} | ||
| />, | ||
| mountpoint | ||
| ); | ||
| render( | ||
| <NativeRenderer | ||
| render={renderSpy} | ||
| nativeProps={{ state: testState, setState: testCallback }} | ||
| />, | ||
| mountpoint | ||
| ); | ||
| expect(renderSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should render again once if props change', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = { a: 'abc' }; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={{ a: 'def' }} />, | ||
| mountpoint | ||
| ); | ||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={{ a: 'def' }} />, | ||
| mountpoint | ||
| ); | ||
| expect(renderSpy).toHaveBeenCalledTimes(2); | ||
| const containerElement = mountpoint.firstElementChild; | ||
| expect(renderSpy).lastCalledWith(containerElement, { a: 'def' }); | ||
| }); | ||
|
|
||
| it('should render again once if props is just a string', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = 'abc'; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| renderAndTriggerHooks(<NativeRenderer render={renderSpy} nativeProps="def" />, mountpoint); | ||
| renderAndTriggerHooks(<NativeRenderer render={renderSpy} nativeProps="def" />, mountpoint); | ||
| expect(renderSpy).toHaveBeenCalledTimes(2); | ||
| const containerElement = mountpoint.firstElementChild; | ||
| expect(renderSpy).lastCalledWith(containerElement, 'def'); | ||
| }); | ||
|
|
||
| it('should render again if props are extended', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = { a: 'abc' }; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={{ a: 'abc', b: 'def' }} />, | ||
| mountpoint | ||
| ); | ||
| expect(renderSpy).toHaveBeenCalledTimes(2); | ||
| const containerElement = mountpoint.firstElementChild; | ||
| expect(renderSpy).lastCalledWith(containerElement, { a: 'abc', b: 'def' }); | ||
| }); | ||
|
|
||
| it('should render again if props are limited', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = { a: 'abc', b: 'def' }; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={{ a: 'abc' }} />, | ||
| mountpoint | ||
| ); | ||
| expect(renderSpy).toHaveBeenCalledTimes(2); | ||
| const containerElement = mountpoint.firstElementChild; | ||
| expect(renderSpy).lastCalledWith(containerElement, { a: 'abc' }); | ||
| }); | ||
|
|
||
| it('should render a div as container', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = { a: 'abc' }; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| const containerElement: Element = mountpoint.firstElementChild!; | ||
| expect(containerElement.nodeName).toBe('DIV'); | ||
| }); | ||
|
|
||
| it('should render a specified element as container', () => { | ||
| const renderSpy = jest.fn(); | ||
| const testProps = { a: 'abc' }; | ||
|
|
||
| renderAndTriggerHooks( | ||
| <NativeRenderer render={renderSpy} tag="span" nativeProps={testProps} />, | ||
| mountpoint | ||
| ); | ||
| const containerElement: Element = mountpoint.firstElementChild!; | ||
| expect(containerElement.nodeName).toBe('SPAN'); | ||
| }); | ||
| }); | ||
80 changes: 80 additions & 0 deletions
80
x-pack/plugins/lens/public/native_renderer/native_renderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import React, { useEffect, useRef } from 'react'; | ||
|
|
||
| export interface NativeRendererProps<T> { | ||
| render: (domElement: Element, props: T) => void; | ||
| nativeProps: T; | ||
| tag?: string; | ||
| children?: never; | ||
| } | ||
|
|
||
| function is(x: unknown, y: unknown) { | ||
| return (x === y && (x !== 0 || 1 / (x as number) === 1 / (y as number))) || (x !== x && y !== y); | ||
| } | ||
|
|
||
| function isShallowDifferent<T>(objA: T, objB: T): boolean { | ||
| if (is(objA, objB)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | ||
| return true; | ||
| } | ||
|
|
||
| const keysA = Object.keys(objA) as Array<keyof T>; | ||
| const keysB = Object.keys(objB) as Array<keyof T>; | ||
|
|
||
| if (keysA.length !== keysB.length) { | ||
| return true; | ||
| } | ||
|
|
||
| for (let i = 0; i < keysA.length; i++) { | ||
| if (!window.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * A component which takes care of providing a mountpoint for a generic render | ||
| * function which takes an html element and an optional props object. | ||
| * It also takes care of calling render again if the props object changes. | ||
| * By default the mountpoint element will be a div, this can be changed with the | ||
| * `tag` prop. | ||
| * | ||
| * @param props | ||
| */ | ||
| export function NativeRenderer<T>({ render, nativeProps, tag }: NativeRendererProps<T>) { | ||
| const elementRef = useRef<Element | null>(null); | ||
| const propsRef = useRef<T | null>(null); | ||
|
|
||
| function renderAndUpdate(element: Element) { | ||
| elementRef.current = element; | ||
| propsRef.current = nativeProps; | ||
| render(element, nativeProps); | ||
| } | ||
|
|
||
| useEffect( | ||
| () => { | ||
| if (elementRef.current && isShallowDifferent(propsRef.current, nativeProps)) { | ||
| renderAndUpdate(elementRef.current); | ||
| } | ||
| }, | ||
| [nativeProps] | ||
| ); | ||
|
|
||
| return React.createElement(tag || 'div', { | ||
| ref: element => { | ||
| if (element && element !== elementRef.current) { | ||
| renderAndUpdate(element); | ||
| } | ||
| }, | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.