@@ -27,7 +27,10 @@ const internal = {};
2727 * @returns {Promise<String> } the saved `cv`
2828 */
2929internal . updateChangeVersion = function ( cv ) {
30- return this . store . setChangeVersion ( cv ) ;
30+ return this . store . setChangeVersion ( cv ) . then ( ( ) => {
31+ this . emit ( 'change-version' , cv ) ;
32+ return cv ;
33+ } ) ;
3134} ;
3235
3336/**
@@ -182,9 +185,10 @@ internal.requestObjectVersion = function( id, version ) {
182185} ;
183186
184187internal . applyChange = function ( change , ghost ) {
185- var acknowledged = internal . findAcknowledgedChange . bind ( this ) ( change ) ,
186- error ,
187- emit ,
188+ const acknowledged = internal . findAcknowledgedChange . bind ( this ) ( change ) ,
189+ updateChangeVersion = internal . updateChangeVersion . bind ( this , change . cv ) ;
190+
191+ let error ,
188192 original ,
189193 patch ,
190194 modified ;
@@ -204,8 +208,6 @@ internal.applyChange = function( change, ghost ) {
204208 return ;
205209 }
206210
207- emit = this . emit . bind ( this , 'change-version' , change . cv , change ) ;
208-
209211 if ( change . o === operation . MODIFY ) {
210212 if ( ghost && ( ghost . version !== change . sv ) ) {
211213 internal . requestObjectVersion . call ( this , change . id , change . sv ) . then ( data => {
@@ -217,10 +219,10 @@ internal.applyChange = function( change, ghost ) {
217219 original = ghost . data ;
218220 patch = change . v ;
219221 modified = jsondiff . apply_object_diff ( original , patch ) ;
220- return internal . updateObjectVersion . bind ( this ) ( change . id , change . ev , modified , original , patch , acknowledged )
221- . then ( emit ) ;
222+ return internal . updateObjectVersion . call ( this , change . id , change . ev , modified , original , patch , acknowledged )
223+ . then ( updateChangeVersion ) ;
222224 } else if ( change . o === operation . REMOVE ) {
223- return internal . removeObject . bind ( this ) ( change . id , acknowledged ) . then ( emit ) ;
225+ return internal . removeObject . bind ( this ) ( change . id , acknowledged ) . then ( updateChangeVersion ) ;
224226 }
225227}
226228
@@ -273,6 +275,9 @@ internal.indexingComplete = function() {
273275 * messages are stripped of their channel number that seperates bucket operations.
274276 * The `Client` maintains which commands should be routed to which channel.
275277 *
278+ * The channel is responsible for creating all change operations and downloading
279+ * bucket data.
280+ *
276281 */
277282export default function Channel ( appid , access_token , store , name ) {
278283 const channel = this ;
@@ -310,7 +315,6 @@ export default function Channel( appid, access_token, store, name ) {
310315
311316 // Handle change errors caused by changes originating from this client
312317 this . localQueue . on ( 'error' , internal . handleChangeError . bind ( this ) ) ;
313- this . on ( 'change-version' , internal . updateChangeVersion . bind ( this ) ) ;
314318}
315319
316320/**
0 commit comments