This repository was archived by the owner on Dec 30, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Expand file tree Collapse file tree 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 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 ,
Original file line number Diff line number Diff line change 11import algolia from 'algoliasearch' ;
22import algoliaHelper from 'algoliasearch-helper' ;
33import { version } from '../package.json' ;
4+ import {
5+ serialize as serializeHelper ,
6+ unserialize as unserializeHelper ,
7+ } from './helper-serializer' ;
48
59export const FACET_AND = 'and' ;
610export 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+
3343const 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 ( ) ;
You can’t perform that action at this time.
0 commit comments