-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51926db
commit 969d4af
Showing
9 changed files
with
999 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/*can-fixture-socket@1.0.0-pre.1#can-fixture-socket*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'./src/index', | ||
'./src/store' | ||
], function (require, exports, module) { | ||
var fixtureSocket = require('./src/index'); | ||
var fixtureStore = require('./src/store'); | ||
module.exports = { | ||
Server: fixtureSocket.Server, | ||
requestHandlerToListener: fixtureStore.requestHandlerToListener, | ||
storeToListeners: fixtureStore.storeToListeners | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*can-fixture-socket@1.0.0-pre.1#src/feathers-client*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'./store', | ||
'can-util/js/assign' | ||
], function (require, exports, module) { | ||
var storeToListeners = require('./store').storeToListeners; | ||
var assign = require('can-util/js/assign'); | ||
function subscribeFeathersStoreToServer(serviceName, fixtureStore, mockServer, options) { | ||
var listeners = storeToListeners(fixtureStore); | ||
mockServer.on(serviceName + '::find', toFeathersDataHandler(listeners.getListData, null, toFeathersFind)); | ||
mockServer.on(serviceName + '::get', toFeathersDataHandler(listeners.getData, wrapToId(options), null)); | ||
mockServer.on(serviceName + '::remove', toFeathersRemoveHandler(listeners.getData, listeners.destroyData, options)); | ||
mockServer.on(serviceName + '::create', toFeathersCreateHandler(listeners.createData)); | ||
mockServer.on(serviceName + '::update', toFeathersUpdateHandler(listeners.updateData, options)); | ||
} | ||
function toFeathersDataHandler(method, queryTransformer, dataTransformer) { | ||
return function (query) { | ||
var args = Array.prototype.slice.call(arguments), fn; | ||
if (typeof args[args.length - 1] === 'function') { | ||
fn = args[args.length - 1]; | ||
} | ||
query = queryTransformer ? queryTransformer(query) : query; | ||
method(query, function (err, data) { | ||
if (err) { | ||
fn && fn(err); | ||
} else { | ||
data = dataTransformer ? dataTransformer(data) : data; | ||
fn && fn(null, data); | ||
} | ||
}); | ||
}; | ||
} | ||
function wrapToId(options) { | ||
return function (id) { | ||
var o = {}, idProp = options && options.id || 'id'; | ||
o[idProp] = id; | ||
return o; | ||
}; | ||
} | ||
function toFeathersFind(data) { | ||
return { | ||
total: data.count, | ||
limit: data.limit, | ||
skip: data.offset, | ||
data: data.data | ||
}; | ||
} | ||
function toFeathersRemoveHandler(getData, destroyData, options) { | ||
return function (id, query, fn) { | ||
var setQuery = wrapToId(options)(id); | ||
getData(setQuery, function (err, item) { | ||
if (err) { | ||
fn(err); | ||
} else { | ||
destroyData(setQuery, function (err, data) { | ||
if (err) { | ||
fn(err); | ||
} else { | ||
fn(null, item); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
} | ||
function toFeathersUpdateHandler(updateData, options) { | ||
return function (id, data, query, fn) { | ||
var setQuery = wrapToId(options)(id); | ||
updateData(assign(setQuery, data), function (err, data2) { | ||
if (err) { | ||
fn(err); | ||
} else { | ||
fn(null, assign(setQuery, assign(data, data2))); | ||
} | ||
}); | ||
}; | ||
} | ||
function toFeathersCreateHandler(createData) { | ||
return function (data, query, fn) { | ||
createData(data, function (err, data2) { | ||
if (err) { | ||
fn(err); | ||
} else { | ||
fn(null, assign(data, data2)); | ||
} | ||
}); | ||
}; | ||
} | ||
module.exports = { subscribeFeathersStoreToServer: subscribeFeathersStoreToServer }; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/*can-fixture-socket@1.0.0-pre.1#src/index*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'./feathers-client' | ||
], function (require, exports, module) { | ||
var subscribeFeathersStoreToServer = require('./feathers-client').subscribeFeathersStoreToServer; | ||
var MockedServer = function (io) { | ||
this.io = io; | ||
this.events = {}; | ||
this.subscribers = {}; | ||
resetManagerCache(io.managers); | ||
this.origs = mockManager(io.Manager.prototype, this); | ||
}; | ||
MockedServer.prototype.on = function (event, cb) { | ||
var self = this; | ||
var events = {}; | ||
if (typeof event === 'string') { | ||
events[event] = cb; | ||
} | ||
if (typeof event === 'object') { | ||
events = event; | ||
} | ||
Object.keys(events).forEach(function (name) { | ||
sub(self.events, name, events[name]); | ||
}); | ||
}; | ||
MockedServer.prototype.emit = function (event) { | ||
var dataArgs = Array.prototype.slice.call(arguments, 1); | ||
pub(this.subscribers, event, dataArgs); | ||
}; | ||
MockedServer.prototype.onFeathersService = function (serviceName, fixtureStore, options) { | ||
subscribeFeathersStoreToServer(serviceName, fixtureStore, this, options); | ||
}; | ||
MockedServer.prototype.restore = function () { | ||
restoreManager(this.io.Manager.prototype, this.origs); | ||
resetManagerCache(this.io.managers); | ||
}; | ||
var MockedSocket = function (server) { | ||
this._server = server; | ||
this.io = { engine: this }; | ||
}; | ||
MockedSocket.prototype = { | ||
on: function (event, cb) { | ||
debug('MockedSocket.on ... ' + event); | ||
sub(this._server.subscribers, event, cb); | ||
}, | ||
emit: function (event) { | ||
var dataArgs = Array.prototype.slice.call(arguments, 1); | ||
debug('MockedSocket.emit ...' + event); | ||
pub(this._server.events, event, dataArgs); | ||
}, | ||
once: function () { | ||
debug('MockedSocket.once ...'); | ||
}, | ||
off: function (event, cb) { | ||
debug('MockedSocket.off ... ' + event); | ||
unsub(this._server.subscribers, event, cb); | ||
}, | ||
open: function () { | ||
return this.connect(); | ||
}, | ||
connect: function () { | ||
this.connected = true; | ||
this.disconnected = false; | ||
}, | ||
close: function () { | ||
return this.disconnect(); | ||
}, | ||
disconnect: function () { | ||
this.connected = false; | ||
this.disconnected = true; | ||
} | ||
}; | ||
function pub(pubsub, event, dataArgs) { | ||
debug(' >>> pub ' + event); | ||
var subscribers = pubsub[event] || []; | ||
subscribers.forEach(function (subscriber) { | ||
subscriber.apply(null, dataArgs); | ||
}); | ||
} | ||
function sub(pubsub, event, cb) { | ||
debug(' <<< sub ' + event); | ||
if (!pubsub[event]) { | ||
pubsub[event] = []; | ||
} | ||
pubsub[event].push(cb); | ||
} | ||
function unsub(pubsub, event, cb) { | ||
debug(' <<< unsub ' + event); | ||
pubsub[event].forEach(function (registeredCb, index) { | ||
if (registeredCb === cb) { | ||
pubsub[event].splice(index, 1); | ||
} | ||
}); | ||
} | ||
function mockManager(managerProto, server) { | ||
var methods = [ | ||
'open', | ||
'socket' | ||
]; | ||
var origs = methods.map(function (name) { | ||
return { | ||
name: name, | ||
method: managerProto[name] | ||
}; | ||
}); | ||
managerProto.open = managerProto.connect = function () { | ||
debug('MockedManager.prototype.open or connect ... arguments:', arguments); | ||
setTimeout(function () { | ||
pub(server.subscribers, 'connect'); | ||
pub(server.events, 'connection'); | ||
}, 0); | ||
}; | ||
managerProto.socket = function () { | ||
debug('MockedManager.prototype.socket ...'); | ||
var socket = new MockedSocket(server); | ||
socket.connected = true; | ||
socket.disconnected = false; | ||
return socket; | ||
}; | ||
return origs; | ||
} | ||
function restoreManager(managerProto, origs) { | ||
debug('Restore.'); | ||
origs.forEach(function (orig) { | ||
managerProto[orig.name] = orig.method; | ||
}); | ||
} | ||
function resetManagerCache(cache) { | ||
for (var i in cache) { | ||
if (cache.hasOwnProperty(i)) { | ||
delete cache[i]; | ||
} | ||
} | ||
} | ||
var _DEBUG = false; | ||
function debug(msg, obj) { | ||
if (_DEBUG) { | ||
console.log.apply(console, arguments); | ||
} | ||
} | ||
module.exports = { | ||
Server: MockedServer, | ||
mockSocketManager: mockManager, | ||
restoreManager: restoreManager | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*can-fixture-socket@1.0.0-pre.1#src/store*/ | ||
define([ | ||
'require', | ||
'exports', | ||
'module', | ||
'can-fixture/core' | ||
], function (require, exports, module) { | ||
var extractResponse = require('can-fixture/core').extractResponse; | ||
function requestHandlerToListener(method) { | ||
return function (query, fn) { | ||
var req = { data: query }; | ||
var res = function () { | ||
var response = extractResponse.apply(null, arguments); | ||
if (response[0] === 200) { | ||
fn(null, response[1]); | ||
} else { | ||
fn(response[1]); | ||
} | ||
}; | ||
method(req, res); | ||
}; | ||
} | ||
function storeToListeners(fixtureStore) { | ||
var methods = [ | ||
'getListData', | ||
'getData', | ||
'updateData', | ||
'createData', | ||
'destroyData' | ||
]; | ||
return methods.reduce(function (listeners, method) { | ||
listeners[method] = requestHandlerToListener(fixtureStore[method]); | ||
return listeners; | ||
}, {}); | ||
} | ||
module.exports = { | ||
requestHandlerToListener: requestHandlerToListener, | ||
storeToListeners: storeToListeners | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/*can-fixture-socket@1.0.0-pre.1#can-fixture-socket*/ | ||
var fixtureSocket = require('./src/index.js'); | ||
var fixtureStore = require('./src/store.js'); | ||
module.exports = { | ||
Server: fixtureSocket.Server, | ||
requestHandlerToListener: fixtureStore.requestHandlerToListener, | ||
storeToListeners: fixtureStore.storeToListeners | ||
}; |
Oops, something went wrong.