Skip to content

Commit

Permalink
Format packages/core
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Nov 7, 2021
1 parent be458d9 commit dd9968c
Show file tree
Hide file tree
Showing 40 changed files with 664 additions and 505 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type vec3 = [number, number, number];
*/
export type vec4 = [number, number, number, number];

// prettier-ignore
/**
* 3x3 matrix, e.g. an affine transform of a 2D vector.
* @hidden
Expand All @@ -53,6 +54,7 @@ export type mat3 = [
number, number, number,
];

// prettier-ignore
/**
* 4x4 matrix, e.g. an affine transform of a 3D vector.
* @hidden
Expand All @@ -65,7 +67,7 @@ export type mat4 = [
];

/** @hidden */
export type bbox = {min: vec3, max: vec3};
export type bbox = { min: vec3; max: vec3 };

/** @hidden */
export const GLB_BUFFER = '@glb.bin';
Expand All @@ -80,7 +82,13 @@ export type TypedArray = Float32Array | Uint32Array | Uint16Array | Uint8Array |
* Abstraction representing the typed array constructors supported by glTF and JavaScript.
* @hidden
*/
export type TypedArrayConstructor = Float32ArrayConstructor | Uint32ArrayConstructor | Uint16ArrayConstructor | Uint8ArrayConstructor | Int16ArrayConstructor | Int8ArrayConstructor;
export type TypedArrayConstructor =
| Float32ArrayConstructor
| Uint32ArrayConstructor
| Uint16ArrayConstructor
| Uint8ArrayConstructor
| Int16ArrayConstructor
| Int8ArrayConstructor;

/** String IDs for core {@link Property} types. */
export enum PropertyType {
Expand Down
54 changes: 51 additions & 3 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,57 @@
export { Document, Transform } from './document';
export { JSONDocument } from './json-document';
export { Extension } from './extension';
export { Accessor, Animation, AnimationChannel, AnimationSampler, Buffer, Camera, ExtensionProperty, Property, Material, Mesh, Node, Primitive, PrimitiveTarget, Root, Scene, Skin, Texture, TextureInfo, TextureLink, AttributeLink, IndexLink, COPY_IDENTITY } from './properties';
export {
Accessor,
Animation,
AnimationChannel,
AnimationSampler,
Buffer,
Camera,
ExtensionProperty,
Property,
Material,
Mesh,
Node,
Primitive,
PrimitiveTarget,
Root,
Scene,
Skin,
Texture,
TextureInfo,
TextureLink,
AttributeLink,
IndexLink,
COPY_IDENTITY,
} from './properties';
export { Graph, GraphChild, GraphChildList, Link } from './graph/';
export { PlatformIO, NodeIO, WebIO, ReaderContext, WriterContext } from './io/';
export { BufferUtils, ColorUtils, FileUtils, ImageUtils, ImageUtilsFormat, Logger, MathUtils, bounds, uuid } from './utils/';
export { TypedArray, TypedArrayConstructor, PropertyType, Format, TextureChannel, VertexLayout, vec2, vec3, vec4, mat3, mat4, bbox, GLB_BUFFER, VERSION } from './constants';
export {
BufferUtils,
ColorUtils,
FileUtils,
ImageUtils,
ImageUtilsFormat,
Logger,
MathUtils,
bounds,
uuid,
} from './utils/';
export {
TypedArray,
TypedArrayConstructor,
PropertyType,
Format,
TextureChannel,
VertexLayout,
vec2,
vec3,
vec4,
mat3,
mat4,
bbox,
GLB_BUFFER,
VERSION,
} from './constants';
export { GLTF } from './types/gltf';
42 changes: 30 additions & 12 deletions packages/core/src/document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { PropertyType } from './constants';
import { Extension } from './extension';
import { Accessor, Animation, AnimationChannel, AnimationSampler, Buffer, Camera, ExtensionProperty, Material, Mesh, Node, Primitive, PrimitiveTarget, Property, PropertyGraph, Root, Scene, Skin, Texture } from './properties';
import {
Accessor,
Animation,
AnimationChannel,
AnimationSampler,
Buffer,
Camera,
ExtensionProperty,
Material,
Mesh,
Node,
Primitive,
PrimitiveTarget,
Property,
PropertyGraph,
Root,
Scene,
Skin,
Texture,
} from './properties';
import { Logger } from './utils';

export type Transform = (doc: Document) => void;
Expand Down Expand Up @@ -100,11 +119,9 @@ export class Document {
/** Merges the content of another Document into this one, without affecting the original. */
public merge(other: Document): this {
// 1. Attach extensions.
const thisExtensions: {[key: string]: Extension} = {};
const thisExtensions: { [key: string]: Extension } = {};
for (const otherExtension of other.getRoot().listExtensionsUsed()) {
const thisExtension = this.createExtension(
otherExtension.constructor as new (doc: Document) => Extension
);
const thisExtension = this.createExtension(otherExtension.constructor as new (doc: Document) => Extension);
if (otherExtension.isRequired()) thisExtension.setRequired(true);
thisExtensions[thisExtension.extensionName] = thisExtension;
}
Expand All @@ -129,11 +146,11 @@ export class Document {
otherProp = thisProp as Property;
} else {
// For other property types, create stub classes.
const PropertyClass = thisProp.constructor as
new(g: PropertyGraph, e?: Extension) => Property;
otherProp = thisProp instanceof ExtensionProperty
? new PropertyClass(this._graph, thisExtensions[thisProp.extensionName])
: new PropertyClass(this._graph);
const PropertyClass = thisProp.constructor as new (g: PropertyGraph, e?: Extension) => Property;
otherProp =
thisProp instanceof ExtensionProperty
? new PropertyClass(this._graph, thisExtensions[thisProp.extensionName])
: new PropertyClass(this._graph);
}

propertyMap.set(thisProp as Property, otherProp);
Expand Down Expand Up @@ -188,8 +205,9 @@ export class Document {
* extension is already enabled for this Document, the previous Extension reference is reused.
*/
createExtension<T extends Extension>(ctor: new (doc: Document) => T): T {
const extensionName = (ctor as unknown as {EXTENSION_NAME: 'string'}).EXTENSION_NAME;
const prevExtension = this.getRoot().listExtensionsUsed()
const extensionName = (ctor as unknown as { EXTENSION_NAME: 'string' }).EXTENSION_NAME;
const prevExtension = this.getRoot()
.listExtensionsUsed()
.find((ext) => ext.extensionName === extensionName);
return (prevExtension || new ctor(this)) as T;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export abstract class Extension implements ExtensionPropertyParent {
protected properties: Set<ExtensionProperty> = new Set();

/** @hidden */
constructor (protected readonly doc: Document) {
constructor(protected readonly doc: Document) {
doc.getRoot()._enableExtension(this);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/graph/graph-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const DECORATOR_PREFIX = '__';
* @hidden
* @category Graph
*/
export function GraphChild (target: any, propertyKey: string): void {
export function GraphChild(target: any, propertyKey: string): void {
Object.defineProperty(target, propertyKey, {
get: function () {
return this[DECORATOR_PREFIX + propertyKey];
Expand All @@ -37,12 +37,12 @@ export function GraphChild (target: any, propertyKey: string): void {
// if (value) console.log('[GraphChild] Assigning link: ' + propertyKey, value);
this[DECORATOR_PREFIX + propertyKey] = value;
},
enumerable: true
enumerable: true,
});
}

/**
* @hidden
* @category Graph
*/
export function GraphChildList (target: any, propertyKey: string): void {}
export function GraphChildList(target: any, propertyKey: string): void {}
21 changes: 13 additions & 8 deletions packages/core/src/graph/graph-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ import { GraphNode } from './graph-node';
export class Link<Parent extends GraphNode, Child extends GraphNode> {
private _disposed = false;
private readonly _listeners: (() => void)[] = [];
constructor(
private readonly _name: string,
private readonly _parent: Parent,
private _child: Child) {
constructor(private readonly _name: string, private readonly _parent: Parent, private _child: Child) {
if (!_parent.canLink(_child)) {
throw new Error('Cannot link disconnected graphs/documents.');
}
}

/** Name. */
getName(): string { return this._name; }
getName(): string {
return this._name;
}

/** Owner node. */
getParent(): Parent { return this._parent; }
getParent(): Parent {
return this._parent;
}

/** Resource node. */
getChild(): Child { return this._child; }
getChild(): Child {
return this._child;
}

/**
* Sets the child node.
Expand Down Expand Up @@ -58,5 +61,7 @@ export class Link<Parent extends GraphNode, Child extends GraphNode> {
}

/** Whether this link has been destroyed. */
isDisposed(): boolean { return this._disposed; }
isDisposed(): boolean {
return this._disposed;
}
}
8 changes: 4 additions & 4 deletions packages/core/src/graph/graph-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export abstract class GraphNode {
}

/** Returns true if the node has been permanently removed from the graph. */
public isDisposed(): boolean { return this._disposed; }
public isDisposed(): boolean {
return this._disposed;
}

/**
* Removes both inbound references to and outbound references from this object. At the end
Expand Down Expand Up @@ -69,9 +71,7 @@ export abstract class GraphNode {
*
* @hidden
*/
protected addGraphChild(
links: Link<GraphNode, GraphNode>[],
link: Link<GraphNode, GraphNode>): this {
protected addGraphChild(links: Link<GraphNode, GraphNode>[], link: Link<GraphNode, GraphNode>): this {
links.push(link);
link.onDispose(() => {
const remaining = links.filter((l) => l !== link);
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Graph<T extends GraphNode> {
private _parentRefs: Map<T, Set<Link<T, T>>> = new Map();
private _childRefs: Map<T, Set<Link<T, T>>> = new Map();

private _listeners: {[event: string]: ((target: unknown) => void)[]} = {};
private _listeners: { [event: string]: ((target: unknown) => void)[] } = {};

public on(type: string, fn: (target: unknown) => void): this {
this._listeners[type] = this._listeners[type] || [];
Expand Down Expand Up @@ -83,11 +83,11 @@ export class Graph<T extends GraphNode> {
}

/**
* Creates a link between two {@link GraphNode} instances. Link is returned
* for the caller to store.
* @param a Owner
* @param b Resource
*/
* Creates a link between two {@link GraphNode} instances. Link is returned
* for the caller to store.
* @param a Owner
* @param b Resource
*/
public link<A extends T>(name: string, a: A, b: null): null;
public link<A extends T, B extends T>(name: string, a: A, b: B): Link<A, B>;
public link<A extends T, B extends T>(name: string, a: A, b: B | null): Link<A, B> | null;
Expand Down Expand Up @@ -116,11 +116,11 @@ export class Graph<T extends GraphNode> {
}

/**
* Removes the link from the graph. This method should only be invoked by
* the onDispose() listener created in {@link link()}. The public method
* of removing a link is {@link link.dispose()}.
* @param link
*/
* Removes the link from the graph. This method should only be invoked by
* the onDispose() listener created in {@link link()}. The public method
* of removing a link is {@link link.dispose()}.
* @param link
*/
private unlink(link: Link<T, T>): this {
this._links.delete(link);
this._parentRefs.get(link.getParent())!.delete(link);
Expand Down
Loading

0 comments on commit dd9968c

Please sign in to comment.