From 54ce9989235c401ee8e2f88b6098919b7dc3d9c5 Mon Sep 17 00:00:00 2001 From: Ethan Hathaway Date: Wed, 11 Mar 2020 17:51:56 -0700 Subject: [PATCH] Feat: add rehydration flag --- README.md | 1 + dev/app/context.ts | 37 +++++++++++++++++++++++++++---------- package.json | 2 +- src/history.ts | 4 ++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ed59953..438a21d 100644 --- a/README.md +++ b/README.md @@ -1050,6 +1050,7 @@ IHistoryPersister { | ---------------- | ------------------------------------------------------------ | --------------------------------------------------------------------- | | history | the recorded history | `Array` | | currentLocationInHistoryCursor | the location in history, changed by going 'back' or 'forward' | `number` | +| hasRehydratedLocation | flag to tell if any location was rehydrated from when the `rehydrate` method was called | ## Verbose Examples diff --git a/dev/app/context.ts b/dev/app/context.ts index da09e48..0ae4941 100644 --- a/dev/app/context.ts +++ b/dev/app/context.ts @@ -17,6 +17,7 @@ import { localStorageHistoryPersister } from '../../src'; import {IRangeConfig} from '../../src/filters/range_filter'; +import {reaction} from 'mobx'; // import {toJS} from 'mobx'; const exampleFormInstance = new ExampleForm(); @@ -104,9 +105,9 @@ const creatorCRM = new Manager(client, { }); // gqlClient.createClient().then(() => { -creatorCRM.getFieldNamesAndTypes().then(() => { - creatorCRM.runStartQuery(); -}); +// creatorCRM.getFieldNamesAndTypes().then(() => { +// creatorCRM.runStartQuery(); +// }); // setTimeout(() => console.log('hur', toJS(creatorCRM.fieldsWithFiltersAndSuggestions)), 3000); // }); @@ -128,14 +129,20 @@ 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"}}}}' - // ) - // ); +// 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"}}}}' +// ) +// ); + +creatorCRM.getFieldNamesAndTypes().then(() => { creatorCRMHistory.rehydrate(); -}, 5000); + if (!creatorCRMHistory.hasRehydratedLocation) { + creatorCRM.runStartQuery(); + } +}); +// }, 5000); // console.log(history); export default { @@ -144,3 +151,13 @@ export default { creatorCRM: React.createContext(creatorCRM), creatorCRMHistory: React.createContext(creatorCRMHistory) }; + +reaction( + () => ({ + queue: [...creatorCRM._sideEffectQueue], + isSideEffectRunning: !!creatorCRM.isSideEffectRunning + }), + data => { + console.log(creatorCRM._sideEffectQueue.map(k => k.kind)); + } +); diff --git a/package.json b/package.json index dff1208..fa12abe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@social-native/snpkg-client-elasticsearch", - "version": "3.5.0", + "version": "3.6.0", "description": "", "main": "dist/index.cjs.js", "module": "dist/index.es.js", diff --git a/src/history.ts b/src/history.ts index 52fd068..78015a5 100644 --- a/src/history.ts +++ b/src/history.ts @@ -78,6 +78,7 @@ class History { public currentLocationInHistoryCursor: number; public currentLocationStore: ICurrentLocationStore; public historyPersister: IHistoryPersister | undefined; + public hasRehydratedLocation: boolean; constructor( manager: Manager, @@ -94,6 +95,7 @@ class History { this.currentLocationInHistoryCursor = 0; this.history = []; this.historyPersister = options && options.historyPersister; + this.hasRehydratedLocation = false; if (options && options.rehydrateOnStart) { this.rehydrate(); } @@ -146,6 +148,7 @@ class History { const persistedHistory = this.historyPersister.getHistory(); this.history = persistedHistory; if (persistedHistory.length > 0) { + this.hasRehydratedLocation = true; const existingStateFromUrl = this.currentLocationStore.getState(); if (!existingStateFromUrl) { const newHistoryLocation = this._deepCopy( @@ -160,6 +163,7 @@ class History { } else { const existingStateFromUrl = this.currentLocationStore.getState(); if (existingStateFromUrl) { + this.hasRehydratedLocation = true; this._rehydrateFromLocation(existingStateFromUrl); } }