-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
createFindOne.ts
33 lines (29 loc) · 1.05 KB
/
createFindOne.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
import {Meteor} from 'meteor/meteor';
import {batch, createSignal} from 'solid-js';
import type {Accessor} from 'solid-js';
import {createStore, reconcile} from 'solid-js/store';
import type {Store} from 'solid-js/store';
import {createTracker} from './createTracker';
const createFindOneClient = <T extends object>(factory: () => T | undefined | null): [Accessor<boolean>, Store<T | {}>] => {
const [exists, setExists] = createSignal<boolean>(false);
const [store, setStore] = createStore<T | {}>({});
createTracker(() => {
const document = factory();
batch(() => {
setExists(Boolean(document));
setStore(reconcile(document || {}));
});
});
return [exists, store];
};
// On the server, just fetch without reactivity
const createFindOneServer = <T extends object>(factory: () => T | undefined | null): [Accessor<boolean>, Store<T> | {}] => {
const document = factory();
return [
() => Boolean(document),
() => document || {},
];
};
export const createFindOne = Meteor.isServer
? createFindOneServer
: createFindOneClient;