Skip to content

Commit bdfa2e5

Browse files
authored
refactor(web): registering plugins and lazy loading (#102)
* refactor(web): registering plugins with types * feat(web): add lazyload * feat(web): add init hook * refactor(web): rename fn name by context * refactor(web): clean config
1 parent 8419f15 commit bdfa2e5

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

src/definitions.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { Authentication, User } from "./user";
22

3-
declare module "@capacitor/core" {
4-
interface PluginRegistry {
5-
GoogleAuth: GoogleAuthPlugin;
6-
}
7-
}
8-
93
export interface GoogleAuthPlugin {
104
signIn(): Promise<User>;
115
refresh(): Promise<Authentication>;
126
signOut(): Promise<any>;
7+
8+
/** Init hook for load gapi and init plugin */
9+
init(): void;
1310
}

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
import { registerPlugin } from '@capacitor/core';
2+
import type { GoogleAuthPlugin } from './definitions';
3+
4+
const GoogleAuth = registerPlugin<GoogleAuthPlugin>('GoogleAuth', {
5+
web: () => import('./web').then(m => new m.GoogleAuthWeb()),
6+
});
7+
18
export * from './definitions';
2-
export * from './web';
9+
export { GoogleAuth };

src/web.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { registerPlugin, WebPlugin } from '@capacitor/core';
1+
import { WebPlugin } from '@capacitor/core';
22
import { GoogleAuthPlugin } from './definitions';
33
import { User, Authentication } from './user';
44

@@ -18,24 +18,10 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
1818
}
1919

2020
constructor() {
21-
super({
22-
name: 'GoogleAuth',
23-
platforms: ['web']
24-
});
25-
26-
if (!this.webConfigured)
27-
return;
28-
29-
this.gapiLoaded = new Promise(resolve => {
30-
// HACK: Relying on window object, can't get property in gapi.load callback
31-
(window as any).gapiResolve = resolve;
32-
this.initialize();
33-
});
34-
35-
this.addUserChangeListener();
21+
super();
3622
}
3723

38-
initialize() {
24+
loadScript() {
3925
var head = document.getElementsByTagName('head')[0];
4026
var script = document.createElement('script');
4127
script.type = 'text/javascript';
@@ -46,6 +32,19 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
4632
head.appendChild(script);
4733
}
4834

35+
init(){
36+
if (!this.webConfigured)
37+
return;
38+
39+
this.gapiLoaded = new Promise(resolve => {
40+
// HACK: Relying on window object, can't get property in gapi.load callback
41+
(window as any).gapiResolve = resolve;
42+
this.loadScript();
43+
});
44+
45+
this.addUserChangeListener();
46+
}
47+
4948
platformJsLoaded() {
5049
gapi.load('auth2', () => {
5150
const clientConfig: gapi.auth2.ClientConfig = {
@@ -135,8 +134,3 @@ export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin {
135134
return user;
136135
}
137136
}
138-
139-
const GoogleAuth = registerPlugin('GoogleAuth', { web: new GoogleAuthWeb() });
140-
141-
export { GoogleAuth };
142-

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"dom",
88
"es2015"
99
],
10-
"module": "es2015",
10+
"module": "ESNext",
1111
"moduleResolution": "node",
1212
"noImplicitAny": true,
1313
"noUnusedLocals": true,
1414
"noUnusedParameters": true,
1515
"outDir": "dist/esm",
1616
"sourceMap": true,
17-
"target": "es2015"
17+
"target": "es2017"
1818
},
1919
"files": [
2020
"src/index.ts"

0 commit comments

Comments
 (0)