on 261ea40 help sign in
bridge/src/lib/need.js
62 lines 1.4 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 addressFromWallet = obj => wallet(obj).address;
38export const point = needBuilder(() => {
39 throw new Error(BRIDGE_ERROR.MISSING_POINT);
40});
42export const pointCache = needBuilder(() => {
43 throw new Error(BRIDGE_ERROR.MISSING_POINT);
44});
46export const fromPointCache = (cache, point) => {
47 if (!(point in cache)) {
48 throw new Error(BRIDGE_ERROR.MISSING_POINT);
49 }
51 return cache[point];
52};
54export const keystore = obj => {
55 const ks = needBuilder(BRIDGE_ERROR.MISSING_KEYSTORE)(obj);
56 return ks.value.matchWith({
57 Ok: result => result.value,
58 Error: _ => {
59 throw new Error(BRIDGE_ERROR.MISSING_KEYSTORE);
60 },
61 });
62};