Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
fix(atlas): emit afterPrepare events for non-atlas observers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrossmann committed Dec 13, 2018
1 parent 1a2f3c5 commit 3e4f2e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
21 changes: 10 additions & 11 deletions packages/atlas/src/atlas.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,19 @@ class Atlas {
}
}

// Prepare all components, in parallel 馃挭
// Prepare hooks
await Promise.all([...hooks].map(([, container]) => container.prepare({ catalog })))

// Prepare actions and services in parallel 馃殌
void (await Promise.all([
...Array.from(hooks).map(async ([, container]) => ({
expose: false,
value: await container.prepare({ catalog }),
...Array.from(actions).map(async ([alias, container]) => ({
expose: container.Component.internal ? false : 'actions',
value: await container.prepare({ catalog, hooks }),
alias,
})),
...Array.from(services).map(async ([alias, container]) => ({
expose: container.Component.internal ? false : 'services',
value: await container.prepare({ catalog }),
alias,
})),
...Array.from(actions).map(async ([alias, container]) => ({
expose: container.Component.internal ? false : 'actions',
value: await container.prepare({ catalog }),
value: await container.prepare({ catalog, hooks }),
alias,
})),
]))
Expand All @@ -447,7 +446,7 @@ class Atlas {
await Promise.all([
...Array.from(hooks),
...Array.from(actions),
].map(([, container]) => container.start({ hooks })))
].map(([, container]) => container.start()))

// Start all services, in the order they were added to the instance 馃挭
// Ordering is important here! Some services should be started as the last ones because they
Expand Down
11 changes: 6 additions & 5 deletions packages/atlas/src/private/component-container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,18 @@ class ComponentContainer {
*
* @param {Object} options Input options
* @param {Object} options.catalog Atlas catalog of all components
* @param {Map} options.hooks Hooks available in the application
* @return {Promise<this>}
*/
async prepare(options) {
this::mkcatalog(this.#catalog, options)
this::mkobservers(this.#observers, { hooks: options.hooks })

switch (this.type) {
case 'service': {
this.component.log.trace('prepare:before')
this.instance = await this.component.prepare()
await this.#observers::dispatch('afterPrepare', this.instance)
this.component.log.trace('prepare:after')
break
}
Expand All @@ -136,17 +139,14 @@ class ComponentContainer {
/**
* Start the component
*
* @param {Object} opts Additional options
* @param {Map} opts.hooks Hooks available in the application
* @return {Promise<this.component>}
*/
async start(opts) {
async start() {
if (this.started) {
return this.component
}

this.component.log.trace('start:before')
this::mkobservers(this.#observers, { hooks: opts.hooks })

switch (this.type) {
case 'service':
Expand All @@ -167,6 +167,7 @@ class ComponentContainer {
break
}

this.component.log.trace('start:after')
this.started = true

return this.component
Expand Down Expand Up @@ -221,7 +222,7 @@ class ComponentContainer {
* @return {void}
*/
function mkobservers(observers, { hooks }) {
for (const [alias, container] of hooks) {
for (const [alias, container] of hooks || new Map()) {
const target = container.aliases[container.Component.observes]

if (this.alias === target) {
Expand Down

0 comments on commit 3e4f2e4

Please sign in to comment.