Unverified Commit c0424102 authored by Dan Jones's avatar Dan Jones
Browse files

fix: adapter start opens socket or polls

call pollEveryXSeconds repeatedly
bind this context and interval
try catch in receive method
parent d40746e8
......@@ -94,11 +94,9 @@ class Adapter {
}
pollEveryXSeconds(interval) {
this.poll()
.then(() => {
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.poll, interval*1000);
});
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000);
this.poll();
}
/**
......@@ -141,13 +139,17 @@ class Adapter {
}
receive(message) {
const parsed = JSON.parse(message.message);
const validation = this.validate(parsed);
if (validation.valid) {
const type = this.protocol.getType(parsed);
this.protocol.decode(type, parsed);
} else {
this.protocol.receivedInvalid(parsed, validation);
try {
const parsed = JSON.parse(message.message);
const validation = this.validate(parsed);
if (validation.valid) {
const type = this.protocol.getType(parsed);
this.protocol.decode(type, parsed);
} else {
this.protocol.receivedInvalid(parsed, validation);
}
} catch(err) {
console.error('receive failed', err);
}
}
......
......@@ -96,11 +96,9 @@ class Adapter {
}
pollEveryXSeconds(interval) {
this.poll()
.then(() => {
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.poll, interval*1000);
});
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000);
this.poll();
}
/**
......@@ -143,13 +141,17 @@ class Adapter {
}
receive(message) {
const parsed = JSON.parse(message.message);
const validation = this.validate(parsed);
if (validation.valid) {
const type = this.protocol.getType(parsed);
this.protocol.decode(type, parsed);
} else {
this.protocol.receivedInvalid(parsed, validation);
try {
const parsed = JSON.parse(message.message);
const validation = this.validate(parsed);
if (validation.valid) {
const type = this.protocol.getType(parsed);
this.protocol.decode(type, parsed);
} else {
this.protocol.receivedInvalid(parsed, validation);
}
} catch(err) {
console.error('receive failed', err);
}
}
......
......@@ -94,11 +94,9 @@ export class Adapter {
}
pollEveryXSeconds(interval) {
this.poll()
.then(() => {
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.poll, interval*1000);
});
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000);
this.poll();
}
/**
......@@ -141,13 +139,17 @@ export class Adapter {
}
receive(message) {
const parsed = JSON.parse(message.message);
const validation = this.validate(parsed);
if (validation.valid) {
const type = this.protocol.getType(parsed);
this.protocol.decode(type, parsed);
} else {
this.protocol.receivedInvalid(parsed, validation);
try {
const parsed = JSON.parse(message.message);
const validation = this.validate(parsed);
if (validation.valid) {
const type = this.protocol.getType(parsed);
this.protocol.decode(type, parsed);
} else {
this.protocol.receivedInvalid(parsed, validation);
}
} catch(err) {
console.error('receive failed', err);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment