Skip to content

Commit 44f78c8

Browse files
committed
wip
1 parent eda68ad commit 44f78c8

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

modules/json/src/json-converter.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {executeFunction} from './helpers/execute-function';
99
import {assert} from './utils/assert';
1010
import {parseJSON} from './helpers/parse-json';
1111

12-
const isObject = value => value && typeof value === 'object';
12+
function isObject(value: unknown): value is Record<string, unknown> {
13+
return !!value && typeof value === 'object';
14+
}
1315

1416
export type JSONConverterProps = JSONConfiguration & {
1517
onJSONChange: () => void;
@@ -30,9 +32,9 @@ export type JSONConverterProps = JSONConfiguration & {
3032
export class JSONConverter {
3133
log = console; // eslint-disable-line
3234
configuration!: JSONConfiguration;
33-
onJSONChange = () => {};
34-
json = null;
35-
convertedJson = null;
35+
onJSONChange: () => void = () => {};
36+
json: unknown = null;
37+
convertedJson: unknown = null;
3638

3739
constructor(props) {
3840
this.setProps(props);
@@ -56,11 +58,11 @@ export class JSONConverter {
5658
}
5759
}
5860

59-
mergeConfiguration(config) {
61+
mergeConfiguration(config: JSONConfiguration) {
6062
this.configuration.merge(config);
6163
}
6264

63-
convert(json) {
65+
convert(json: unknown): unknown {
6466
// Use shallow equality to ensure we only convert same json once
6567
if (!json || json === this.json) {
6668
return this.convertedJson;
@@ -86,14 +88,14 @@ export class JSONConverter {
8688
}
8789
}
8890

89-
function convertJSON(json, configuration) {
91+
function convertJSON(json: unknown, configuration: JSONConfiguration) {
9092
// Fixup configuration
9193
configuration = new JSONConfiguration(configuration);
9294
return convertJSONRecursively(json, '', configuration);
9395
}
9496

9597
/** Converts JSON to props ("hydrating" classes, resolving enums and functions etc). */
96-
function convertJSONRecursively(json, key, configuration) {
98+
function convertJSONRecursively(json: unknown, key, configuration) {
9799
if (Array.isArray(json)) {
98100
return json.map((element, i) => convertJSONRecursively(element, String(i), configuration));
99101
}
@@ -121,13 +123,13 @@ function convertJSONRecursively(json, key, configuration) {
121123
}
122124

123125
/** Returns true if an object has a `type` field */
124-
function isClassInstance(json, configuration) {
126+
function isClassInstance(json: unknown, configuration: JSONConfiguration) {
125127
const {typeKey} = configuration;
126128
const isClass = isObject(json) && Boolean(json[typeKey]);
127129
return isClass;
128130
}
129131

130-
function convertClassInstance(json, configuration) {
132+
function convertClassInstance(json: unknown, configuration: JSONConfiguration) {
131133
// Extract the class type field
132134
const {typeKey} = configuration;
133135
const type = json[typeKey];
@@ -142,7 +144,7 @@ function convertClassInstance(json, configuration) {
142144
}
143145

144146
/** Plain JS object, embed functions. */
145-
function convertFunctionObject(json, configuration) {
147+
function convertFunctionObject(json, configuration:JSONConfiguration) {
146148
// Extract the target function field
147149
const {functionKey} = configuration;
148150
const targetFunction = json[functionKey];
@@ -157,8 +159,10 @@ function convertFunctionObject(json, configuration) {
157159
}
158160

159161
/** Plain JS object, convert each key and return. */
160-
function convertPlainObject(json, configuration) {
161-
assert(isObject(json));
162+
function convertPlainObject(json: unknown, configuration: JSONConfiguration) {
163+
if (!isObject(json)) {
164+
throw new Error('convertPlainObject: expected an object');
165+
}
162166

163167
const result = {};
164168
for (const key in json) {
@@ -171,7 +175,7 @@ function convertPlainObject(json, configuration) {
171175
/** Convert one string value in an object
172176
* @todo We could also support string syntax for hydrating other types, like regexps... But no current use case
173177
*/
174-
function convertString(string, key, configuration) {
178+
function convertString(string, key, configuration: JSONConfiguration) {
175179
// Here the JSON value is supposed to be treated as a function
176180
if (string.startsWith(FUNCTION_IDENTIFIER) && configuration.convertFunction) {
177181
string = string.replace(FUNCTION_IDENTIFIER, '');

modules/json/src/transports/transport.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
// SPDX-License-Identifier: MIT
33
// Copyright (c) vis.gl contributors
44

5+
export type TransportCallbacks = {
6+
onInitialize: (message: any) => void;
7+
onFinalize: (message: any) => void;
8+
onMessage: (message: any) => void;
9+
};
10+
511
/* global document */
6-
const state = {
12+
const state: TransportCallbacks = {
713
onInitialize: _ => _,
814
onFinalize: _ => _,
915
onMessage: _ => _
1016
};
1117

1218
/** Helper class for Python / Jupyter integration */
1319
export class Transport {
14-
static setCallbacks({onInitialize, onFinalize, onMessage}) {
20+
static setCallbacks({onInitialize, onFinalize, onMessage}: Partial<TransportCallbacks>) {
1521
if (onInitialize) {
1622
state.onInitialize = onInitialize;
1723
}

test/modules/json/helpers/convert-functions.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Based on https://github.com/donmccurdy/expression-eval under MIT license
66
import test from 'tape-promise/tape';
7-
import convertFunctions from '@deck.gl/json/helpers/convert-functions';
7+
import {_convertFunctions as convertFunctions} from '@deck.gl/json';
88

99
const TEST_CASES = [
1010
{expr: 'true', expected: true}, // boolean literal

test/modules/json/helpers/parse-expression-string.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Based on https://github.com/donmccurdy/expression-eval under MIT license
66
import test from 'tape-promise/tape';
7-
import parseExpressionString from '@deck.gl/json/helpers/parse-expression-string';
7+
import {_parseExpressionString as parseExpressionString} from '@deck.gl/json';
88

99
const row = Object.freeze({
1010
foo: {

test/modules/json/json-converter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import test from 'tape-promise/tape';
66
import {makeSpy} from '@probe.gl/test-utils';
77

8-
import {COORDINATE_SYSTEM} from '@deck.gl/core/lib/constants';
8+
import {COORDINATE_SYSTEM} from '@deck.gl/core';
99
import {MapController} from '@deck.gl/core';
1010
import {JSONConverter} from '@deck.gl/json';
1111
import configuration, {log, calculateRadius} from './json-configuration-for-deck';

test/modules/json/transports/transport.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright (c) vis.gl contributors
44

55
import test from 'tape-promise/tape';
6-
import Transport from '@deck.gl/json/transports/transport';
6+
import {Transport} from '@deck.gl/json';
77

88
test('delayed onInitialized()', t => {
99
Transport.setCallbacks({

0 commit comments

Comments
 (0)