File tree Expand file tree Collapse file tree 3 files changed +48
-1
lines changed Expand file tree Collapse file tree 3 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -376,6 +376,10 @@ module.exports = function(Chart) {
376
376
377
377
updateConfig ( me ) ;
378
378
379
+ // plugins options references might have change, let's invalidate the cache
380
+ // https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
381
+ plugins . _invalidate ( me ) ;
382
+
379
383
if ( plugins . notify ( me , 'beforeUpdate' ) === false ) {
380
384
return ;
381
385
}
Original file line number Diff line number Diff line change @@ -121,7 +121,7 @@ module.exports = {
121
121
* @private
122
122
*/
123
123
descriptors : function ( chart ) {
124
- var cache = chart . _plugins || ( chart . _plugins = { } ) ;
124
+ var cache = chart . $plugins || ( chart . $plugins = { } ) ;
125
125
if ( cache . id === this . _cacheId ) {
126
126
return cache . descriptors ;
127
127
}
@@ -157,6 +157,16 @@ module.exports = {
157
157
cache . descriptors = descriptors ;
158
158
cache . id = this . _cacheId ;
159
159
return descriptors ;
160
+ } ,
161
+
162
+ /**
163
+ * Invalidates cache for the given chart: descriptors hold a reference on plugin option,
164
+ * but in some cases, this reference can be changed by the user when updating options.
165
+ * https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
166
+ * @private
167
+ */
168
+ _invalidate : function ( chart ) {
169
+ delete chart . $plugins ;
160
170
}
161
171
} ;
162
172
Original file line number Diff line number Diff line change @@ -339,6 +339,39 @@ describe('Chart.plugins', function() {
339
339
340
340
expect ( plugin . hook ) . toHaveBeenCalled ( ) ;
341
341
expect ( plugin . hook . calls . first ( ) . args [ 1 ] ) . toEqual ( { a : 'foobar' } ) ;
342
+
343
+ delete Chart . defaults . global . plugins . a ;
344
+ } ) ;
345
+
346
+ // https://github.com/chartjs/Chart.js/issues/5111#issuecomment-355934167
347
+ it ( 'should invalidate cache when update plugin options' , function ( ) {
348
+ var plugin = { id : 'a' , hook : function ( ) { } } ;
349
+ var chart = window . acquireChart ( {
350
+ plugins : [ plugin ] ,
351
+ options : {
352
+ plugins : {
353
+ a : {
354
+ foo : 'foo'
355
+ }
356
+ }
357
+ } ,
358
+ } ) ;
359
+
360
+ spyOn ( plugin , 'hook' ) ;
361
+
362
+ Chart . plugins . notify ( chart , 'hook' ) ;
363
+
364
+ expect ( plugin . hook ) . toHaveBeenCalled ( ) ;
365
+ expect ( plugin . hook . calls . first ( ) . args [ 1 ] ) . toEqual ( { foo : 'foo' } ) ;
366
+
367
+ chart . options . plugins . a = { bar : 'bar' } ;
368
+ chart . update ( ) ;
369
+
370
+ plugin . hook . calls . reset ( ) ;
371
+ Chart . plugins . notify ( chart , 'hook' ) ;
372
+
373
+ expect ( plugin . hook ) . toHaveBeenCalled ( ) ;
374
+ expect ( plugin . hook . calls . first ( ) . args [ 1 ] ) . toEqual ( { bar : 'bar' } ) ;
342
375
} ) ;
343
376
} ) ;
344
377
} ) ;
You can’t perform that action at this time.
0 commit comments