1- import { api as entityModule } from '../../ entity-module/GraphicEntityModule.js'
2- import { toggles } from '../../config .js'
3- import { ErrorLog } from '../core/ErrorLog .js'
4- import { MissingToggleError } from './errors/MissingToggleError .js'
1+ import { api as entityModule } from '../entity-module/GraphicEntityModule.js'
2+ import { ErrorLog } from '../core/ErrorLog .js'
3+ import { MissingToggleError } from './errors/MissingToggleError .js'
4+ import { DuplicateToggleValueError } from './errors/DuplicateToggleValueError .js'
55
66export class ToggleModule {
77 constructor ( assets ) {
88 this . previousFrame = { }
99 this . missingToggles = { }
10+
1011 ToggleModule . refreshContent = ( ) => {
1112 if ( ! this . currentFrame ) {
1213 return
1314 }
1415 for ( const registeredEntity in this . currentFrame . registered ) {
1516 const entity = entityModule . entities . get ( parseInt ( registeredEntity ) )
1617 const toggleInfo = this . currentFrame . registered [ registeredEntity ]
17- const toggleState = toggles [ toggleInfo . name ]
18+ const toggleState = ToggleModule . toggles [ toggleInfo . name ]
1819
1920 if ( toggleState == null && ! this . missingToggles [ toggleInfo . name ] ) {
2021 ErrorLog . push ( new MissingToggleError ( toggleInfo . name ) )
@@ -25,9 +26,24 @@ export class ToggleModule {
2526 )
2627 }
2728 }
29+
30+ pushDuplicateErrors ( )
2831 }
32+
2933 static refreshContent ( ) { }
3034
35+ static defineToggle ( option ) {
36+ checkDuplicates ( option )
37+
38+ ToggleModule . toggles [ option . toggle ] = option . default
39+ option . get = ( ) => ToggleModule . toggles [ option . toggle ]
40+ option . set = ( value ) => {
41+ ToggleModule . toggles [ option . toggle ] = value
42+ ToggleModule . refreshContent ( )
43+ }
44+ return option
45+ }
46+
3147 static get name ( ) {
3248 return 'toggles'
3349 }
@@ -53,3 +69,32 @@ export class ToggleModule {
5369 ToggleModule . refreshContent ( )
5470 }
5571}
72+
73+ ToggleModule . toggles = { }
74+ ToggleModule . optionValues = { }
75+
76+ function checkDuplicates ( option ) {
77+ ToggleModule . optionValues [ option . toggle ] = [ ]
78+
79+ for ( const key in option . values ) {
80+ const value = option . values [ key ]
81+ let matchedPair = ToggleModule . optionValues [ option . toggle ] . find ( elem => elem . value === value )
82+
83+ if ( ! matchedPair ) {
84+ matchedPair = { keys : [ ] , value }
85+ ToggleModule . optionValues [ option . toggle ] . push ( matchedPair )
86+ }
87+
88+ matchedPair . keys . push ( key )
89+ }
90+ }
91+
92+ function pushDuplicateErrors ( ) {
93+ for ( const toggle in ToggleModule . optionValues ) {
94+ for ( const optionValues of ToggleModule . optionValues [ toggle ] ) {
95+ if ( optionValues . keys . length > 1 ) {
96+ ErrorLog . push ( new DuplicateToggleValueError ( toggle , optionValues ) )
97+ }
98+ }
99+ }
100+ }
0 commit comments