Skip to content

Commit 5369bd4

Browse files
committed
feat(site): add reset function and throw error when initializing twice
1 parent 8a090b2 commit 5369bd4

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

src/HnServerResponse.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export interface Entity {
2+
__hn?: {
3+
view_modes: {
4+
[viewMode: string]: boolean,
5+
};
6+
entity: {
7+
type: string,
8+
bundle: string,
9+
};
10+
url: string;
11+
status?: number;
12+
};
13+
}
14+
15+
export default interface HnServerResponse {
16+
data?: {
17+
[uuid: string]: Entity | any,
18+
};
19+
paths?: {
20+
[path: string]: string,
21+
};
22+
__hn?: {
23+
request?: {
24+
user?: string,
25+
token?: string,
26+
};
27+
};
28+
}

src/Site.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,50 @@ import * as deepmerge from 'deepmerge';
44
import * as getNested from 'get-nested';
55
import { stringify } from 'query-string';
66
import SiteInitializeParams from './SiteInitializeParams';
7+
import HnServerResponse from './HnServerResponse';
78

89
polyfill();
910

1011
const propertiesToHydrate = ['tokensToVerify', 'user', 'data'];
1112

1213
class Site {
1314

14-
private initialized: boolean = false;
15+
private initialized: boolean;
1516

1617
// Created when initializing
1718
private url: string;
1819

1920
// Can be hydrated and dehydrated
20-
private tokensToVerify: string[] = [];
21+
private tokensToVerify: string[];
2122
private user: string;
22-
private data = {
23-
data: {},
24-
paths: {},
25-
};
23+
private data: HnServerResponse;
2624

2725
// Not hydrated
28-
private pagesLoading = {};
26+
private pagesLoading: {[s: string]: Promise<string>};
2927

3028
constructor(initParams?: SiteInitializeParams) {
29+
this.reset();
3130
if (initParams) this.initialize(initParams);
3231
}
3332

3433
initialize({ url }: SiteInitializeParams) {
34+
if (this.initialized) throw Error('The site is already initialized.');
3535
this.initialized = true;
3636
this.url = url;
3737
}
3838

39+
reset() {
40+
this.initialized = false;
41+
this.url = null;
42+
this.tokensToVerify = [];
43+
this.user = null;
44+
this.data = {
45+
data: {},
46+
paths: {},
47+
};
48+
this.pagesLoading = {};
49+
}
50+
3951
/**
4052
* Creates an object that can be hydrated by the hydrate function.
4153
*/
@@ -56,7 +68,7 @@ class Site {
5668
});
5769
}
5870

59-
private fetch(path, options = {}): Promise<object> {
71+
private fetch(path, options = {}): Promise<HnServerResponse> {
6072
if (!this.initialized) {
6173
throw Error('Site is not intitialized. Pass an object when creating a site, or use the ' +
6274
'initialize method.');
@@ -77,7 +89,7 @@ class Site {
7789
});
7890
}
7991

80-
public getPage(path, loadFromServer = false): Promise<void> {
92+
public getPage(path, loadFromServer = false): Promise<string> {
8193
if (!this.pagesLoading[ path ]) {
8294
const dataMaybeAlreadyLoaded = getNested(() => this.data.data[this.data.paths[path]]);
8395
if (getNested(() => dataMaybeAlreadyLoaded.__hn.view_modes.includes('default'))) {
@@ -96,16 +108,16 @@ class Site {
96108
_hn_user: this.user ? this.user : undefined,
97109
_hn_verify: tokensToVerify,
98110
}))
99-
.then((page: Response) => {
111+
.then((page: HnServerResponse) => {
100112

101113
// Get the user id, to pass to all new requests.
102-
this.user = getNested(() => page['__hn'].request.user, this.user);
114+
this.user = getNested(() => page.__hn.request.user, this.user);
103115

104116
// Remove all sent tokens from the tokensToVerify.
105117
this.tokensToVerify = this.tokensToVerify.filter(t => tokensToVerify.indexOf(t) === -1);
106118

107119
// Add new token to tokensToVerify.
108-
const newToken = getNested(() => page['__hn'].request.token);
120+
const newToken = getNested(() => page.__hn.request.token);
109121
if (newToken) this.tokensToVerify.push(newToken);
110122

111123
// Add all data to the global data storage.
@@ -119,12 +131,13 @@ class Site {
119131
[path]: '500',
120132
},
121133
});
122-
});
134+
})
135+
.then(() => this.data.paths[ path ]);
123136
}
124-
return this.pagesLoading[ path ].then(() => this.data.paths[ path ]);
137+
return this.pagesLoading[ path ];
125138
}
126139

127-
private addData(data: object) {
140+
private addData(data: HnServerResponse) {
128141
this.data = deepmerge(this.data, data, { arrayMerge: (a, b) => b });
129142
}
130143

0 commit comments

Comments
 (0)