Skip to content

Commit c31df02

Browse files
refactor: migrate cordova-web to TS and support async init (#6)
* refactor: migrate cordova-web to TS and support async init * fix: fix window typing * fix: sync dist
1 parent 4068359 commit c31df02

File tree

14 files changed

+305
-227
lines changed

14 files changed

+305
-227
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,4 @@ Thumbs.db
113113

114114
# Cordova specific
115115
platforms/
116-
plugins/
117116
www/

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"printWidth": 120,
33
"useTabs": true,
4+
"tabWidth": 4,
45
"semi": false,
56
"singleQuote": true
67
}

cordova-web.js

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

dist/index.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@
3939
"prettier": "3.7.3",
4040
"tsup": "^8.5.1",
4141
"typescript": "^5.9.3"
42+
},
43+
"dependencies": {
44+
"@open-condo/bridge": "^2.3.0"
4245
}
4346
}

src/cordova/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { CordovaWebPlugin } from './types'
2+
3+
export class CordovaWeb {
4+
platformId = 'web'
5+
plugins: Record<string, CordovaWebPlugin> = {}
6+
7+
constructor(plugins: Array<CordovaWebPlugin>) {
8+
for (const plugin of plugins) {
9+
this.plugins[plugin.name] = plugin
10+
}
11+
}
12+
13+
async _init() {
14+
const initFns = []
15+
for (const plugin of Object.values(this.plugins)) {
16+
if (plugin._init) {
17+
initFns.push(plugin._init())
18+
}
19+
}
20+
await Promise.allSettled(initFns)
21+
}
22+
}

src/cordova/plugins/condo.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import bridge from '@open-condo/bridge'
2+
import { wrapPromiseWithCallbacks, sendCordovaMessage } from '../utils'
3+
import type { CordovaWebPlugin, SuccessCallback, ErrorCallback } from '../types'
4+
5+
export class CondoWebPlugin implements CordovaWebPlugin {
6+
name = 'condo'
7+
8+
requestServerAuthorizationByUrl(
9+
url: string,
10+
_options: Record<string, never>,
11+
success: SuccessCallback,
12+
error: ErrorCallback,
13+
) {
14+
wrapPromiseWithCallbacks(
15+
bridge.send('CondoWebAppRequestAuth', { url }).then((data) => data.response),
16+
success,
17+
error,
18+
)
19+
}
20+
21+
// TODO: deprecate this method, since each app must request its own fields via API after auth
22+
getCurrentResident(success: SuccessCallback, error: ErrorCallback) {
23+
wrapPromiseWithCallbacks(
24+
sendCordovaMessage('condo-cordova-legacy', 'CondoWebAppGetCurrentResident', {}, 10_000),
25+
success,
26+
error,
27+
)
28+
}
29+
30+
// TODO: implement me later?
31+
closeApplication(success: SuccessCallback, _error: ErrorCallback) {
32+
success(true)
33+
}
34+
35+
getLaunchContext(success: SuccessCallback, error: ErrorCallback) {
36+
wrapPromiseWithCallbacks(
37+
bridge.send('CondoWebAppGetFragment').then((data) => data.fragment ?? null),
38+
success,
39+
error,
40+
)
41+
}
42+
43+
setInputsEnabled(value: boolean, success: SuccessCallback, _error: ErrorCallback) {
44+
success(value)
45+
}
46+
47+
// TODO: migrate to bridge and add docs, since it will be stable API
48+
history = {
49+
pushState(state: unknown, title: string, success: SuccessCallback, error: ErrorCallback) {
50+
wrapPromiseWithCallbacks(
51+
sendCordovaMessage('condo-cordova', 'CondoWebAppPushHistoryState', {
52+
state,
53+
title,
54+
}),
55+
success,
56+
error,
57+
)
58+
},
59+
replaceState(state: unknown, title: string, success: SuccessCallback, error: ErrorCallback) {
60+
wrapPromiseWithCallbacks(
61+
sendCordovaMessage('condo-cordova', 'CondoWebAppReplaceHistoryState', {
62+
state,
63+
title,
64+
}),
65+
success,
66+
error,
67+
)
68+
},
69+
back(success: SuccessCallback, error: ErrorCallback) {
70+
wrapPromiseWithCallbacks(
71+
sendCordovaMessage('condo-cordova', 'CondoWebAppPopHistoryState', {
72+
amount: 1,
73+
}),
74+
success,
75+
error,
76+
)
77+
},
78+
go(amount: number, success: SuccessCallback, error: ErrorCallback) {
79+
wrapPromiseWithCallbacks(
80+
sendCordovaMessage('condo-cordova', 'CondoWebAppPopHistoryState', {
81+
amount: -amount,
82+
}),
83+
success,
84+
error,
85+
)
86+
},
87+
}
88+
}

src/cordova/plugins/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { CondoWebPlugin } from './condo'

src/cordova/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface CordovaWebPlugin {
2+
name: string
3+
_init?(): Promise<void>
4+
}
5+
6+
export type SuccessCallback = (result: unknown) => void
7+
export type ErrorCallback = (err: Error) => void

0 commit comments

Comments
 (0)