tamaina 2021-03-24 02:15:26 +09:00
parent ba315b7765
commit 745eecae56
3 changed files with 22 additions and 22 deletions

View File

@ -94,12 +94,18 @@ export default defineComponent({
getNoteSummary: (text: string) => getNoteSummary(text, i18n.locale), getNoteSummary: (text: string) => getNoteSummary(text, i18n.locale),
followRequestDone: false, followRequestDone: false,
groupInviteDone: false, groupInviteDone: false,
connection: null, readObserver: null as IntersectionObserver | null,
readObserver: null,
faIdCardAlt, faPlus, faQuoteLeft, faQuoteRight, faRetweet, faReply, faAt, faClock, faCheck, faPollH faIdCardAlt, faPlus, faQuoteLeft, faQuoteRight, faRetweet, faReply, faAt, faClock, faCheck, faPollH
}; };
}, },
watch: {
'notification.isRead'() {
this.readObserver?.unobserve(this.$el);
this.readObserver = null;
},
},
mounted() { mounted() {
if (!this.notification.isRead) { if (!this.notification.isRead) {
this.readObserver = new IntersectionObserver((entries, observer) => { this.readObserver = new IntersectionObserver((entries, observer) => {
@ -107,30 +113,19 @@ export default defineComponent({
os.stream.send('readNotification', { os.stream.send('readNotification', {
id: this.notification.id id: this.notification.id
}); });
entries.map(({ target }) => observer.unobserve(target)); for (const { target } of entries) {
observer.unobserve(target);
}
this.readObserver = null;
}); });
this.readObserver.observe(this.$el); this.readObserver.observe(this.$el);
this.connection = os.stream.useSharedConnection('main');
//
// notification.isRead
this.connection.on('readAllNotifications', () => {
this.readObserver.unobserve(this.$el);
});
this.connection.on('readNotifications', notificationIds => {
if (notificationIds.includes(this.notification.id)) {
this.readObserver.unobserve(this.$el);
}
})
} }
}, },
beforeUnmount() { beforeUnmount() {
if (!this.notification.isRead) { if (!this.notification.isRead) {
this.readObserver.unobserve(this.$el); this.readObserver?.unobserve(this.$el);
this.connection.dispose();
} }
}, },

View File

@ -87,9 +87,11 @@ export default defineComponent({
this.connection = os.stream.useSharedConnection('main'); this.connection = os.stream.useSharedConnection('main');
this.connection.on('notification', this.onNotification); this.connection.on('notification', this.onNotification);
// queue
this.connection.on('readAllNotifications', () => { this.connection.on('readAllNotifications', () => {
this.queue = this.queue.map(x => markNotificationRead(x)); this.queue = this.queue.map(x => markNotificationRead(x));
for (const item of this.items) {
item.isRead = true;
}
}); });
this.connection.on('readNotifications', notificationIds => { this.connection.on('readNotifications', notificationIds => {
if (this.queue.length === 0) return; if (this.queue.length === 0) return;
@ -98,6 +100,9 @@ export default defineComponent({
if (notificationIds.includes(this.queue[i].id)) { if (notificationIds.includes(this.queue[i].id)) {
this.queue[i] = markNotificationRead(this.queue[i]); this.queue[i] = markNotificationRead(this.queue[i]);
} }
if (notificationIds.includes(this.items[i].id)) {
this.items[i].isRead = true;
}
} }
}); });
}, },

View File

@ -51,11 +51,11 @@ export default defineComponent({
const connection = stream.useSharedConnection('main', 'UI'); const connection = stream.useSharedConnection('main', 'UI');
connection.on('notification', onNotification); connection.on('notification', onNotification);
//#region Listen message from SW
if ('serviceWorker' in navigator) {
const navHook = inject('navHook', null); const navHook = inject('navHook', null);
const sideViewHook = inject('sideViewHook', null); const sideViewHook = inject('sideViewHook', null);
//#region Listen message from SW
if ('serviceWorker' in navigator) {
navigator.serviceWorker.addEventListener('message', ev => { navigator.serviceWorker.addEventListener('message', ev => {
if (_DEV_) { if (_DEV_) {
console.log('sw msg', ev.data); console.log('sw msg', ev.data);