Skip to content

Commit

Permalink
Merge pull request #28 from social-native/feat/add-ability-to-trigger…
Browse files Browse the repository at this point in the history
…-rehydrate-on-demand

Chore: update readme
  • Loading branch information
erhathaway authored Mar 11, 2020
2 parents 5eacd6f + ef45507 commit e7c093b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ The `options` looks like:
historySize?: number;
currentLocationStore?: UrlStore<State>;
historyPersister?: IHistoryPersister;
rehydrateOnStart?: boolean; // whether to run the `rehydrate` method in the constructor
}
```

Expand All @@ -1041,7 +1042,7 @@ IHistoryPersister {
| setCurrentState | sets the current state of filters and suggestgions | `(location: HistoryLocation): void` |
| back | goes back in the history | `(): void` |
| forward | goes forward in the history | `(): void` |

| rehydrate | rehydrates from URL or persistent storage | `(): void` |

#### Attributes

Expand Down
15 changes: 8 additions & 7 deletions dev/app/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ const creatorCRMHistory = new History(creatorCRM, 'influencer_crm', {

(global as any).crmHistory = creatorCRMHistory;
(global as any).crm = creatorCRM;
// setTimeout(() => {
// creatorCRMHistory.setCurrentState(
// JSON.parse(
// '{"filters":{"multiselect":{"fieldKinds":{"tags":"should"},"fieldFilters":{"tags":{"carolsdaugther":{"inclusion":"include"}}}},"exists":{"fieldKinds":{"instagram.id":"must"},"fieldFilters":{"instagram.id":{"exists":true}}},"range":{"fieldKinds":{"user_profile.age":"must"},"fieldFilters":{"user_profile.age":{"lessThan":68,"greaterThan":35}}}},"suggestions":{"prefix":{"fieldKinds":{"tags":"should"},"fieldSearches":{"tags":"car"}}}}'
// )
// );
// }, 5000);
setTimeout(() => {
// creatorCRMHistory.setCurrentState(
// JSON.parse(
// '{"filters":{"multiselect":{"fieldKinds":{"tags":"should"},"fieldFilters":{"tags":{"carolsdaugther":{"inclusion":"include"}}}},"exists":{"fieldKinds":{"instagram.id":"must"},"fieldFilters":{"instagram.id":{"exists":true}}},"range":{"fieldKinds":{"user_profile.age":"must"},"fieldFilters":{"user_profile.age":{"lessThan":68,"greaterThan":35}}}},"suggestions":{"prefix":{"fieldKinds":{"tags":"should"},"fieldSearches":{"tags":"car"}}}}'
// )
// );
creatorCRMHistory.rehydrate();
}, 5000);
// console.log(history);

export default {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@social-native/snpkg-client-elasticsearch",
"version": "3.4.0",
"version": "3.5.0",
"description": "",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down Expand Up @@ -44,7 +44,7 @@
"lodash.debounce": "^4.0.8",
"mobx": "^5.14.2",
"mobx-react": "^6.1.4",
"query-params-data": "^0.1.1",
"query-params-data": "^0.2.2",
"query-string": "^6.11.1"
},
"peerDependencies": {
Expand All @@ -54,7 +54,7 @@
"lodash.chunk": "^4.2.0",
"lodash.debounce": "^4.0.8",
"query-string": "^6.11.1",
"query-params-data": "^0.1.1"
"query-params-data": "^0.2.2"
},
"devDependencies": {
"@rollup/plugin-json": "^4.0.2",
Expand Down
57 changes: 40 additions & 17 deletions src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IHistoryOptions<State> {
historySize?: number;
currentLocationStore?: ICurrentLocationStore<State>;
historyPersister?: IHistoryPersister;
rehydrateOnStart?: boolean;
}

export type CurrentLocationStateObserver<State> = (newState: State | undefined) => any;
Expand All @@ -36,7 +37,10 @@ export interface ICurrentLocationStore<State> {
}
) => void;
getState: () => State | undefined | void;
subscribeToStateChanges: (observer: CurrentLocationStateObserver<State>) => void;
subscribeToStateChanges: (
observer: CurrentLocationStateObserver<State>,
options?: {getCurrentState: boolean}
) => void;
}

export interface IHistoryPersister {
Expand Down Expand Up @@ -90,25 +94,14 @@ class History {
this.currentLocationInHistoryCursor = 0;
this.history = [];
this.historyPersister = options && options.historyPersister;
if (this.historyPersister) {
const persistedHistory = this.historyPersister.getHistory();
this.history = persistedHistory;
if (persistedHistory.length > 0) {
const existingStateFromUrl = this.currentLocationStore.getState();
if (!existingStateFromUrl) {
const newHistoryLocation = this._deepCopy(
persistedHistory[0] as HistoryLocation
);
this.currentLocationStore.setState(newHistoryLocation);
this._rehydrateFromLocation(newHistoryLocation);
} else {
this._rehydrateFromLocation(existingStateFromUrl);
}
}
if (options && options.rehydrateOnStart) {
this.rehydrate();
}
});

this.currentLocationStore.subscribeToStateChanges(this._currentStateSubscriber);
this.currentLocationStore.subscribeToStateChanges(this._currentStateSubscriber, {
getCurrentState: false
});

const debounceHistoryChange = debounce(this._recordHistoryChange, 300);

Expand Down Expand Up @@ -144,6 +137,36 @@ class History {
);
}

/**
* Rehydrates state from current state store (URL) or persistent storage (localStorage)
*/
public rehydrate = () => {
runInAction(() => {
if (this.historyPersister) {
const persistedHistory = this.historyPersister.getHistory();
this.history = persistedHistory;
if (persistedHistory.length > 0) {
const existingStateFromUrl = this.currentLocationStore.getState();
if (!existingStateFromUrl) {
const newHistoryLocation = this._deepCopy(
persistedHistory[0] as HistoryLocation
);
this.currentLocationStore.setState(newHistoryLocation);

this._rehydrateFromLocation(newHistoryLocation);
} else {
this._rehydrateFromLocation(existingStateFromUrl);
}
} else {
const existingStateFromUrl = this.currentLocationStore.getState();
if (existingStateFromUrl) {
this._rehydrateFromLocation(existingStateFromUrl);
}
}
}
});
};

// tslint:disable-next-line
public _recordHistoryChange = () => {
const filters = Object.keys(this.manager.filters).reduce((acc, filterName) => {
Expand Down

0 comments on commit e7c093b

Please sign in to comment.