Skip to content

Commit

Permalink
feat: introduce a $ready store for when captp is initialised
Browse files Browse the repository at this point in the history
Also clean up the state resetting.
  • Loading branch information
michaelfig committed Aug 7, 2020
1 parent 9a22fc0 commit d9d73d2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/dapp-svelte-wallet/ui/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Purses from './Purses.svelte';
import Config from './Config.svelte';
import Transactions from './Transactions.svelte';
import { connected } from './store';
import { connected, ready } from './store';
import ListItems from '../lib/ListItems.svelte';
import MenuButton from '../lib/MenuButton.svelte';
Expand Down Expand Up @@ -195,7 +195,7 @@
</nav>
</header>
</div>
{#if !$connected}
{#if !$ready}
<div
class="disconnected-background"
on:click|preventDefault|stopPropagation={() => {}} />
Expand Down
7 changes: 4 additions & 3 deletions packages/dapp-svelte-wallet/ui/src/Payment.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import Select from "smelte/src/components/Select/Select.svelte";
return E(item.actions).deposit(destination ? destination.purse : undefined);
};
$: purseItems = [{ value: null, text: 'Automatic' }, ...(
$purses ? $purses.filter(({ brand }) => brand === item.brand).map(p => ({ value: p, text: p.text })) : []
)];
$: purseItems = [
{ value: null, text: 'Automatic' },
...$purses.filter(({ brand }) => brand === item.brand).map(p => ({ value: p, text: p.text }))
];
// $: console.log('purseItems', purseItems);
</script>

Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-svelte-wallet/ui/src/Payments.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ListCard from "../lib/ListCard.svelte";
import Card from "smelte/src/components/Card";
$: paymentItems = $payments && $payments.filter(pmt => pmt.status !== 'deposited');
$: paymentItems = $payments.filter(pmt => pmt.status !== 'deposited');
</script>

<ListCard items={paymentItems}>
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-svelte-wallet/ui/src/Transfer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
}
};
$: contactItems = $contacts ? $contacts.map(({ value, text }) => ({ value, text })) : [];
$: purseItems = $purses ? $purses.filter(({ brand }) => brand === source.brand).map(p => ({ value: p, text: p.text })) : [];
$: contactItems = $contacts.map(({ value, text }) => ({ value, text }));
$: purseItems = $purses.filter(({ brand }) => brand === source.brand).map(p => ({ value: p, text: p.text }));
</script>

<div>
Expand Down
8 changes: 3 additions & 5 deletions packages/dapp-svelte-wallet/ui/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ export function makeCapTPConnection(makeConnection, { onReset }) {
await E.G(getBootstrap()).LOADING;

// Begin the flow of messages to our wallet, which
// we refetch from the new, loaded, bootstrap object.
const bootPresence = getBootstrap();

bootPK.resolve(bootPresence);
// we refetch from the new, loaded, bootstrap promise.
bootPK.resolve(getBootstrap());
}

// This is the public state, a promise that never resolves,
Expand All @@ -61,7 +59,7 @@ export function makeCapTPConnection(makeConnection, { onReset }) {

// Prepare the first reset, delayed so that our caller
// can use makePermanentPresence.
setTimeout(() => onReset(Promise.resolve(true)), 1);
setTimeout(() => onReset(bootPK.promise), 1);

return { makeStableForwarder, ...props };
}
15 changes: 11 additions & 4 deletions packages/dapp-svelte-wallet/ui/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const walletP = makeStableForwarder(bootP => E.G(bootP).wallet);
export const boardP = makeStableForwarder(bootP => E.G(bootP).board);

const resetAlls = [];

// We initialize as false, but reset to true on disconnects.
const [ready, setReady] = makeReadable(false, true);
const [inbox, setInbox] = makeReadable([]);
const [purses, setPurses] = makeReadable([]);
const [dapps, setDapps] = makeReadable([]);
Expand All @@ -26,7 +29,7 @@ const [contacts, setContacts] = makeReadable([]);
const [selfContact, setSelfContact] = makeReadable();
const [issuers, setIssuers] = makeReadable([]);

export { inbox, purses, dapps, payments, issuers, contacts, selfContact };
export { ready, inbox, purses, dapps, payments, issuers, contacts, selfContact };

function cmp(a, b) {
return a < b ? -1 : a === b ? 0 : 1;
Expand All @@ -39,6 +42,10 @@ function kv(keyObj, val) {
}

function onReset(readyP) {
// Reset is beginning, set unready.
setReady(false);

// When the ready promise fires, reset to ready.
readyP.then(() => resetAlls.forEach(fn => fn()));
E(walletP).getSelfContact().then(sc => setSelfContact({ contactPetname: 'Self', ...kv('Self', sc) }));
// Set up our subscriptions.
Expand Down Expand Up @@ -77,8 +84,8 @@ function onReset(readyP) {
}

// like React useHook, return a store and a setter for it
function makeReadable(value, start = undefined) {
const store = writable(value, start);
resetAlls.push(() => store.set(start));
function makeReadable(value, reset = value) {
const store = writable(value);
resetAlls.push(() => store.set(reset));
return [{ subscribe: store.subscribe }, store.set];
}

0 comments on commit d9d73d2

Please sign in to comment.