Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Serializable to runtime-definitions #1228

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions packages/runtime/matrix/src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import {
import {
IComponentRuntime,
IObjectStorageService,
Jsonable,
JsonablePrimitive,
Serializable,
} from "@microsoft/fluid-runtime-definitions";
import { makeHandlesSerializable, parseHandles, SharedObject } from "@microsoft/fluid-shared-object-base";
import { fromBase64ToUtf8 } from "@microsoft/fluid-core-utils";
import { IComponentHandle } from "@microsoft/fluid-component-core-interfaces";
import { ObjectStoragePartition } from "@microsoft/fluid-runtime-utils";
import { IMatrixProducer, IMatrixConsumer, IMatrixReader } from "@tiny-calc/nano";
import { debug } from "./debug";
Expand All @@ -37,9 +35,9 @@ export const enum SnapshotPath {
cells = "cells"
}

export class SharedMatrix<
T extends Jsonable<JsonablePrimitive | IComponentHandle> = Jsonable<JsonablePrimitive | IComponentHandle>
> extends SharedObject implements IMatrixProducer<T | undefined | null>, IMatrixReader<T | undefined | null> {
export class SharedMatrix<T extends Serializable = Serializable> extends SharedObject
implements IMatrixProducer<T | undefined | null>, IMatrixReader<T | undefined | null>
{
private readonly consumers = new Set<IMatrixConsumer<T>>();

public static getFactory() { return new SharedMatrixFactory(); }
Expand Down
6 changes: 2 additions & 4 deletions packages/runtime/matrix/src/ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* Licensed under the MIT License.
*/

import { Jsonable, JsonablePrimitive } from "@microsoft/fluid-runtime-definitions";

import { IComponentHandle } from "@microsoft/fluid-component-core-interfaces";
import { Serializable } from "@microsoft/fluid-runtime-definitions";

export enum MatrixOp {
spliceCols,
Expand All @@ -27,5 +25,5 @@ export interface IMatrixCellMsg extends IMatrixMsg {
type: MatrixOp.set;
row: number;
col: number;
value: Jsonable<JsonablePrimitive | IComponentHandle>;
value: Serializable;
}
13 changes: 5 additions & 8 deletions packages/runtime/matrix/test/matrix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@

import 'mocha';

import { IComponentHandle } from '@microsoft/fluid-component-core-interfaces';
import { TestHost } from '@microsoft/fluid-local-test-server';
import { Jsonable, JsonablePrimitive } from '@microsoft/fluid-runtime-definitions';
import { Serializable } from '@microsoft/fluid-runtime-definitions';
import { MockDeltaConnectionFactory, MockRuntime, MockStorage } from '@microsoft/fluid-test-runtime-utils';
import { strict as assert } from 'assert';
import { SharedMatrix, SharedMatrixFactory } from '../src';
import { fill, check, insertFragmented } from './utils';

type Json = Jsonable<JsonablePrimitive | IComponentHandle>;

function extract<T extends Json>(actual: SharedMatrix<T>): ReadonlyArray<ReadonlyArray<T>> {
function extract<T extends Serializable>(actual: SharedMatrix<T>): ReadonlyArray<ReadonlyArray<T>> {
const m: T[][] = [];
for (let r = 0; r < actual.numRows; r++) {
const row: T[] = [];
Expand All @@ -29,12 +26,12 @@ function extract<T extends Json>(actual: SharedMatrix<T>): ReadonlyArray<Readonl
return m;
}

function expectSize<T extends Json>(actual: SharedMatrix<T>, numRows: number, numCols: number) {
function expectSize<T extends Serializable>(actual: SharedMatrix<T>, numRows: number, numCols: number) {
assert.equal(actual.numRows, numRows);
assert.equal(actual.numCols, numCols);
}

async function snapshot<T extends Json>(matrix: SharedMatrix<T>) {
async function snapshot<T extends Serializable>(matrix: SharedMatrix<T>) {
const objectStorage = new MockStorage(matrix.snapshot());
const runtime = new MockRuntime();
const matrix2 = new SharedMatrix(runtime, `load(${matrix.id})`);
Expand All @@ -59,7 +56,7 @@ describe('Matrix', () => {
await TestHost.sync(host1, host2);
}

async function expect<T extends Json>(expected: ReadonlyArray<ReadonlyArray<T>>) {
async function expect<T extends Serializable>(expected: ReadonlyArray<ReadonlyArray<T>>) {
assert.deepEqual(extract(matrix), expected, 'Matrix must match expected.');

// Ensure ops are ACKed prior to snapshot. Otherwise, the unACKed segments won't be included.
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/runtime-definitions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
export * from "./agent";
export * from "./channel";
export * from "./componentFactory";
export * from "./componentRegistry";
export * from "./components";
export * from "./jsonable";
export * from "./protocol";
export * from "./serializable";
export * from "./storage";
export * from "./componentRegistry";
17 changes: 17 additions & 0 deletions packages/runtime/runtime-definitions/src/serializable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { IComponentHandle } from "@microsoft/fluid-component-core-interfaces";
import { Jsonable, JsonablePrimitive } from ".";

/**
* A union of the types that Fluid can instrinsically serialize, which is any type is that is
* Json serializable + Json serializable objects/arrays with IComponentHandles at the leaves.
*
* Convenient when declaring type constraints, such as `<T extends Serializable>`.
*
* (See Jsonable for caveats regarding serialization of `undefined` and non-finate numbers.)
*/
export type Serializable = Jsonable<JsonablePrimitive | IComponentHandle>;