Repository navigation
MMKV Plugin Multiple MMKV Id instances #443
Replies: 2 comments
|
I'm sure there is a better way, but I moved the creation of the observable and its syncObserver into a function that recreates the Observable when a user is logged in. Here is a code snippet. Still hoping to hear from someone needing this type of functionality as I would love to hear how you accomplished. I'm excited to use Legend State, so I want to utilize it to its fullest! export let tags$: Observable<AllTags>;
const initialize = () => {
tags$ = observable<AllTags>({
allTags: [],
addTag: (tagname) => {
const allTags = tags$.allTags.peek() || [];
const newTag = { id: uuid.v4(), name: tagname, position: allTags.length + 1 };
tags$.allTags.set([...allTags, newTag]);
currStorage?.setItem('tags', tags$.allTags.peek());
console.log('MMKV ID', currStorage?.getItem('mmkvid'));
},
removeTag: (tagId) => {
const allTags = tags$.allTags.peek();
const newTags = allTags.filter((el) => el.id !== tagId);
tags$.allTags.set(newTags);
currStorage?.setItem('tags', newTags);
},
reset: () => {
tags$.allTags.set([]);
currStorage?.setItem('tags', []);
},
});
const customMMKVConfig = new ObservablePersistMMKV({ id: `${authManager?.currentUser?.id}` });
const syncedTags = syncObservable(tags$, {
persist: {
plugin: customMMKVConfig,
name: 'tags',
options: {},
},
});
observe(() => {
console.log(`OBSERVED TAGS-${authManager?.currentUser?.name}`, tags$.allTags.peek());
});
};
initialize();
authManager.subscribe(() => {
initialize();
}); |
|
I looked at this closely because your workaround (recreating the whole observable + calling The root problem: Recreating |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have a simple app that allows a person to create multiple users.
When logging in as a user, I use the userId as a MMKV Instance ID so that each "users" data is stored in a separate instance.
I'm having difficulty in understanding how to clear the observable variables when I switch.
It seems that if I empty the observable state, it clears the persisted state that I'm trying to load.
Here is the reset function from the tags$ observable:
I know it is something I'm doing wrong, so any pointers would be appreciated.
Thanks!
All reactions