-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
Copy pathutils.ts
74 lines (65 loc) · 1.78 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import type { Config as CoreConfig } from "@ionic/core/components";
import {
LIFECYCLE_DID_ENTER,
LIFECYCLE_DID_LEAVE,
LIFECYCLE_WILL_ENTER,
LIFECYCLE_WILL_LEAVE,
} from "@ionic/core/components";
import type { Ref, ComponentPublicInstance } from "vue";
type LIFECYCLE_EVENTS =
| typeof LIFECYCLE_WILL_ENTER
| typeof LIFECYCLE_DID_ENTER
| typeof LIFECYCLE_WILL_LEAVE
| typeof LIFECYCLE_DID_LEAVE;
// TODO(FW-2969): types
export enum LifecycleHooks {
WillEnter = "onIonViewWillEnter",
DidEnter = "onIonViewDidEnter",
WillLeave = "onIonViewWillLeave",
DidLeave = "onIonViewDidLeave",
}
const hookNames = {
[LIFECYCLE_WILL_ENTER]: LifecycleHooks.WillEnter,
[LIFECYCLE_DID_ENTER]: LifecycleHooks.DidEnter,
[LIFECYCLE_WILL_LEAVE]: LifecycleHooks.WillLeave,
[LIFECYCLE_DID_LEAVE]: LifecycleHooks.DidLeave,
};
const ids: { [k: string]: number } = { main: 0 };
export const generateId = (type = "main") => {
const id = (ids[type] ?? 0) + 1;
ids[type] = id;
return id.toString();
};
export const fireLifecycle = (
vueComponent: any,
vueInstance: Ref<ComponentPublicInstance>,
lifecycle: LIFECYCLE_EVENTS
) => {
if (vueComponent?.[lifecycle]) {
vueComponent[lifecycle].bind(vueInstance?.value)();
}
const instance = vueInstance?.value as any;
if (instance?.[lifecycle]) {
instance[lifecycle]();
}
/**
* Fire any Composition API
* Ionic Lifecycle hooks
*/
if (instance) {
const hook = hookNames[lifecycle];
const hooks = instance[hook];
if (hooks) {
hooks.forEach((hook: Function) => hook());
}
}
};
export const getConfig = (): CoreConfig | null => {
if (typeof (window as any) !== "undefined") {
const Ionic = (window as any).Ionic;
if (Ionic && Ionic.config) {
return Ionic.config;
}
}
return null;
};