| 48 | } |
| 49 | |
| 50 | class BaseScriptingContext implements IScriptingContext { |
| 51 | private _objects:{ [id: string] : IBaseObject; } = {}; |
| 52 | private stage:StageElement; |
| 53 | |
| 54 | constructor (environment:IEnvironment, stage:StageElement) { |
| 55 | this.stage = stage; |
| 56 | } |
| 57 | |
| 58 | public registerObject(objectId:string, serialized:ISerializedData):void { |
| 59 | if (this.hasObject(objectId)) { |
| 60 | throw new ScriptingContextError('Attempting to register object (' + |
| 61 | objectId + ') but object already present.'); |
| 62 | } |
| 63 | var obj = new StageObject(this.stage, |
| 64 | Unpacker.unpack(serialized.class, serialized)); |
| 65 | obj.construct(); |
| 66 | |
| 67 | this._objects[objectId] = obj; |
| 68 | } |
| 69 | |
| 70 | public deregisterObject(objectId:string):void { |
| 71 | // Forces a destruction of the object |
| 72 | if (this.hasObject(objectId)) { |
| 73 | this._objects[objectId].destroy(); |
| 74 | delete this._objects[objectId]; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public updateProperty(objectId:string, propertyName:string, |
| 79 | propertyValue:any):void { |
| 80 | |
| 81 | this.getObject(objectId)[propertyName] = propertyValue; |
| 82 | } |
| 83 | |
| 84 | public callMethod(objectId:string, methodName:string, |
| 85 | parameters:any[]):void { |
| 86 | |
| 87 | var obj = this.getObject(objectId); |
| 88 | if(typeof obj[methodName] !== 'function') { |
| 89 | throw new ScriptingContextError('Object(' + objectId + |
| 90 | ') does not have method "' + methodName + '".'); |
| 91 | } else { |
| 92 | obj[methodName](parameters); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public hasObject(id:string):boolean { |
| 97 | return id in this._objects; |
| 98 | } |
| 99 | |
| 100 | public getObject(id:string):IBaseObject { |
| 101 | if (this.hasObject(id)) { |
| 102 | return this._objects[id].unpackedObject; |
| 103 | } |
| 104 | throw new ScriptingContextError('Object (' + id + ') not found.'); |
| 105 | } |
| 106 | |
| 107 | public reset():void { |
nothing calls this directly
no outgoing calls
no test coverage detected