A manager to ease resolution, downloading and executing additional code from:
ScriptManager is globally available under ScriptManager.shared
in main bundle, chunks and containers.
Use ScriptManager.shared
instead of creating new instance of ScriptManager
.
This API is mainly useful, if you are working with any form of Code Splitting.
ScriptManager
is also an EventEmitter
and emits the following events:
resolving
with { scriptId, caller }
resolved
with scriptId: string, caller?: string, locator: NormalizedScriptLocator, cache: boolean
prefetching
with scriptId: string, caller?: string, locator: NormalizedScriptLocator, cache: boolean
loading
with scriptId: string, caller?: string, locator: NormalizedScriptLocator, cache: boolean
loaded
with scriptId: string, caller?: string, locator: NormalizedScriptLocator, cache: boolean
error
with error: Error
Example of using this API with async Webpack chunk:
EventEmitter
↳ ScriptManager
• Protected
new ScriptManager(nativeScriptManager?
)
Constructs instance of ScriptManager
.
Should not be called directly - use ScriptManager.shared
.
internal
Name | Type | Default value |
---|---|---|
nativeScriptManager |
any |
NativeModules.ScriptManager |
EventEmitter.constructor
packages/repack/src/modules/ScriptManager/ScriptManager.ts:103
• Protected
cache: Cache
= {}
packages/repack/src/modules/ScriptManager/ScriptManager.ts:91
• Protected
cacheInitialized: boolean
= false
packages/repack/src/modules/ScriptManager/ScriptManager.ts:92
• Protected
resolvers: [number
, ScriptLocatorResolver
][] = []
packages/repack/src/modules/ScriptManager/ScriptManager.ts:93
• Protected
Optional
storage: StorageApi
packages/repack/src/modules/ScriptManager/ScriptManager.ts:94
▪ Static
Readonly
captureRejectionSymbol: typeof captureRejectionSymbol
EventEmitter.captureRejectionSymbol
node_modules/@types/node/events.d.ts:38
▪ Static
captureRejections: boolean
Sets or gets the default captureRejection value for all emitters.
EventEmitter.captureRejections
node_modules/@types/node/events.d.ts:44
▪ Static
defaultMaxListeners: number
EventEmitter.defaultMaxListeners
node_modules/@types/node/events.d.ts:45
▪ Static
Readonly
errorMonitor: typeof errorMonitor
This symbol shall be used to install a listener for only monitoring 'error'
events. Listeners installed using this symbol are called before the regular
'error'
listeners are called.
Installing a listener using this symbol does not change the behavior once an
'error'
event is emitted, therefore the process will still crash if no
regular 'error'
listener is installed.
EventEmitter.errorMonitor
node_modules/@types/node/events.d.ts:37
• Static
get
shared(): ScriptManager
packages/repack/src/modules/ScriptManager/ScriptManager.ts:84
▸ __destroy(): void
void
packages/repack/src/modules/ScriptManager/ScriptManager.ts:138
▸ addListener(event
, listener
): ScriptManager
Name | Type |
---|---|
event |
string | symbol |
listener |
(...args : any []) => void |
EventEmitter.addListener
node_modules/@types/node/events.d.ts:57
▸ addResolver(resolver
, options?
): void
Adds new script locator resolver.
Resolver is an async function to resolve script locator data - in other words, it's a function to tell the ScriptManager how to fetch the script.
There's no limitation on what logic you can run inside this function - it can include:
Name | Type | Description |
---|---|---|
resolver |
ScriptLocatorResolver |
Resolver function to add. |
options |
ResolverOptions |
Resolver options. |
void
packages/repack/src/modules/ScriptManager/ScriptManager.ts:174
▸ emit(event
, ...args
): boolean
Name | Type |
---|---|
event |
string | symbol |
...args |
any [] |
boolean
EventEmitter.emit
node_modules/@types/node/events.d.ts:67
▸ eventNames(): (string
| symbol
)[]
(string
| symbol
)[]
EventEmitter.eventNames
node_modules/@types/node/events.d.ts:72
▸ getMaxListeners(): number
number
EventEmitter.getMaxListeners
node_modules/@types/node/events.d.ts:64
▸ Protected
handleError(error
, message
, ...args
): never
Name | Type |
---|---|
error |
any |
message |
string |
...args |
any [] |
never
packages/repack/src/modules/ScriptManager/ScriptManager.ts:220
▸ Protected
initCache(): Promise
<void
>
Promise
<void
>
packages/repack/src/modules/ScriptManager/ScriptManager.ts:206
▸ invalidateScripts(scriptIds?
): Promise
<void
>
Clears the cache (if configured in ScriptManager.setStorage) and removes downloaded files for given scripts from the filesystem. This function can be awaited to detect if the scripts were invalidated and for error handling.
Use ScriptManager.shared.on('invalidated', (scriptIds) => { })
to listen for when
the invalidation completes.
Name | Type | Default value | Description |
---|---|---|---|
scriptIds |
string [] |
[] |
Array of script ids to clear from cache and remove from filesystem. |
Promise
<void
>
packages/repack/src/modules/ScriptManager/ScriptManager.ts:410
▸ listenerCount(event
): number
Name | Type |
---|---|
event |
string | symbol |
number
EventEmitter.listenerCount
node_modules/@types/node/events.d.ts:68
▸ listeners(event
): Function
[]
Name | Type |
---|---|
event |
string | symbol |
Function
[]
EventEmitter.listeners
node_modules/@types/node/events.d.ts:65
▸ loadScript(scriptId
, caller?
, webpackContext?
): Promise
<void
>
Resolves given script's location, downloads and executes it. The execution of the code is handled internally by threading in React Native.
Use ScriptManager.shared.on('loading', (script) => { })
to listen for when
the script is about to be loaded.
Use ScriptManager.shared.on('loaded', (script) => { })
to listen for when
the script is loaded.
Name | Type | Description |
---|---|---|
scriptId |
string |
Id of the script to load. |
caller? |
string |
Name of the calling script - it can be for example: name of the bundle, chunk or container. |
webpackContext |
WebpackContext |
- |
Promise
<void
>
packages/repack/src/modules/ScriptManager/ScriptManager.ts:333
▸ off(event
, listener
): ScriptManager
Name | Type |
---|---|
event |
string | symbol |
listener |
(...args : any []) => void |
EventEmitter.off
node_modules/@types/node/events.d.ts:61
▸ on(event
, listener
): ScriptManager
Name | Type |
---|---|
event |
string | symbol |
listener |
(...args : any []) => void |
EventEmitter.on
node_modules/@types/node/events.d.ts:58
▸ once(event
, listener
): ScriptManager
Name | Type |
---|---|
event |
string | symbol |
listener |
(...args : any []) => void |
EventEmitter.once
node_modules/@types/node/events.d.ts:59
▸ prefetchScript(scriptId
, caller?
, webpackContext?
): Promise
<void
>
Resolves given script's location and downloads it without executing. This function can be awaited to detect if the script was downloaded and for error handling.
Use ScriptManager.shared.on('prefetching', (script) => { })
to listen for when
the script's prefetch beings.
Name | Type | Description |
---|---|---|
scriptId |
string |
Id of the script to prefetch. |
caller? |
string |
Name of the calling script - it can be for example: name of the bundle, chunk or container. |
webpackContext |
WebpackContext |
- |
Promise
<void
>
packages/repack/src/modules/ScriptManager/ScriptManager.ts:379
▸ prependListener(event
, listener
): ScriptManager
Name | Type |
---|---|
event |
string | symbol |
listener |
(...args : any []) => void |
EventEmitter.prependListener
node_modules/@types/node/events.d.ts:70
▸ prependOnceListener(event
, listener
): ScriptManager
Name | Type |
---|---|
event |
string | symbol |
listener |
(...args : any []) => void |
EventEmitter.prependOnceListener
node_modules/@types/node/events.d.ts:71
▸ rawListeners(event
): Function
[]
Name | Type |
---|---|
event |
string | symbol |
Function
[]
EventEmitter.rawListeners
node_modules/@types/node/events.d.ts:66
▸ removeAllListeners(event?
): ScriptManager
Name | Type |
---|---|
event? |
string | symbol |
EventEmitter.removeAllListeners
node_modules/@types/node/events.d.ts:62
▸ removeAllResolvers(): void
Removes all previously added resolvers.
void
packages/repack/src/modules/ScriptManager/ScriptManager.ts:202
▸ removeListener(event
, listener
): ScriptManager
Name | Type |
---|---|
event |
string | symbol |
listener |
(...args : any []) => void |
EventEmitter.removeListener
node_modules/@types/node/events.d.ts:60
▸ removeResolver(resolver
): boolean
Removes previously added resolver.
Name | Type | Description |
---|---|---|
resolver |
ScriptLocatorResolver |
Resolver function to remove. |
boolean
true
if resolver was found and removed, false
otherwise.
packages/repack/src/modules/ScriptManager/ScriptManager.ts:189
▸ resolveScript(scriptId
, caller?
, webpackContext?
): Promise
<Script
>
Resolves a Script instance with normalized locator data.
Resolution will use previously added (via ScriptManager.shared.addResolver(...)
) resolvers
in series, util one returns a locator data or will throw if no resolver handled the request.
Use ScriptManager.shared.on('resolving', ({ scriptId, caller }) => { })
to listen for when
the script resolution begins.
Use ScriptManager.shared.on('resolved', (script) => { })
to listen for when
the script's locator data is resolved.
Name | Type | Description |
---|---|---|
scriptId |
string |
Id of the script to resolve. |
caller? |
string |
Name of the calling script - it can be for example: name of the bundle, chunk or container. |
webpackContext |
WebpackContext |
- |
Promise
<Script
>
packages/repack/src/modules/ScriptManager/ScriptManager.ts:241
▸ Protected
saveCache(): Promise
<void
>
Promise
<void
>
packages/repack/src/modules/ScriptManager/ScriptManager.ts:216
▸ setMaxListeners(n
): ScriptManager
Name | Type |
---|---|
n |
number |
EventEmitter.setMaxListeners
node_modules/@types/node/events.d.ts:63
▸ setStorage(storage?
): void
Sets a storage backend to cache resolved scripts locator data.
The stored data is used to detect if scripts locator data of previously downloaded script hasn't changed to avoid over-fetching the script.
Name | Type | Description |
---|---|---|
storage? |
StorageApi |
Implementation of storage functions. |
void
packages/repack/src/modules/ScriptManager/ScriptManager.ts:154
▸ Static
listenerCount(emitter
, event
): number
deprecated
since v4.0.0
Name | Type |
---|---|
emitter |
EventEmitter |
event |
string | symbol |
number
EventEmitter.listenerCount
node_modules/@types/node/events.d.ts:26
▸ Static
on(emitter
, event
): AsyncIterableIterator
<any
>
Name | Type |
---|---|
emitter |
EventEmitter |
event |
string |
AsyncIterableIterator
<any
>
EventEmitter.on
node_modules/@types/node/events.d.ts:23
▸ Static
once(emitter
, event
): Promise
<any
[]>
Name | Type |
---|---|
emitter |
NodeEventTarget |
event |
string | symbol |
Promise
<any
[]>
EventEmitter.once
node_modules/@types/node/events.d.ts:21
▸ Static
once(emitter
, event
): Promise
<any
[]>
Name | Type |
---|---|
emitter |
DOMEventTarget |
event |
string |
Promise
<any
[]>
EventEmitter.once
node_modules/@types/node/events.d.ts:22