Closed ThomasGysemans closed 11 months ago
It's not mentioned anywhere in docs or readme, but this package is intended to be exactly the same as the web LocalStorage. It doesn't have any methods like writeData
or optional write
arg.
I understand the problem this PR is trying to solve, although the proposed way is not acceptable, since it will break compatibility with the web implementation of LocalStorage.
My updates allow the developer to better control when the data is written down on the JSON file.
If for example the user is storing a huge amount of data (several megabytes for example), data fetched from a database for a future offline mode maybe, then the developer will want their users to be able to delete the data, or to control it. In the event that multiple keys must be deleted using
deleteItem(String key)
, looping over the keys one by one and calling this method is extremely slow as the file is re-written at every single iteration. That behaviour is not ideal.My update introduces a new way to control when the write and delete operations are made.
To delete multiple items:
BAD
GOOD
To set multiple items
BAD
GOOD
New helper features
setItem()
), the stream emits an event with value:{ "itemWasSet": true }
deleteItem()
ordeleteItems()
. The stream emits an event with value:{ "itemWasRemoved": true }
for each key.{ "size": some int }
(the number of bytes in the file).It is also possible to get the exact amount of bytes stored in the JSON file:
Breaking changes
The only breaking change concerns the parameters of
setItem()
. Indeed,toEncodable
is no longer an optional positional argument. Here the exact signature: