Skip to content

Commit

Permalink
decouple three bindings and three-maxscript bridge, make them injecta…
Browse files Browse the repository at this point in the history
…ble from inversify
  • Loading branch information
nmalex committed Feb 18, 2019
1 parent a29caf5 commit 14f5d6c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@ export enum SessionServiceEvents {
export interface IThreeMaxscriptBridge {
PostScene(sceneJson: any): Promise<any>;
}

export interface ISceneObjectBinding {
PostObject(objectJson: any): Promise<string>;
}
4 changes: 4 additions & 0 deletions src/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { MaxScriptClientPool } from "./services/maxscript_client_pool";
import { IMaxscriptClient, IThreeMaxscriptBridge } from "./interfaces";
import { ThreeMaxscriptBridgePool } from "./services/three_maxscript_bridge_pool";
import { ThreeMaxscriptBridgeFactory } from "./maxscript/three_maxscript_bridge_factory";
import { SceneBinding } from "./maxscript/three_maxscript_bindings/scene_binding";

const myContainer = new Container();

Expand Down Expand Up @@ -57,6 +58,9 @@ myContainer.bind<interfaces.ISessionPool<IThreeMaxscriptBridge>>(TYPES.IThreeMax
myContainer.bind<interfaces.IFactory<interfaces.IMaxscriptClient>>(TYPES.IMaxscriptClientFactory).to(MaxscriptClientFactory).inSingletonScope();
myContainer.bind<interfaces.IFactory<interfaces.IThreeMaxscriptBridge>>(TYPES.IThreeMaxscriptBridgeFactory).to(ThreeMaxscriptBridgeFactory).inSingletonScope();

// bindings
myContainer.bind<interfaces.ISceneObjectBinding>(TYPES.ISceneObjectBinding).to(SceneBinding);

// tip: this is how to export same instance with different interfaces
// EXAMPLE: myContainer.bind<interfaces.ISessionObserver>(TYPES.ISessionObserver).toService(TYPES.ISessionObserver);
// ===
Expand Down
9 changes: 9 additions & 0 deletions src/maxscript/three_maxscript_bindings/scene_binding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { injectable } from "inversify";
import { ISceneObjectBinding } from "../../interfaces";

@injectable()
export class SceneBinding implements ISceneObjectBinding {
public async PostObject(objectJson: any): Promise<string> {
return JSON.stringify(objectJson);
}
}
7 changes: 5 additions & 2 deletions src/maxscript/three_maxscript_bridge.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { IMaxscriptClient, IThreeMaxscriptBridge } from "../interfaces";
import { IMaxscriptClient, IThreeMaxscriptBridge, ISceneObjectBinding } from "../interfaces";
import { isArray } from "util";
import { multiInject } from "inversify";

// translates three json objects and changes to maxscript commands,
// to keep scene up to date with latest changes in three.js scene.
export class ThreeMaxscriptBridge implements IThreeMaxscriptBridge {
private _maxscript: IMaxscriptClient;
private _sceneObjectBindings: ISceneObjectBinding[];

private _sceneJson: any;

constructor(
// @multiInject("IWeapon") weapons: IThreeConverter2[],
maxscript: IMaxscriptClient,
sceneObjectBindings: ISceneObjectBinding[],
) {
this._maxscript = maxscript;
this._sceneObjectBindings = sceneObjectBindings;
}

public async PostScene(sceneJson: any): Promise<any> {
Expand Down
9 changes: 6 additions & 3 deletions src/maxscript/three_maxscript_bridge_factory.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { injectable, inject } from "inversify";
import { injectable, inject, multiInject } from "inversify";
import { TYPES } from "../types";
import { IFactory, IMaxscriptClient, ISessionPool, IThreeMaxscriptBridge } from "../interfaces";
import { IFactory, IMaxscriptClient, ISessionPool, IThreeMaxscriptBridge, ISceneObjectBinding } from "../interfaces";
import { Session } from "../database/model/session";
import { ThreeMaxscriptBridge } from "./three_maxscript_bridge";

@injectable()
export class ThreeMaxscriptBridgeFactory implements IFactory<IThreeMaxscriptBridge> {
private _maxscriptClientPool: ISessionPool<IMaxscriptClient>;
private _sceneObjectBindings: ISceneObjectBinding[];

constructor(
@inject(TYPES.IMaxscriptClientPool) maxscriptClientPool: ISessionPool<IMaxscriptClient>,
@multiInject(TYPES.ISceneObjectBinding) sceneObjectBindings: ISceneObjectBinding[],
) {
this._maxscriptClientPool = maxscriptClientPool;
this._sceneObjectBindings = sceneObjectBindings;
}

public async Create(session: Session): Promise<IThreeMaxscriptBridge> {
let maxscript: IMaxscriptClient = await this._maxscriptClientPool.Get(session);
return new ThreeMaxscriptBridge(maxscript);
return new ThreeMaxscriptBridge(maxscript, this._sceneObjectBindings);
}
}
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const TYPES = {
ISessionService: Symbol.for("ISessionService"),
IMaxscriptClientPool: Symbol.for("ISessionPool<IMaxscriptClient>"),
IThreeMaxscriptBridgePool: Symbol.for("ISessionPool<IThreeMaxscriptBridge>"),

// bindings
ISceneObjectBinding: Symbol.for("ISceneObjectBinding"),
};

export { TYPES };
File renamed without changes.
2 changes: 1 addition & 1 deletion test_client/js/rfarm_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function pad(num, size) {
var rfarm = {
apiKey: "75f5-4d53-b0f4",
workspace: "55a0bd33-9f15-4bc0-a482-17899eb67af3",
baseUrl: "https://acc.renderfarmjs.com",
baseUrl: "https://localhost:8000",

geometries: {}, // here we map scene geometry uuid <==> backend geometry resource
materials: {}, // here we map scene material uuid <==> backend material resource
Expand Down

0 comments on commit 14f5d6c

Please sign in to comment.