Settings Tab (#42)

* Settings tab
This commit is contained in:
Maciej Hirsz
2018-09-21 18:07:14 +02:00
committed by GitHub
parent 07b5880e5f
commit ca0ab38bd2
11 changed files with 422 additions and 101 deletions
+9 -8
View File
@@ -1,4 +1,5 @@
import { Opaque, Maybe } from './helpers';
import { stringify, parse, Stringified } from './stringify';
import {
FeedVersion,
Address,
@@ -123,11 +124,11 @@ export type Message =
| Variants.PongMessage;
/**
* Opaque data type to be sent to the feed. Passing through
* strings means we can only serialize once, no matter how
* many feed clients are listening in.
* Data type to be sent to the feed. Passing through strings means we can only serialize once,
* no matter how many feed clients are listening in.
*/
export type Data = Opaque<string, 'FeedMessage.Data'>;
export interface SquashedMessages extends Array<Action | Payload> {};
export type Data = Stringified<SquashedMessages>;
/**
* Serialize an array of `Message`s to a single JSON string.
@@ -137,7 +138,7 @@ export type Data = Opaque<string, 'FeedMessage.Data'>;
* Action `string`s are converted to opcodes using the `actionToCode` mapping.
*/
export function serialize(messages: Array<Message>): Data {
const squashed = new Array(messages.length * 2);
const squashed: SquashedMessages = new Array(messages.length * 2);
let index = 0;
messages.forEach((message) => {
@@ -147,20 +148,20 @@ export function serialize(messages: Array<Message>): Data {
squashed[index++] = payload;
})
return JSON.stringify(squashed) as Data;
return stringify(squashed);
}
/**
* Deserialize data to an array of `Message`s.
*/
export function deserialize(data: Data): Array<Message> {
const json: Array<Action | Payload> = JSON.parse(data);
const json = parse(data);
if (!Array.isArray(json) || json.length === 0 || json.length % 2 !== 0) {
throw new Error('Invalid FeedMessage.Data');
}
const messages: Array<Message> = new Array(json.length / 2);
const messages = new Array<Message>(json.length / 2);
for (const index of messages.keys()) {
const [ action, payload ] = json.slice(index * 2);
+2 -1
View File
@@ -1,6 +1,7 @@
export * from './helpers';
export * from './id';
export * from './block';
export * from './stringify';
import * as Types from './types';
import * as FeedMessage from './feed';
@@ -8,4 +9,4 @@ import * as FeedMessage from './feed';
export { Types, FeedMessage };
// Increment this if breaking changes were made to types in `feed.ts`
export const VERSION: Types.FeedVersion = 11 as Types.FeedVersion;
export const VERSION: Types.FeedVersion = 12 as Types.FeedVersion;
+4
View File
@@ -0,0 +1,4 @@
export abstract class Stringified<T> { public __PHANTOM__: T };
export const parse = JSON.parse as any as <T>(val: Stringified<T>) => T;
export const stringify = JSON.stringify as any as <T>(val: T) => Stringified<T>;