Skip to content

Commit e8ff5ed

Browse files
nponirosdfahlander
authored andcommitted
Change the format of an intercomm message
Don't merge the contents of the "message" property into the msg before triggering on('message'). Adjust tests-observable-misc and Dexie.Syncable for that. Add MessageEvent interface. This is the type received by the on('message') event. Remove requestId and isFailure from sendMessage() options. These are used internally to handle response messages and should not be available to a consumer.
1 parent 7d0aa37 commit e8ff5ed

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

addons/Dexie.Observable/src/Dexie.Observable.d.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@ declare module 'dexie' {
1515
// (makes it valid to do new db.observable.SyncNode())
1616
observable: {
1717
SyncNode: Dexie.Observable.SyncNodeConstructor;
18+
Foo: Dexie.Observable.MessageEvent;
1819
sendMessage(
19-
type: string,
20-
message: any,
20+
type: string, // Don't use 'response' as it is used internally by the framework
21+
message: any, // anything that can be saved by IndexedDB
2122
destinationNode: number,
2223
options: {
23-
wantReply?: boolean,
24-
isFailure?: boolean,
25-
requestId?: number
26-
})
27-
: Promise<any>;
24+
wantReply?: boolean;
25+
}
26+
): Promise<any> | void; // When wantReply is undefined or false return is void
2827

2928
broadcastMessage(
3029
type: string,
31-
message: any,
30+
message: any, // anything that can be saved by IndexedDB
3231
bIncludeSelf: boolean
3332
): void;
3433
}
@@ -45,7 +44,7 @@ declare module 'dexie' {
4544
interface DbEvents {
4645
(eventName: 'changes', subscriber: (changes: IDatabaseChange[], partial: boolean)=>void): void;
4746
(eventName: 'cleanup', subscriber: ()=>any): void;
48-
(eventName: 'message', subscriber: (msg: any)=>any): void;
47+
(eventName: 'message', subscriber: (msg: Dexie.Observable.MessageEvent)=>any): void;
4948
}
5049

5150
// Extended IndexSpec with uuid boolean for primary key.
@@ -100,6 +99,17 @@ declare module 'dexie' {
10099
(eventName: 'intercomm', subscriber: (dbName: string) => void): void;
101100
(eventName: 'beforeunload', subscriber: () => void): void;
102101
}
102+
103+
// Object received by on('message') after sendMessage() or broadcastMessage()
104+
interface MessageEvent {
105+
id: number;
106+
type: string;
107+
message: any;
108+
destinationNode: number;
109+
wantReply?: boolean;
110+
resolve(result: any): void;
111+
reject(error: any): void;
112+
}
103113
}
104114
}
105115
}

addons/Dexie.Observable/src/intercomm.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@ export default function initIntercomm(db, Observable, SyncNode, mySyncNode, loca
121121
msg.reject = function (error) {
122122
db.observable.sendMessage('response', {error: error.toString()}, msg.sender, {isFailure: true, requestId: msg.id});
123123
};
124-
var message = msg.message;
125-
delete msg.message;
126-
Dexie.extend(msg, message);
127124
db.on.message.fire(msg);
128125
}
129126
}

addons/Dexie.Observable/test/test-typings/test-typings.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ syncNode.lastHeartBeat.toExponential();
2525
syncNode.myRevision.toFixed();
2626

2727
db.on('message', msg => {
28+
msg.type;
29+
msg.message;
30+
msg.destinationNode * 1;
31+
msg.wantReply;
32+
msg.resolve('foo');
33+
msg.reject(new Error('Foo'));
2834
});
29-
db.observable.sendMessage('myMsgType', {foo: 'bar'}, 13, {wantReply: true});
35+
db.observable.sendMessage('myMsgType', {foo: 'bar'}, 13, {wantReply: true}).then(() => {});
36+
db.observable.sendMessage('myMsgType', 'foobar', 13, {wantReply: false});
3037

3138
db.observable.broadcastMessage('myBroadcastMsgType', {foo2: 'bar2'}, false);
3239

addons/Dexie.Observable/test/tests-observable-misc.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
let senderID;
100100
let receiverID;
101101
db2.on('message', (msg) => {
102-
strictEqual(msg.foo, 'foobar', 'We got the correct message');
102+
strictEqual(msg.message, 'foobar', 'We got the correct message');
103103
strictEqual(msg.sender, senderID, 'We got the right sender ID');
104104
strictEqual(msg.type, 'request', 'We got the correct type');
105105
strictEqual(msg.destinationNode, receiverID, 'We got the correct destination node');
@@ -117,7 +117,7 @@
117117
})
118118
.then((arr) => {
119119
receiverID = arr.filter((node) => node.id !== senderID)[0].id;
120-
db1.observable.sendMessage('request', {foo: 'foobar'}, receiverID, {});
120+
db1.observable.sendMessage('request', 'foobar', receiverID, {});
121121
})
122122
.catch((e) => {
123123
ok(false, 'Error: ' + e);
@@ -128,7 +128,7 @@
128128
const db1 = createDB();
129129
let senderID;
130130
db1.on('message', (msg) => {
131-
strictEqual(msg.foo, 'foobar', 'We got the correct message');
131+
deepEqual(msg.message, {foo: 'foobar'}, 'We got the correct message');
132132
strictEqual(msg.sender, senderID, 'We got the right sender ID');
133133
strictEqual(msg.type, 'request', 'We got the correct type');
134134
strictEqual(msg.destinationNode, senderID, 'We got the correct destination node');
@@ -203,7 +203,7 @@
203203
let senderID;
204204
let receiverID;
205205
db2.on('message', (msg) => {
206-
strictEqual(msg.foo, 'foobar', 'We got the correct message');
206+
deepEqual(msg.message, {foo: 'foobar'}, 'We got the correct message');
207207
strictEqual(msg.sender, senderID, 'We got the right sender ID');
208208
strictEqual(msg.type, 'request', 'We got the correct type');
209209
strictEqual(msg.destinationNode, receiverID, 'We got the correct destination node');
@@ -235,7 +235,7 @@
235235
let receiverID;
236236

237237
db1.on('message', (msg) => {
238-
strictEqual(msg.foo, 'foobar', 'We got the correct message');
238+
deepEqual(msg.message, {foo: 'foobar'}, 'We got the correct message');
239239
strictEqual(msg.sender, senderID, 'We got the right sender ID');
240240
strictEqual(msg.type, 'broadcast', 'We got the correct type');
241241
strictEqual(msg.destinationNode, senderID, 'We got the correct destination node');
@@ -265,7 +265,7 @@
265265
let senderID;
266266
let receiverID;
267267
db2.on('message', (msg) => {
268-
strictEqual(msg.foo, 'foobar', 'We got the correct message');
268+
deepEqual(msg.message, {foo: 'foobar'}, 'We got the correct message');
269269
strictEqual(msg.sender, senderID, 'We got the right sender ID');
270270
strictEqual(msg.type, 'request', 'We got the correct type');
271271
strictEqual(msg.destinationNode, receiverID, 'We got the correct destination node');

addons/Dexie.Syncable/src/Dexie.Syncable.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ export default function Syncable (db) {
4141
Dexie.vip(function() {
4242
if (msg.type === 'connect') {
4343
// We are master node and another non-master node wants us to do the connect.
44-
db.syncable.connect(msg.protocolName, msg.url, msg.options).then(msg.resolve, msg.reject);
44+
db.syncable.connect(msg.message.protocolName, msg.message.url, msg.message.options).then(msg.resolve, msg.reject);
4545
} else if (msg.type === 'disconnect') {
46-
db.syncable.disconnect(msg.url).then(msg.resolve, msg.reject);
46+
db.syncable.disconnect(msg.message.url).then(msg.resolve, msg.reject);
4747
} else if (msg.type === 'syncStatusChanged') {
4848
// We are client and a master node informs us about syncStatus change.
4949
// Lookup the connectedProvider and call its event
50-
db.syncable.on.statusChanged.fire(msg.newStatus, msg.url);
50+
db.syncable.on.statusChanged.fire(msg.message.newStatus, msg.message.url);
5151
}
5252
});
5353
});

0 commit comments

Comments
 (0)