1 | import { createStore } from "./store.js";
|
2 | export const getCollection = (conn, key, fetchCollection, subscribeUpdates) => {
|
3 | if (conn[key]) {
|
4 | return conn[key];
|
5 | }
|
6 | let active = 0;
|
7 | let unsubProm;
|
8 | let store = createStore();
|
9 | const refresh = () => fetchCollection(conn).then(state => store.setState(state, true));
|
10 | const refreshSwallow = () => refresh().catch((err) => {
|
11 |
|
12 |
|
13 |
|
14 | if (conn.socket.readyState == conn.socket.OPEN) {
|
15 | throw err;
|
16 | }
|
17 | });
|
18 | conn[key] = {
|
19 | get state() {
|
20 | return store.state;
|
21 | },
|
22 | refresh,
|
23 | subscribe(subscriber) {
|
24 | active++;
|
25 |
|
26 | if (active === 1) {
|
27 | if (subscribeUpdates) {
|
28 | unsubProm = subscribeUpdates(conn, store);
|
29 | }
|
30 |
|
31 | conn.addEventListener("ready", refreshSwallow);
|
32 | refreshSwallow();
|
33 | }
|
34 | const unsub = store.subscribe(subscriber);
|
35 | if (store.state !== undefined) {
|
36 |
|
37 |
|
38 | setTimeout(() => subscriber(store.state), 0);
|
39 | }
|
40 | return () => {
|
41 | unsub();
|
42 | active--;
|
43 | if (!active) {
|
44 |
|
45 | if (unsubProm)
|
46 | unsubProm.then(unsub => {
|
47 | unsub();
|
48 | });
|
49 | conn.removeEventListener("ready", refresh);
|
50 | }
|
51 | };
|
52 | }
|
53 | };
|
54 | return conn[key];
|
55 | };
|
56 |
|
57 | export const createCollection = (key, fetchCollection, subscribeUpdates, conn, onChange) => getCollection(conn, key, fetchCollection, subscribeUpdates).subscribe(onChange);
|