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

Added reset() #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export interface AsyncTrunkOptions {
* @param error
*/
onError?: (error: any) => void;

/**
* If the values of non-ignored properties before the trunk.init() should be
* volatilly saved so they can be later `reset()`ed.
*
* Defaults to `false`.
*/
saveDefaultValues?: boolean;
}

export class AsyncTrunk {
Expand All @@ -58,20 +66,25 @@ export class AsyncTrunk {
readonly delay: number;
readonly onError: (error: any) => void;

readonly saveDefaultValues: boolean;
private defaultValues: any;

constructor(
store: any,
{
storage = localStorage,
storageKey = KeyDefaultKey,
delay = 0,
onError = noop,
saveDefaultValues = false,
}: AsyncTrunkOptions = {},
) {
this.store = store;
this.storage = storage;
this.storageKey = storageKey;
this.delay = delay;
this.onError = onError;
this.saveDefaultValues = saveDefaultValues;
}

async persist() {
Expand Down Expand Up @@ -104,8 +117,32 @@ export class AsyncTrunk {
delay: this.delay,
onError: this.onError,
});
// Save default values
if (this.saveDefaultValues)
parseStore(this.store, initialState, false, this.defaultValues);
}

/**
* It will reset the store properties to their default values and also save on storage.
*
* You must have set the `saveDefaultValues` parameter in this trunk constructor.
*/
async reset() {
if (!this.saveDefaultValues) {
this.onError(
"mobx-sync reset() was called but saveDefaultValues parameter wasn't set on the trunk constructor.",
);
return;
}

parseStore(
this.store,
JSON.parse(JSON.stringify(this.defaultValues)),
false,
);
return this.persist();
}
/** Erases the data from the storage. */
async clear() {
return this.storage.removeItem(this.storageKey);
}
Expand Down
41 changes: 20 additions & 21 deletions src/parse-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import { action, isObservableArray, isObservableMap, observable } from 'mobx';
import { KeyFormat, KeyNodeVersion, KeyVersions } from './keys';
import { isPrimitive } from './utils';

let parseStore = (store: any, data: any, isFromServer: boolean) => {
/** Target allows saving the current data elsewhere than the store itself */
let parseStore = (
store: any,
data: any,
isFromServer: boolean,
target: any = store,
) => {
// if store or data is empty, break it
if (!store || !data) {
return;
Expand All @@ -21,10 +27,8 @@ let parseStore = (store: any, data: any, isFromServer: boolean) => {
const storeVersions = store[KeyVersions] || {};
const deserializers = store[KeyFormat] || {};
// version control for node
if ((KeyNodeVersion in dataVersions)
|| (KeyNodeVersion in storeVersions)) {
if (dataVersions[KeyNodeVersion]
!== storeVersions[KeyNodeVersion]) {
if (KeyNodeVersion in dataVersions || KeyNodeVersion in storeVersions) {
if (dataVersions[KeyNodeVersion] !== storeVersions[KeyNodeVersion]) {
return;
}
}
Expand Down Expand Up @@ -52,27 +56,22 @@ let parseStore = (store: any, data: any, isFromServer: boolean) => {
const storeValue = store[key];
const dataValue = data[key];
if (deserializers[key] && deserializers[key].deserializer) {
store[key] = deserializers[key].deserializer(dataValue, storeValue);
}
else if (isObservableArray(storeValue)) {
target[key] = deserializers[key].deserializer(dataValue, storeValue);
} else if (isObservableArray(storeValue)) {
// mobx array
store[key] = observable.array(dataValue);
}
else if (isObservableMap(storeValue)) {
target[key] = observable.array(dataValue);
} else if (isObservableMap(storeValue)) {
// mobx map
store[key] = observable.map(dataValue);
}
else if (isPrimitive(dataValue)) {
target[key] = observable.map(dataValue);
} else if (isPrimitive(dataValue)) {
// js/mobx primitive objects
store[key] = dataValue;
}
else if (!storeValue) {
target[key] = dataValue;
} else if (!storeValue) {
// if store value is empty, assign persisted data to it directly
store[key] = dataValue;
}
else {
target[key] = dataValue;
} else {
// nested pure js object or mobx observable object
parseStore(storeValue, dataValue, isFromServer);
parseStore(storeValue, dataValue, isFromServer, target[key]);
}
}
}
Expand Down