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(#285): add composeWithStateSync to resolve issues with enhancer order #296

Merged
merged 5 commits into from
Feb 16, 2021
Merged

Conversation

sneljo1
Copy link
Contributor

@sneljo1 sneljo1 commented Feb 9, 2021

As discussed in #285. Added a new composeWithStateSync function which mimics the composer function and should replace that function when using more than one enhancer. When using one enhancer, the other API is still the same.

To accommodate this, I have also added 2 more things:

  • preventActionReplay was added to the options because in extensionCompose() we re-use the stateSyncEnhancer, but we do not want the actions to start forwarding there because then it will not pick up the ones dispatched by the middleware. We rather want them to start forwarding in the last part of the chain, hence forwardActionEnhancer. This is also possible with denyList, but I thought this was cleaner. WDYT?
  • To simplify the compose function, I have opted to check whether in renderer or main process, inside the lib. With that, I have also added a helper function stateSyncEnhancer which can wrap around the process specific enhancer to do the same thing.

Will see if I can write a test for it tomorrow. Do you want me to go ahead and update the README already as well @matmalkowski ?

@CLAassistant
Copy link

CLAassistant commented Feb 9, 2021

CLA assistant check
All committers have signed the CLA.

@matmalkowski matmalkowski linked an issue Feb 9, 2021 that may be closed by this pull request
@matmalkowski
Copy link
Collaborator

Thanks for the contribution!

We will try to take a look at the change in next few days, you could also add some info into the README file, like you mentioned. I've also started a docs page for the project (/docs folder) - you might as well add a paragraph there or just add a placeholder so we can cover it later.

@slapbox if you have some time, you might also take a look at this branch & try it with your issue #294 and redux-saga

@matmalkowski matmalkowski added the v2 All issues related to v2 label Feb 10, 2021
@Nantris
Copy link
Contributor

Nantris commented Feb 10, 2021

I'd really like to give this a try!

Unfortunately my TypeScript noobishness may be getting in the way.

I cloned the repo, checked out the alpha branch, and ran yarn && yarn build, but I get an error like rollup has no idea what TypeScript is. Am I overlooking something obvious?

image

@Nantris
Copy link
Contributor

Nantris commented Feb 10, 2021

Friggin' Windows... Builds fine on Linux, will update soon.

@Nantris
Copy link
Contributor

Nantris commented Feb 10, 2021

@Superjo149 there's no code changes needed on our end to take advantage of these changes, right?

Unfortunately it doesn't seem to remedy the issue with Redux Saga.

I took a look at how to setup RxJS and it looks very similar to setting up sagas. Can you share the part of your store configuration where you're applying your enhancers and middlewares so I can compare it with our setup?

I also tried importing composeWithStateSync and replacing our compose with that, but that leads to Error: Attempted to register a second handler for 'electron-redux.INIT_STATE_ASYNC'

@sneljo1
Copy link
Contributor Author

sneljo1 commented Feb 10, 2021

@Superjo149 there's no code changes needed on our end to take advantage of these changes, right?

Unfortunately it doesn't seem to remedy the issue with Redux Saga.

I took a look at how to setup RxJS and it looks very similar to setting up sagas. Can you share the part of your store configuration where you're applying your enhancers and middlewares so I can compare it with our setup?

I also tried importing composeWithStateSync and replacing our compose with that, but that leads to Error: Attempted to register a second handler for 'electron-redux.INIT_STATE_ASYNC'

@slapbox There is some kind of code change required, yes. I will properly document this tomorrow along with the tests. But this is the jist. The error sounds like your are using both the old API and the compose function.

Current approach:

Just selected the main process here as an example.

const middleware = applyMiddleware(countMiddleware)
const enhancer = compose(middleware, mainStateSyncEnhancer())
const store = createStore(reducer, defaultState, enhancer)

Approach with multiple enhancers (Saga middleware,... etc) using composeWithStateSync:

Note that when using composeWithStateSync, neither mainStateSyncEnhancer or rendererStateSyncEnhancer are required as this will be handled by the compose function. This in line with how redux-devtools-extension works. Also note that this needs to be done in both processes.

const middleware = applyMiddleware(countMiddleware)
const enhancer = composeWithStateSync(middleware)
const store = createStore(reducer, defaultState, enhancer)

@Nantris
Copy link
Contributor

Nantris commented Feb 10, 2021

@Superjo149 thanks so much for your help, that makes perfect sense now and I can confirm that everything seems to work properly!

Not only a fix, but a further streamlining of an already great API; wow, great work!

PS: Is there a way to get Redux Devtools working with this too?

@Nantris
Copy link
Contributor

Nantris commented Feb 10, 2021

Oh, you can just wrap compose functions around compose functions. Never knew that, so that's the solution for Redux Devtools in case anyone is lurking this thread.

Copy link
Collaborator

@matmalkowski matmalkowski left a comment

Choose a reason for hiding this comment

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

This looks exciting! I believe this is the right direction, and will get us closer to releasing stable v2 version, thanks a lot for all the hard work you put into figuring out how to implement this 👍🏻

yarn.lock Outdated Show resolved Hide resolved
src/utils/actions.ts Show resolved Hide resolved
src/composeWithStateSync.ts Outdated Show resolved Hide resolved
@sneljo1 sneljo1 marked this pull request as ready for review February 15, 2021 07:30
@matmalkowski matmalkowski merged commit cce8018 into klarna:alpha Feb 16, 2021
@matmalkowski
Copy link
Collaborator

🎉 This PR is included in version 2.0.0-alpha.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

@sneljo1
Copy link
Contributor Author

sneljo1 commented Feb 20, 2021

Glad I could contribute to this 🎉

matmalkowski added a commit that referenced this pull request Oct 2, 2024
* feat: migrate to typescript (#259)

* feat: migrate to typescript

This is WIP

* feat: package.json cleanup

* chore: remove eslint config rules

* chore: more cleaning

* chore: add tsc tests

* chore: reorganize export structure

* chore: add some basic e2e tests

* buid: add GH action for PR

* fix: e2e test memory leak fix

* chore: vscode workspace settings

* fix: update username in package.json

* chore: rollup.config simplify

* chore: add eslint (#268)

* chore: add eslint

This PR adds eslint setup to the reposiory. It doesn't fix existing errors/warnings - should get addressed in follow up Pull Requests since the codebase isn't final now anyway. Errors so far:

```
/Users/maciej.malkowski/git/github.com/klarna/electron-redux/src/syncMain.ts
  18:56  error  Async arrow function has no 'await' expression  @typescript-eslint/require-await
  46:3   error  Unsafe return of an any typed value             @typescript-eslint/no-unsafe-return

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/src/syncRenderer.ts
   21:8   error    Unsafe assignment of an any value                                                                @typescript-eslint/no-unsafe-assignment
   61:4   error    Unsafe return of an any typed value                                                              @typescript-eslint/no-unsafe-return
   76:3   error    Unsafe return of an any typed value                                                              @typescript-eslint/no-unsafe-return
   85:27  warning  Unexpected any. Specify a different type                                                         @typescript-eslint/no-explicit-any
   93:3   error    Promises must be handled appropriately or explicitly marked as ignored with the `void` operator  @typescript-eslint/no-floating-promises
  105:3   error    Unsafe return of an any typed value                                                              @typescript-eslint/no-unsafe-return
  105:32  warning  Unexpected any. Specify a different type                                                         @typescript-eslint/no-explicit-any

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/src/utils/actions.ts
  15:31  warning  Missing return type on function  @typescript-eslint/explicit-module-boundary-types

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/src/utils/json.ts
   6:9   error    Unsafe assignment of an any value                     @typescript-eslint/no-unsafe-assignment
   7:3   error    Unsafe member access .__hydrate_type on an any value  @typescript-eslint/no-unsafe-member-access
  22:24  warning  Missing return type on function                       @typescript-eslint/explicit-module-boundary-types
  22:36  warning  Argument 'value' should be typed with a non-any type  @typescript-eslint/explicit-module-boundary-types
  22:43  warning  Unexpected any. Specify a different type              @typescript-eslint/no-explicit-any
  23:6   error    Unsafe member access .__hydrate_type on an any value  @typescript-eslint/no-unsafe-member-access
  27:13  error    Unsafe member access .__hydrate_type on an any value  @typescript-eslint/no-unsafe-member-access
  28:18  error    Unsafe member access .items on an any value           @typescript-eslint/no-unsafe-member-access
  31:2   error    Unsafe return of an any typed value                   @typescript-eslint/no-unsafe-return

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/src/utils/misc.ts
   7:44  warning  Missing return type on function  @typescript-eslint/explicit-module-boundary-types
  24:29  warning  Missing return type on function  @typescript-eslint/explicit-module-boundary-types
  33:31  warning  Missing return type on function  @typescript-eslint/explicit-module-boundary-types

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/tests/e2e.spec.ts
  21:5   error  Unexpected `await` of a non-Promise (non-"Thenable") value  @typescript-eslint/await-thenable
  36:12  error  Unexpected `await` of a non-Promise (non-"Thenable") value  @typescript-eslint/await-thenable
  45:12  error  Unexpected `await` of a non-Promise (non-"Thenable") value  @typescript-eslint/await-thenable

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/tests/e2e/main/index.ts
  31:5   error  Promises must be handled appropriately or explicitly marked as ignored with the `void` operator  @typescript-eslint/no-floating-promises
  31:44  error  Invalid type "string | undefined" of template literal expression                                 @typescript-eslint/restrict-template-expressions
  33:5   error  Promises must be handled appropriately or explicitly marked as ignored with the `void` operator  @typescript-eslint/no-floating-promises
  54:1   error  Promises must be handled appropriately or explicitly marked as ignored with the `void` operator  @typescript-eslint/no-floating-promises

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/tests/e2e/renderer/index.ts
   9:5  warning  Forbidden non-null assertion  @typescript-eslint/no-non-null-assertion
  18:5  warning  Forbidden non-null assertion  @typescript-eslint/no-non-null-assertion
  22:5  warning  Forbidden non-null assertion  @typescript-eslint/no-non-null-assertion
  32:5  warning  Forbidden non-null assertion  @typescript-eslint/no-non-null-assertion

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/tests/typescript/syncMain.ts
  5:7  warning  'store' is assigned a value but never used  @typescript-eslint/no-unused-vars

/Users/maciej.malkowski/git/github.com/klarna/electron-redux/tests/typescript/syncRenderer.ts
  5:7  warning  'store' is assigned a value but never used  @typescript-eslint/no-unused-vars

✖ 35 problems (20 errors, 15 warnings)
```

* fix: fix test related type issues

* chore: add prettier & format the code (#269)

This PR adds the prettier code formatter with custom config, and formats existing code according to those rulles.

Pre-commit hook to format staged files was also added.

* feat: add Code of Conduct & bump version (#270)

This pull request adds Code of Conducts and marks the change as breaking for semanric release to
bump the version to v2

BREAKING CHANGE: TS rewrite

* feat: add semantic-release setup for automatic deployments (#271)

* feat: add semantic-release setup for automatic deployments

This adds the setup for semantic-release. master/beta/alpha branch changes will get deployed to npm
with version depending on the commit message

* chore: add PR linter

* chore: add more events tro trigger the check

* fix: remove default custom serialization and make it an opt-in option (#272)

Removing custom serializer as a default, since its not alligned with redux recomendation about store
& serialization

fix #261

* fix: reduce bundle size by limiting external dependencies (#276)

I removed external dependency of `flux-standard-action` that includes entire loadash with it when imported. Instead, I re-implemented single function we are using and relied on single modules from loadash that are required. Should reduce the size significantly

* feat: ignore devtools when sending actions to renderers (#275)

Co-authored-by: Maciej Małkowski <monkey3310@gmail.com>

* chore: add README & LICENSE (#281)

* chore: add README & LICENSE

As per title, adding initial README file & LICENSE. It will need some work for sure, I've left the missing links as TODO

* chore: more badges

* chore: apply suggestions from code review

Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com>

* Update LICENSE.md

Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com>

* feat: make renderer state initialization synchronous by default (#280)

* feat: make renderer state initialization synchronous by default

Implements #278

* code review comments

* chore: spelling

Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com>

* chore: code review comments

Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com>

* docs: setup base documentation website  (#277)

* docussaurus initial setup

* base docussaurus config

* add base gh-pages deployment action

* remove unnecessary job step

* test changing baseurl

* fix last commit

* fix last commit

* remove trailing slash

* fix baseUrl config

* add basic content based on readme

* use yarn in gh-pages action

Co-authored-by: Maciej Małkowski <monkey3310@gmail.com>

* feat: add option for denyList (#274)

* fix: align enhancer style with standards to wrap the middleware (#284)

* fix: align enhancer style with standards to wrap the middleware

* fix: align enhancer style with standards to wrap the middleware

* docs: adding initial content for v2 (#290)

* docs: adding initial content for v2

Adding some initial content for Version 2, including expected file tree of documentation & legacy v1 docs paragraph.

* chore: apply suggestions from code review

Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com>

Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com>

* docs: fixing doc build issue with missing paths (#295)

Also adding PR build step to prevent this from happening again

* chore: update semrel lint to run on forks

* fix(#285): add composeWithStateSync to resolve issues with enhancer order (#296)

* fix(#285): add composeWithStateSync to resolve issues with enhancer order

* fix: resolve PR comments

* chore: add type tests

* docs: update README getting started  and add changes to FAQ

Co-authored-by: Maciej Małkowski <monkey3310@gmail.com>

* chore: update README

* chore: update README links

* chore: adding basic example of usage in raw scenario + moving integration tests (#299)

* chore: adding basic example of usage in raw scenario

* chore: work in progress

* chore: trying to add tests

* chore: fixing e2e tests and making them runnable

* fix package.json

* chore: code review comments addressed

* feat: use contextBridge in favour of requiring nodeIntegration (#300)

* feat: use contextBridge in favour of requiring nodeIntegration

Due to security concerns related to usage of nodeIntegration flag, according to best electron practices, renderer functions should be exposed with contextBridge. This PR does exactly that. It also changes a bit API to accomodate for this feature

* feat: fixing issues with test enviroment

* fix: add missing  preventDoubleInitialization() check

* change the scope of the contextBridge bindings to only expose high level API

---------

Co-authored-by: Paul Tiedtke <PaulTiedtke@web.de>
Co-authored-by: Burkhard Reffeling <burkhard.reffeling@gmail.com>
Co-authored-by: António Almeida <theantonioalmeida@gmail.com>
Co-authored-by: Jonas Snellinckx <jo.snel@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released on @alpha v2 All issues related to v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

v2 does not work with redux-observable
4 participants