diff --git a/src/app.js b/src/app.js index ac89973..23c07f4 100644 --- a/src/app.js +++ b/src/app.js @@ -86,17 +86,21 @@ const PerfectyPush = (() => { const askPermissionsDirectly = async () => { if (Permission.askedAlready()) { // if we already have asked for permissions, we don't ask again - return true + return Permission.isGranted() } await Permission.askIfNotDenied() - if (Permission.isGranted()) { - Logger.info('User has granted permissions') - - const userId = Storage.userId() - await ServiceInstaller.installIfMissing() - await Registration.register(userId, true) + if (!Permission.isGranted()) { + return false } + + Logger.info('User has granted permissions') + + const userId = Storage.userId() + await ServiceInstaller.installIfMissing() + await Registration.register(userId, true) + + return true } const checkInstallation = async () => { @@ -112,8 +116,35 @@ const PerfectyPush = (() => { await Registration.check(Storage.userId(), Storage.optedOut()) } + const register = async () => { + Options.askPermissionsDirectly = true + Storage.setOptedOut(false) + + if (!Permission.isGranted()) { + return askPermissionsDirectly() + } + + const userId = Storage.userId() + await ServiceInstaller.installIfMissing() + return Registration.register(userId, true) + } + + const unregister = async () => { + const userId = Storage.userId() + await Registration.unregister(userId) + Storage.setOptedOut(true) + await ServiceInstaller.removeInstallation() + } + + const isRegister = () => { + return Permission.isGranted() && Storage.optedOut() === false + } + return { - start + start, + register, + unregister, + isRegister } })() diff --git a/webpack.config.js b/webpack.config.js index 014ffea..e3d5961 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -3,8 +3,15 @@ const ESLintPlugin = require('eslint-webpack-plugin') module.exports = { entry: { - 'perfecty-push-sdk': './src/app.js', - 'perfecty-push-sw': './src/service-worker.js' + 'perfecty-push-sdk': { + import: './src/app.js', + library: { + name: 'PerfectyPush', + type: 'var', + export: 'default' + } + }, + 'perfecty-push-sw': './src/service-worker.js', }, mode: 'development', devtool: 'source-map', diff --git a/webpack.prod.config.js b/webpack.prod.config.js index 1715685..20d0c56 100644 --- a/webpack.prod.config.js +++ b/webpack.prod.config.js @@ -2,7 +2,14 @@ const path = require('path') module.exports = { entry: { - 'perfecty-push-sdk': './src/app.js', + 'perfecty-push-sdk': { + import: './src/app.js', + library: { + name: 'PerfectyPush', + type: 'var', + export: 'default' + } + }, 'perfecty-push-sw': './src/service-worker.js' }, mode: 'production',