1import { writable, derived } from 'svelte/store';
2
3const package_name = writable('svelte');
4const download_count = derived(
5 package_name,
6 ($package_name, set) => {
7 fetch('https://api.npmjs.org/downloads/point/last-week/' + $package_name)
8 .then(response => response.json())
9 .then(data => set(data.downloads));
10
11 return () => {
12 // We override the `set` function to eliminate race conditions
13 // This does *not* abort running fetch() requests, it only prevents
14 // them from overriding the store.
15 // To learn about canceling fetch requests, search the internet for `AbortController`
16 set = () => {}
17 }
18 }
19);
20
21// Updating `$package_name` will asynchronously update `$download_count`
22
1let package_name = 'svelte';
2let download_count = 0;
3$: fetch('https://api.npmjs.org/downloads/point/last-week/' + package_name)
4 .then(response => response.json())
5 .then(data => download_count = data.downloads || 0);
6
7// Updating `package_name` will asynchronously update `download_count`
8