From ce3f69c249e5f469c6dce0a3a1e28de54f012953 Mon Sep 17 00:00:00 2001 From: John Knox Date: Thu, 15 Aug 2019 07:29:11 -0700 Subject: [PATCH] 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 --- headless/index.js | 2 +- src/utils/__tests__/serialization.node.js | 2 +- src/utils/{serialization.js => serialization.tsx} | 15 ++++++++------- 3 files changed, 10 insertions(+), 9 deletions(-) rename src/utils/{serialization.js => serialization.tsx} (95%) diff --git a/headless/index.js b/headless/index.js index f1da85dec..c91b325f5 100644 --- a/headless/index.js +++ b/headless/index.js @@ -22,7 +22,7 @@ import {listDevices} from '../src/utils/listDevices.tsx'; import setup from '../static/setup.js'; import type {Store} from '../src/reducers/index.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 {getStringFromErrorLike} from '../src/utils/index.tsx'; diff --git a/src/utils/__tests__/serialization.node.js b/src/utils/__tests__/serialization.node.js index f8a4e1f39..b5b2bb0ed 100644 --- a/src/utils/__tests__/serialization.node.js +++ b/src/utils/__tests__/serialization.node.js @@ -5,7 +5,7 @@ * @format */ -import {makeObjectSerializable, deserializeObject} from '../serialization'; +import {makeObjectSerializable, deserializeObject} from '../serialization.tsx'; class TestObject extends Object { constructor(title: Object, map: ?Map, set: ?Set) { diff --git a/src/utils/serialization.js b/src/utils/serialization.tsx similarity index 95% rename from src/utils/serialization.js rename to src/utils/serialization.tsx index a4b11ef9c..9845e7e3f 100644 --- a/src/utils/serialization.js +++ b/src/utils/serialization.tsx @@ -5,7 +5,7 @@ * @format */ -import {Idler} from './Idler.tsx'; +import {Idler} from './Idler'; export async function serialize( obj: Object, 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)); } function processArray( element: any, - array: [any], + array: Array, stack: Array, dict: Map, -): {childNeedsIteration: boolean, outputArr: Array} { +): {childNeedsIteration: boolean; outputArr: Array} { // 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 = []; let childNeedsIteration = false; @@ -74,7 +74,7 @@ export function processMapElement( obj: Map, dict: Map, stack: Array, -): {childNeedsIteration: boolean, outputArray: Array} { +): {childNeedsIteration: boolean; outputArray: Array} { const arr = []; let childNeedsIteration = false; for (const item of [...obj]) { @@ -95,7 +95,7 @@ export function processObjectToBeSerialized( element: Object, dict: Map, stack: Array, -): {childNeedsIteration: boolean, outputObject: Object} { +): {childNeedsIteration: boolean; outputObject: Object} { const array = Object.entries(element); let obj = {}; let childNeedsIteration = false; @@ -122,7 +122,7 @@ export async function makeObjectSerializable( obj: any, idler?: Idler, statusUpdate?: (msg: string) => void, -): any { +): Promise { if (!(obj instanceof Object)) { return obj; } @@ -213,6 +213,7 @@ export function deserializeObject(obj: any): any { switch (type) { case 'Map': { return new Map( + // @ts-ignore [...obj.data].map(item => [...item].map(deserializeObject)), ); }