-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cherry-pick(#28978): chore: build import registry source
- Loading branch information
1 parent
d47ed6a
commit 06518b2
Showing
11 changed files
with
203 additions
and
144 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
[importRegistry.ts] | ||
../types/** | ||
[vitePlugin.ts] | ||
generated/indexSource.ts | ||
|
||
[mount.ts] | ||
generated/serializers.ts | ||
injected/** |
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
packages/playwright-ct-core/src/injected/importRegistry.ts
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,49 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export type ImportRef = { | ||
__pw_type: 'importRef', | ||
id: string, | ||
property?: string, | ||
}; | ||
|
||
export function isImportRef(value: any): value is ImportRef { | ||
return typeof value === 'object' && value && value.__pw_type === 'importRef'; | ||
} | ||
|
||
export class ImportRegistry { | ||
private _registry = new Map<string, () => Promise<any>>(); | ||
|
||
initialize(components: Record<string, () => Promise<any>>) { | ||
for (const [name, value] of Object.entries(components)) | ||
this._registry.set(name, value); | ||
} | ||
|
||
async resolveImportRef(importRef: ImportRef): Promise<any> { | ||
const importFunction = this._registry.get(importRef.id); | ||
if (!importFunction) | ||
throw new Error(`Unregistered component: ${importRef.id}. Following components are registered: ${[...this._registry.keys()]}`); | ||
let importedObject = await importFunction(); | ||
if (!importedObject) | ||
throw new Error(`Could not resolve component: ${importRef.id}.`); | ||
if (importRef.property) { | ||
importedObject = importedObject[importRef.property]; | ||
if (!importedObject) | ||
throw new Error(`Could not instantiate component: ${importRef.id}.${importRef.property}.`); | ||
} | ||
return importedObject; | ||
} | ||
} |
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,21 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ImportRegistry } from './importRegistry'; | ||
import { unwrapObject } from './serializers'; | ||
|
||
window.__pwRegistry = new ImportRegistry(); | ||
window.__pwUnwrapObject = unwrapObject; |
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,73 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { isImportRef } from './importRegistry'; | ||
|
||
type FunctionRef = { | ||
__pw_type: 'function'; | ||
ordinal: number; | ||
}; | ||
|
||
function isFunctionRef(value: any): value is FunctionRef { | ||
return value && typeof value === 'object' && value.__pw_type === 'function'; | ||
} | ||
|
||
export function wrapObject(value: any, callbacks: Function[]): any { | ||
if (typeof value === 'function') { | ||
const ordinal = callbacks.length; | ||
callbacks.push(value as Function); | ||
const result: FunctionRef = { | ||
__pw_type: 'function', | ||
ordinal, | ||
}; | ||
return result; | ||
} | ||
if (value === null || typeof value !== 'object') | ||
return value; | ||
if (Array.isArray(value)) { | ||
const result = []; | ||
for (const item of value) | ||
result.push(wrapObject(item, callbacks)); | ||
return result; | ||
} | ||
const result: any = {}; | ||
for (const [key, prop] of Object.entries(value)) | ||
result[key] = wrapObject(prop, callbacks); | ||
return result; | ||
} | ||
|
||
export async function unwrapObject(value: any): Promise<any> { | ||
if (value === null || typeof value !== 'object') | ||
return value; | ||
if (isFunctionRef(value)) { | ||
return (...args: any[]) => { | ||
window.__ctDispatchFunction(value.ordinal, args); | ||
}; | ||
} | ||
if (isImportRef(value)) | ||
return window.__pwRegistry.resolveImportRef(value); | ||
|
||
if (Array.isArray(value)) { | ||
const result = []; | ||
for (const item of value) | ||
result.push(await unwrapObject(item)); | ||
return result; | ||
} | ||
const result: any = {}; | ||
for (const [key, prop] of Object.entries(value)) | ||
result[key] = await unwrapObject(prop); | ||
return result; | ||
} |
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
Oops, something went wrong.