agile-ts / agile

🌌 Global State and Logic Library for JavaScript/Typescript applications
https://agile-ts.org
MIT License
95 stars 8 forks source link

Merge loaded Group value from Storage into existing Group Value? #82

Closed bennobuilder closed 3 years ago

bennobuilder commented 3 years ago

🐛 (Bug report) Think report

🤖 Current Behavior

If we persist a Group, and than manually add a new ItemKey to it (in code), it overwrites this manually added ItemKey because it ovewrites it with the Storage value.

🎯 Expected behavior

Not sure yet It should merge the loaded value into the existing value, so that no ItemKey, that gets added after the first permantent save, gets missing.

📄 Reproducible example

Checkout the Examples of AgileTs

➕ Additional Notes

grafik Job when Group value gets loaded from local storage

Initial Value that got added after the Group got persisted for the first time. grafik

bennobuilder commented 3 years ago

Current workaround is to simply clear the local storage

bennobuilder commented 3 years ago

Ok, I decided to not add an option to merge the loaded(persisted) value into the current(initial) value. Why?

  1. The persisted value is loaded into the State shortly after the instantiation of the State -> This issue can't really occur in production since the persisted value nearly always includes the initial Group value. Maybe not if you fetch some Items from an API and then add them as initial Group itemKeys. But well, why not adding them after the creation of the Collection. Or why adding them at all without any itemValue.

    const MY_COLLECTION = App.createCollection((collection) => {
       groups: {
          myGroup: collection.Group(getItemKeysFromApi())
       }   
    });
    
    const MY_COLLECTION = App.createCollection((collection) => {
       groups: {
          myGroup: collection.Group([])
       }   
    });
    MY_COLLECTION.getGroup('myGroup')?.add(getItemKeysFromApi());

    -> this issue will mainly occur if the Group value got changed manually in the code and is out of sync with the persisted value.. In a production environment, it won't happen if using AgileTs correctly.

  2. How should I merge it.. if I merge then the persisted value ['2'] into the initial value ['2'] it would be ['2', '2'] and if I remove all twice items, I might destroy other merge use cases like in no Group States.

Anyway, if you find any useful use case for merging the persisted value into the current value.. don't hesitate to comment in this thread ;D

Thanks ^^