Skip to content

Commit 5900de0

Browse files
committed
Changed onsubscribe instruction on protocol to be more standard
1 parent c51799e commit 5900de0

File tree

7 files changed

+34
-32
lines changed

7 files changed

+34
-32
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = function(grunt) {
9090
dest: 'dist/'+dopmin
9191
},
9292
options: {
93-
banner: '/* dop@<%= pkg.version %> - (c) 2016 Josema Gonzalez - MIT Licensed */\n'
93+
banner: '/* dop - (c) 2016 Josema Gonzalez - MIT Licensed */\n'
9494
}
9595
},
9696

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "0.22.9",
3+
"version": "0.22.10",
44
"main": "./dist/dop.nodejs.js",
55
"browser": "./dist/dop.js",
66
"unpkg": "./dist/dop.min.js",

src/core/constructors/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ dop.core.node.prototype.subscribe = function() {
2828
};
2929

3030
dop.core.node.prototype.unsubscribe = function(object) {
31+
dop.util.invariant(dop.isRegistered(object), 'Node.unsubscribe needs a subscribed object');
3132
return dop.protocol.unsubscribe(this, object);
3233
};

src/dop.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(function factory(root) {
22

33
var dop = {
4-
name: 'dop',
54
version: '{{VERSION}}',
65
create: factory,
76

src/protocol/_onsubscribe.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,41 @@ dop.protocol._onsubscribe = function(node, request_id, request, response) {
77
request.promise.reject(dop.core.getRejectError(response[0]));
88

99
else {
10-
var object_path = typeof response[1]=='number' ? [response[1]] : response[1],
11-
object_owner_id = object_path[0],
10+
var object_owner_id = response[1],
1211
object_owner = response[2],
12+
object_path = isArray(object_owner) ? object_owner : [],
1313
object, collector;
14-
15-
if (!isArray(object_path) || typeof object_owner_id!='number')
16-
request.promise.reject(dop.core.error.reject_local.OBJECT_NOT_FOUND);
1714

18-
else {
19-
if (node.owner[object_owner_id] === undefined) {
20-
collector = dop.collect();
21-
if (dop.isRegistered(request.into))
22-
object = dop.core.setPatch(request.into, object_owner, dop.core.setPatchFunctionMutator);
23-
else
24-
object = dop.register((request.into===undefined) ?
25-
object_owner
26-
:
27-
dop.core.setPatch(request.into, object_owner, dop.core.setPatchMutator)
28-
);
29-
dop.core.registerOwner(node, object, object_owner_id);
30-
collector.emit();
31-
}
32-
else
33-
object = dop.data.object[node.owner[object_owner_id]].object;
15+
// New object
16+
if (node.owner[object_owner_id] === undefined) {
3417

35-
object = dop.util.get(object, object_path.slice(1));
36-
37-
if (!isObject(object))
18+
// If is new object and third parameter is an array we must reject
19+
if (object_owner===object_path)
3820
request.promise.reject(dop.core.error.reject_local.OBJECT_NOT_FOUND);
21+
22+
collector = dop.collect();
23+
if (dop.isRegistered(request.into))
24+
object = dop.core.setPatch(request.into, object_owner, dop.core.setPatchFunctionMutator);
3925
else
40-
request.promise.resolve(dop.getObjectProxy(object));
26+
object = dop.register((request.into===undefined) ?
27+
object_owner
28+
:
29+
dop.core.setPatch(request.into, object_owner, dop.core.setPatchMutator)
30+
);
31+
dop.core.registerOwner(node, object, object_owner_id);
32+
collector.emit();
4133
}
34+
// Already registered
35+
else
36+
object = dop.data.object[node.owner[object_owner_id]].object;
37+
38+
object = dop.util.get(object, object_path);
39+
40+
if (!isObject(object))
41+
request.promise.reject(dop.core.error.reject_local.OBJECT_NOT_FOUND);
42+
else
43+
request.promise.resolve(dop.getObjectProxy(object));
44+
4245
}
4346
}
4447
};

src/protocol/instructions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dop.protocol.instructions = {
2626
// Subscriptor -> Owner
2727
subscribe: 1, // [ 1234, <instruction>, <params...>]
2828
// [-1234, 0, <object_id>, <data_object>]
29-
// [-1234, 0, [<object_id>, 'path']]
29+
// [-1234, 0, <object_id>, ['path']]
3030

3131
// Subscriptor -> Owner
3232
unsubscribe: 2, // [ 1234, <instruction>, <object_id>]

src/protocol/onsubscribe.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ dop.protocol.onsubscribe = function(node, request_id, request) {
1717
if (dop.core.registerSubscriber(node, object_root))
1818
response.push(object_id, object_root);
1919

20-
// Object already registered
20+
// Object already subscribed
2121
else
22-
response.push(object_path);
23-
22+
response.push(object_id, object_path.slice(1));
2423

2524
dop.core.storeSendMessages(node, response, dop.encodeFunction);
2625
return object;

0 commit comments

Comments
 (0)