Skip to content

Commit

Permalink
Filter patches to the ones that might be applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Mar 12, 2024
1 parent 844a722 commit 879ca9c
Showing 1 changed file with 21 additions and 4 deletions.
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

0 comments on commit 879ca9c

Please sign in to comment.