Skip to content

Commit 6eae475

Browse files
committed
cache the properties passed to inert
1 parent 671b2c3 commit 6eae475

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/core/middleware/inert.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const inert = factory(({ middleware: { node, destroy, icache } }) => {
3535
if (previousSettings && enable === previousSettings.enable && invert === previousSettings.invert) {
3636
return;
3737
}
38-
38+
icache.set(key, { enable, invert });
3939
if (invert) {
4040
const inertNodes = inertInvertedNodeMap.get(key) || [];
4141
if (enable) {

tests/core/unit/middleware/inert.ts

+52
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,56 @@ jsdomDescribe('inert middleware', () => {
121121
assert.strictEqual(childTwo.inert, false);
122122
assert.strictEqual(childThree.inert, false);
123123
});
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+
});
124176
});

0 commit comments

Comments
 (0)