-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
StructuredAssignmentTracker.js
94 lines (88 loc) · 3.03 KB
/
StructuredAssignmentTracker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const SET_KEY = { type: 'SET_KEY' };
export const UNKNOWN_KEY = { type: 'UNKNOWN_KEY' };
export default class StructuredAssignmentTracker {
constructor () {
this._assignments = new Map( [ [ SET_KEY, new Set() ] ] );
}
addAtPath ( path, assignment ) {
if ( path.length === 0 ) {
this._assignments.get( SET_KEY ).add( assignment );
} else {
const [ nextPath, ...remainingPath ] = path;
if ( !this._assignments.has( nextPath ) ) {
this._assignments.set( nextPath, new StructuredAssignmentTracker() );
}
this._assignments.get( nextPath ).addAtPath( remainingPath, assignment );
}
}
forEachAtPath ( path, callback ) {
const [ nextPath, ...remainingPath ] = path;
this._assignments.get( SET_KEY ).forEach( assignment => callback( path, assignment ) );
if ( path.length > 0 ) {
if ( nextPath === UNKNOWN_KEY ) {
this._assignments.forEach( ( assignment, subPath ) => {
if ( subPath !== SET_KEY ) {
assignment.forEachAtPath( remainingPath, callback );
}
} );
} else {
if ( this._assignments.has( nextPath ) ) {
this._assignments.get( nextPath ).forEachAtPath( remainingPath, callback );
}
if ( this._assignments.has( UNKNOWN_KEY ) ) {
this._assignments.get( UNKNOWN_KEY ).forEachAtPath( remainingPath, callback );
}
}
}
}
forEachAssignedToPath ( path, callback ) {
if ( path.length > 0 ) {
const [ nextPath, ...remainingPath ] = path;
if ( this._assignments.has( nextPath ) ) {
this._assignments.get( nextPath ).forEachAssignedToPath( remainingPath, callback );
}
} else {
this._assignments.forEach( ( assignment, subPath ) => {
if ( subPath === SET_KEY ) {
assignment.forEach( subAssignment => callback( [], subAssignment ) );
} else {
assignment.forEachAssignedToPath( [],
( relativePath, assignment ) => callback( [ subPath, ...relativePath ], assignment ) );
}
} );
}
}
hasAtPath ( path, assignment ) {
if ( path.length === 0 ) {
return this._assignments.get( SET_KEY ).has( assignment );
} else {
const [ nextPath, ...remainingPath ] = path;
if ( !this._assignments.has( nextPath ) ) {
return false;
}
return this._assignments.get( nextPath ).hasAtPath( remainingPath, assignment );
}
}
someAtPath ( path, predicateFunction ) {
const [ nextPath, ...remainingPath ] = path;
return Array.from( this._assignments.get( SET_KEY ) ).some( assignment => predicateFunction( path, assignment ) )
|| (
path.length > 0
&& (
(nextPath === UNKNOWN_KEY
&& Array.from( this._assignments ).some( ( [ subPath, assignment ] ) => {
if ( subPath !== SET_KEY ) {
return assignment.someAtPath( remainingPath, predicateFunction );
}
} ))
|| (nextPath !== UNKNOWN_KEY
&& (
(this._assignments.has( nextPath )
&& this._assignments.get( nextPath ).someAtPath( remainingPath, predicateFunction ))
|| (this._assignments.has( UNKNOWN_KEY )
&& this._assignments.get( UNKNOWN_KEY ).someAtPath( remainingPath, predicateFunction ))
))
)
);
}
}