diff --git a/src/Molecule-Tests/MolComponentAnnouncementTest.class.st b/src/Molecule-Tests/MolComponentAnnouncementTest.class.st index 1b6dfa6..b9be74d 100644 --- a/src/Molecule-Tests/MolComponentAnnouncementTest.class.st +++ b/src/Molecule-Tests/MolComponentAnnouncementTest.class.st @@ -52,7 +52,7 @@ MolComponentAnnouncementTest >> testAnnounceInstanciated [ | announcement component component2 | announcement := nil. - SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ]. + SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ]. "test by standard instanciation" MolMyUserComponentImpl deploy. diff --git a/src/Molecule-Tests/MolComponentImplTest.class.st b/src/Molecule-Tests/MolComponentImplTest.class.st index 56d49ba..c1dca94 100644 --- a/src/Molecule-Tests/MolComponentImplTest.class.st +++ b/src/Molecule-Tests/MolComponentImplTest.class.st @@ -658,6 +658,21 @@ MolComponentImplTest >> testStart2 [ self assert: component componentName equals: #compA. ] +{ #category : #'tests - component creation' } +MolComponentImplTest >> testStartWithGeneratedName [ + + | instance instance2 | + instance := MolCompleteComponentImpl startWithGeneratedName. + self assert: instance componentName notNil. + self assert: instance componentName isSymbol. + + instance2 := MolCompleteComponentImpl startWithGeneratedName. + self assert: instance2 componentName notNil. + self assert: instance2 componentName isSymbol. + self deny: instance componentName equals: instance2 componentName. + +] + { #category : #'tests - component creation' } MolComponentImplTest >> testStartWithName [ @@ -706,6 +721,7 @@ MolComponentImplTest >> testStartWithWrongNames [ { #category : #'tests - component creation' } MolComponentImplTest >> testStop [ + MolCompleteComponentImpl start. MolCompleteComponentImpl stop. self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil. @@ -715,3 +731,16 @@ MolComponentImplTest >> testStop [ self assert: (MolUtils instanceOf: MolCompleteComponentImpl named: #compA) equals: nil. ] + +{ #category : #'tests - component creation' } +MolComponentImplTest >> testStopInstance [ + + | instance | + instance := MolCompleteComponentImpl start. + instance stop. + self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil. + + instance := MolCompleteComponentImpl start: #test. + instance stop. + self assert: (MolUtils instanceOf: MolCompleteComponentImpl) equals: nil. +] diff --git a/src/Molecule-Tests/MolComponentManagerTest.class.st b/src/Molecule-Tests/MolComponentManagerTest.class.st index 07d9296..92a1d65 100644 --- a/src/Molecule-Tests/MolComponentManagerTest.class.st +++ b/src/Molecule-Tests/MolComponentManagerTest.class.st @@ -42,9 +42,9 @@ MolComponentManagerTest >> testCleanUp [ MolComponentManagerTest >> testCleanUpAnnouncements [ | announcement list selection | announcement := nil. - SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ]. + SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ]. SystemAnnouncer uniqueInstance when: MolComponentPassivated do:[ :a | announcement := a ]. - SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ]. + SystemAnnouncer uniqueInstance when: MolComponentInstantiated do:[ :a | announcement := a ]. list := MolAnnouncement withAllSubclasses. selection := SystemAnnouncer uniqueInstance subscriptions subscriptions select: [ :e | list includes: e announcementClass] . self assert: (selection size > 0). diff --git a/src/Molecule/MolComponentImpl.trait.st b/src/Molecule/MolComponentImpl.trait.st index 6a210fe..fcde883 100644 --- a/src/Molecule/MolComponentImpl.trait.st +++ b/src/Molecule/MolComponentImpl.trait.st @@ -178,6 +178,12 @@ MolComponentImpl classSide >> start: aComponentName [ ^ MolUtils startComponent: self named: aComponentName ] +{ #category : #'start & stop' } +MolComponentImpl classSide >> startWithGeneratedName [ + + ^ self start: (MolUtils generateComponentNameForType: self componentType) +] + { #category : #'start & stop' } MolComponentImpl classSide >> stop [ @@ -449,3 +455,10 @@ MolComponentImpl >> servicesProviders [ ifNotNil: [ :e | e servicesProviders ] ifNil: [ MolComponentConnector defaultServicesProviders ] ] + +{ #category : #stop } +MolComponentImpl >> stop [ + "Stop me, whatever my name is" + + self class stop: self componentName +] diff --git a/src/Molecule/MolComponentInstanciated.class.st b/src/Molecule/MolComponentInstantiated.class.st similarity index 82% rename from src/Molecule/MolComponentInstanciated.class.st rename to src/Molecule/MolComponentInstantiated.class.st index 9c98bba..ce48303 100644 --- a/src/Molecule/MolComponentInstanciated.class.st +++ b/src/Molecule/MolComponentInstantiated.class.st @@ -2,7 +2,7 @@ Announcement when a component is instanciated by the ComponentManager. " Class { - #name : #MolComponentInstanciated, + #name : #MolComponentInstantiated, #superclass : #MolComponentAnnouncement, #category : #'Molecule-Announcements' } diff --git a/src/Molecule/MolHomeServices.class.st b/src/Molecule/MolHomeServices.class.st index 5331545..979198f 100644 --- a/src/Molecule/MolHomeServices.class.st +++ b/src/Molecule/MolHomeServices.class.st @@ -206,7 +206,7 @@ MolHomeServices >> instantiateComponent: aComponentClass named: aName [ connector := MolComponentConnector new. component componentConnector: connector. component componentInitialize. - self announcer announce: (MolComponentInstanciated new + self announcer announce: (MolComponentInstantiated new component: component; componentName: aName; yourself). MolUtils log: diff --git a/src/Molecule/MolUtils.class.st b/src/Molecule/MolUtils.class.st index 42bee57..ed87158 100644 --- a/src/Molecule/MolUtils.class.st +++ b/src/Molecule/MolUtils.class.st @@ -96,7 +96,7 @@ MolUtils class >> createAllComponents [ components keysDo: [ :aClass | homeServices activateComponent: aClass ] ] -{ #category : #accessing } +{ #category : #name } MolUtils class >> defaultComponentName [ "Used when a component instanciation is not nammed" @@ -131,6 +131,33 @@ MolUtils class >> deployAndInitializeComponents: aComponentClassList [ ^ components ] +{ #category : #private } +MolUtils class >> doRandomComponentNameForType: aComponentType [ + + ^ (aComponentType printString, (Random new next * 10000000) rounded printString) asSymbol + +] + +{ #category : #name } +MolUtils class >> generateComponentNameForType: aComponentType [ + + | instances componentName usedNames found | + aComponentType ifNil: [ ^ nil ]. + componentName := self doRandomComponentNameForType: aComponentType. + found := false. + + "check if the name is not used" + [ found ] whileFalse: [ + instances := self allComponentInstancesOfType: aComponentType. + usedNames := instances collect: [ :c | c componentName ]. + componentName := self doRandomComponentNameForType: aComponentType. + found := usedNames isEmpty + ifTrue: [ true ] + ifFalse: [ (usedNames includes: componentName) not ] ]. + + ^ componentName +] + { #category : #accessing } MolUtils class >> instanceKindOf: aClass [