chore: update

This commit is contained in:
Benjamin Canac
2021-12-09 17:46:13 +01:00
parent 637450495c
commit f3f40dd862
14 changed files with 219 additions and 20 deletions

View File

@@ -0,0 +1,19 @@
import { defineNuxtPlugin } from '#app'
function Ticker (callback, interval) {
let id
this.start = function () {
id = window.setInterval(callback, interval)
}
this.stop = function () {
window.clearInterval(id)
}
this.start()
}
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.provide('ticker', Ticker)
})

View File

@@ -0,0 +1,34 @@
import { defineNuxtPlugin } from '#app'
function Timer (callback, delay) {
let timerId
let start
let remaining = delay
this.pause = function () {
window.clearTimeout(timerId)
remaining -= new Date() - start
}
this.resume = function () {
start = new Date()
window.clearTimeout(timerId)
timerId = window.setTimeout(callback, remaining)
}
this.reset = function () {
start = new Date()
window.clearTimeout(timerId)
timerId = window.setTimeout(callback, delay)
}
this.stop = function () {
window.clearTimeout(timerId)
}
this.resume()
}
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.provide('timer', Timer)
})

View File

@@ -0,0 +1,53 @@
import { nanoid } from 'nanoid'
import { Ref } from 'vue'
import { Notification } from '../types'
import { defineNuxtPlugin, useState } from '#app'
export default defineNuxtPlugin((nuxtApp) => {
const notifications: Ref<Notification[]> = useState('notifications', () => [])
function addNotification (notification: Partial<Notification>) {
const body = {
id: nanoid(),
...notification
}
const index = notifications.value.findIndex((n: Notification) => n.id === body.id)
if (index === -1) {
notifications.value.push(body as Notification)
}
return body
}
function removeNotification (id: string) {
notifications.value = notifications.value.filter((n: Notification) => n.id !== id)
}
nuxtApp.provide('toast', {
addNotification,
removeNotification,
success ({ title, description }: { title?: string, description?: string } = {}) {
addNotification({
type: 'success',
title,
description,
timeout: 4000
})
},
error ({ title = 'An error occurred!', description }: { title?: string, description?: string } = {}) {
addNotification({
type: 'error',
title,
description,
timeout: 4000
})
}
})
})
declare module '#app' {
interface NuxtApp {
$toast: object
}
}