-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathUserNamespace.ts
More file actions
125 lines (100 loc) · 3.22 KB
/
UserNamespace.ts
File metadata and controls
125 lines (100 loc) · 3.22 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import type { UserChangeEvent } from '../page/models/UserChangeEvent';
import { EventListenerBase } from '../page/userModel/EventListenerBase';
import Emitter from '../shared/libraries/Emitter';
import { Subscription } from '../shared/models/Subscription';
import PushSubscriptionNamespace from './PushSubscriptionNamespace';
import User from './User';
export default class UserNamespace extends EventListenerBase {
private _currentUser?: User;
readonly PushSubscription = new PushSubscriptionNamespace(false);
static emitter = new Emitter();
constructor(
initialize: boolean,
subscription?: Subscription,
permission?: NotificationPermission,
) {
super();
if (initialize) {
this._currentUser = User.createOrGetInstance();
this.PushSubscription = new PushSubscriptionNamespace(
true,
subscription,
permission,
);
}
}
/* P U B L I C A P I */
get onesignalId(): string | undefined {
return this._currentUser?.onesignalId;
}
get externalId(): string | undefined {
const identityModel = OneSignal.coreDirector.getIdentityModel();
return identityModel?.externalId;
}
public addAlias(label: string, id: string): void {
this._currentUser?.addAlias(label, id);
}
public addAliases(aliases: { [key: string]: string }): void {
this._currentUser?.addAliases(aliases);
}
public removeAlias(label: string): void {
this._currentUser?.removeAlias(label);
}
public removeAliases(labels: string[]): void {
this._currentUser?.removeAliases(labels);
}
public addEmail(email: string): void {
this._currentUser?.addEmail(email);
}
public removeEmail(email: string): void {
this._currentUser?.removeEmail(email);
}
public addSms(smsNumber: string): void {
this._currentUser?.addSms(smsNumber);
}
public removeSms(smsNumber: string): void {
this._currentUser?.removeSms(smsNumber);
}
public addTag(key: string, value: string): void {
this._currentUser?.addTag(key, value);
}
public addTags(tags: { [key: string]: string }): void {
this._currentUser?.addTags(tags);
}
public removeTag(key: string): void {
this._currentUser?.removeTag(key);
}
public removeTags(keys: string[]): void {
this._currentUser?.removeTags(keys);
}
public getTags(): { [key: string]: string } {
return this._currentUser?.getTags() || {};
}
public setLanguage(language: string): void {
this._currentUser?.setLanguage(language);
}
public getLanguage(): string {
return this._currentUser?.getLanguage() || '';
}
/**
* Track a custom event. Note that this will be queued until the user is logged in or accepts notifications permissions.
*
* @param name - The name of the event.
* @param properties - The properties of the event.
*/
public trackEvent(name: string, properties?: Record<string, unknown>) {
return this._currentUser?.trackEvent(name, properties);
}
addEventListener(
event: 'change',
listener: (userChange: UserChangeEvent) => void,
): void {
UserNamespace.emitter.on(event, listener);
}
removeEventListener(
event: 'change',
listener: (userChange: UserChangeEvent) => void,
): void {
UserNamespace.emitter.off(event, listener);
}
}