Convert serialization.js to TS

Summary:
Convert serialization to typescript.

I tried to type the deserialize function but don't know enough about the expected format to be able to tell what the types should be.

Reviewed By: passy

Differential Revision: D16785945

fbshipit-source-id: 45de7ee1c8972314a52abcf20d428ba44f031a00
This commit is contained in:
John Knox
2019-08-15 07:29:11 -07:00
committed by Facebook Github Bot
parent d0da0d66a5
commit ce3f69c249
3 changed files with 10 additions and 9 deletions

View File

@@ -22,7 +22,7 @@ import {listDevices} from '../src/utils/listDevices.tsx';
import setup from '../static/setup.js'; import setup from '../static/setup.js';
import type {Store} from '../src/reducers/index.tsx'; import type {Store} from '../src/reducers/index.tsx';
import {getPersistentPlugins} from '../src/utils/pluginUtils.tsx'; import {getPersistentPlugins} from '../src/utils/pluginUtils.tsx';
import {serialize} from '../src/utils/serialization'; import {serialize} from '../src/utils/serialization.tsx';
import type BaseDevice from '../src/devices/BaseDevice.tsx'; import type BaseDevice from '../src/devices/BaseDevice.tsx';
import {getStringFromErrorLike} from '../src/utils/index.tsx'; import {getStringFromErrorLike} from '../src/utils/index.tsx';

View File

@@ -5,7 +5,7 @@
* @format * @format
*/ */
import {makeObjectSerializable, deserializeObject} from '../serialization'; import {makeObjectSerializable, deserializeObject} from '../serialization.tsx';
class TestObject extends Object { class TestObject extends Object {
constructor(title: Object, map: ?Map<any, any>, set: ?Set<any>) { constructor(title: Object, map: ?Map<any, any>, set: ?Set<any>) {

View File

@@ -5,7 +5,7 @@
* @format * @format
*/ */
import {Idler} from './Idler.tsx'; import {Idler} from './Idler';
export async function serialize( export async function serialize(
obj: Object, obj: Object,
idler?: Idler, idler?: Idler,
@@ -16,16 +16,16 @@ export async function serialize(
); );
} }
export function deserialize(str: string): Object { export function deserialize(str: string): any {
return deserializeObject(JSON.parse(str)); return deserializeObject(JSON.parse(str));
} }
function processArray( function processArray(
element: any, element: any,
array: [any], array: Array<any>,
stack: Array<any>, stack: Array<any>,
dict: Map<any, any>, dict: Map<any, any>,
): {childNeedsIteration: boolean, outputArr: Array<any>} { ): {childNeedsIteration: boolean; outputArr: Array<any>} {
// Adds the array item to the stack if it needs to undergo iteration to serialise it. Otherwise it adds the serialized version of the item to the memoization dict // Adds the array item to the stack if it needs to undergo iteration to serialise it. Otherwise it adds the serialized version of the item to the memoization dict
const outputArr = []; const outputArr = [];
let childNeedsIteration = false; let childNeedsIteration = false;
@@ -74,7 +74,7 @@ export function processMapElement(
obj: Map<any, any>, obj: Map<any, any>,
dict: Map<any, any>, dict: Map<any, any>,
stack: Array<any>, stack: Array<any>,
): {childNeedsIteration: boolean, outputArray: Array<any>} { ): {childNeedsIteration: boolean; outputArray: Array<any>} {
const arr = []; const arr = [];
let childNeedsIteration = false; let childNeedsIteration = false;
for (const item of [...obj]) { for (const item of [...obj]) {
@@ -95,7 +95,7 @@ export function processObjectToBeSerialized(
element: Object, element: Object,
dict: Map<any, any>, dict: Map<any, any>,
stack: Array<any>, stack: Array<any>,
): {childNeedsIteration: boolean, outputObject: Object} { ): {childNeedsIteration: boolean; outputObject: Object} {
const array = Object.entries(element); const array = Object.entries(element);
let obj = {}; let obj = {};
let childNeedsIteration = false; let childNeedsIteration = false;
@@ -122,7 +122,7 @@ export async function makeObjectSerializable(
obj: any, obj: any,
idler?: Idler, idler?: Idler,
statusUpdate?: (msg: string) => void, statusUpdate?: (msg: string) => void,
): any { ): Promise<any> {
if (!(obj instanceof Object)) { if (!(obj instanceof Object)) {
return obj; return obj;
} }
@@ -213,6 +213,7 @@ export function deserializeObject(obj: any): any {
switch (type) { switch (type) {
case 'Map': { case 'Map': {
return new Map( return new Map(
// @ts-ignore
[...obj.data].map(item => [...item].map(deserializeObject)), [...obj.data].map(item => [...item].map(deserializeObject)),
); );
} }