Skip to content

Commit 7dd74eb

Browse files
committed
performance improvements... cache the 'project' ts-morph object, adjust chokidar settings to not reload for every file upon initialization, reload only once
1 parent cde9f0a commit 7dd74eb

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/pageObjectModel.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export module pageObjectModel {
1919
} = <any>{};
2020
let currentPageFilePath!: string;
2121
let currentPageFilePathWatcher!: chokidar.FSWatcher;
22+
let _project: Project;
2223

2324
const lock = new AsyncLock();
2425
export async function init(testFileDir: string, config: PlaywrightLiveRecorderConfig_pageObjectModel, evalScope: (s: string) => any, page: Page) {
@@ -39,10 +40,10 @@ export module pageObjectModel {
3940

4041
await currentPageFilePathWatcher?.close();
4142
const cwd = nodePath.join(process.cwd(), _state.config.path);
42-
currentPageFilePathWatcher = chokidar.watch(currentPageFilePath, { cwd })
43+
currentPageFilePathWatcher = chokidar.watch(currentPageFilePath, { cwd, ignoreInitial: true })
4344
.on( 'add', /*path*/() => reload(_state.page))
4445
.on('change', /*path*/() => reload(_state.page));
45-
46+
await reload(_state.page);
4647
return currentPageFilePath;
4748
}
4849

@@ -59,14 +60,17 @@ export module pageObjectModel {
5960
console.time('pageObjectModel.reload');
6061
await lock.acquire('reload', async (release) => {
6162
try {
62-
const filePathsToWatch = await _reload(page, [_state.config.globalPageFilePath, currentPageFilePath]);
63+
64+
_project = _project ?? new Project({ tsConfigFilePath: await fs.access(nodePath.join(process.cwd(), 'tsconfig.json')).then(() => true).catch(() => false) ? 'tsconfig.json' : undefined });
65+
const filePathsToWatch = await _reload(page, [_state.config.globalPageFilePath, currentPageFilePath], _project);
6366

6467
await currentPageFilePathWatcher?.close();
6568
const cwd = nodePath.join(process.cwd(), _state.config.path);
66-
currentPageFilePathWatcher = chokidar.watch(filePathsToWatch, { cwd })
69+
currentPageFilePathWatcher = chokidar.watch(filePathsToWatch, { cwd, ignoreInitial: true })
6770
.on( 'add', /*path*/() => reload(_state.page))
6871
.on('change', /*path*/() => reload(_state.page));
6972

73+
await reload(_state.page);
7074
await page.evaluate('if (reload_page_object_model_elements) reload_page_object_model_elements()');
7175
} catch (e) {
7276
console.error(`error calling page.addScriptTag for page object model ${currentPageFilePath}`);

0 commit comments

Comments
 (0)