@streamlayer/sdk-web-notifications 0.14.2 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +1 -0
- package/lib/index.js +7 -1
- package/lib/notifications.d.ts +3 -0
- package/lib/notifications.js +29 -0
- package/lib/queue/index.d.ts +2 -2
- package/lib/queue/index.js +17 -10
- package/package.json +7 -7
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -4,8 +4,14 @@ export { NotificationType, Notifications } from './notifications';
|
|
|
4
4
|
* notifications plugin, connect notifications to sdk
|
|
5
5
|
*/
|
|
6
6
|
export const notifications = (instance, opts, done) => {
|
|
7
|
-
instance.notifications = new Notifications();
|
|
7
|
+
instance.notifications = new Notifications(undefined);
|
|
8
8
|
instance.addNotification = instance.notifications.add;
|
|
9
9
|
instance.sdk.getNotificationsStore = () => instance.notifications.getQueueStore();
|
|
10
|
+
instance.sdk.getActiveNotification = () => instance.notifications.getActiveNotification();
|
|
11
|
+
instance.sdk.onMount(() => {
|
|
12
|
+
return () => {
|
|
13
|
+
instance.notifications.queue.drain();
|
|
14
|
+
};
|
|
15
|
+
});
|
|
10
16
|
done();
|
|
11
17
|
};
|
package/lib/notifications.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { InstantView, QuestionType, GameSettings, TweetHistory } from '@streamlayer/sdk-web-types';
|
|
2
2
|
import { NotificationsQueue, NotificationsQueueOptions } from './queue';
|
|
3
3
|
export type NotificationData = {
|
|
4
|
+
questionId: string;
|
|
4
5
|
questionType: QuestionType;
|
|
5
6
|
question?: {
|
|
6
7
|
title: string;
|
|
@@ -40,6 +41,7 @@ export type Notification<M extends Record<string, Function> = never> = {
|
|
|
40
41
|
action?: (...args: unknown[]) => void;
|
|
41
42
|
close?: (...args: unknown[]) => void;
|
|
42
43
|
methods?: M;
|
|
44
|
+
emitEvent?: boolean;
|
|
43
45
|
data: NotificationData;
|
|
44
46
|
id: string;
|
|
45
47
|
persistent?: boolean;
|
|
@@ -53,6 +55,7 @@ export declare class Notifications {
|
|
|
53
55
|
constructor(options?: Partial<NotificationsQueueOptions>);
|
|
54
56
|
add: (notification: Notification) => void;
|
|
55
57
|
close: (notificationId: string, markAsViewed?: boolean) => void;
|
|
58
|
+
getActiveNotification: () => Notification | null;
|
|
56
59
|
getQueueStore: () => import("nanostores").WritableAtom<Map<string, Notification> | undefined>;
|
|
57
60
|
markAsViewed: (notificationId: string) => void;
|
|
58
61
|
}
|
package/lib/notifications.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { eventBus } from '@streamlayer/sdk-web-interfaces';
|
|
1
2
|
import { NotificationsQueue } from './queue';
|
|
2
3
|
import { NotificationStorage } from './storage';
|
|
3
4
|
export var NotificationType;
|
|
@@ -19,6 +20,21 @@ export class Notifications {
|
|
|
19
20
|
add = (notification) => {
|
|
20
21
|
const isViewed = this.storage.isOpened(notification.id);
|
|
21
22
|
if (!isViewed) {
|
|
23
|
+
if (notification.data && notification.emitEvent) {
|
|
24
|
+
const action = notification.action;
|
|
25
|
+
notification.action = (...args) => {
|
|
26
|
+
if (action) {
|
|
27
|
+
action(...args);
|
|
28
|
+
}
|
|
29
|
+
eventBus.emit('notification', {
|
|
30
|
+
action: 'opened',
|
|
31
|
+
payload: {
|
|
32
|
+
questionId: notification.data.questionId,
|
|
33
|
+
questionType: notification.data.questionType,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
}
|
|
22
38
|
this.queue.addToQueue(notification);
|
|
23
39
|
}
|
|
24
40
|
};
|
|
@@ -28,6 +44,19 @@ export class Notifications {
|
|
|
28
44
|
this.markAsViewed(notificationId);
|
|
29
45
|
}
|
|
30
46
|
};
|
|
47
|
+
getActiveNotification = () => {
|
|
48
|
+
const notifications = this.queue.notificationsList.get();
|
|
49
|
+
if (!notifications?.size) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
for (const notification of notifications.values()) {
|
|
53
|
+
if (notification.type === NotificationType.ONBOARDING) {
|
|
54
|
+
return notification;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const notification = notifications.values().next().value;
|
|
58
|
+
return notification;
|
|
59
|
+
};
|
|
31
60
|
getQueueStore = () => {
|
|
32
61
|
return this.queue.notificationsList;
|
|
33
62
|
};
|
package/lib/queue/index.d.ts
CHANGED
|
@@ -7,7 +7,6 @@ export type NotificationsQueueOptions = {
|
|
|
7
7
|
export type NotificationsList = ReturnType<typeof createComputedStore<Notification[]>>;
|
|
8
8
|
export declare class NotificationsQueue {
|
|
9
9
|
notificationsList: ReturnType<SingleStore<Map<Notification['id'], Notification>>['getStore']>;
|
|
10
|
-
private notifications;
|
|
11
10
|
private store;
|
|
12
11
|
private timeouts;
|
|
13
12
|
private waitingQueue;
|
|
@@ -18,5 +17,6 @@ export declare class NotificationsQueue {
|
|
|
18
17
|
addToQueue: (notification: Notification) => void;
|
|
19
18
|
tickWaitingQueue: () => void;
|
|
20
19
|
tickActiveQueue: (notificationId: string) => void;
|
|
21
|
-
closeNotification: (notificationId: string) =>
|
|
20
|
+
closeNotification: (notificationId: string) => Notification | undefined;
|
|
21
|
+
drain: () => void;
|
|
22
22
|
}
|
package/lib/queue/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createSingleStore } from '@streamlayer/sdk-web-interfaces';
|
|
2
2
|
import { createLogger } from '@streamlayer/sdk-web-logger';
|
|
3
3
|
export class NotificationsQueue {
|
|
4
4
|
notificationsList;
|
|
5
|
-
notifications;
|
|
6
5
|
store;
|
|
7
6
|
timeouts;
|
|
8
7
|
waitingQueue;
|
|
@@ -16,8 +15,7 @@ export class NotificationsQueue {
|
|
|
16
15
|
this.timeouts = new Map();
|
|
17
16
|
this.waitingQueue = new Set();
|
|
18
17
|
this.activeQueue = new Set();
|
|
19
|
-
this.
|
|
20
|
-
this.notificationsList = this.notifications.getStore();
|
|
18
|
+
this.notificationsList = createSingleStore(new Map());
|
|
21
19
|
}
|
|
22
20
|
addToQueue = (notification) => {
|
|
23
21
|
if (this.store.has(notification.id)) {
|
|
@@ -90,9 +88,9 @@ export class NotificationsQueue {
|
|
|
90
88
|
}
|
|
91
89
|
const timeout = setTimeout(() => {
|
|
92
90
|
const closureId = notificationId;
|
|
93
|
-
const prevQueue = new Map(this.
|
|
91
|
+
const prevQueue = new Map(this.notificationsList.get());
|
|
94
92
|
prevQueue.set(notification.id, notification);
|
|
95
|
-
this.
|
|
93
|
+
this.notificationsList.set(prevQueue);
|
|
96
94
|
const timeout = setTimeout(() => {
|
|
97
95
|
this.logger.debug({ notificationId: closureId, delay: notification.autoHideDuration || 5000 }, 'notification hiding by autoHideDuration');
|
|
98
96
|
this.closeNotification(closureId);
|
|
@@ -105,17 +103,17 @@ export class NotificationsQueue {
|
|
|
105
103
|
this.tickWaitingQueue();
|
|
106
104
|
};
|
|
107
105
|
closeNotification = (notificationId) => {
|
|
108
|
-
const prevQueue = new Map(this.
|
|
106
|
+
const prevQueue = new Map(this.notificationsList.get());
|
|
109
107
|
const notification = prevQueue.get(notificationId);
|
|
110
108
|
if (notification) {
|
|
111
109
|
// do not hide notification if we have more than one notification in waiting queue,
|
|
112
110
|
// because we need to show next notification immediately
|
|
113
111
|
notification.hiding = !(this.waitingQueue.size >= this.options.concurrency);
|
|
114
|
-
this.
|
|
112
|
+
this.notificationsList.set(prevQueue);
|
|
115
113
|
const timeout = setTimeout(() => {
|
|
116
|
-
const prevQueue = new Map(this.
|
|
114
|
+
const prevQueue = new Map(this.notificationsList.get());
|
|
117
115
|
prevQueue.delete(notificationId);
|
|
118
|
-
this.
|
|
116
|
+
this.notificationsList.set(prevQueue);
|
|
119
117
|
const timeout = this.timeouts.get(notificationId);
|
|
120
118
|
if (timeout !== undefined) {
|
|
121
119
|
clearTimeout(timeout);
|
|
@@ -130,5 +128,14 @@ export class NotificationsQueue {
|
|
|
130
128
|
this.waitingQueue.delete(notificationId);
|
|
131
129
|
this.tickWaitingQueue();
|
|
132
130
|
this.logger.debug({ notificationId }, 'notification hiding');
|
|
131
|
+
return notification;
|
|
132
|
+
};
|
|
133
|
+
drain = () => {
|
|
134
|
+
this.store.clear();
|
|
135
|
+
this.timeouts.clear();
|
|
136
|
+
this.waitingQueue.clear();
|
|
137
|
+
this.activeQueue.clear();
|
|
138
|
+
this.notificationsList.off();
|
|
139
|
+
this.notificationsList.set(new Map());
|
|
133
140
|
};
|
|
134
141
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamlayer/sdk-web-notifications",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"typings": "./lib/index.d.ts",
|
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
"package.json"
|
|
10
10
|
],
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@streamlayer/sdk-web-interfaces": "^0.
|
|
13
|
-
"@streamlayer/sdk-web-logger": "^0.5.
|
|
14
|
-
"@streamlayer/sdk-web-
|
|
15
|
-
"@streamlayer/sdk-web-
|
|
12
|
+
"@streamlayer/sdk-web-interfaces": "^0.21.0",
|
|
13
|
+
"@streamlayer/sdk-web-logger": "^0.5.18",
|
|
14
|
+
"@streamlayer/sdk-web-storage": "^0.4.5",
|
|
15
|
+
"@streamlayer/sdk-web-types": "^0.23.0"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@nx/playwright": "
|
|
26
|
-
"@nx/webpack": "
|
|
25
|
+
"@nx/playwright": "17.3.0",
|
|
26
|
+
"@nx/webpack": "17.3.0",
|
|
27
27
|
"@playwright/test": "^1.41.1",
|
|
28
28
|
"tslib": "^2.6.2",
|
|
29
29
|
"webpack": "^5.90.0"
|