Skip to content

Commit e688c6a

Browse files
Merge pull request #9 from MichaelMarner/feature/8-start-action
Handles the START response
2 parents e3abec6 + 437deba commit e688c6a

File tree

2 files changed

+67
-26
lines changed

2 files changed

+67
-26
lines changed

lib/src/remote_devtools_middleware.dart

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
part of redux_remote_devtools;
22

3+
/// The connection state of the middleware
4+
enum RemoteDevToolsStatus {
5+
/// No socket connection to the remote host
6+
notConnected,
7+
8+
/// Attempting to open socket
9+
connecting,
10+
11+
/// Connected to remote, but not started
12+
connected,
13+
14+
/// Awating start response
15+
starting,
16+
17+
/// Sending and receiving actions
18+
started
19+
}
20+
321
class RemoteDevToolsMiddleware extends MiddlewareClass {
422
/**
523
* The remote-devtools server to connect to. Should include
@@ -12,7 +30,7 @@ class RemoteDevToolsMiddleware extends MiddlewareClass {
1230
SocketClusterWrapper socket;
1331
Store store;
1432
String _channel;
15-
bool _started = false;
33+
RemoteDevToolsStatus status = RemoteDevToolsStatus.notConnected;
1634

1735
ActionEncoder actionEncoder;
1836
StateEncoder stateEncoder;
@@ -22,16 +40,17 @@ class RemoteDevToolsMiddleware extends MiddlewareClass {
2240
this.stateEncoder = const JsonStateEncoder(),
2341
this.socket}) {
2442
if (socket == null) {
25-
this.socket =
26-
new SocketClusterWrapper('ws://${this._host}/socketcluster/');
43+
this.socket = new SocketClusterWrapper('ws://${this._host}/socketcluster/');
2744
}
2845
}
2946

3047
connect() async {
48+
_setStatus(RemoteDevToolsStatus.connecting);
3149
await this.socket.connect();
50+
_setStatus(RemoteDevToolsStatus.connected);
3251
this._channel = await this._login();
52+
_setStatus(RemoteDevToolsStatus.starting);
3353
this._relay('START');
34-
_started = true;
3554
this.socket.on(_channel, (String name, dynamic data) {
3655
this.handleEventFromRemote(data as Map<String, dynamic>);
3756
});
@@ -42,8 +61,7 @@ class RemoteDevToolsMiddleware extends MiddlewareClass {
4261

4362
Future<String> _login() {
4463
Completer<String> c = new Completer<String>();
45-
this.socket.emit('login', 'master',
46-
(String name, dynamic error, dynamic data) {
64+
this.socket.emit('login', 'master', (String name, dynamic error, dynamic data) {
4765
c.complete(data as String);
4866
});
4967
return c.future;
@@ -69,6 +87,10 @@ class RemoteDevToolsMiddleware extends MiddlewareClass {
6987
case 'DISPATCH':
7088
_handleDispatch(data['action']);
7189
break;
90+
// The START action is a response indicating that remote devtools is up and running
91+
case 'START':
92+
_setStatus(RemoteDevToolsStatus.started);
93+
break;
7294
default:
7395
print('Unknown type:' + data['type'].toString());
7496
}
@@ -78,9 +100,7 @@ class RemoteDevToolsMiddleware extends MiddlewareClass {
78100
switch (action['type'] as String) {
79101
case 'JUMP_TO_STATE':
80102
if (this.store != null) {
81-
this
82-
.store
83-
.dispatch(new DevToolsAction.jumpToState(action['index'] as int));
103+
this.store.dispatch(new DevToolsAction.jumpToState(action['index'] as int));
84104
} else {
85105
print('No store reference set, cannot dispatch remote action');
86106
}
@@ -91,8 +111,12 @@ class RemoteDevToolsMiddleware extends MiddlewareClass {
91111
/// Middleware function called by redux, dispatches actions to devtools
92112
call(Store store, dynamic action, NextDispatcher next) {
93113
next(action);
94-
if (_started && !(action is DevToolsAction)) {
114+
if (this.status == RemoteDevToolsStatus.started && !(action is DevToolsAction)) {
95115
this._relay('ACTION', store.state, action);
96116
}
97117
}
118+
119+
_setStatus(RemoteDevToolsStatus value) {
120+
this.status = value;
121+
}
98122
}

test/remote_devtools_middleware_test.dart

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,25 @@ void main() {
4141
test('it sends the login message', () async {
4242
when(socket.connect()).thenAnswer((_) => new Future.value());
4343
when(socket.id).thenReturn('testId');
44-
when(socket.emit("login", "master", captureAny))
45-
.thenAnswer((Invocation i) {
44+
when(socket.emit("login", "master", captureAny)).thenAnswer((Invocation i) {
4645
Function fn = i.positionalArguments[2];
4746
fn('testChannel', 'err', 'data');
4847
});
4948
await devtools.connect();
5049
verify(socket.emit("login", "master", captureAny));
5150
});
5251
test('it sends the start message message', () async {
53-
when(socket.emit("login", "master", captureAny))
54-
.thenAnswer((Invocation i) {
52+
when(socket.emit("login", "master", captureAny)).thenAnswer((Invocation i) {
5553
Function fn = i.positionalArguments[2];
5654
fn('testChannel', 'err', 'data');
5755
});
5856
when(socket.id).thenReturn('testId');
5957
connectResponse = await devtools.connect();
60-
verify(socket.emit("log",
61-
{'type': "START", 'id': 'testId', 'name': 'flutter'}, captureAny));
58+
verify(
59+
socket.emit("log", {'type': "START", 'id': 'testId', 'name': 'flutter'}, captureAny));
6260
});
6361
test('it sends the state', () async {
64-
when(socket.emit("login", "master", captureAny))
65-
.thenAnswer((Invocation i) {
62+
when(socket.emit("login", "master", captureAny)).thenAnswer((Invocation i) {
6663
Function fn = i.positionalArguments[2];
6764
fn('testChannel', 'err', 'data');
6865
});
@@ -71,8 +68,8 @@ void main() {
7168
when(store.state).thenReturn('TEST STATE');
7269
devtools.store = store;
7370
connectResponse = await devtools.connect();
74-
verify(socket.emit("log",
75-
{'type': "START", 'id': 'testId', 'name': 'flutter'}, captureAny));
71+
verify(
72+
socket.emit("log", {'type': "START", 'id': 'testId', 'name': 'flutter'}, captureAny));
7673
verify(socket.emit(
7774
"log",
7875
{
@@ -95,8 +92,7 @@ void main() {
9592
store = new MockStore();
9693
when(store.state).thenReturn({'state': 42});
9794
socket = new MockSocket();
98-
when(socket.emit("login", "master", captureAny))
99-
.thenAnswer((Invocation i) {
95+
when(socket.emit("login", "master", captureAny)).thenAnswer((Invocation i) {
10096
Function fn = i.positionalArguments[2];
10197
fn('testChannel', 'err', 'data');
10298
});
@@ -105,7 +101,22 @@ void main() {
105101
devtools = RemoteDevToolsMiddleware('example.com', socket: socket);
106102
await devtools.connect();
107103
});
104+
test('nothing sent if status is not started', () {
105+
devtools.call(store, TestActions.SomeAction, next.next);
106+
verifyNever(socket.emit(
107+
'log',
108+
{
109+
'type': 'ACTION',
110+
'id': 'testId',
111+
'name': 'flutter',
112+
'payload': '{"state":42}',
113+
'action': '{"type":"TestActions.SomeAction"}',
114+
'nextActionId': null
115+
},
116+
captureAny));
117+
});
108118
test('the action and state are sent', () {
119+
devtools.status = RemoteDevToolsStatus.started;
109120
devtools.call(store, TestActions.SomeAction, next.next);
110121
verify(socket.emit(
111122
'log',
@@ -132,8 +143,7 @@ void main() {
132143
store = new MockStore();
133144
when(store.state).thenReturn({'state': 42});
134145
socket = new MockSocket();
135-
when(socket.emit("login", "master", captureAny))
136-
.thenAnswer((Invocation i) {
146+
when(socket.emit("login", "master", captureAny)).thenAnswer((Invocation i) {
137147
Function fn = i.positionalArguments[2];
138148
fn('testChannel', 'err', 'data');
139149
});
@@ -143,6 +153,14 @@ void main() {
143153
devtools.store = store;
144154
await devtools.connect();
145155
});
156+
test('START response sets status to STARTED', () {
157+
var remoteData = {
158+
'type': 'START',
159+
};
160+
expect(devtools.status, RemoteDevToolsStatus.starting);
161+
devtools.handleEventFromRemote(remoteData);
162+
expect(devtools.status, RemoteDevToolsStatus.started);
163+
});
146164
test('handles time travel', () {
147165
var remoteData = {
148166
'type': 'DISPATCH',
@@ -157,8 +175,7 @@ void main() {
157175
'type': 'DISPATCH',
158176
'action': {'type': 'JUMP_TO_STATE', 'index': 4}
159177
};
160-
expect(
161-
() => devtools.handleEventFromRemote(remoteData), returnsNormally);
178+
expect(() => devtools.handleEventFromRemote(remoteData), returnsNormally);
162179
});
163180
});
164181
});

0 commit comments

Comments
 (0)