Skip to content

Commit

Permalink
moved types around to better reflect domains
Browse files Browse the repository at this point in the history
  • Loading branch information
WesleyClements committed Mar 31, 2022
1 parent 7c0fc9f commit fc84e37
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
4 changes: 1 addition & 3 deletions src/annotations/Context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { type } from '.';
import { SchemaDefinition } from '..';
import { DefinitionType, SchemaDefinition, type } from '.';
import { Schema } from '../Schema';
import { DefinitionType } from './defineTypes';

// Colyseus integration
export type ClientWithSessionId = { sessionId: string } & any;
Expand Down
29 changes: 28 additions & 1 deletion src/annotations/SchemaDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import { FilterCallback, FilterChildrenCallback } from '.';
import { DefinitionType } from './defineTypes';
import { getType } from '../types';
import { Schema } from '../Schema';

/**
* Data Types
*/

export type PrimitiveType =
"string" |
"number" |
"boolean" |
"int8" |
"uint8" |
"int16" |
"uint16" |
"int32" |
"uint32" |
"int64" |
"uint64" |
"float32" |
"float64" |
typeof Schema;

export type DefinitionType = PrimitiveType
| [PrimitiveType]
| { array: PrimitiveType }
| { map: PrimitiveType }
| { collection: PrimitiveType }
| { set: PrimitiveType };

export type Definition = { [field: string]: DefinitionType };

Expand Down
9 changes: 1 addition & 8 deletions src/annotations/defineTypes.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { filter, FilterCallback, filterChildren, FilterChildrenCallback, PrimitiveType, type } from '.';
import { DefinitionType, filter, FilterCallback, filterChildren, FilterChildrenCallback, type } from '.';
import { Schema } from '../Schema';
import { ArraySchema } from '../types/ArraySchema';
import { MapSchema } from '../types/MapSchema';
import { Context, globalContext } from './Context';

export type DefinitionType = PrimitiveType
| [PrimitiveType]
| { array: PrimitiveType }
| { map: PrimitiveType }
| { collection: PrimitiveType }
| { set: PrimitiveType };

const isPrimitiveType = (type: any) => typeof type === "string" || Schema.is(type);
const isDefinitionType = (type: any): type is DefinitionType => isPrimitiveType(type) || ArraySchema.is(type) || MapSchema.is(type) || type["type"] == null;

Expand Down
26 changes: 3 additions & 23 deletions src/annotations/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
import { ClientWithSessionId, Context, globalContext } from './Context';
import { Schema } from '../Schema';
import { DefinitionType } from './defineTypes';
import { SchemaDefinition } from './SchemaDefinition';
import { DefinitionType, SchemaDefinition } from './SchemaDefinition';
import { ArraySchema, getArrayProxy } from '../types/ArraySchema';
import { getMapProxy, MapSchema } from '../types/MapSchema';
import { ChangeTree } from '../changes/ChangeTree';

/**
* Types
*/

export type PrimitiveType =
"string" |
"number" |
"boolean" |
"int8" |
"uint8" |
"int16" |
"uint16" |
"int32" |
"uint32" |
"int64" |
"uint64" |
"float32" |
"float64" |
typeof Schema;

export type FilterCallback<
T extends Schema = any,
Expand Down Expand Up @@ -233,5 +213,5 @@ export function filterChildren<T extends Schema, K, V, R extends Schema>(cb: Fil
}

export { ClientWithSessionId, Context, globalContext } from "./Context";
export { SchemaDefinition, Definition } from "./SchemaDefinition";
export type { DefinitionType, DefinitionTypeOptions } from "./defineTypes";
export { SchemaDefinition, Definition, DefinitionType, PrimitiveType } from "./SchemaDefinition";
export type { DefinitionTypeOptions } from "./defineTypes";
5 changes: 2 additions & 3 deletions src/types/ArraySchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChangeTree } from "../changes/ChangeTree";
import { OPERATION } from "../spec";
import { SchemaDecoderCallbacks, Schema } from "../Schema";
import { DefinitionType } from '../annotations/defineTypes';

//
// Notes:
Expand Down Expand Up @@ -94,13 +93,13 @@ export class ArraySchema<V=any> implements Array<V>, SchemaDecoderCallbacks {
public onRemove?: (item: V, key: number) => void;
public onChange?: (item: V, key: number) => void;

static is(type: DefinitionType) {
static is(type: any) {
return (
// type format: ["string"]
Array.isArray(type) ||

// type format: { array: "string" }
(type['array'] !== undefined)
(type?.['array'] !== undefined)
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/types/MapSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChangeTree } from "../changes/ChangeTree";
import { OPERATION } from "../spec";
import { SchemaDecoderCallbacks, Schema } from "../Schema";
import { DefinitionType } from '../annotations/defineTypes';

export function getMapProxy(value: MapSchema) {
value['$proxy'] = true;
Expand Down Expand Up @@ -61,8 +60,9 @@ export class MapSchema<V=any> implements Map<string, V>, SchemaDecoderCallbacks
public onRemove?: (item: V, key: string) => void;
public onChange?: (item: V, key: string) => void;

static is(type: DefinitionType) {
return type['map'] !== undefined;
static is(type: any) {
// type format: { map: "string" }
return type?.['map'] !== undefined;
}

constructor (initialValues?: Map<string, V> | any) {
Expand Down
1 change: 1 addition & 0 deletions src/types/SetSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class SetSchema<V=any> implements SchemaDecoderCallbacks {
public onChange?: (item: V, key: number) => void;

static is(type: any) {
// type format: { set: "string" }
return type['set'] !== undefined;
}

Expand Down

0 comments on commit fc84e37

Please sign in to comment.