Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 90f5347

Browse files
committed
feat(store): add serializing capabilities
1 parent 63f8679 commit 90f5347

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/helper-serializer.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import algoliaClient from 'algoliasearch';
2+
import algoliaHelper from 'algoliasearch-helper';
3+
4+
export const serialize = function(helper) {
5+
if (!(helper instanceof algoliaHelper.AlgoliaSearchHelper)) {
6+
throw new TypeError('Serialize expects an algolia helper instance.');
7+
}
8+
9+
const client = helper.getClient();
10+
11+
const serialized = {
12+
searchParameters: Object.assign({}, helper.state),
13+
appId: client.applicationID,
14+
apiKey: client.apiKey,
15+
response: helper.lastResults._rawResults,
16+
};
17+
18+
return serialized;
19+
};
20+
21+
export const unserialize = function(data) {
22+
const client = algoliaClient(data.appId, data.apiKey);
23+
const helper = algoliaHelper(
24+
client,
25+
data.searchParameters.index,
26+
data.searchParameters
27+
);
28+
helper.lastResults = new algoliaHelper.SearchResults(
29+
helper.state,
30+
data.response
31+
);
32+
33+
return helper;
34+
};

src/instantsearch.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
HIGHLIGHT_POST_TAG,
77
createFromAlgoliaCredentials,
88
createFromAlgoliaClient,
9+
createFromSerialized,
910
Store,
1011
} from './store';
1112

@@ -85,6 +86,7 @@ export {
8586
HIGHLIGHT_POST_TAG,
8687
createFromAlgoliaCredentials,
8788
createFromAlgoliaClient,
89+
createFromSerialized,
8890
Store,
8991
Index,
9092
Highlight,

src/store.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import algolia from 'algoliasearch';
22
import algoliaHelper from 'algoliasearch-helper';
33
import { version } from '../package.json';
4+
import {
5+
serialize as serializeHelper,
6+
unserialize as unserializeHelper,
7+
} from './helper-serializer';
48

59
export const FACET_AND = 'and';
610
export const FACET_OR = 'or';
@@ -30,6 +34,12 @@ export const createFromAlgoliaClient = client => {
3034
return new Store(helper);
3135
};
3236

37+
export const createFromSerialized = data => {
38+
const helper = unserializeHelper(data);
39+
40+
return new Store(helper);
41+
};
42+
3343
const onHelperChange = function() {
3444
if (this._stoppedCounter === 0) {
3545
this.refresh();
@@ -319,6 +329,10 @@ export class Store {
319329
this._helper.setState(newSearchParameters);
320330
}
321331

332+
serialize() {
333+
return serializeHelper(this._helper);
334+
}
335+
322336
// Todo: find a better name for this function.
323337
refresh() {
324338
this._helper.search();

0 commit comments

Comments
 (0)