fix: various fixes
This commit is contained in:
@@ -38,6 +38,7 @@
|
||||
"must": "^0.13.2",
|
||||
"rimraf": "^2.5.4",
|
||||
"standard": "^8.6.0",
|
||||
"xo-collection": "^0.4.1",
|
||||
"xo-lib": "^0.8.2"
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must
|
||||
import expect from 'must'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
import {createUser, deleteUsers, getConnection, getMainConnection, getUser} from './util'
|
||||
import {
|
||||
createUser,
|
||||
deleteUsers,
|
||||
getConnection,
|
||||
getMainConnection,
|
||||
getUser
|
||||
} from './util'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
@@ -17,6 +18,8 @@ describe('user', () => {
|
||||
xo = await getMainConnection()
|
||||
})
|
||||
|
||||
afterAll(() => xo.close())
|
||||
|
||||
afterEach(async () => {
|
||||
await deleteUsers(xo, userIds)
|
||||
userIds = []
|
||||
@@ -28,12 +31,13 @@ describe('user', () => {
|
||||
it.only('creates an user and returns its id', async () => {
|
||||
const userId = await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'batman'})
|
||||
password: 'batman'
|
||||
})
|
||||
|
||||
expect(userId).to.be.a.string()
|
||||
expect(typeof userId).toBe('string')
|
||||
|
||||
const user = await getUser(xo, userId)
|
||||
expect(user).to.be.eql({
|
||||
expect(user).toEqual({
|
||||
id: userId,
|
||||
email: 'wayne@vates.fr',
|
||||
groups: [],
|
||||
|
||||
51
src/util.js
51
src/util.js
@@ -1,5 +1,6 @@
|
||||
import expect from 'must'
|
||||
import Xo from 'xo-lib'
|
||||
import XoCollection from 'xo-collection'
|
||||
import {find, forEach, map, once, cloneDeep} from 'lodash'
|
||||
|
||||
export async function getConfig () {
|
||||
@@ -56,31 +57,47 @@ export async function getConnection ({
|
||||
//
|
||||
// TODO: integrate in xo-lib.
|
||||
const watchers = {}
|
||||
xo.waitObject = id => {
|
||||
return new Promise(resolve => {
|
||||
watchers[id] = resolve
|
||||
})
|
||||
}
|
||||
const waitObject = xo.waitObject = id => new Promise(resolve => {
|
||||
watchers[id] = resolve
|
||||
}) // FIXME: work with multiple listeners.
|
||||
|
||||
const onUpdate = objects => {
|
||||
forEach(objects, (object, id) => {
|
||||
if (watchers[id]) {
|
||||
watchers[id](object)
|
||||
const objects = xo.objects = new XoCollection()
|
||||
xo.on('notification', ({ method, params }) => {
|
||||
if (method !== 'all') {
|
||||
return
|
||||
}
|
||||
|
||||
const fn = params.type === 'exit'
|
||||
? objects.unset
|
||||
: objects.set
|
||||
|
||||
forEach(params.items, (item, id) => {
|
||||
fn.call(objects, id, item)
|
||||
|
||||
const watcher = watchers[id]
|
||||
if (watcher) {
|
||||
watcher(item)
|
||||
delete watchers[id]
|
||||
}
|
||||
})
|
||||
}
|
||||
xo.objects.on('add', onUpdate)
|
||||
xo.objects.on('update', onUpdate)
|
||||
xo.objects.on('remove', onUpdate)
|
||||
})
|
||||
forEach(await xo.call('xo.getAllObjects'), (object, id) => {
|
||||
objects.set(id, object)
|
||||
|
||||
xo.getOrWaitObject = async function (id) {
|
||||
const object = this.objects.all[id]
|
||||
const watcher = watchers[id]
|
||||
if (watcher) {
|
||||
watcher(object)
|
||||
delete watchers[id]
|
||||
}
|
||||
})
|
||||
|
||||
xo.getOrWaitObject = async id => {
|
||||
const object = objects.all[id]
|
||||
if (object) {
|
||||
return object
|
||||
}
|
||||
|
||||
return await this.waitObject(id)
|
||||
return waitObject(id)
|
||||
}
|
||||
|
||||
return xo
|
||||
@@ -96,7 +113,7 @@ export async function getAllUsers (xo) {
|
||||
|
||||
export async function getUser (xo, id) {
|
||||
const users = await getAllUsers(xo)
|
||||
return find(users, {id: id})
|
||||
return find(users, { id })
|
||||
}
|
||||
|
||||
export async function createUser (xo, userIds, params) {
|
||||
|
||||
Reference in New Issue
Block a user