Skip to content

Commit c5ac359

Browse files
committed
Remove cancelable promise dependency
1 parent 52ff761 commit c5ac359

File tree

11 files changed

+92
-108
lines changed

11 files changed

+92
-108
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
"@pyoner/svelte-ts-preprocess": "^1.2.1",
2828
"@rollup/plugin-json": "^4.0.0",
2929
"@types/lodash.debounce": "^4.0.6",
30-
"@types/node": "^12.12.3",
3130
"babel-plugin-external-helpers": "^6.18.0",
3231
"rimraf": "^3.0.0",
33-
"rollup": "^1.12.0",
32+
"rollup": "^1.27.5",
3433
"rollup-plugin-commonjs": "^10.0.0",
3534
"rollup-plugin-img": "^1.1.0",
3635
"rollup-plugin-node-resolve": "^5.2.0",
@@ -49,7 +48,6 @@
4948
"bowser": "^2.5.2",
5049
"fortmatic": "^0.8.2",
5150
"lodash.debounce": "^4.0.8",
52-
"promise-cancelable": "^2.1.1",
5351
"regenerator-runtime": "^0.13.3",
5452
"squarelink": "^1.1.4"
5553
},

rollup.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ export default {
4242
importee === 'svelte' || importee.startsWith('svelte/')
4343
}),
4444
commonjs(),
45-
typescript()
45+
typescript({
46+
clean: true
47+
})
4648
],
4749
external: [
4850
'bowser',
4951
'bnc-sdk',
5052
'bignumber.js',
51-
'promise-cancelable',
5253
'@portis/web3',
5354
'@walletconnect/web3-provider',
5455
'fortmatic',

src/@types/images.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/@types/svelte-i18n.d.ts renamed to src/@types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
declare module 'fortmatic'
2+
declare module 'squarelink'
3+
declare module '@walletconnect/web3-provider'
4+
5+
declare module '*.png'
6+
declare module '*.svg'
7+
18
declare module 'svelte-i18n' {
29
interface Options {
310
fallback: string

src/@types/libraries.d.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/interfaces.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,11 @@ export interface AppState {
208208
walletCheckCompleted: boolean
209209
}
210210

211-
export interface QuerablePromise extends CancelablePromise {
212-
isFulfilled: () => boolean
213-
isResolved: () => boolean
214-
isRejected: () => boolean
211+
export interface CancelablePromise extends Promise<any> {
212+
cancel: () => void
215213
}
216214

217-
export interface CancelablePromise extends Promise<any> {
218-
cancel: (func: () => void) => void
215+
export interface QueryablePromise extends CancelablePromise {
219216
isFulfilled: () => boolean
220217
isResolved: () => boolean
221218
isRejected: () => boolean

src/stores.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import { writable, derived, get } from 'svelte/store'
21
import { getBlocknative } from './services'
3-
import { wait, makeQuerablePromise } from './utilities'
2+
import { writable, derived, get } from 'svelte/store'
3+
import debounce from 'lodash.debounce'
4+
import { wait, makeQueryablePromise, makeCancelable } from './utilities'
45
import { validateWalletInterface, validateType } from './validation'
56
import {
67
WritableStore,
78
WalletInterfaceStore,
89
WalletInterface,
910
WalletStateSliceStore,
1011
StateSyncer,
11-
BalanceStore
12+
BalanceStore,
13+
QueryablePromise
1214
} from './interfaces'
1315

14-
const { default: Cancelable } = require('promise-cancelable')
15-
const debounce = require('lodash.debounce')
16-
1716
export const app: WritableStore = writable({
1817
dappId: '',
1918
networkId: 1,
@@ -27,7 +26,7 @@ export const app: WritableStore = writable({
2726
})
2827

2928
export const balanceSyncStatus: {
30-
syncing: null | Promise<undefined>
29+
syncing: null | QueryablePromise
3130
error: string
3231
} = {
3332
syncing: null,
@@ -253,22 +252,7 @@ function syncStateWithTimeout(options: {
253252
return () => {}
254253
}
255254

256-
const prom = makeQuerablePromise(
257-
new Cancelable(
258-
(
259-
resolve: (val: string | number | null) => void,
260-
reject: (err: any) => void,
261-
onCancel: (callback: () => void) => void
262-
) => {
263-
getState().then(resolve)
264-
265-
onCancel(() => {
266-
balanceSyncStatus.error =
267-
'There was a problem getting the balance of this wallet'
268-
})
269-
}
270-
).catch(() => {})
271-
)
255+
const prom = makeCancelable(getState())
272256

273257
balanceSyncStatus.syncing = prom
274258

@@ -286,10 +270,8 @@ function syncStateWithTimeout(options: {
286270
const timedOut = wait(timeout)
287271

288272
timedOut.then(() => {
289-
if (!prom.isFulfilled()) {
290-
prom.cancel(() => {})
291-
}
273+
prom.cancel()
292274
})
293275

294-
return () => prom.cancel(() => {})
276+
return () => prom.cancel()
295277
}

src/utilities.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import bowser from 'bowser'
22
import BigNumber from 'bignumber.js'
33

4-
import {
5-
QuerablePromise,
6-
WalletInterface,
7-
CancelablePromise
8-
} from './interfaces'
4+
import { WalletInterface, QueryablePromise } from './interfaces'
95

106
export function getNetwork(provider: any): Promise<number | any> {
117
return new Promise((resolve, reject) => {
@@ -210,22 +206,36 @@ export function wait(time: number) {
210206
return new Promise(resolve => setTimeout(resolve, time))
211207
}
212208

213-
export function makeQuerablePromise(
214-
promise: CancelablePromise
215-
): QuerablePromise {
209+
export function makeCancelable(promise: any) {
210+
let rejectFn: any
211+
212+
const wrappedPromise: any = new Promise((resolve, reject) => {
213+
rejectFn = reject
214+
215+
promise.then(resolve).catch(reject)
216+
})
217+
218+
wrappedPromise.cancel = () => {
219+
rejectFn('poop')
220+
}
221+
222+
return wrappedPromise
223+
}
224+
225+
export function makeQueryablePromise(promise: any): QueryablePromise {
216226
let isResolved = false
217227
let isRejected = false
218228

219-
promise.then(
220-
function(v: any) {
221-
isResolved = true
222-
return v
223-
},
224-
function(e: any) {
225-
isRejected = true
226-
throw e
227-
}
228-
)
229+
promise.then(function(v: any) {
230+
isResolved = true
231+
return v
232+
})
233+
234+
promise.catch(() => {
235+
console.log('caught 2')
236+
isRejected = true
237+
})
238+
229239
promise.isFulfilled = function() {
230240
return isResolved || isRejected
231241
}

src/views/WalletCheck.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@
149149
) => {
150150
for (const module of modules) {
151151
if (balanceSyncStatus.syncing) {
152-
await balanceSyncStatus.syncing.catch(() => {})
152+
try {
153+
await balanceSyncStatus.syncing
154+
} catch (error) {}
155+
153156
balanceSyncStatus.syncing = null
154157
}
155158

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
4747
// "typeRoots": [] /* List of folders to include type definitions from. */,
4848
"types": [
49-
"svelte",
50-
"node"
49+
"svelte"
5150
] /* Type declaration files to be included in compilation. */,
5251
"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
5352
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */

0 commit comments

Comments
 (0)