Skip to content

Commit 46d752f

Browse files
fix(store): ignore Ivy in runtime checks (#2491)
Closes #2404
1 parent 39a4b91 commit 46d752f

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

modules/store/spec/meta-reducers/immutability_reducer.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ describe('immutabilityCheckMetaReducer:', () => {
3333
}).toThrow();
3434
});
3535

36+
it('should not throw on ivy properties (because these are ignored)', () => {
37+
let dispatchedAction: any;
38+
expect(() =>
39+
invokeActionReducer((state: any, action: any) => {
40+
dispatchedAction = action;
41+
return state;
42+
})
43+
).not.toThrow();
44+
45+
expect(() => {
46+
dispatchedAction.ɵIvyProperty.value = 2;
47+
}).not.toThrow();
48+
});
49+
3650
it('should not throw when check is off', () => {
3751
expect(() =>
3852
invokeActionReducer((state: any, action: any) => {
@@ -46,7 +60,15 @@ describe('immutabilityCheckMetaReducer:', () => {
4660
immutabilityCheckMetaReducer((state, action) => reduce(state, action), {
4761
action: () => checkIsOn,
4862
state: () => false,
49-
})({}, { type: 'invoke', numbers: [1, 2, 3], fun: function() {} });
63+
})(
64+
{},
65+
{
66+
type: 'invoke',
67+
numbers: [1, 2, 3],
68+
fun: function() {},
69+
ɵIvyProperty: { value: 1 },
70+
}
71+
);
5072
}
5173
});
5274

modules/store/spec/meta-reducers/serialization_reducer.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import { Component } from '@angular/core';
12
import { serializationCheckMetaReducer } from '../../src/meta-reducers';
23

34
describe('serializationCheckMetaReducer:', () => {
5+
class AComponent {}
6+
Object.defineProperty(AComponent, 'ɵcmp', {});
7+
48
const serializables: Record<string, any> = {
59
number: { value: 4 },
610
boolean: { value: true },
@@ -10,6 +14,7 @@ describe('serializationCheckMetaReducer:', () => {
1014
nested: { value: { number: 7, array: ['n', 'g', 'r', 'x'] } },
1115
null: { value: null },
1216
undefined: { value: undefined },
17+
component: AComponent, // components should not throw (because these are ignored)
1318
};
1419

1520
const unSerializables: Record<string, any> = {

modules/store/src/meta-reducers/immutability_reducer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ function freeze(target: any) {
2020
const targetIsFunction = isFunction(target);
2121

2222
Object.getOwnPropertyNames(target).forEach(prop => {
23+
// Ignore Ivy properties, ref: https://github.com/ngrx/platform/issues/2109#issuecomment-582689060
24+
if (prop.startsWith('ɵ')) {
25+
return;
26+
}
27+
2328
if (
2429
hasOwnProperty(target, prop) &&
2530
(targetIsFunction

modules/store/src/meta-reducers/serialization_reducer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
isString,
99
isArray,
1010
RUNTIME_CHECK_URL,
11+
isComponent,
1112
} from './utils';
1213

1314
export function serializationCheckMetaReducer(
@@ -51,6 +52,11 @@ function getUnserializable(
5152

5253
const value = (target as any)[key];
5354

55+
// Ignore Ivy components
56+
if (isComponent(value)) {
57+
return result;
58+
}
59+
5460
if (
5561
isUndefined(value) ||
5662
isNull(value) ||

modules/store/src/meta-reducers/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export function isFunction(target: any): target is Function {
4646
return typeof target === 'function';
4747
}
4848

49+
export function isComponent(target: any) {
50+
return isFunction(target) && target.hasOwnProperty('ɵcmp');
51+
}
52+
4953
export function hasOwnProperty(target: object, propertyName: string): boolean {
5054
return Object.prototype.hasOwnProperty.call(target, propertyName);
5155
}

0 commit comments

Comments
 (0)