Skip to content

Commit

Permalink
typescript type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Sep 15, 2020
1 parent b5d2fc9 commit e5f9922
Show file tree
Hide file tree
Showing 48 changed files with 1,784 additions and 56 deletions.
2 changes: 2 additions & 0 deletions examples/benchmarks/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class App extends PureComponent {
constructor(props) {
super(props);

// @ts-ignore
this.suite = new Bench({
log: this._logResult.bind(this)
});
Expand All @@ -44,6 +45,7 @@ export default class App extends PureComponent {
componentDidMount() {
this.suite
// Calibrate performance
// @ts-ignore
.calibrate()
.run()
// when running in browser, notify test the driver that it's done
Expand Down
3 changes: 3 additions & 0 deletions examples/browser-test/test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const test = require('tape');

// Add BrowserTestDriver hooks
// @ts-ignore
test.onFailure(window.browserTestDriver_fail);
// @ts-ignore
test.onFinish(window.browserTestDriver_finish);

test('A test', t => {
Expand All @@ -11,6 +13,7 @@ test('A test', t => {
const app = require('./app').render();

window
// @ts-ignore
.browserTestDriver_captureAndDiffScreen({
threshold: 0.99,
goldenImage: 'test/golden-image.png',
Expand Down
1 change: 1 addition & 0 deletions modules/bench/src/bench-loggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function logResultsAsTreeWithElapsed({entry, id, itersPerSecond, error, t
);
break;
default:
// @ts-ignore
logResultsAsTree({entry, id, itersPerSecond, time});
}
}
33 changes: 33 additions & 0 deletions modules/bench/src/bench.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const LOG_ENTRY: {
GROUP: 'group',
TEST: 'test',
COMPLETE: 'complete'
};

/** A suite of benchmarks. Benchmarks can be added and the suite can then be run. */
export default class Bench {
constructor(options?: {
/** Name is needed for regression (storing/loading) */
id: string,
log?: any,
time?: number,
delay?: number;
minIterations?: number;
});

calibrate(id, func1, func2, opts): Bench;
run(): Promise<void>;

group(id: string): Bench;

add(priority, id, func1, func2): Bench;

/** Marks the added test as async (i.e. test returns promise) */
addAsync(priority, id, func1, func2): Bench;

onBenchmarkComplete(options: {
id: string, time: number, iterations: number, itersPerSecond: number
}): void;
onSuiteComplete(): void;
updateTable(current: any, saved: any): any;
}
5 changes: 3 additions & 2 deletions modules/bench/src/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const LOG_ENTRY = {

export default class Bench {
constructor({
id, // Name is needed for regression (storing/loading)
log,
id = '', // Name is needed for regression (storing/loading)
log = null,
time = TIME_THRESHOLD_MS,
delay = TIME_COOLDOWN_MS,
minIterations = MIN_ITERATIONS
Expand Down Expand Up @@ -192,6 +192,7 @@ function logEntry(test, opts) {
// Run a list of bench test case asynchronously (with short timeouts inbetween)
async function runTests({tests, onBenchmarkComplete = noop}) {
// Run default warm up and calibration tests
// @ts-ignore
runCalibrationTests({tests, onBenchmarkComplete});

// Run the suite tests
Expand Down
6 changes: 6 additions & 0 deletions modules/bench/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {default as Bench, LOG_ENTRY} from './bench';
export {
logResultsAsMarkdownTable,
logResultsAsTree,
logResultsAsTreeWithElapsed
} from './bench-loggers';
4 changes: 4 additions & 0 deletions modules/bench/test/bench.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ test('Bench#run', t => {
log: ({message}) => t.comment(message)
});

// @ts-ignore
suite.add('initFunc in options', {initialize: () => 1, unit: 'initializations'}, value => {
// @ts-ignore
if (!value === 1) {
t.fail();
}
});

// @ts-ignore
suite.add(
'initFunc as param (deprecated)',
() => 1,
value => {
// @ts-ignore
if (!value === 1) {
t.fail();
}
Expand Down
1 change: 1 addition & 0 deletions modules/bench/test/format-utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable max-len */
import test from 'tape-catch';
// @ts-ignore
import {formatSI} from '@probe.gl/bench/format-utils';

const FORMAT_SI_TESTS = [
Expand Down
1 change: 1 addition & 0 deletions modules/bench/test/stat-utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable max-len */
import test from 'tape-catch';
// @ts-ignore
import {mean, std, cv} from '@probe.gl/bench/stat-utils';

// wolfram alpha: mean {1, 2, 3}
Expand Down
2 changes: 2 additions & 0 deletions modules/bench/test/utils/parse-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Parse array or string color
function parseColor(color, target, index = 0) {
if (Array.isArray(color) || ArrayBuffer.isView(color)) {
// @ts-ignore
if (!target && color.length === 4) {
return color;
}
Expand All @@ -11,6 +12,7 @@ function parseColor(color, target, index = 0) {
target[index + 0] = color[0];
target[index + 1] = color[1];
target[index + 2] = color[2];
// @ts-ignore
target[index + 3] = color.length === 4 ? color[4] : 255;
return target;
}
Expand Down
4 changes: 4 additions & 0 deletions modules/core/src/env/get-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import isBrowser from './is-browser';
import isElectron from './is-electron';

export function isMobile() {
// @ts-ignore
return typeof window.orientation !== 'undefined';
}

Expand Down Expand Up @@ -54,12 +55,15 @@ export default function getBrowser(mockUserAgent) {
if (isMSIE || isTrident) {
return 'IE';
}
// @ts-ignore
if (window.chrome) {
return 'Chrome';
}
// @ts-ignore
if (window.safari) {
return 'Safari';
}
// @ts-ignore
if (window.mozInnerScreenX) {
return 'Firefox';
}
Expand Down
1 change: 1 addition & 0 deletions modules/core/src/env/is-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isElectron from './is-electron';
export default function isBrowser() {
// Check if in browser by duck-typing Node context
const isNode =
// @ts-ignore
typeof process === 'object' && String(process) === '[object process]' && !process.browser;

return !isNode || isElectron();
Expand Down
2 changes: 2 additions & 0 deletions modules/core/src/env/is-electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default function isElectron(mockUserAgent) {
if (
typeof window !== 'undefined' &&
typeof window.process === 'object' &&
// @ts-ignore
window.process.type === 'renderer'
) {
return true;
Expand All @@ -14,6 +15,7 @@ export default function isElectron(mockUserAgent) {
if (
typeof process !== 'undefined' &&
typeof process.versions === 'object' &&
// @ts-ignore
Boolean(process.versions.electron)
) {
return true;
Expand Down
1 change: 1 addition & 0 deletions modules/core/src/init.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import {global} from './utils/globals';
// @ts-ignore
global.probe = {};
8 changes: 7 additions & 1 deletion modules/core/src/lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function getTableHeader(table) {
// A console wrapper

export default class Log {
constructor({id} = {}) {
constructor({id} = {id: ''}) {
this.id = id;
this.VERSION = VERSION;
this._startTs = getHiResTimestamp();
Expand Down Expand Up @@ -163,11 +163,13 @@ export default class Log {

// Warn, but only once, no console flooding
warn(message) {
// @ts-ignore
return this._getLogFunction(0, message, originalConsole.warn, arguments, ONCE);
}

// Print an error
error(message) {
// @ts-ignore
return this._getLogFunction(0, message, originalConsole.error, arguments);
}

Expand All @@ -184,6 +186,7 @@ in a later version. Use \`${newUsage}\` instead`);

// Log to a group
probe(logLevel, message) {
// @ts-ignore
return this._getLogFunction(logLevel, message, originalConsole.log, arguments, {
time: true,
once: true
Expand All @@ -192,11 +195,13 @@ in a later version. Use \`${newUsage}\` instead`);

// Log a debug message
log(logLevel, message) {
// @ts-ignore
return this._getLogFunction(logLevel, message, originalConsole.debug, arguments);
}

// Log a normal message
info(logLevel, message) {
// @ts-ignore
return this._getLogFunction(logLevel, message, console.info, arguments);
}

Expand All @@ -206,6 +211,7 @@ in a later version. Use \`${newUsage}\` instead`);
logLevel,
message,
originalConsole.debug || originalConsole.info,
// @ts-ignore
arguments,
ONCE
);
Expand Down
1 change: 1 addition & 0 deletions modules/core/src/utils/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {self, window, global, document, process, console} from '../env/globals';

// Extract injected version from package.json (injected by babel plugin)
/* global __VERSION__ */
// @ts-ignore
export const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'untranspiled source';

export const isBrowser = checkIfBrowser();
2 changes: 2 additions & 0 deletions modules/core/src/utils/hi-res-timestamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// @ts-nocheck

/**
* Common environment setup
*/
Expand Down
2 changes: 2 additions & 0 deletions modules/core/src/utils/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

function getStorage(type) {
try {
/** @type {Storage} */
// @ts-ignore
const storage = window[type];
const x = '__storage_test__';
storage.setItem(x, x);
Expand Down
1 change: 1 addition & 0 deletions modules/core/test/lib/log.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable max-statements */
import Probe, {Log} from 'probe.gl';
// @ts-ignore
import {normalizeArguments} from 'probe.gl/lib/log';
import test from 'tape-catch';

Expand Down
2 changes: 2 additions & 0 deletions modules/react-bench/src/components/bench-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ export class BenchResults extends Component {
&lt; 1M iterations/s
</BarCell>
<div style={{width: 20}} />
{/* @ts-ignore */}
<BarCell color={ORANGE} percent={100}>
1M - 10M iterations/s
</BarCell>
<div style={{width: 20}} />
{/* @ts-ignore */}
<BarCell color={GREEN} percent={100}>
&gt; 10M iterations/s
</BarCell>
Expand Down
4 changes: 4 additions & 0 deletions modules/seer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
"scripts": {
"docs": "cat docs/header.md > README.md && jsdoc2md index.js >> README.md && sed -i '' '30,1000s/##/###/g' README.md"
},
"devDependencies": {
"ava": "^3.5.0",
"sinon": "^9.0.0"
},
"gitHead": "95541d68783bfd50cbaf9b2a15b3cb41f1ff8284"
}

0 comments on commit e5f9922

Please sign in to comment.