This repository was archived by the owner on Dec 30, 2022. It is now read-only.
Commit 90f5347 1 parent 63f8679 commit 90f5347 Copy full SHA for 90f5347
File tree 3 files changed +50
-0
lines changed
3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 6
6
HIGHLIGHT_POST_TAG ,
7
7
createFromAlgoliaCredentials ,
8
8
createFromAlgoliaClient ,
9
+ createFromSerialized ,
9
10
Store ,
10
11
} from './store' ;
11
12
@@ -85,6 +86,7 @@ export {
85
86
HIGHLIGHT_POST_TAG ,
86
87
createFromAlgoliaCredentials ,
87
88
createFromAlgoliaClient ,
89
+ createFromSerialized ,
88
90
Store ,
89
91
Index ,
90
92
Highlight ,
Original file line number Diff line number Diff line change 1
1
import algolia from 'algoliasearch' ;
2
2
import algoliaHelper from 'algoliasearch-helper' ;
3
3
import { version } from '../package.json' ;
4
+ import {
5
+ serialize as serializeHelper ,
6
+ unserialize as unserializeHelper ,
7
+ } from './helper-serializer' ;
4
8
5
9
export const FACET_AND = 'and' ;
6
10
export const FACET_OR = 'or' ;
@@ -30,6 +34,12 @@ export const createFromAlgoliaClient = client => {
30
34
return new Store ( helper ) ;
31
35
} ;
32
36
37
+ export const createFromSerialized = data => {
38
+ const helper = unserializeHelper ( data ) ;
39
+
40
+ return new Store ( helper ) ;
41
+ } ;
42
+
33
43
const onHelperChange = function ( ) {
34
44
if ( this . _stoppedCounter === 0 ) {
35
45
this . refresh ( ) ;
@@ -319,6 +329,10 @@ export class Store {
319
329
this . _helper . setState ( newSearchParameters ) ;
320
330
}
321
331
332
+ serialize ( ) {
333
+ return serializeHelper ( this . _helper ) ;
334
+ }
335
+
322
336
// Todo: find a better name for this function.
323
337
refresh ( ) {
324
338
this . _helper . search ( ) ;
You can’t perform that action at this time.
0 commit comments