Commit 78eb17e331e6158000fb02d94ece2aaa7b29de65
tidy
Dominic Tarr committed on 4/16/2017, 12:07:30 PMParent: b89be0e4eebccbafefdaea2b2ee1081fc8df5a1c
Files changed
state.js | changed |
state.js | ||
---|---|---|
@@ -41,9 +41,9 @@ | ||
41 | 41 … | //this is not a reduce, and it has side effects. |
42 | 42 … | exports.read = function (state) { |
43 | 43 … | if(state.ready == null) return state |
44 | 44 … | var _ready = state.ready |
45 | - state.ready = null | |
45 … | + state.ready = null | |
46 | 46 … | if(isMessage(_ready)) { |
47 | 47 … | if(state.remote.seq != null && Math.max(state.remote.seq, state.remote.req) +1 !== _ready.sequence) { |
48 | 48 … | throw new Error('out of order!') |
49 | 49 … | } |
@@ -80,52 +80,33 @@ | ||
80 | 80 … | exports.receiveMessage = function (state, msg) { |
81 | 81 … | if(!isMessage(msg)) throw new Error('expected a Message!') |
82 | 82 … | var _state = clone(state) |
83 | 83 … | |
84 | -// if(state.remote.tx === false) { | |
85 | -// console.log('received msg, when !tx') | |
86 | -// console.log(msg, state) | |
87 | -// } | |
88 | -// | |
89 | 84 … | _state.remote.req = Math.max(state.remote.req || 0, msg.sequence) |
85 … | + //not the same^ | |
90 | 86 … | _state.remote.seq = Math.max(state.remote.seq || 0, msg.sequence) |
91 | 87 … | |
92 | - if(state.remote.seq > msg.sequence) { | |
93 | -// console.log('receiveMessage', state, msg) | |
94 | -// throw new Error('remote sent out of order') | |
95 | - } | |
96 | - //we may have sent something to them | |
97 | - // _state.remote.seq = Math.max(msg.sequence, _state.remote.seq) | |
98 | - | |
99 | 88 … | if(state.remote.tx == null) |
100 | 89 … | throw new Error('we received a message, when we where waiting for remote to send initial request') |
101 | 90 … | |
102 | 91 … | var seq = state.local.seq |
103 | 92 … | if(isMessage(state.ready)) { |
104 | - if(state.ready.sequence <= msg.sequence) { | |
93 … | + if(state.ready.sequence <= msg.sequence) | |
105 | 94 … | state.ready = null |
106 | - //throw new Error('about to send message we just received') | |
107 | - } | |
108 | 95 … | } |
109 | 96 … | if(isOldMessage(state, msg)) { |
110 | 97 … | //we already know this, please shut up! |
111 | - if(state.remote.tx) { | |
112 | - //let read move us out of tx mode, | |
113 | - //incase this note is overridden. | |
114 | - //_state.remote.tx = false | |
98 … | + //let read move us out of tx mode, | |
99 … | + if(state.remote.tx) | |
115 | 100 … | _state.ready = -seq |
116 | - } | |
117 | 101 … | } |
118 | 102 … | else if(isNextRxMessage(state, msg)) { |
119 | 103 … | //since we now know they are ahead, stop transmitting to them |
120 | 104 … | if(state.ready != null) |
121 | 105 … | _state.ready = null |
122 | 106 … | _state.effect = msg |
123 | 107 … | if(state.remote.tx == false) |
124 | 108 … | state.ready = state.local.seq |
125 | - //update local seq, instead of calling to append? | |
126 | - //so that we can handle the next incomming? | |
127 | - //what if an invalid message arrives? we should abort this receiver... | |
128 | 109 … | } |
129 | 110 … | else { |
130 | 111 … | //this means something went really wrong |
131 | 112 … | _state.error = true |
@@ -139,12 +120,8 @@ | ||
139 | 120 … | var seq = state.local.seq |
140 | 121 … | var requested = note >= 0 |
141 | 122 … | var _seq = Math.max(Math.abs(note), state.remote.seq) |
142 | 123 … | |
143 | -// if(state.local.tx != requested) { | |
144 | -// console.log('enable tx') | |
145 | -// } | |
146 | -// | |
147 | 124 … | _state.local.tx = requested |
148 | 125 … | _state.remote.req = Math.max(_seq, _state.remote.req) |
149 | 126 … | |
150 | 127 … | if(state.local.req == null) return _state |
@@ -192,18 +169,13 @@ | ||
192 | 169 … | _state.ready = null |
193 | 170 … | |
194 | 171 … | if(state.local.seq > Math.max(state.remote.req,state.remote.seq)) |
195 | 172 … | state.effect = Math.max(state.remote.req,state.remote.seq) + 1 |
196 | -// if(msg.sequence > state.remote.seq) { | |
197 | -// //console.log('can send!!', msg, state) | |
198 | -// state.ready = state.remote.seq + 1 | |
199 | -// } | |
200 | 173 … | } |
201 | 174 … | } |
202 | 175 … | else if(!state.local.tx) { |
203 | 176 … | //unless we know they are up to this, send a note |
204 | 177 … | if(msg.sequence > state.remote.req) { |
205 | - //console.log('APPEND_TURN_OFF_R_TX', msg, state) | |
206 | 178 … | _state.ready = state.remote.tx ? msg.sequence : -msg.sequence //SEND NOTE |
207 | 179 … | } |
208 | 180 … | else if(isNote(state.ready) && state.ready > 0) |
209 | 181 … | state.ready = msg.sequence //UPDATE NOTE |
@@ -219,21 +191,11 @@ | ||
219 | 191 … | var _state = clone(state) |
220 | 192 … | if (isNextTxMessage(state, msg)) { |
221 | 193 … | _state.ready = msg |
222 | 194 … | } |
223 | - else { | |
224 | - //do nothing, it's an old message, so send no notes. | |
225 | - //we should only get here is we triggered a `get` effect | |
226 | - //but then changed state before it completed. | |
227 | - ; | |
228 | - } | |
229 | - if(!state.local.tx) //do nothing | |
230 | - //if we are not in sending state, just stop. | |
231 | - //otherwise, the next retrival will be triggered by READ | |
232 | - ; | |
195 … | + //if it's not the next message | |
196 … | + //we must have changed state while retriving this message | |
197 … | + //anyway, just get on with things | |
233 | 198 … | return _state |
234 | 199 … | } |
235 | 200 … | |
236 | 201 … | |
237 | - | |
238 | - | |
239 | - |
Built with git-ssb-web