Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter patches to the ones that might be applicable #489

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions new-packages/ts-project/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,17 @@ function updateParameterizedObjectLocation(
codeChanges.replaceWith(expression, c.string(newType));
}

function hasPatchPath(obj: {}, path: Patch['path']) {
for (let i = 0; i < path.length; i++) {
const segment = path[i];
if (!(segment in obj)) {
return false;
}
obj = obj[segment as never];
}
return true;
}

function createProjectMachine({
host,
fileName,
Expand Down Expand Up @@ -525,16 +536,22 @@ function createProjectMachine({
const { sourceFile, createMachineCall } = findOwnCreateMachineCall();
const currentState = state!;

// TODO: currently it throws when running with the Studio open - presumably because the patch might contain data that are not part of this local `digraph`
const filteredPatches = patches.filter((patch) => {
if (patch.op === 'add') {
return hasPatchPath(currentState.digraph!, patch.path.slice(0, -1));
}
return hasPatchPath(currentState.digraph!, patch.path);
});

currentState.digraph = applyPatches(
currentState.digraph!,
patches,
filteredPatches,
) as any;

const deferredArrayPatches: Patch[] = [];

for (let i = 0; i < patches.length; i++) {
const patch = patches[i];
for (let i = 0; i < filteredPatches.length; i++) {
const patch = filteredPatches[i];

switch (patch.op) {
case 'add':
Expand Down
Loading