Skip to content

Commit

Permalink
Fixes #85 getList now returns same instance
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserf committed May 17, 2016
1 parent dff576b commit 26e20b2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/record/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ var List = function( recordHandler, name, options ) {
this._record._applyUpdate = this._applyUpdate.bind( this );

this._record.on( 'delete', this.emit.bind( this, 'delete' ) );
this._record.on( 'discard', this.emit.bind( this, 'discard' ) );
this._record.on( 'discard', this._onDiscard.bind( this ) );
this._record.on( 'ready', this._onReady.bind( this ) );

this.isDestroyed = this._record.isDestroyed;
this.isReady = this._record.isReady;
this.name = name;
this._queuedMethods = [];
Expand Down Expand Up @@ -212,6 +213,18 @@ List.prototype._onReady = function() {
this.emit( 'ready' );
};

/**
* Listens for the record discard event and applies
* changes to list
*
* @private
* @returns {void}
*/
List.prototype._onDiscard = function() {
this.isDestroyed = true;
this.emit( 'discard' );
};

/**
* Proxies the underlying Record's _update method. Set's
* data to an empty array if no data is provided.
Expand Down
9 changes: 8 additions & 1 deletion src/record/record-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var RecordHandler = function( options, connection, client ) {
this._connection = connection;
this._client = client;
this._records = {};
this._lists = {};
this._listener = {};
this._destroyEventEmitter = new EventEmitter();

Expand Down Expand Up @@ -63,7 +64,12 @@ RecordHandler.prototype.getRecord = function( name, recordOptions ) {
* @returns {List}
*/
RecordHandler.prototype.getList = function( name, options ) {
return new List( this, name, options );
if( !this._lists[ name ] ) {
this._lists[ name ] = new List( this, name, options );
} else {
this._records[ name ].usages++;
}
return this._lists[ name ];
};

/**
Expand Down Expand Up @@ -275,6 +281,7 @@ RecordHandler.prototype._onDestroyPending = function( recordName ) {
*/
RecordHandler.prototype._removeRecord = function( recordName ) {
delete this._records[ recordName ];
delete this._lists[ recordName ];
};

module.exports = RecordHandler;
73 changes: 73 additions & 0 deletions test-unit/unit/record/record-handler-listSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* global expect, it, describe, jasmine */
var RecordHandler = require( '../../../src/record/record-handler' ),
ClientMock = require( '../../mocks/client-mock' ),
ConnectionMock = require( '../../mocks/message/connection-mock' ),
msg = require( '../../test-helper/test-helper' ).msg,
options = {};

describe( 'record handler returns the right list', function(){

var recordHandler,
listA,
listA2,
onDiscard = jasmine.createSpy( 'onDiscard' ),
connection = new ConnectionMock(),
client = new ClientMock();

it( 'creates the RecordHandler', function(){
recordHandler = new RecordHandler( options, connection, client );
expect( typeof recordHandler.getList ).toBe( 'function' );
});

it( 'retrieves listA', function(){
expect( connection.lastSendMessage ).toBe( null );
listA = recordHandler.getList( 'listA' );
listA.on( 'discard', onDiscard );

expect( connection.lastSendMessage ).toBe( msg( 'R|CR|listA+' ) );
});

it( 'retrieves listA again', function() {
connection.lastSendMessage = null;

listA2 = recordHandler.getList( 'listA' );
expect( listA ).toBe( listA2 );

expect( connection.lastSendMessage ).toBe( null );
});

it( 'initialises listA', function(){
expect( listA.isReady ).toBe( false );
recordHandler._$handle({
topic: 'R',
action: 'R',
data: [ 'listA', 0, '{}' ]
});
expect( listA.isReady ).toBe( true );
});

it( 'discards listA', function(){
listA.discard();
listA2.discard();

expect( onDiscard ).not.toHaveBeenCalled();
expect( listA.isDestroyed ).toBe( false );
expect( connection.lastSendMessage ).toBe( msg( 'R|US|listA+' ) );
});

it( 'returns a new listA and resubscribes', function(){
expect( recordHandler.getList( 'listA' ) ).not.toBe( listA );
expect( listA.isDestroyed ).toBe( false );
expect( connection.lastSendMessage ).toBe( msg( 'R|CR|listA+' ) );
});

it( 'has destroyed listA when discard ack is received', function(){
recordHandler._$handle({
topic: 'R',
action: 'A',
data: [ 'US', 'listA' ]
});
expect( onDiscard ).toHaveBeenCalled();
expect( listA.isDestroyed ).toBe( true );
});
});
File renamed without changes.

0 comments on commit 26e20b2

Please sign in to comment.