forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeScriptify visualization components (elastic#20940)
* Refactor vis components to TypeScript * Fix issue with ResizeChecker * Fix calling onInit for no data * Explicit named export * Add title to vistype * Fix error in test file * Move onInit to no VisualizationNoResults * Make listenOnChange changeable * Add memoize util * Use memoize for no results check * Address issue with uiState * Optimize memoize function
- Loading branch information
Showing
18 changed files
with
577 additions
and
243 deletions.
There are no files selected for viewing
This file contains 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 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 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,50 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { memoizeLast } from './memoize'; | ||
|
||
describe('memoizeLast', () => { | ||
type SumFn = (a: number, b: number) => number; | ||
let originalSum: SumFn; | ||
let memoizedSum: SumFn; | ||
|
||
beforeEach(() => { | ||
originalSum = jest.fn((a, b) => a + b); | ||
memoizedSum = memoizeLast(originalSum); | ||
}); | ||
|
||
it('should call through function', () => { | ||
expect(memoizedSum(26, 16)).toBe(42); | ||
expect(originalSum).toHaveBeenCalledWith(26, 16); | ||
}); | ||
|
||
it('should memoize the last call', () => { | ||
memoizedSum(26, 16); | ||
expect(originalSum).toHaveBeenCalledTimes(1); | ||
memoizedSum(26, 16); | ||
expect(originalSum).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should use parameters as cache keys', () => { | ||
expect(memoizedSum(26, 16)).toBe(42); | ||
expect(originalSum).toHaveBeenCalledTimes(1); | ||
expect(memoizedSum(16, 26)).toBe(42); | ||
expect(originalSum).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains 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,61 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
interface MemoizedCall { | ||
args: any[]; | ||
returnValue: any; | ||
this: any; | ||
} | ||
|
||
// A symbol expressing, that the memoized function has never been called | ||
const neverCalled: unique symbol = Symbol(); | ||
type NeverCalled = typeof neverCalled; | ||
|
||
/** | ||
* A simple memoize function, that only stores the last returned value | ||
* and uses the identity of all passed parameters as a cache key. | ||
*/ | ||
function memoizeLast<T extends (...args: any[]) => any>(func: T): T { | ||
let prevCall: MemoizedCall | NeverCalled = neverCalled; | ||
|
||
// We need to use a `function` here for proper this passing. | ||
// tslint:disable-next-line:only-arrow-functions | ||
const memoizedFunction = function(this: any, ...args: any[]) { | ||
if ( | ||
prevCall !== neverCalled && | ||
prevCall.this === this && | ||
prevCall.args.length === args.length && | ||
prevCall.args.every((arg, index) => arg === args[index]) | ||
) { | ||
return prevCall.returnValue; | ||
} | ||
|
||
prevCall = { | ||
args, | ||
this: this, | ||
returnValue: func.apply(this, args), | ||
}; | ||
|
||
return prevCall.returnValue; | ||
} as T; | ||
|
||
return memoizedFunction; | ||
} | ||
|
||
export { memoizeLast }; |
This file contains 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 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 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 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,40 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { Vis } from '..'; | ||
import { Status } from '../update_status'; | ||
|
||
export class VisualizationController { | ||
constructor(element: HTMLElement, vis: Vis); | ||
public render(visData: any, update: { [key in Status]: boolean }): Promise<void>; | ||
public destroy(): void; | ||
public isLoaded?(): Promise<void> | void; | ||
} | ||
|
||
export interface VisType { | ||
title: string; | ||
visualization: typeof VisualizationController; | ||
isAccessible?: boolean; | ||
|
||
// Since we haven't typed everything here yet, we basically "any" the rest | ||
// of that interface. This should be removed as soon as this type definition | ||
// has been completed. But that way we at least have typing for a couple of | ||
// properties on that type. | ||
[key: string]: any; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/ui/public/visualize/components/__snapshots__/visualization_noresults.test.js.snap
This file contains 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,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`VisualizationNoResults should render according to snapshot 1`] = ` | ||
<div | ||
class="text-center visualize-error visualize-chart" | ||
> | ||
<div | ||
class="item top" | ||
/> | ||
<div | ||
class="item" | ||
> | ||
<h2 | ||
aria-hidden="true" | ||
> | ||
<i | ||
aria-hidden="true" | ||
class="fa fa-meh-o" | ||
/> | ||
</h2> | ||
<h4> | ||
No results found | ||
</h4> | ||
</div> | ||
<div | ||
class="item bottom" | ||
/> | ||
</div> | ||
`; |
This file contains 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,20 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { Visualization } from './visualization'; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.