Skip to content

Commit fb38431

Browse files
committed
enhancement: make svelte stores react to query/path changes
1 parent a8b5550 commit fb38431

File tree

4 files changed

+96
-64
lines changed

4 files changed

+96
-64
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Create a Svelte App and install Firebase.
4949
npx degit sveltejs/template fireapp
5050
cd fireapp
5151

52-
npm install sveltefire firebase
52+
npm i && npm install sveltefire firebase
5353
```
5454

5555

@@ -375,6 +375,8 @@ Slot Props & Events:
375375

376376
- *data* collection data as array.
377377
- *ref* CollectionReference for writes
378+
- *first* the first result in the query, useful for pagination.
379+
- *last* the last result in the query, useful for pagination.
378380
- *error* current error
379381

380382
```html
@@ -385,6 +387,8 @@ Slot Props & Events:
385387
log
386388
let:data={comments}
387389
let:ref={commentsRef}
390+
let:last={firstComment}
391+
let:first={lastComment}
388392
on:data
389393
on:ref
390394
>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.0.4",
2+
"version": "0.0.5",
33
"name": "sveltefire",
44
"svelte": "src/index.js",
55
"main": "dist/index.js",

src/Collection.svelte

+46-32
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,54 @@
11
<script>
2-
export let path;
3-
export let query = null;
4-
export let traceId = '';
5-
export let log = false;
6-
export let startWith = undefined;
7-
export let maxWait = 10000;
8-
import { onMount, onDestroy, createEventDispatcher } from 'svelte';
9-
10-
import { collectionStore } from './firestore';
11-
12-
const store = collectionStore(path, query, { startWith, traceId, log, maxWait });
13-
14-
const dispatch = createEventDispatcher();
15-
16-
let unsub;
17-
18-
onMount(() => {
19-
dispatch('ref', { ref: store.ref });
20-
21-
unsub = store.subscribe(data => {
22-
dispatch('data', {
23-
data
24-
});
25-
});
2+
export let path;
3+
export let query = null;
4+
export let traceId = "";
5+
export let log = false;
6+
export let startWith = undefined;
7+
export let maxWait = 10000;
8+
9+
import { onDestroy, createEventDispatcher } from "svelte";
10+
11+
import { collectionStore } from "./firestore";
12+
13+
let store = collectionStore(path, query, {
14+
startWith,
15+
traceId,
16+
log,
17+
maxWait
18+
});
19+
20+
const dispatch = createEventDispatcher();
21+
22+
let unsub;
23+
24+
// Props changed
25+
$: {
26+
if (unsub) {
27+
unsub();
28+
store = collectionStore(path, query, {
29+
startWith,
30+
traceId,
31+
log,
32+
maxWait
33+
});
34+
}
35+
36+
dispatch("ref", { ref: store.ref });
37+
38+
unsub = store.subscribe(data => {
39+
dispatch("data", {
40+
data
41+
});
2642
});
43+
}
2744
28-
onDestroy(() => unsub());
29-
30-
45+
onDestroy(() => unsub());
3146
</script>
3247

33-
3448
{#if $store}
35-
<slot data={$store} ref={store.ref} error={store.error}></slot>
49+
<slot data={$store} ref={store.ref} error={store.error} />
3650
{:else if store.loading}
37-
<slot name="loading"></slot>
51+
<slot name="loading" />
3852
{:else}
39-
<slot name="fallback"></slot>
40-
{/if}
53+
<slot name="fallback" />
54+
{/if}

src/Doc.svelte

+44-30
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,52 @@
11
<script>
2-
export let path;
3-
export let log = false;
4-
export let traceId = '';
5-
export let startWith = undefined; // Why? Firestore returns null for docs that don't exist, predictible loading state.
6-
export let maxWait = 10000;
7-
8-
import { onMount, onDestroy, createEventDispatcher } from 'svelte';
9-
import { docStore } from './firestore';
10-
11-
12-
let store = docStore(path, { startWith, traceId, log, maxWait });
13-
14-
// CUSTOM EVENTS
15-
16-
const dispatch = createEventDispatcher();
17-
18-
let unsub;
19-
20-
onMount(() => {
21-
dispatch('ref', { ref: store.ref });
22-
unsub = store.subscribe(data => {
23-
dispatch('data', {
24-
data
25-
});
26-
});
2+
export let path;
3+
export let log = false;
4+
export let traceId = "";
5+
export let startWith = undefined; // Why? Firestore returns null for docs that don't exist, predictible loading state.
6+
export let maxWait = 10000;
7+
8+
import { onDestroy, createEventDispatcher } from "svelte";
9+
import { docStore } from "./firestore";
10+
11+
let store = docStore(path, {
12+
startWith,
13+
traceId,
14+
log,
15+
maxWait
16+
});
17+
18+
const dispatch = createEventDispatcher();
19+
20+
let unsub;
21+
22+
// Props changed
23+
$: {
24+
if (unsub) {
25+
unsub();
26+
store = docStore(path, {
27+
startWith,
28+
traceId,
29+
log,
30+
maxWait
31+
});
32+
}
33+
34+
dispatch("ref", { ref: store.ref });
35+
36+
unsub = store.subscribe(data => {
37+
dispatch("data", {
38+
data
39+
});
2740
});
41+
}
2842
29-
onDestroy(() => unsub());
43+
onDestroy(() => unsub());
3044
</script>
3145

3246
{#if $store}
33-
<slot data={$store} ref={store.ref} firestore={store.firestore} error={store.error}></slot>
47+
<slot data={$store} ref={store.ref} error={store.error} />
3448
{:else if store.loading}
35-
<slot name="loading"></slot>
49+
<slot name="loading" />
3650
{:else}
37-
<slot name="fallback"></slot>
38-
{/if}
51+
<slot name="fallback" />
52+
{/if}

0 commit comments

Comments
 (0)