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

fix: circular reference destructuring works with all models #947

Merged
merged 3 commits into from
Nov 9, 2021
Merged

fix: circular reference destructuring works with all models #947

merged 3 commits into from
Nov 9, 2021

Conversation

sushantdhiman
Copy link
Contributor

@sushantdhiman sushantdhiman commented Nov 9, 2021

Fixes #811

Short version of this problem given below

/**---Problem---**/

const dispatch = {};

const gen = ({ a, b }) => {
  return {
    log: () => {
      console.log(a, b);
    }
  };
};

["a", "b"].forEach((item) => {
  const x = {};
  dispatch[item] = x;
  x['run' + item] = gen(dispatch);
});

dispatch["a"].runa.log();
dispatch["b"].runb.log();


/**--- Solution ---**/

const dispatch = {
  a: {},
  b: {}
};

const gen = ({ a, b }) => {
  return {
    log: () => {
      console.log(a, b);
    }
  };
};

["a", "b"].forEach((item) => {
  const x = dispatch[item] || {};
  dispatch[item] = x;
  x['run' + item] = gen(dispatch);
});

dispatch.a.runa.log();
dispatch.b.runb.log();
{ runa: { log: [Function: log] } } undefined
{ runa: { log: [Function: log] } } { runb: { log: [Function: log] } }

{ runa: { log: [Function: log] } } { runb: { log: [Function: log] } }
{ runa: { log: [Function: log] } } { runb: { log: [Function: log] } }

When effects method is called it can only access object references which were available to it at that time. I have separated model creation and effects creation process. Now correct models reference is available at the time of effects creation.


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


@semoal semoal requested a review from tianzhich November 9, 2021 10:03
@semoal
Copy link
Member

semoal commented Nov 9, 2021

LGTM, let see what Zhi Tian says

Copy link
Collaborator

@tianzhich tianzhich left a comment

Choose a reason for hiding this comment

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

Except for one place about the function naming, all changes look great to me too.

packages/core/src/dispatcher.ts Outdated Show resolved Hide resolved
@tianzhich
Copy link
Collaborator

Nice explanations here. Thanks for your contributions @sushantdhiman

@semoal semoal changed the title fix: circular reference destruction works with models fix: circular reference destructuring works with all models Nov 9, 2021
@semoal semoal merged commit 7ada366 into rematch:main Nov 9, 2021
@sushantdhiman sushantdhiman deleted the fix/circular-reference branch November 9, 2021 12:35
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.

Regression in destructuring models from effects dispatch param
3 participants