Skip to content

Commit

Permalink
Merge 70b7e20 into d8565ae
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Nov 5, 2021
2 parents d8565ae + 70b7e20 commit 5dc8593
Show file tree
Hide file tree
Showing 28 changed files with 172 additions and 398 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ npm-debug.log
*/**/yarn.lock
yarn-error.log

tsconfig.tsbuildinfo

# editor files
.project
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion modules/bench/src/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default class Bench {
}

onSuiteComplete() {
const localStorage = new LocalStorage({id: this.id});
const localStorage = new LocalStorage(this.id);
const saved = localStorage.getConfiguration();
const current = this.updateTable(this.table, saved);
localStorage.updateConfiguration(current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import {window} from './globals';
import isBrowser from './is-browser';
import isElectron from './is-electron';

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

// Simple browser detection
// `mockUserAgent` parameter allows user agent to be overridden for testing
/* eslint-disable complexity */
export default function getBrowser(mockUserAgent) {
export default function getBrowser(mockUserAgent?: string): string {
if (!mockUserAgent && !isBrowser()) {
return 'Node';
}
Expand All @@ -43,6 +43,7 @@ export default function getBrowser(mockUserAgent) {
}

const navigator_ = typeof navigator !== 'undefined' ? navigator : {};
// @ts-expect-error
const userAgent = mockUserAgent || navigator_.userAgent || '';
// const appVersion = navigator_.appVersion || '';

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import isElectron from './is-electron';

export default function isBrowser() {
export default function isBrowser(): boolean {
// Check if in browser by duck-typing Node context
const isNode =
// @ts-ignore
Expand All @@ -13,6 +13,6 @@ export default function isBrowser() {
}

// document does not exist on worker thread
export function isBrowserMainThread() {
export function isBrowserMainThread(): boolean {
return isBrowser() && typeof document !== 'undefined';
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// based on https://github.com/cheton/is-electron
// https://github.com/electron/electron/issues/2288
/* eslint-disable complexity */
export default function isElectron(mockUserAgent) {
export default function isElectron(mockUserAgent?: string): boolean {
// Renderer process
if (
typeof window !== 'undefined' &&
Expand Down
32 changes: 19 additions & 13 deletions modules/main/src/lib/log.js → modules/main/src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ function getTableHeader(table) {
// A console wrapper

export default class Log {
static VERSION = VERSION;

id: string;
VERSION: string = VERSION;
_startTs: number = getHiResTimestamp();
_deltaTs: number = getHiResTimestamp();
// TODO - fix support from throttling groups
LOG_THROTTLE_TIMEOUT: number = 0; // Time before throttled messages are logged again
_storage: LocalStorage;
userData = {};

constructor({id} = {id: ''}) {
this.id = id;
this.VERSION = VERSION;
this._startTs = getHiResTimestamp();
this._deltaTs = getHiResTimestamp();
// TODO - fix support from throttling groups
this.LOG_THROTTLE_TIMEOUT = 0; // Time before throttled messages are logged again
this._storage = new LocalStorage(`__probe-${this.id}__`, DEFAULT_SETTINGS);
this.userData = {};

Expand Down Expand Up @@ -185,7 +191,7 @@ in a later version. Use \`${newUsage}\` instead`);
// Conditional logging

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

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

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

// Log a normal message, but only once, no console flooding
once(logLevel, message) {
once(logLevel, message?) {
return this._getLogFunction(
logLevel,
message,
Expand Down Expand Up @@ -268,15 +274,17 @@ in a later version. Use \`${newUsage}\` instead`);
);
}

timeStamp(logLevel, message) {
timeStamp(logLevel, message?) {
return this._getLogFunction(logLevel, message, console.timeStamp || noop);
}

group(logLevel, message, opts = {collapsed: false}) {
opts = normalizeArguments({logLevel, message, opts});
const {collapsed} = opts;
// @ts-expect-error
opts.method = (collapsed ? console.groupCollapsed : console.group) || console.info;

// @ts-expect-error
return this._getLogFunction(opts);
}

Expand Down Expand Up @@ -312,7 +320,7 @@ in a later version. Use \`${newUsage}\` instead`);
return this.isEnabled() && this.getLevel() >= normalizeLogLevel(logLevel);
}

_getLogFunction(logLevel, message, method, args = [], opts) {
_getLogFunction(logLevel, message, method, args = [], opts?: Record<string, any>) {
if (this._shouldLog(logLevel)) {
// normalized opts + timings
opts = normalizeArguments({logLevel, message, args, opts});
Expand Down Expand Up @@ -348,8 +356,6 @@ in a later version. Use \`${newUsage}\` instead`);
}
}

Log.VERSION = VERSION;

// Get logLevel from first argument:
// - log(logLevel, message, args) => logLevel
// - log(message, args) => 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function assert(condition, message) {
export default function assert(condition: unknown, message?: string) {
if (!condition) {
throw new Error(message || 'Assertion failed');
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getColor(color) {
return typeof color === 'string' ? COLOR[color.toUpperCase()] || COLOR.WHITE : color;
}

export function addColor(string, color, background) {
export function addColor(string, color, background?) {
if (!isBrowser && typeof string === 'string') {
if (color) {
color = getColor(color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function rightPad(string, length = 8) {
return `${string}${' '.repeat(padLength)}`;
}

export function formatValue(v, opts = {}) {
export function formatValue(v, opts: {isInteger?: boolean} = {}) {
const EPSILON = 1e-16;
const {isInteger = false} = opts;
if (Array.isArray(v) || ArrayBuffer.isView(v)) {
Expand Down
File renamed without changes.
41 changes: 0 additions & 41 deletions modules/main/src/utils/hi-res-timestamp.js

This file was deleted.

22 changes: 22 additions & 0 deletions modules/main/src/utils/hi-res-timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// probe.gl, MIT license

import {window, process, isBrowser} from './globals';

/** Get best timer available. */
export default function getHiResTimestamp() {
let timestamp;
// @ts-expect-error
if (isBrowser && window.performance) {
// @ts-expect-error
timestamp = window.performance.now();
// @ts-expect-error
} else if (process.hrtime) {
// @ts-expect-error
const timeParts = process.hrtime();
timestamp = timeParts[0] * 1000 + timeParts[1] / 1e6;
} else {
timestamp = Date.now();
}

return timestamp;
}
73 changes: 0 additions & 73 deletions modules/main/src/utils/local-storage.js

This file was deleted.

59 changes: 59 additions & 0 deletions modules/main/src/utils/local-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// probe.gl, MIT license

function getStorage(type): Storage {
try {
/** @type {Storage} */
// @ts-ignore
const storage: Storage = window[type];
const x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return storage;
} catch (e) {
return null;
}
}

// Store keys in local storage via simple interface
export default class LocalStorage {
storage: Storage;
id: string;
config: Record<string, any> = {};

constructor(id: string, defaultSettings: Record<string, any> = {}, type = 'sessionStorage') {
this.storage = getStorage(type);
this.id = id;
this.config = {};
Object.assign(this.config, defaultSettings);
this._loadConfiguration();
}

getConfiguration() {
return this.config;
}

setConfiguration(configuration) {
this.config = {};
return this.updateConfiguration(configuration);
}

updateConfiguration(configuration) {
Object.assign(this.config, configuration);
if (this.storage) {
const serialized = JSON.stringify(this.config);
this.storage.setItem(this.id, serialized);
}
return this;
}

// Get config from persistent store, if available
_loadConfiguration() {
let configuration = {};
if (this.storage) {
const serializedConfiguration = this.storage.getItem(this.id);
configuration = serializedConfiguration ? JSON.parse(serializedConfiguration) : {};
}
Object.assign(this.config, configuration);
return this;
}
}
1 change: 1 addition & 0 deletions modules/main/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

0 comments on commit 5dc8593

Please sign in to comment.