-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
TypeScriptify visualization components #20940
Changes from 12 commits
dff08ed
3574c37
ca679e0
91860ac
83b66fd
231d214
9062384
06ee085
361e2a8
8f06918
b8e57cc
dc4c456
a919b5d
84733cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* 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 prevResult: any; | ||
let called: boolean = false; | ||
let prevThis: any; | ||
let prevArgs: any[]; | ||
|
||
// 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 ( | ||
called && | ||
prevThis === this && | ||
prevArgs.length === args.length && | ||
args.every((arg, index) => arg === prevArgs[index]) | ||
) { | ||
return prevResult; | ||
} | ||
called = true; | ||
prevThis = this; | ||
prevArgs = args; | ||
prevResult = func.apply(this, args); | ||
return prevResult; | ||
} as T; | ||
|
||
return memoizedFunction; | ||
} | ||
|
||
export { memoizeLast }; |
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>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that export type StatusUpdates<Keys extends Status> = {
[key in Keys]: boolean;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's actually not working that well, since in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point 👍 |
||
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; | ||
} |
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> | ||
`; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could get rid of
called
by initializingprevArgs
to an array containing a unique symbol that is not exported from this module. Then the conditional insidememoizedFunction
would never be true on the first execution.