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

Fixes #301 - Register inputEntities & inputRelationships in mockJobState #313

Merged
merged 1 commit into from
Aug 26, 2020

Conversation

ndowmon
Copy link
Contributor

@ndowmon ndowmon commented Aug 25, 2020

No description provided.

Comment on lines +49 to +61
inputEntities.forEach((e) => {
duplicateKeyTracker.registerKey(e._key, {
_type: e._type,
});
typeTracker.registerType(e._type);
});
inputRelationships.forEach((r) => {
duplicateKeyTracker.registerKey(r._key as string, {
_type: r._type,
});
typeTracker.registerType(r._type as string);
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about moving this logic below the const ... function declarations, just before the return ...:

addEntities(inputEntities);
addRelationships(inputRelationships);

I might be missing a finer detail 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aiwilliams the addEntities and addRelationships functions also push graph objects onto the collected<GraphObjects> list. That breaks tests since collected<GraphObjects> should only contain graph objects that were added during the step.

I could alternatively refactor a bit to something like this:

const addEntitiesToDuplicateKeyTracker = (newEntities: Entity[]) => {
  newEntities.forEach((e) => {
    duplicateKeyTracker.registerKey(e._key, {
      _type: e._type,
    });
    typeTracker.registerType(e._type);
  });
};
addEntitiesToDuplicateKeyTracker(inputEntities);

const addEntities = async (newEntities: Entity[]): Promise<Entity[]> => {
  addEntitiesToDuplicateKeyTracker(newEntities);
  collectedEntities = collectedEntities.concat(newEntities);
  return Promise.resolve(newEntities);
}

const addRelationshipsToDuplicateKeyTracker = (newRelationships: Relationship[]) => {
  newRelationships.forEach((r) => {
    duplicateKeyTracker.registerKey(r._key as string, {
      _type: r._type,
    });
    typeTracker.registerType(r._type as string);
  });
};
addRelationshipsToDuplicateKeyTracker(inputRelationships);

const addRelationships = async (newRelationships: Relationship[]) => {
  addRelationshipsToDuplicateKeyTracker(newRelationships);
  collectedRelationships = collectedRelationships.concat(newRelationships);
  return Promise.resolve();
};

const iterateEntities = async <T extends Entity = Entity>(
  filter,
  iteratee,
) => {
  const filteredEntities = [...inputEntities, ...collectedEntities].filter(
    (e) => e._type === filter._type,
  );
  for (const entity of filteredEntities as T[]) {
    await iteratee(entity);
  }
};

It removes duplicate code which may be worth the change, but it's still not particularly attractive.

Copy link
Contributor

@aiwilliams aiwilliams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying for me!

@ndowmon ndowmon merged commit 7af0eda into master Aug 26, 2020
@ndowmon ndowmon deleted the 311-mock-find-entity branch August 26, 2020 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants