This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
/
setup-store.ts
76 lines (63 loc) · 1.79 KB
/
setup-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Coordinator, { EventLoggingStrategy, RequestStrategy, SyncStrategy } from '@orbit/coordinator';
import Orbit, { Schema } from '@orbit/data';
import IndexedDBStore from '@orbit/indexeddb';
import JSONAPIStore from '@orbit/jsonapi';
import Store, { Cache } from '@orbit/store';
import { schema as schemaDefinition } from './schema';
declare const __ENV_API_HOST__: string;
export function setupStore(appState): { store: Store; local?: IndexedDBStore; coordinator?: Coordinator } {
let schema = new Schema(schemaDefinition);
let store = new Store({ schema });
if (appState.isSSR) {
let data = appState.appData;
data.orbit.forEach((record) => {
store.cache.patch((t) => t.addRecord(record));
});
return {
store
};
} else {
Orbit.fetch = window.fetch.bind(window);
let host = __ENV_API_HOST__;
let remote = new JSONAPIStore({
name: 'remote',
namespace: 'api',
host,
schema
});
let local = new IndexedDBStore({
name: 'local',
schema
});
let remoteRequestStrategy = new RequestStrategy({
action: 'pull',
blocking: true,
on: 'beforeQuery',
source: 'store',
target: 'remote',
});
let logger = new EventLoggingStrategy({
interfaces: ['queryable', 'syncable']
});
let remoteStoreSync = new SyncStrategy({
blocking: true,
source: 'remote',
target: 'store'
});
const localStoreSync = new SyncStrategy({
source: 'store',
target: 'local',
blocking: true
});
const coordinator = new Coordinator({
sources: [store, local, remote],
strategies: [remoteRequestStrategy, remoteStoreSync, localStoreSync, logger]
});
coordinator.activate();
return {
store,
local,
coordinator
};
}
}