Typescript shared javascript's flawed notion of 'this'.

This commit is contained in:
Eric Harding committed 2018-04-24 22:19:31 -04:00
1 parent 783a71217f
commit f3a2525b8d
7 files changed
+23 -11

No files matched your search

+11
View File
@@ -7,6 +7,17 @@
namespace bts {
namespace api {
template <class T>
const std::string toJSON(const T& t) {
google::protobuf::util::JsonPrintOptions options;
options.add_whitespace = false;
options.always_print_primitive_fields = true;
std::string result;
auto status = google::protobuf::util::MessageToJsonString(t, &result, options);
if (status.ok()) return result;
else return "";
}
template <class T>
const std::string format_qualifier(const char* type_name, const T& t) {
google::protobuf::util::JsonPrintOptions options;
+3
View File
@@ -1,10 +1,12 @@
#include "ApiGateway.h"
#include "lib/logging/logging.h"
#include "lib/api/public/ApiUtils.h"
#include <vector>
#include <unordered_map>
#include <mutex>
using namespace bts::api::gateway;
namespace bts {
@@ -52,6 +54,7 @@ ApiGatewaySession::~ApiGatewaySession(){}
const GatewayResponse ApiGatewaySession::handle(const api::gateway::GatewayRequest& request) {
GatewayResponse response;
LOG(Api,Info, "Gateway Request: {}", api::toJSON(request));
switch(request.request_case()) {
case GatewayRequest::kLogin:
_me->handle_login(request.login(), response.mutable_logon());
@@ -3,10 +3,6 @@ import {root} from 'proto'
import { connect, ServiceContext } from '../src/ServiceContext'
import { HelloService } from '../src/HelloService'
// Fix websocket for running in js
// import WebSocket from 'ws'
// global["WebSocket"] = WebSocket
test('log in and say hi', async () => {
let ctx = await connect("bts", "password");
expect(ctx instanceof ServiceContext).toBe(true)
-1
View File
@@ -2,7 +2,6 @@
setlocal
cd %~dp0
set build=%~dp0/../../../build/web/api
del %dp0\..\..\..\buildlweb\api\ServiceContext.d.ts
:: run protobuf generator on .proto files
call npm run protogen
:: build tyepscript
-1
View File
@@ -1,5 +1,4 @@
module.exports = {
// testEnvironment: 'node',
transform: {
"^.+\\.tsx?$": "ts-jest"
},
+9 -4
View File
@@ -32,7 +32,8 @@ class PrivateContext {
this.url = url || default_ws_uri()
this.username = username
this.password = password
this.sc = new ServiceContext(this.sendgw)
// If you use just 'this.sengw' this breaks because it uses the wrong 'this'
this.sc = new ServiceContext(m => this.sendgw(m))
}
connect() : Promise<ServiceContext|string> {
@@ -57,9 +58,9 @@ class PrivateContext {
console.log("Reply: " + JSON.stringify(response))
if (response.logon.isValid) {
// hooking onclose triggers auto-reconnect
this.ws.onclose = this.onclose
this.ws.onclose = e => this.onclose(e)
// Switch to steady state onmessage
this.ws.onmessage = this.onmessage
this.ws.onmessage = e => this.onmessage(e)
console.log("Authenticated")
this.sc.token = response.logon.token
resolve(this.sc)
@@ -88,7 +89,9 @@ class PrivateContext {
onmessage(e){
const buffer = new Uint8Array(e.data)
const response = GatewayResponse.decode(buffer)
this.sc.on_msg(response.result, response.addendum)
if (response) {
this.sc.on_msg(response.result, response.addendum)
}
}
}
@@ -101,6 +104,7 @@ export class ServiceContext {
constructor(send : (msg)=>void) {
this.next_id = 1000
this.send = send
this.outstanding = {}
}
call(service_id : string, method_id : number, body : Uint8Array) : Promise<Uint8Array> {
@@ -125,6 +129,7 @@ export class ServiceContext {
subscribe() {/*not impl*/}
on_msg(header: {request_id:number, code: number}, body : Uint8Array) {
console.log("message received: " + JSON.stringify(header));
if (header.request_id in this.outstanding) {
const v = this.outstanding[header.request_id]
delete this.outstanding[header.request_id]
-1
View File
@@ -7,7 +7,6 @@ export function assert(condition:boolean, message?:string) {
}
export function assertValid(mtype, message) {
const r = mtype.verify(message)
console.log(r)
assert(r == null, r)
}