Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
22dd6fd6
Commit
22dd6fd6
authored
Apr 21, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Apr 25, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate injector to TS
parent
9029313f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
40 deletions
+54
-40
frame-error-capture.ts
src/shared/frame-error-capture.ts
+1
-1
injector.ts
src/shared/injector.ts
+53
-39
No files found.
src/shared/frame-error-capture.ts
View file @
22dd6fd6
...
...
@@ -45,7 +45,7 @@ function serializeError(err: Error | unknown): ErrorData {
/**
* Convert error data serialized by {@link serializeError} back into an Error.
*/
function
deserializeError
(
data
:
ErrorData
):
Error
Data
{
function
deserializeError
(
data
:
ErrorData
):
Error
{
const
err
=
new
Error
(
data
.
message
);
err
.
stack
=
data
.
stack
;
return
err
;
...
...
src/shared/injector.
j
s
→
src/shared/injector.
t
s
View file @
22dd6fd6
/**
* @typedef Provider
* @prop {unknown} [value] - The value for the object
* @prop {Function} [class] - A class that should be instantiated to create the object
* @prop {Function} [factory] - Function that should be called to create the object
*/
type
ValueProvider
=
{
value
:
unknown
};
/**
* @param {Provider} provider
*/
function
isValidProvider
(
provider
)
{
type
Constructible
=
{
new
(...
args
:
any
[]):
unknown
;
};
type
ClassProvider
=
{
class
:
Constructible
&
{
$inject
?:
string
[]
}
};
type
FactoryProvider
=
{
factory
:
((...
args
:
any
[])
=>
unknown
)
&
{
$inject
?:
string
[]
};
};
type
Provider
=
ValueProvider
|
ClassProvider
|
FactoryProvider
;
function
isValueProvider
(
provider
:
object
):
provider
is
ValueProvider
{
return
'value'
in
provider
;
}
function
isClassProvider
(
provider
:
object
):
provider
is
ClassProvider
{
return
'class'
in
provider
&&
typeof
provider
.
class
===
'function'
;
}
function
isFactoryProvider
(
provider
:
object
):
provider
is
FactoryProvider
{
return
'factory'
in
provider
&&
typeof
provider
.
factory
===
'function'
;
}
function
isValidProvider
(
provider
:
unknown
):
provider
is
Provider
{
if
(
typeof
provider
!==
'object'
||
provider
===
null
)
{
return
false
;
}
return
(
'value'
in
provider
||
typeof
provider
.
class
===
'function'
||
typeof
provider
.
factory
===
'function'
isValueProvider
(
provider
)
||
isClassProvider
(
provider
)
||
isFactoryProvider
(
provider
)
);
}
...
...
@@ -41,25 +57,26 @@ function isValidProvider(provider) {
* use the `run` method.
*/
export
class
Injector
{
constructor
()
{
// Map of name to object specifying how to create/provide that object.
this
.
_providers
=
new
Map
();
/** Map of name to object specifying how to create/provide that object. */
private
_providers
:
Map
<
string
,
Provider
>
;
/** Map of name to existing instance. */
private
_instances
:
Map
<
string
,
unknown
>
;
/** Set of instances already being constructed. Used to detect circular dependencies. */
private
_constructing
:
Set
<
string
>
;
// Map of name to existing instance.
this
.
_instances
=
new
Map
();
// Set of instances already being constructed. Used to detect circular
// dependencies.
this
.
_constructing
=
new
Set
();
constructor
()
{
this
.
_providers
=
new
Map
<
string
,
Provider
>
();
this
.
_instances
=
new
Map
<
string
,
unknown
>
();
this
.
_constructing
=
new
Set
<
string
>
();
}
/**
* Construct or return the existing instance of an object with a given `name`
*
* @param
{string}
name - Name of object to construct
* @return
{unknown} -
The constructed object
* @param name - Name of object to construct
* @return The constructed object
*/
get
(
name
)
{
get
(
name
:
string
):
unknown
{
if
(
this
.
_instances
.
has
(
name
))
{
return
this
.
_instances
.
get
(
name
);
}
...
...
@@ -70,7 +87,7 @@ export class Injector {
throw
new
Error
(
`"
${
name
}
" is not registered`
);
}
if
(
'value'
in
provider
)
{
if
(
isValueProvider
(
provider
)
)
{
this
.
_instances
.
set
(
name
,
provider
.
value
);
return
provider
.
value
;
}
...
...
@@ -85,8 +102,8 @@ export class Injector {
try
{
const
resolvedDependencies
=
[];
const
dependencies
=
(
'class'
in
provider
&&
provider
.
class
.
$inject
)
||
(
'factory'
in
provider
&&
provider
.
factory
.
$inject
)
||
(
isClassProvider
(
provider
)
&&
provider
.
class
.
$inject
)
||
(
isFactoryProvider
(
provider
)
&&
provider
.
factory
.
$inject
)
||
[];
for
(
const
dependency
of
dependencies
)
{
...
...
@@ -103,7 +120,7 @@ export class Injector {
}
let
instance
;
if
(
provider
.
class
)
{
if
(
isClassProvider
(
provider
)
)
{
// eslint-disable-next-line new-cap
instance
=
new
provider
.
class
(...
resolvedDependencies
);
}
else
{
...
...
@@ -124,12 +141,10 @@ export class Injector {
* If `provider` is a function, it is treated like a class. In other words
* `register(name, SomeClass)` is the same as `register(name, { class: SomeClass })`.
*
* @param {string} name - Name of object
* @param {Function|Provider} provider -
* The class or other provider to use to create the object.
* @return {this}
* @param name - Name of object
* @param provider - The class or other provider to use to create the object.
*/
register
(
name
,
provider
)
{
register
(
name
:
string
,
provider
:
Constructible
|
Provider
):
this
{
if
(
typeof
provider
===
'function'
)
{
provider
=
{
class
:
provider
};
}
else
if
(
!
isValidProvider
(
provider
))
{
...
...
@@ -144,18 +159,17 @@ export class Injector {
* Run a function which uses one or more dependencies provided by the
* container.
*
* @template T
* @param {(...args: any[]) => T} callback -
* @param callback -
* A callback to run, with dependencies annotated in the same way as
* functions or classes passed to `register`.
* @return
{any} -
Returns the result of running the function.
* @return Returns the result of running the function.
*/
run
(
callback
)
{
run
<
T
>
(
callback
:
(...
args
:
any
[])
=>
T
):
T
{
const
tempName
=
'Injector.run'
;
this
.
register
(
tempName
,
{
factory
:
callback
});
try
{
return
this
.
get
(
tempName
);
return
this
.
get
(
tempName
)
as
T
;
}
finally
{
this
.
_instances
.
delete
(
tempName
);
this
.
_providers
.
delete
(
tempName
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment