on fed01b6 help sign in
bridge/src/lib/need.js
66 lines 1.5 kB history raw
1// need.js: +need but worse
2//
4import { BRIDGE_ERROR } from './error';
6const needBuilder = fn => obj => {
7 if (!obj) {
8 fn();
9 }
11 return obj.matchWith({
12 Nothing: fn,
13 Just: p => p.value,
14 });
15};
17// simpler function for inline need.value(thing, () => {})
18export const value = (obj, fn) => needBuilder(fn)(obj);
20export const details = needBuilder(() => {
21 throw new Error(BRIDGE_ERROR.MISSING_POINT_DETAILS);
22});
24export const web3 = needBuilder(() => {
25 throw new Error(BRIDGE_ERROR.MISSING_WEB3);
26});
28export const contracts = needBuilder(() => {
29 throw new Error(BRIDGE_ERROR.MISSING_CONTRACTS);
30});
32export const wallet = needBuilder(() => {
33 throw new Error(BRIDGE_ERROR.MISSING_WALLET);
34});
36export const urbitWallet = needBuilder(() => {
37 throw new Error(BRIDGE_ERROR.MISSING_URBIT_WALLET);
38});
40export const addressFromWallet = obj => wallet(obj).address;
42export const point = needBuilder(() => {
43 throw new Error(BRIDGE_ERROR.MISSING_POINT);
44});
46export const pointCache = needBuilder(() => {
47 throw new Error(BRIDGE_ERROR.MISSING_POINT);
48});
50export const fromPointCache = (cache, point) => {
51 if (!(point in cache)) {
52 throw new Error(BRIDGE_ERROR.MISSING_POINT);
53 }
55 return cache[point];
56};
58export const keystore = obj => {
59 const ks = needBuilder(BRIDGE_ERROR.MISSING_KEYSTORE)(obj);
60 return ks.value.matchWith({
61 Ok: result => result.value,
62 Error: _ => {
63 throw new Error(BRIDGE_ERROR.MISSING_KEYSTORE);
64 },
65 });
66};