MCPcopy
hub / github.com/mswjs/msw / defineNetwork

Function defineNetwork

src/core/experimental/define-network.ts:111–244  ·  view source on GitHub ↗
(
  options: DefineNetworkOptions<Sources>,
)

Source from the content-addressed store, hash-verified

109 * await network.enable()
110 */
111export function defineNetwork<Sources extends Array<NetworkSource<any>>>(
112 options: DefineNetworkOptions<Sources>,
113): NetworkApi<Sources> {
114 let readyState: NetworkReadyState = NetworkReadyState.DISABLED
115 const events = new Emitter<MergeEventMaps<Sources>>()
116 const disposable = new Disposable()
117
118 const deriveHandlersController = (
119 handlers: DefineNetworkOptions<Sources>['handlers'],
120 ) => {
121 return handlers instanceof HandlersController
122 ? handlers
123 : new InMemoryHandlersController(handlers || [])
124 }
125
126 let resolvedOptions: DefineNetworkOptions<Sources> = {
127 ...options,
128 }
129
130 /**
131 * @note Create the handlers controller immediately because
132 * certain setup APIs, like `setupServer`, don't await `.enable` (`.listen`).
133 */
134 let handlersController = deriveHandlersController(resolvedOptions.handlers)
135
136 return {
137 get readyState() {
138 return readyState
139 },
140 events,
141 configure(options) {
142 invariant(
143 readyState === NetworkReadyState.DISABLED,
144 'Failed to call "configure()" on the network: cannot configure an already enabled network.',
145 )
146
147 if (
148 options.handlers &&
149 !Object.is(options.handlers, resolvedOptions.handlers)
150 ) {
151 handlersController = deriveHandlersController(options.handlers)
152 }
153
154 resolvedOptions = {
155 ...resolvedOptions,
156 ...options,
157 }
158 },
159 enable() {
160 invariant(
161 readyState === NetworkReadyState.DISABLED,
162 'Failed to call "enable" on the network: already enabled',
163 )
164
165 readyState = NetworkReadyState.ENABLED
166
167 /**
168 * @note Use a session object scoped to the current "enable()"

Callers 6

setupWorkerFunction · 0.90
setupServerFunction · 0.90
constructorMethod · 0.90
setupServerFunction · 0.90

Calls 1

deriveHandlersControllerFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…