Skip to content

Commit f4e0d79

Browse files
authored
Only run the inert middleware when the properties have been changed (#888)
* only run inert for nodes when the options change * cache the properties passed to inert
1 parent ad9f5e4 commit f4e0d79

File tree

2 files changed

+93
-9
lines changed

2 files changed

+93
-9
lines changed

src/core/middleware/inert.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { create, node, destroy } from '../vdom';
2+
import { createICacheMiddleware } from './icache';
23
import { from as arrayFrom } from '../../shim/array';
34
import Map from '../../shim/Map';
45
import '../../shim/inert';
56

6-
const factory = create({ node, destroy });
7+
interface InertState {
8+
[index: string]: {
9+
enable: boolean;
10+
invert: boolean;
11+
};
12+
}
13+
14+
const icache = createICacheMiddleware<InertState>();
15+
16+
const factory = create({ node, destroy, icache });
717

8-
export const inert = factory(({ middleware: { node, destroy } }) => {
18+
export const inert = factory(({ middleware: { node, destroy, icache } }) => {
919
const inertInvertedNodeMap = new Map<string | number, any[]>();
1020
destroy(() => {
1121
inertInvertedNodeMap.forEach((nodes) => {
@@ -21,7 +31,11 @@ export const inert = factory(({ middleware: { node, destroy } }) => {
2131
if (!domNode) {
2232
return;
2333
}
24-
34+
const previousSettings = icache.get(key);
35+
if (previousSettings && enable === previousSettings.enable && invert === previousSettings.invert) {
36+
return;
37+
}
38+
icache.set(key, { enable, invert });
2539
if (invert) {
2640
const inertNodes = inertInvertedNodeMap.get(key) || [];
2741
if (enable) {

tests/core/unit/middleware/inert.ts

+76-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
import global from '../../../../src/shim/global';
2-
const { it } = intern.getInterface('bdd');
2+
const { it, beforeEach } = intern.getInterface('bdd');
33
const { describe: jsdomDescribe } = intern.getPlugin('jsdom');
44
const { assert } = intern.getPlugin('chai');
5-
import { sandbox } from 'sinon';
5+
import { stub } from 'sinon';
66

7+
import icacheMiddleware from '../../../../src/core/middleware/icache';
78
import inertMiddleware from '../../../../src/core/middleware/inert';
89

9-
const sb = sandbox.create();
10+
let invalidatorStub = stub();
1011

1112
jsdomDescribe('inert middleware', () => {
13+
let icache = icacheMiddleware().callback({
14+
properties: () => ({}),
15+
children: () => [],
16+
id: 'test-cache',
17+
middleware: { invalidator: invalidatorStub, destroy: stub() }
18+
});
19+
beforeEach(() => {
20+
icache = icacheMiddleware().callback({
21+
properties: () => ({}),
22+
children: () => [],
23+
id: 'test-cache',
24+
middleware: { invalidator: invalidatorStub, destroy: stub() }
25+
});
26+
});
1227
it('should set node inert property', () => {
1328
const node = global.document.createElement('div');
1429

1530
const inert = inertMiddleware().callback({
1631
id: 'test',
1732
middleware: {
18-
destroy: sb.stub(),
33+
icache,
34+
destroy: stub(),
1935
node: {
2036
get() {
2137
return node;
@@ -45,7 +61,8 @@ jsdomDescribe('inert middleware', () => {
4561
const inert = inertMiddleware().callback({
4662
id: 'test',
4763
middleware: {
48-
destroy: sb.stub(),
64+
icache,
65+
destroy: stub(),
4966
node: {
5067
get() {
5168
return node;
@@ -77,11 +94,12 @@ jsdomDescribe('inert middleware', () => {
7794
parent.appendChild(childTwo);
7895
parent.appendChild(childThree);
7996
parent.appendChild(node);
80-
const destroyStub = sb.stub();
97+
const destroyStub = stub();
8198

8299
const inert = inertMiddleware().callback({
83100
id: 'test',
84101
middleware: {
102+
icache,
85103
destroy: destroyStub,
86104
node: {
87105
get() {
@@ -103,4 +121,56 @@ jsdomDescribe('inert middleware', () => {
103121
assert.strictEqual(childTwo.inert, false);
104122
assert.strictEqual(childThree.inert, false);
105123
});
124+
125+
it('should only set inert state when properties change', () => {
126+
const parent = global.document.createElement('div');
127+
const childOne = global.document.createElement('div');
128+
const childTwo = global.document.createElement('div');
129+
const childThree = global.document.createElement('div');
130+
const childFour = global.document.createElement('div');
131+
const node = global.document.createElement('div');
132+
parent.appendChild(childOne);
133+
parent.appendChild(childTwo);
134+
parent.appendChild(childThree);
135+
parent.appendChild(node);
136+
let _inert = false;
137+
const inertStub = stub();
138+
Object.defineProperty(childFour, 'inert', {
139+
get() {
140+
return _inert;
141+
},
142+
set(value: boolean) {
143+
_inert = value;
144+
inertStub();
145+
}
146+
});
147+
const destroyStub = stub();
148+
const inert = inertMiddleware().callback({
149+
id: 'test',
150+
middleware: {
151+
icache,
152+
destroy: destroyStub,
153+
node: {
154+
get() {
155+
return node;
156+
}
157+
}
158+
},
159+
properties: () => ({}),
160+
children: () => []
161+
});
162+
inert.set('key', true, true);
163+
assert.strictEqual(node.inert, false);
164+
assert.strictEqual(childOne.inert, true);
165+
assert.strictEqual(childTwo.inert, true);
166+
assert.strictEqual(childThree.inert, true);
167+
assert.strictEqual(inertStub.callCount, 0);
168+
parent.appendChild(childFour);
169+
inert.set('key', true, true);
170+
assert.strictEqual(node.inert, false);
171+
assert.strictEqual(childOne.inert, true);
172+
assert.strictEqual(childTwo.inert, true);
173+
assert.strictEqual(childThree.inert, true);
174+
assert.strictEqual(inertStub.callCount, 0);
175+
});
106176
});

0 commit comments

Comments
 (0)