Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Async Connection via async feathersClient #104

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion _docs/can-connect-feathers.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,27 @@ var feathersClient = feathers()
module.exports = feathersClient;
```

> Pro tip: If you are planning on using Done-SSR, exchange the `socket.io-client/dist/socket.io` module for `steal-socket.io` in the above example.
> Pro tip: If you are planning on using Done-SSR, exchange the `socket.io-client/dist/socket.io` module for `steal-socket.io` in the above example.

you can also define the client async as a Promise here is a little conditional loading example using StealJS

```js
// models/feathers-async.js
/* global window */
import loader from '@loader'; // a Global created when loaded with SystemJS or Steal

let clientUrl;

if (window && window.fetch) {
clientUrl = '~/models/feathers/v3/feathers-client.socketio.js'; // => Same content as models/feathers.js
} else {
clientUrl = '~/models/feathers/v3/feathers-client.rest.js'; // => Feathers Client Configured with rest Provider
}

export const feathersClient = loader.import(clientUrl).then((module)=>{ return module.feathersClient; }, err=>{
new Error('Feathers-client Error '+ clientUrl,err);
});


export default feathersClient;
```
2 changes: 2 additions & 0 deletions _docs/service/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ connect([
feathersService,
realtime
], {
// if your feathersClient is a Promise
// feathersService: feathersClient.then((con)=>con.service('/api/todos'))
feathersService: feathersClient.service('/api/todos')
});
```
Expand Down
12 changes: 7 additions & 5 deletions service/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ module.exports = connect.behavior('data/feathers-service', function () {
throw new Error('You must provide a feathersService to the feathers-service behavior: ' + helpURL);
}

var service = this.feathersService;
var service = Promise.resolve(this.feathersService);

return {
init: function () {
var self = this;
// Connect to real-time events.
service.on('created', function (message) { self.createInstance(message); });
service.on('updated', function (message) { self.updateInstance(message); });
service.on('patched', function (message) { self.updateInstance(message); });
service.on('removed', function (message) { self.destroyInstance(message); });
service.then(instance=>{
instance.on('created', function (message) { self.createInstance(message); });
instance.on('updated', function (message) { self.updateInstance(message); });
instance.on('patched', function (message) { self.updateInstance(message); });
instance.on('removed', function (message) { self.destroyInstance(message); });
});
},

getListData: function (params) {
Expand Down