serialize Sandy plugins with serialization utils to support Date/Set/Map

Summary:
Unlike non-sandy plugins, non-sandy plugins weren't serialized using our serialization utility yet. This diff addresses that, meaning that users don't have to bother about how to serialize maps, sets and dates.

Unlike the old fashioned plugins, the `makeObjectSerialize` utility is used, rather than `serialize`. This normalizes the objects, but doesn't serialize them, which is done at the end of the export data process anyway for the whole tree. This avoids creating a double JSON serialization which is fully of ugly escape characters.

This makes the onImport / onExport definition of the logs plugin nicer.

Also improved the docs.

Reviewed By: nikoant

Differential Revision: D26146421

fbshipit-source-id: 6abfb6ee2e3312e2a13a11832ff103dc62fd844c
This commit is contained in:
Michel Weststrate
2021-02-01 11:40:20 -08:00
committed by Facebook GitHub Bot
parent e614993558
commit 594fa4d2bc
5 changed files with 183 additions and 35 deletions

View File

@@ -121,7 +121,7 @@ test('export / import plugin does work', async () => {
"logs": Array [
Object {
"app": "X",
"date": 1611854112859,
"date": 2021-01-28T17:15:12.859Z,
"message": "test1",
"pid": 0,
"tag": "test",
@@ -130,7 +130,7 @@ test('export / import plugin does work', async () => {
},
Object {
"app": "Y",
"date": 1611854117859,
"date": 2021-01-28T17:15:17.859Z,
"message": "test2",
"pid": 2,
"tag": "test",

View File

@@ -321,7 +321,7 @@ export function supportsDevice(device: Device) {
}
type ExportedState = {
logs: (Omit<DeviceLogEntry, 'date'> & {date: number})[];
logs: DeviceLogEntry[];
};
export function devicePlugin(client: DevicePluginClient) {
@@ -346,24 +346,13 @@ export function devicePlugin(client: DevicePluginClient) {
logs: entries
.get()
.slice(-10000)
.map((e) => ({
...e.entry,
date: e.entry.date.getTime(),
})),
.map((e) => e.entry),
};
});
client.onImport<ExportedState>((data) => {
const imported = addEntriesToState(
data.logs.map((log) =>
processEntry(
{
...log,
date: new Date(log.date),
},
'' + counter++,
),
),
data.logs.map((log) => processEntry(log, '' + counter++)),
);
rows.set(imported.rows);
entries.set(imported.entries);