Skip to content

Commit c1fe4d0

Browse files
Merge pull request #15 from richardeschloss/development
Update scope of plugin options
2 parents b3bea19 + ce2a171 commit c1fe4d0

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
# Change Log
33
All notable changes to this project will be documented in this file.
44

5-
## [1.0.3] -
5+
## [1.0.4] - 2019-11-19
6+
7+
### Changed
8+
9+
- Only export plugin opts in TEST mode
10+
- TEST mode can get/set plugin opts from outside the plugin, non-TEST mode can only get plugin opts inside the plugin
11+
12+
## [1.0.3] - 2019-11-18
613

714
### Added
815

io/plugin.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@ import consola from 'consola'
77

88
function PluginOptions() {
99
let _pluginOptions
10-
return Object.freeze({
11-
get() {
12-
if (!process.env.TEST) {
13-
return <%= JSON.stringify(options) %>
14-
}
15-
return _pluginOptions
16-
},
17-
set(opts) {
18-
_pluginOptions = opts
19-
}
20-
})
10+
const svc = {}
11+
if (process.env.TEST) {
12+
svc.get = () => (_pluginOptions)
13+
svc.set = (opts) => _pluginOptions = opts
14+
} else {
15+
svc.get = () => (<%= JSON.stringify(options) %>)
16+
}
17+
return Object.freeze(svc)
2118
}
2219

23-
export const pOptions = PluginOptions()
20+
const _pOptions = PluginOptions()
2421

2522
function nuxtSocket(ioOpts) {
2623
const { name, channel = '', ...connectOpts } = ioOpts
27-
const pluginOptions = pOptions.get()
24+
const pluginOptions = _pOptions.get()
2825
const { sockets } = pluginOptions
2926
const { $store: store } = this
3027

@@ -130,3 +127,9 @@ function nuxtSocket(ioOpts) {
130127
export default function(context, inject) {
131128
inject('nuxtSocket', nuxtSocket)
132129
}
130+
131+
export let pOptions
132+
if (process.env.TEST) {
133+
pOptions = {}
134+
Object.assign(pOptions, _pOptions)
135+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuxt-socket-io",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Socket.io module for Nuxt. Just plug it in and GO",
55
"author": "Richard Schloss",
66
"main": "io/module.js",

test/specs/Plugin.spec.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs'
21
import path from 'path'
32
import consola from 'consola'
43
import { serial as test, before, after } from 'ava'
@@ -14,6 +13,18 @@ state.examples.__ob__ = ''
1413
const src = path.resolve('./io/plugin.js')
1514
const tmpFile = path.resolve('./io/plugin.compiled.js')
1615

16+
async function compile(t) {
17+
const compiled = await compilePlugin({ src, tmpFile, options: io }).catch(
18+
(err) => {
19+
consola.error(err)
20+
t.fail()
21+
}
22+
)
23+
Plugin = compiled.Plugin
24+
pOptions = compiled.pOptions
25+
t.pass()
26+
}
27+
1728
function loadPlugin(t, ioOpts = {}) {
1829
return new Promise((resolve, reject) => {
1930
const context = {}
@@ -50,17 +61,7 @@ function loadPlugin(t, ioOpts = {}) {
5061

5162
let Plugin, pOptions
5263

53-
before('Compile Plugin', async (t) => {
54-
const compiled = await compilePlugin({ src, tmpFile, options: io }).catch(
55-
(err) => {
56-
consola.error(err)
57-
t.fail()
58-
}
59-
)
60-
Plugin = compiled.Plugin
61-
pOptions = compiled.pOptions
62-
t.pass()
63-
})
64+
before('Compile Plugin', compile)
6465

6566
before('Init IO Server', ioServerInit)
6667

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

174175
test('Socket plugin (from nuxt.config)', async (t) => {
176+
delete require.cache[tmpFile]
175177
delete process.env.TEST
178+
await compile(t)
176179
const { ioServer } = t.context
177180
const testSocket = await loadPlugin(t, {
178181
name: 'test',

0 commit comments

Comments
 (0)