@@ -52,73 +52,73 @@ MysqlSubscription = function(connection, name /* arguments */){
52
52
if ( _ . filter ( buffer , function ( sub ) {
53
53
return sub . name === name && sub . connection === connection ;
54
54
} ) . length === 1 ) {
55
- registerStore ( connection , name ) ;
56
- }
57
-
58
- } ;
59
-
60
- var registerStore = function ( connection , name ) {
61
- connection . registerStore ( name , {
62
- beginUpdate : function ( ) { } ,
63
- update : function ( msg ) {
64
- var subBuffer = _ . filter ( buffer , function ( sub ) {
65
- return sub . subscriptionId === msg . id ;
66
- } ) [ 0 ] ;
67
- var sub = subBuffer . instance ;
68
-
69
- if ( msg . msg === 'added' &&
70
- msg . fields && msg . fields . reset === true ) {
71
- // This message indicates a reset of a result set
72
- if ( subBuffer . resetOnDiff === false ) {
73
- sub . dispatchEvent ( 'reset' , msg ) ;
74
- sub . splice ( 0 , sub . length ) ;
75
- }
76
- } else if ( msg . msg === 'added' &&
77
- msg . fields && 'diff' in msg . fields ) {
78
- // Aggregation of changes has arrived
55
+ connection . registerStore ( name , {
56
+ update : function ( msg ) {
57
+ var subBuffers = _ . filter ( buffer , function ( sub ) {
58
+ return sub . subscriptionId === msg . id ;
59
+ } ) ;
79
60
80
- if ( subBuffer . resetOnDiff === true ) {
81
- sub . splice ( 0 , sub . length ) ;
82
- subBuffer . resetOnDiff = false ;
83
- }
61
+ // If no existing subscriptions match this message's subscriptionId,
62
+ // discard message as it is most likely due to a subscription that has
63
+ // been destroyed.
64
+ // See test/MysqlSubscription :: Quick Change test cases
65
+ if ( subBuffers . length === 0 ) return ;
66
+
67
+ var subBuffer = subBuffers [ 0 ] ;
68
+ var sub = subBuffer . instance ;
69
+
70
+ if ( msg . msg === 'added' &&
71
+ msg . fields && msg . fields . reset === true ) {
72
+ // This message indicates a reset of a result set
73
+ if ( subBuffer . resetOnDiff === false ) {
74
+ sub . dispatchEvent ( 'reset' , msg ) ;
75
+ sub . splice ( 0 , sub . length ) ;
76
+ }
77
+ } else if ( msg . msg === 'added' &&
78
+ msg . fields && 'diff' in msg . fields ) {
79
+ // Aggregation of changes has arrived
84
80
85
- msg . fields . diff . forEach ( function ( event ) {
86
- var index = event . pop ( ) ;
87
- var oldRow ;
81
+ if ( subBuffer . resetOnDiff === true ) {
82
+ sub . splice ( 0 , sub . length ) ;
83
+ subBuffer . resetOnDiff = false ;
84
+ }
88
85
89
- // Provide generic update event
90
- sub . dispatchEvent ( 'update' , index , {
91
- msg : event [ 0 ] ,
92
- collection : msg . collection ,
93
- id : msg . id ,
94
- fields : event [ 0 ] === 'update' ? event [ 2 ] : event [ 1 ]
86
+ msg . fields . diff . forEach ( function ( event ) {
87
+ var index = event . pop ( ) ;
88
+ var oldRow ;
89
+
90
+ // Provide generic update event
91
+ sub . dispatchEvent ( 'update' , index , {
92
+ msg : event [ 0 ] ,
93
+ collection : msg . collection ,
94
+ id : msg . id ,
95
+ fields : event [ 0 ] === 'update' ? event [ 2 ] : event [ 1 ]
96
+ } ) ;
97
+
98
+ // Provide specific change event and perform change
99
+ switch ( event [ 0 ] ) {
100
+ case 'added' :
101
+ sub . splice ( index , 0 , event [ 1 ] ) ;
102
+ sub . dispatchEvent ( event [ 0 ] , index , event [ 1 ] ) ;
103
+ break ;
104
+ case 'changed' :
105
+ oldRow = _ . clone ( sub [ index ] ) ;
106
+ sub [ index ] = _ . extend ( sub [ index ] , event [ 2 ] ) ;
107
+ sub . dispatchEvent ( event [ 0 ] , index , oldRow , sub [ index ] ) ;
108
+ break ;
109
+ case 'removed' :
110
+ oldRow = _ . clone ( sub [ index ] ) ;
111
+ sub . splice ( index , 1 ) ;
112
+ sub . dispatchEvent ( event [ 0 ] , index , oldRow ) ;
113
+ break ;
114
+ }
95
115
} ) ;
96
-
97
- // Provide specific change event and perform change
98
- switch ( event [ 0 ] ) {
99
- case 'added' :
100
- sub . splice ( index , 0 , event [ 1 ] ) ;
101
- sub . dispatchEvent ( event [ 0 ] , index , event [ 1 ] ) ;
102
- break ;
103
- case 'changed' :
104
- oldRow = _ . clone ( sub [ index ] ) ;
105
- sub [ index ] = _ . extend ( sub [ index ] , event [ 2 ] ) ;
106
- sub . dispatchEvent ( event [ 0 ] , index , oldRow , sub [ index ] ) ;
107
- break ;
108
- case 'removed' :
109
- oldRow = _ . clone ( sub [ index ] ) ;
110
- sub . splice ( index , 1 ) ;
111
- sub . dispatchEvent ( event [ 0 ] , index , oldRow ) ;
112
- break ;
113
- }
114
- } ) ;
116
+ }
117
+ sub . changed ( ) ;
115
118
}
116
- sub . changed ( ) ;
117
- } ,
118
- endUpdate : function ( ) { } ,
119
- saveOriginals : function ( ) { } ,
120
- retrieveOriginals : function ( ) { }
121
- } ) ;
119
+ } ) ;
120
+ }
121
+
122
122
} ;
123
123
124
124
// Inherit from Array and Tracker.Dependency
0 commit comments