-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⚗️[Logs] POC dependency injection #2520
Draft
bcaudan
wants to merge
13
commits into
main
Choose a base branch
from
bcaudan/di
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2e8a5f3
♻️ extract reportError
bcaudan 76fa592
♻️ extract logsTelemetry
bcaudan 0e6adc1
♻️ extract rumInternalContext
bcaudan 0fe6f4a
♻️ move telemetry configuration to logsTelemetry
bcaudan 1356fbe
♻️ more consistent dependency
bcaudan d186743
Bootstrap dependency injector
bcaudan dfc6821
Setup dependency injection
bcaudan 8df718d
Bootstrap spec injector
bcaudan c503a47
experiment with function in place of component ids
BenoitZugmeyer b261e4a
one step further: startLogs is a component
BenoitZugmeyer 820225a
one step further: makeLogsPublicApi is a component
BenoitZugmeyer 1bcfa90
apply the same isolated module workaround as RUM for Logs
BenoitZugmeyer b45abf7
remove now unused `logsComponents` module
BenoitZugmeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { noop } from './utils/functionUtils' | ||
import type { Component, Injector } from './injector' | ||
import { createInjector } from './injector' | ||
|
||
type Foo = { key: string; stop: () => void } | ||
const initFoo: Component<Foo, [Bar]> = (bar) => ({ | ||
key: `foo+${bar.key}`, | ||
stop: noop, | ||
}) | ||
|
||
initFoo.$deps = [initBar] | ||
|
||
type Bar = ReturnType<typeof initBar> | ||
function initBar() { | ||
return { | ||
key: 'bar', | ||
} | ||
} | ||
|
||
describe('injector', () => { | ||
let injector: Injector | ||
|
||
beforeEach(() => { | ||
injector = createInjector() | ||
}) | ||
|
||
it('should init component with its dependencies', () => { | ||
expect(injector.run(initFoo).key).toBe('foo+bar') | ||
}) | ||
|
||
it('should override context with a stub component', () => { | ||
injector.override(initBar, () => ({ | ||
key: 'qux', | ||
})) | ||
expect(injector.run(initFoo).key).toBe('foo+qux') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
export interface Component<Instance, Deps extends any[]> { | ||
(...args: Deps): Instance | ||
$deps: Readonly<{ [K in keyof Deps]: AnyComponent<Deps[K]> }> | ||
} | ||
|
||
interface SimpleComponent<Instance> { | ||
(): Instance | ||
} | ||
|
||
export type AnyComponent<Instance> = Component<Instance, any> | SimpleComponent<Instance> | ||
|
||
export interface Injector { | ||
run: <T>(component: AnyComponent<T>) => T | ||
get: <T>(component: AnyComponent<T>) => T | ||
override: <T>(originalComponent: AnyComponent<T>, newComponent: AnyComponent<T>) => void | ||
stop: () => void | ||
} | ||
|
||
export function createInjector(): Injector { | ||
const instances = new Map<AnyComponent<any>, any>() | ||
const overrides = new Map<AnyComponent<any>, AnyComponent<any>>() | ||
overrides.set(getInjector, () => injector) | ||
|
||
const injector: Injector = { | ||
run(component) { | ||
if (instances.has(component)) { | ||
throw new Error(`Component ${component.name} already started`) | ||
} | ||
if (overrides.has(component)) { | ||
component = overrides.get(component)! | ||
} | ||
|
||
const args = ('$deps' in component ? component.$deps : []).map((dependency): any => | ||
instances.has(dependency) ? instances.get(dependency) : injector.run(dependency) | ||
) | ||
|
||
const instance: any = component(...args) | ||
|
||
instances.set(component, instance) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return instance | ||
}, | ||
|
||
get<T>(component: AnyComponent<T>) { | ||
if (!instances.has(component)) { | ||
throw new Error(`Component ${component.name} not started`) | ||
} | ||
return instances.get(component) as T | ||
}, | ||
|
||
override(originalComponent, newComponent) { | ||
if (instances.has(originalComponent)) { | ||
throw new Error(`Component ${originalComponent.name} already started`) | ||
} | ||
overrides.set(originalComponent, newComponent) | ||
}, | ||
|
||
stop() { | ||
instances.forEach((instance) => { | ||
if (instance !== injector && instance?.stop) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
instance.stop() | ||
} | ||
}) | ||
instances.clear() | ||
}, | ||
} | ||
|
||
return injector | ||
} | ||
|
||
export function getInjector(): Injector { | ||
throw new Error('No injector available') | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This assertion and the following ones (in
get
andoverride
) are not strictly needed, but helps to debug weird injection cases.