UNPKG

858 BJavaScriptView Raw
1import { union, without } from './utils/js_utils';
2export default class EnterLeaveCounter {
3 constructor(isNodeInDocument) {
4 this.entered = [];
5 this.isNodeInDocument = isNodeInDocument;
6 }
7 enter(enteringNode) {
8 const previousLength = this.entered.length;
9 const isNodeEntered = (node) => this.isNodeInDocument(node) &&
10 (!node.contains || node.contains(enteringNode));
11 this.entered = union(this.entered.filter(isNodeEntered), [enteringNode]);
12 return previousLength === 0 && this.entered.length > 0;
13 }
14 leave(leavingNode) {
15 const previousLength = this.entered.length;
16 this.entered = without(this.entered.filter(this.isNodeInDocument), leavingNode);
17 return previousLength > 0 && this.entered.length === 0;
18 }
19 reset() {
20 this.entered = [];
21 }
22}