immerjs / immer

Create the next immutable state by mutating the current one
https://immerjs.github.io/immer/
MIT License
27.5k stars 850 forks source link

Add Optional Support For Multiple References to an Object #1088

Open BellCubeDev opened 8 months ago

BellCubeDev commented 8 months ago

What This Does

Some state trees may need to reference an object more than once (such as the tree for my fomod library). In essence, we store existing drafts when an off-by-default Immer class configuration option is enabled. This should be a painless solution. Specifics are described below.

Implementation Details

[!NOTE] Because of the extra computation involved with checking every proxied object against a map and traversing every object in a tree, enabling multi-ref will have a significant performance impact—even on trees which contain no repeated references.

Tests

The file __tests__/multiref.ts contains a number of tests related to this multi-reference support implementation. Such tests seek to verify that:

Additionally, I've added allowMultiRefs into the base.js test matrix and adjusted tests as necessary to accommodate for allowMultiRefs' required functionality.

Tests are performed on all relevant object archetypes where applicable.

Outstanding Discussion TODOs

[!NOTE] I have yet to compare the performance of stock Immer to the version of Immer created by my pull request without allowMultiRefs enabled. There may be no significant difference but work might be needed to address significant performance regressions should they exist.

[!CAUTION] Some tests are still failing when allowMultiRefs is set to true. That said, NO TESTS FAIL WHEN allowMultiRefs IS SET TO false.