Skip to content

Commit

Permalink
fix(store): ignore Ivy in runtime checks (#2491)
Browse files Browse the repository at this point in the history
Closes #2404
  • Loading branch information
timdeschryver authored Apr 18, 2020
1 parent 39a4b91 commit 46d752f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
24 changes: 23 additions & 1 deletion modules/store/spec/meta-reducers/immutability_reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ describe('immutabilityCheckMetaReducer:', () => {
}).toThrow();
});

it('should not throw on ivy properties (because these are ignored)', () => {
let dispatchedAction: any;
expect(() =>
invokeActionReducer((state: any, action: any) => {
dispatchedAction = action;
return state;
})
).not.toThrow();

expect(() => {
dispatchedAction.ɵIvyProperty.value = 2;
}).not.toThrow();
});

it('should not throw when check is off', () => {
expect(() =>
invokeActionReducer((state: any, action: any) => {
Expand All @@ -46,7 +60,15 @@ describe('immutabilityCheckMetaReducer:', () => {
immutabilityCheckMetaReducer((state, action) => reduce(state, action), {
action: () => checkIsOn,
state: () => false,
})({}, { type: 'invoke', numbers: [1, 2, 3], fun: function() {} });
})(
{},
{
type: 'invoke',
numbers: [1, 2, 3],
fun: function() {},
ɵIvyProperty: { value: 1 },
}
);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Component } from '@angular/core';
import { serializationCheckMetaReducer } from '../../src/meta-reducers';

describe('serializationCheckMetaReducer:', () => {
class AComponent {}
Object.defineProperty(AComponent, 'ɵcmp', {});

const serializables: Record<string, any> = {
number: { value: 4 },
boolean: { value: true },
Expand All @@ -10,6 +14,7 @@ describe('serializationCheckMetaReducer:', () => {
nested: { value: { number: 7, array: ['n', 'g', 'r', 'x'] } },
null: { value: null },
undefined: { value: undefined },
component: AComponent, // components should not throw (because these are ignored)
};

const unSerializables: Record<string, any> = {
Expand Down
5 changes: 5 additions & 0 deletions modules/store/src/meta-reducers/immutability_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function freeze(target: any) {
const targetIsFunction = isFunction(target);

Object.getOwnPropertyNames(target).forEach(prop => {
// Ignore Ivy properties, ref: https://github.com/ngrx/platform/issues/2109#issuecomment-582689060
if (prop.startsWith('ɵ')) {
return;
}

if (
hasOwnProperty(target, prop) &&
(targetIsFunction
Expand Down
6 changes: 6 additions & 0 deletions modules/store/src/meta-reducers/serialization_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isString,
isArray,
RUNTIME_CHECK_URL,
isComponent,
} from './utils';

export function serializationCheckMetaReducer(
Expand Down Expand Up @@ -51,6 +52,11 @@ function getUnserializable(

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

// Ignore Ivy components
if (isComponent(value)) {
return result;
}

if (
isUndefined(value) ||
isNull(value) ||
Expand Down
4 changes: 4 additions & 0 deletions modules/store/src/meta-reducers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export function isFunction(target: any): target is Function {
return typeof target === 'function';
}

export function isComponent(target: any) {
return isFunction(target) && target.hasOwnProperty('ɵcmp');
}

export function hasOwnProperty(target: object, propertyName: string): boolean {
return Object.prototype.hasOwnProperty.call(target, propertyName);
}

0 comments on commit 46d752f

Please sign in to comment.