@@ -71,12 +71,16 @@ function sync(ripple, server) {
7171/* istanbul ignore next */
7272 if ( ! _client2 . default && ! server ) return ;
7373/* istanbul ignore next */
74- if ( ! _client2 . default ) ripple . to = clean ( ripple . to ) , ( 0 , _values2 . default ) ( ripple . types ) . map ( headers ( ripple ) ) ;
74+ if ( ! _client2 . default ) ripple . to = clean ( ripple . to ) , ( 0 , _values2 . default ) ( ripple . types ) . map ( function ( type ) {
75+ return type . parse = headers ( ripple ) ( type . parse ) ;
76+ } ) ;
7577
7678 ripple . stream = stream ( ripple ) ;
79+ ripple . respond = respond ( ripple ) ;
7780 ripple . io = io ( server ) ;
7881 ripple . on ( 'change.stream' , ripple . stream ( ) ) ; // both - broadcast change to everyone
7982 ripple . io . on ( 'change' , consume ( ripple ) ) ; // client - receive change
83+ ripple . io . on ( 'response' , response ( ripple ) ) ; // client - receive response
8084 ripple . io . on ( 'connection' , function ( s ) {
8185 return s . on ( 'change' , consume ( ripple ) ) ;
8286 } ) ; // server - receive change
@@ -87,6 +91,27 @@ function sync(ripple, server) {
8791 return ripple ;
8892}
8993
94+ var respond = function respond ( ripple ) {
95+ return function ( socket , name , time ) {
96+ return function ( reply ) {
97+ socket . emit ( 'response' , [ name , time , reply ] ) ;
98+ } ;
99+ } ;
100+ } ;
101+
102+ var response = function response ( ripple ) {
103+ return function ( _ref ) {
104+ /* istanbul ignore next */
105+ var _ref2 = _slicedToArray ( _ref , 3 ) ;
106+
107+ var name = _ref2 [ 0 ] ;
108+ var time = _ref2 [ 1 ] ;
109+ var reply = _ref2 [ 2 ] ;
110+
111+ ripple . resources [ name ] . body . emit ( 'response._' + time , reply ) ;
112+ } ;
113+ } ;
114+
90115// send diff to all or some sockets
91116var stream = function stream ( ripple ) {
92117 return function ( sockets ) {
@@ -143,28 +168,29 @@ var to = function to(ripple, res, change) {
143168
144169// incoming transforms
145170var consume = function consume ( ripple ) {
146- return function ( _ref ) {
171+ return function ( _ref3 ) {
147172/* istanbul ignore next */
148- var _ref2 = _slicedToArray ( _ref , 3 ) ;
173+ var _ref4 = _slicedToArray ( _ref3 , 3 ) ;
149174
150- var name = _ref2 [ 0 ] ;
151- var change = _ref2 [ 1 ] ;
152- var _ref2 $ = _ref2 [ 2 ] ;
153- var req = _ref2 $ === undefined ? { } : _ref2 $;
175+ var name = _ref4 [ 0 ] ;
176+ var change = _ref4 [ 1 ] ;
177+ var _ref4 $ = _ref4 [ 2 ] ;
178+ var req = _ref4 $ === undefined ? { } : _ref4 $;
154179
155180 log ( 'receiving' , name ) ;
156181
157182 var res = ripple . resources [ name ] ,
158183 xall = ripple . from ,
159- xtype = type ( ripple ) ( res ) . from || type ( ripple ) ( req ) . from ,
184+ xtype = type ( ripple ) ( res ) . from || type ( ripple ) ( req ) . from // is latter needed?
185+ ,
160186 xres = ( 0 , _header2 . default ) ( 'from' ) ( res ) ,
161- types = ripple . types ,
162187 next = ( 0 , _set2 . default ) ( change ) ,
163- silent = silence ( this ) ;
188+ silent = silence ( this ) ,
189+ respond = ripple . respond ( this , name , change . time ) ;
164190
165- return xall && ! xall . call ( this , req , change ) ? debug ( 'skip all' , name ) // rejected - by xall
166- : xtype && ! xtype . call ( this , req , change ) ? debug ( 'skip type' , name ) // rejected - by xtype
167- : xres && ! xres . call ( this , req , change ) ? debug ( 'skip res' , name ) // rejected - by xres
191+ return xall && ! xall . call ( this , req , change , respond ) ? debug ( 'skip all' , name ) // rejected - by xall
192+ : xtype && ! xtype . call ( this , req , change , respond ) ? debug ( 'skip type' , name ) // rejected - by xtype
193+ : xres && ! xres . call ( this , req , change , respond ) ? debug ( 'skip res' , name ) // rejected - by xres
168194 : ! change ? ripple ( silent ( req ) ) // accept - replace (new)
169195 : ! change . key ? ripple ( silent ( { name : name , body : change . value } ) ) // accept - replace at root
170196 : ( silent ( res ) , next ( res . body ) ) ; // accept - deep change
@@ -178,16 +204,14 @@ var count = function count(total, name) {
178204} ;
179205
180206var headers = function headers ( ripple ) {
181- return function ( type ) {
182- /* istanbul ignore next */
183- var parse = type . parse || _noop2 . default ;
184- type . parse = function ( res ) {
207+ return function ( next ) {
208+ return function ( res ) {
185209 var existing = ripple . resources [ res . name ] ,
186210 from = ( 0 , _header2 . default ) ( 'from' ) ( res ) || ( 0 , _header2 . default ) ( 'from' ) ( existing ) ,
187211 to = ( 0 , _header2 . default ) ( 'to' ) ( res ) || ( 0 , _header2 . default ) ( 'to' ) ( existing ) ;
188212 if ( from ) res . headers . from = from ;
189213 if ( to ) res . headers . to = to ;
190- return parse . apply ( this , arguments ) , res ;
214+ return next ? next ( res ) : res ;
191215 } ;
192216 } ;
193217} ;
@@ -206,10 +230,10 @@ var setIP = function setIP(socket, next) {
206230} ;
207231
208232var clean = function clean ( next ) {
209- return function ( _ref3 , change ) {
210- var name = _ref3 . name ;
211- var body = _ref3 . body ;
212- var headers = _ref3 . headers ;
233+ return function ( _ref5 , change ) {
234+ var name = _ref5 . name ;
235+ var body = _ref5 . body ;
236+ var headers = _ref5 . headers ;
213237
214238 if ( change ) return next ? next . apply ( this , arguments ) : true ;
215239
0 commit comments