Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Commit 9a0981e

Browse files
committed
Merge pull request #11 from feathersjs/instantiate-client
Allow to instantiate a client instance
2 parents 75eb25f + 368fd2a commit 9a0981e

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/client.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@ export default function(connection) {
44
if(!connection) {
55
throw new Error('Socket.io connection needs to be provided');
66
}
7+
8+
const defaultService = function(name) {
9+
return new Service({ name, connection, method: 'emit' });
10+
};
711

8-
return function() {
12+
const initialize = function() {
13+
if(typeof this.defaultService === 'function') {
14+
throw new Error('Only one default client provider can be configured');
15+
}
16+
917
this.io = connection;
10-
this.defaultService = function(name) {
11-
return new Service({ name, connection, method: 'emit' });
12-
};
18+
this.defaultService = defaultService;
1319
};
20+
21+
initialize.Service = Service;
22+
initialize.service = defaultService;
23+
24+
return initialize;
1425
}

test/client.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ describe('feathers-socketio/client', function() {
3232
it('app has the io attribute', () => {
3333
assert.ok(app.io);
3434
});
35+
36+
it('throws an error when configured twice', () => {
37+
try {
38+
app.configure(socketio(socket));
39+
assert.ok(false, 'Should never get here');
40+
} catch (e) {
41+
assert.equal(e.message, 'Only one default client provider can be configured');
42+
}
43+
});
44+
45+
it('can initialize a client instance', done => {
46+
const init = socketio(socket);
47+
const todos = init.service('todos');
48+
49+
assert.ok(todos instanceof init.Service, 'Returned service is a client');
50+
todos.find({}).then(todos => assert.deepEqual(todos, [
51+
{
52+
text: 'some todo',
53+
complete: false,
54+
id: 0
55+
}
56+
])).then(() => done()).catch(done);
57+
});
3558

3659
baseTests(service);
3760
});

0 commit comments

Comments
 (0)