File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const { EventEmitter } = require ( 'events' ) ;
4+
5+ function AddOnlySet ( it ) {
6+ this . set = new Set ( it ) ;
7+ this . emitter = new EventEmitter ( ) ;
8+ this . size = it . length ;
9+ }
10+
11+ AddOnlySet . prototype . add = function ( value ) {
12+ this . set . add ( value ) ;
13+ this . size = this . set . size ;
14+ this . emitter . emit ( 'add' , value ) ;
15+ } ;
16+
17+ AddOnlySet . prototype . on = function ( name , listener ) {
18+ this . emitter . on ( name , listener ) ;
19+ } ;
20+
21+ AddOnlySet . prototype . toString = function ( ) {
22+ return [ ...this . set . values ( ) ] . join ( ) ;
23+ } ;
24+
25+ const s1 = new AddOnlySet ( [ 'uno' , 'due' ] ) ;
26+ s1 . on ( 'add' , value => console . log ( `Added "${ value } "` ) ) ;
27+ s1 . add ( 'tre' ) ;
28+ console . dir ( { result : s1 . toString ( ) } ) ;
Original file line number Diff line number Diff line change 1- Composition
2- ===============
1+ # Composition
You can’t perform that action at this time.
0 commit comments