Skip to content

Commit

Permalink
Merge pull request #15 from richardeschloss/development
Browse files Browse the repository at this point in the history
Update scope of plugin options
  • Loading branch information
richardeschloss authored Nov 20, 2019
2 parents b3bea19 + ce2a171 commit c1fe4d0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
# Change Log
All notable changes to this project will be documented in this file.

## [1.0.3] -
## [1.0.4] - 2019-11-19

### Changed

- Only export plugin opts in TEST mode
- TEST mode can get/set plugin opts from outside the plugin, non-TEST mode can only get plugin opts inside the plugin

## [1.0.3] - 2019-11-18

### Added

Expand Down
29 changes: 16 additions & 13 deletions io/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@ import consola from 'consola'

function PluginOptions() {
let _pluginOptions
return Object.freeze({
get() {
if (!process.env.TEST) {
return <%= JSON.stringify(options) %>
}
return _pluginOptions
},
set(opts) {
_pluginOptions = opts
}
})
const svc = {}
if (process.env.TEST) {
svc.get = () => (_pluginOptions)
svc.set = (opts) => _pluginOptions = opts
} else {
svc.get = () => (<%= JSON.stringify(options) %>)
}
return Object.freeze(svc)
}

export const pOptions = PluginOptions()
const _pOptions = PluginOptions()

function nuxtSocket(ioOpts) {
const { name, channel = '', ...connectOpts } = ioOpts
const pluginOptions = pOptions.get()
const pluginOptions = _pOptions.get()
const { sockets } = pluginOptions
const { $store: store } = this

Expand Down Expand Up @@ -130,3 +127,9 @@ function nuxtSocket(ioOpts) {
export default function(context, inject) {
inject('nuxtSocket', nuxtSocket)
}

export let pOptions
if (process.env.TEST) {
pOptions = {}
Object.assign(pOptions, _pOptions)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt-socket-io",
"version": "1.0.3",
"version": "1.0.4",
"description": "Socket.io module for Nuxt. Just plug it in and GO",
"author": "Richard Schloss",
"main": "io/module.js",
Expand Down
27 changes: 15 additions & 12 deletions test/specs/Plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from 'fs'
import path from 'path'
import consola from 'consola'
import { serial as test, before, after } from 'ava'
Expand All @@ -14,6 +13,18 @@ state.examples.__ob__ = ''
const src = path.resolve('./io/plugin.js')
const tmpFile = path.resolve('./io/plugin.compiled.js')

async function compile(t) {
const compiled = await compilePlugin({ src, tmpFile, options: io }).catch(
(err) => {
consola.error(err)
t.fail()
}
)
Plugin = compiled.Plugin
pOptions = compiled.pOptions
t.pass()
}

function loadPlugin(t, ioOpts = {}) {
return new Promise((resolve, reject) => {
const context = {}
Expand Down Expand Up @@ -50,17 +61,7 @@ function loadPlugin(t, ioOpts = {}) {

let Plugin, pOptions

before('Compile Plugin', async (t) => {
const compiled = await compilePlugin({ src, tmpFile, options: io }).catch(
(err) => {
consola.error(err)
t.fail()
}
)
Plugin = compiled.Plugin
pOptions = compiled.pOptions
t.pass()
})
before('Compile Plugin', compile)

before('Init IO Server', ioServerInit)

Expand Down Expand Up @@ -172,7 +173,9 @@ test('Socket plugin (malformed emitBacks)', async (t) => {
})

test('Socket plugin (from nuxt.config)', async (t) => {
delete require.cache[tmpFile]
delete process.env.TEST
await compile(t)
const { ioServer } = t.context
const testSocket = await loadPlugin(t, {
name: 'test',
Expand Down

0 comments on commit c1fe4d0

Please sign in to comment.