Skip to content

Commit 66a3398

Browse files
committed
Allows the channel to be set after the bucket is constructed
Lets external API's remain backwards compatible
1 parent 1b732e1 commit 66a3398

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

src/simperium/bucket.js

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,52 @@ export default function Bucket( name, storeProvider, channel ) {
4747
this.store = storeProvider( this );
4848
this.storeAPI = promiseAPI( this.store );
4949
this.isIndexing = false;
50-
this.channel = channel;
5150

51+
/**
52+
* Listeners for channel events
53+
*/
54+
this.onChannelIndex = this.emit.bind( this, 'index' );
55+
this.onChannelError = this.emit.bind( this, 'error' );
56+
this.onChannelUpdate = ( id, data ) => {
57+
this.update( id, data, { sync: false } );
58+
};
59+
60+
this.onChannelIndexingStateChange = ( isIndexing ) => {
61+
this.isIndexing = isIndexing;
62+
if ( isIndexing ) {
63+
this.emit( 'indexing' );
64+
}
65+
};
66+
67+
this.onChannelRemove = ( id ) => this.remove( id );
68+
69+
if ( channel ) {
70+
this.setChannel( channel );
71+
}
72+
}
73+
74+
inherits( Bucket, EventEmitter );
75+
76+
Bucket.prototype.setChannel = function( channel ) {
77+
if ( this.channel ) {
78+
this.channel
79+
.removeListener( 'index', this.onChannelIndex )
80+
.removeListener( 'error', this.onChannelError )
81+
.removeListener( 'update', this.onChannelUpdate )
82+
.removeListener( 'indexingStateChange', this.onChannelIndexingStateChange )
83+
.removeListener( 'remove', this.onChannelRemove );
84+
}
85+
this.channel = channel;
5286
channel
5387
// forward the index and error events from the channel
54-
.on( 'index', ( ... args ) => this.emit( 'index', ... args ) )
55-
.on( 'error', ( ... args ) => this.emit( 'error', ... args ) )
88+
.on( 'index', this.onChannelIndex )
89+
.on( 'error', this.onChannelError )
5690
// when the channel updates or removes data, the bucket should apply
5791
// the same updates
58-
.on( 'update', ( id, data ) => {
59-
this.update( id, data, { sync: false } );
60-
} )
61-
.on( 'indexingStateChange', ( isIndexing ) => {
62-
this.isIndexing = isIndexing;
63-
if ( isIndexing ) {
64-
this.emit( 'indexing' );
65-
}
66-
} )
67-
.on( 'remove', ( id ) => {
68-
// TODO, there needs te be a way to remove without telling the
69-
// channel to do it
70-
this.remove( id );
71-
} );
72-
}
73-
74-
inherits( Bucket, EventEmitter );
92+
.on( 'update', this.onChannelUpdate )
93+
.on( 'indexingStateChange', this.onChannelIndexingStateChange )
94+
.on( 'remove', this.onChannelRemove );
95+
};
7596

7697
Bucket.prototype.reload = function() {
7798
this.channel.reload();

src/simperium/client.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ inherits( Client, EventEmitter );
5454
Client.prototype.bucket = function( name ) {
5555
var channelId = this.buckets.length,
5656
bucket = new Bucket( name, this.options.objectStoreProvider ),
57-
channel = new Channel( this.appId, this.accessToken, bucket, this.options.ghostStoreProvider( bucket ) ),
57+
channel = new Channel( this.appId, this.accessToken, this.options.ghostStoreProvider( bucket ), name ),
5858
send = this.sendChannelMessage.bind( this, channelId ),
5959
receive = channel.handleMessage.bind( channel );
6060

61+
bucket.setChannel( channel );
62+
6163
this.buckets.push( bucket );
6264

6365
channel.on( 'unauthorized', this.onUnauthorized.bind( this ) );

0 commit comments

Comments
 (0)