@@ -7,9 +7,18 @@ var no_password_is_set = /no password is set/;
77var loading = / L O A D I N G / ;
88var RedisClient = require ( '../' ) . RedisClient ;
99
10- /********************************
11- Replace built-in redis functions
12- ********************************/
10+ /********************************************************************************************
11+ Replace built-in redis functions
12+
13+ The callback may be hooked as needed. The same does not apply to the rest of the function.
14+ State should not be set outside of the callback if not absolutly necessary.
15+ This is important to make sure it works the same as single command or in a multi context.
16+ To make sure everything works with the offline queue use the "call_on_write" function.
17+ This is going to be executed while writing to the stream.
18+
19+ TODO: Implement individal command generation as soon as possible to prevent divergent code
20+ on single and multi calls!
21+ ********************************************************************************************/
1322
1423RedisClient . prototype . multi = RedisClient . prototype . MULTI = function multi ( args ) {
1524 var multi = new Multi ( this , args ) ;
@@ -209,3 +218,227 @@ RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function hmset () {
209218 }
210219 return this . internal_send_command ( 'hmset' , arr , callback ) ;
211220} ;
221+
222+ RedisClient . prototype . subscribe = RedisClient . prototype . SUBSCRIBE = function subscribe ( ) {
223+ var arr ,
224+ len = arguments . length ,
225+ callback ,
226+ i = 0 ;
227+ if ( Array . isArray ( arguments [ 0 ] ) ) {
228+ arr = arguments [ 0 ] ;
229+ callback = arguments [ 1 ] ;
230+ } else {
231+ len = arguments . length ;
232+ // The later should not be the average use case
233+ if ( len !== 0 && ( typeof arguments [ len - 1 ] === 'function' || typeof arguments [ len - 1 ] === 'undefined' ) ) {
234+ len -- ;
235+ callback = arguments [ len ] ;
236+ }
237+ arr = new Array ( len ) ;
238+ for ( ; i < len ; i += 1 ) {
239+ arr [ i ] = arguments [ i ] ;
240+ }
241+ }
242+ var self = this ;
243+ var call_on_write = function ( ) {
244+ self . pub_sub_mode = self . pub_sub_mode || self . command_queue . length + 1 ;
245+ } ;
246+ return this . internal_send_command ( 'subscribe' , arr , callback , call_on_write ) ;
247+ } ;
248+
249+ Multi . prototype . subscribe = Multi . prototype . SUBSCRIBE = function subscribe ( ) {
250+ var arr ,
251+ len = arguments . length ,
252+ callback ,
253+ i = 0 ;
254+ if ( Array . isArray ( arguments [ 0 ] ) ) {
255+ arr = arguments [ 0 ] ;
256+ callback = arguments [ 1 ] ;
257+ } else {
258+ len = arguments . length ;
259+ // The later should not be the average use case
260+ if ( len !== 0 && ( typeof arguments [ len - 1 ] === 'function' || typeof arguments [ len - 1 ] === 'undefined' ) ) {
261+ len -- ;
262+ callback = arguments [ len ] ;
263+ }
264+ arr = new Array ( len ) ;
265+ for ( ; i < len ; i += 1 ) {
266+ arr [ i ] = arguments [ i ] ;
267+ }
268+ }
269+ var self = this . _client ;
270+ var call_on_write = function ( ) {
271+ self . pub_sub_mode = self . pub_sub_mode || self . command_queue . length + 1 ;
272+ } ;
273+ this . queue . push ( [ 'subscribe' , arr , callback , call_on_write ] ) ;
274+ return this ;
275+ } ;
276+
277+ RedisClient . prototype . unsubscribe = RedisClient . prototype . UNSUBSCRIBE = function unsubscribe ( ) {
278+ var arr ,
279+ len = arguments . length ,
280+ callback ,
281+ i = 0 ;
282+ if ( Array . isArray ( arguments [ 0 ] ) ) {
283+ arr = arguments [ 0 ] ;
284+ callback = arguments [ 1 ] ;
285+ } else {
286+ len = arguments . length ;
287+ // The later should not be the average use case
288+ if ( len !== 0 && ( typeof arguments [ len - 1 ] === 'function' || typeof arguments [ len - 1 ] === 'undefined' ) ) {
289+ len -- ;
290+ callback = arguments [ len ] ;
291+ }
292+ arr = new Array ( len ) ;
293+ for ( ; i < len ; i += 1 ) {
294+ arr [ i ] = arguments [ i ] ;
295+ }
296+ }
297+ var self = this ;
298+ var call_on_write = function ( ) {
299+ // Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
300+ self . pub_sub_mode = self . pub_sub_mode || self . command_queue . length + 1 ;
301+ } ;
302+ return this . internal_send_command ( 'unsubscribe' , arr , callback , call_on_write ) ;
303+ } ;
304+
305+ Multi . prototype . unsubscribe = Multi . prototype . UNSUBSCRIBE = function unsubscribe ( ) {
306+ var arr ,
307+ len = arguments . length ,
308+ callback ,
309+ i = 0 ;
310+ if ( Array . isArray ( arguments [ 0 ] ) ) {
311+ arr = arguments [ 0 ] ;
312+ callback = arguments [ 1 ] ;
313+ } else {
314+ len = arguments . length ;
315+ // The later should not be the average use case
316+ if ( len !== 0 && ( typeof arguments [ len - 1 ] === 'function' || typeof arguments [ len - 1 ] === 'undefined' ) ) {
317+ len -- ;
318+ callback = arguments [ len ] ;
319+ }
320+ arr = new Array ( len ) ;
321+ for ( ; i < len ; i += 1 ) {
322+ arr [ i ] = arguments [ i ] ;
323+ }
324+ }
325+ var self = this . _client ;
326+ var call_on_write = function ( ) {
327+ // Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
328+ self . pub_sub_mode = self . pub_sub_mode || self . command_queue . length + 1 ;
329+ } ;
330+ this . queue . push ( [ 'unsubscribe' , arr , callback , call_on_write ] ) ;
331+ return this ;
332+ } ;
333+
334+ RedisClient . prototype . psubscribe = RedisClient . prototype . PSUBSCRIBE = function psubscribe ( ) {
335+ var arr ,
336+ len = arguments . length ,
337+ callback ,
338+ i = 0 ;
339+ if ( Array . isArray ( arguments [ 0 ] ) ) {
340+ arr = arguments [ 0 ] ;
341+ callback = arguments [ 1 ] ;
342+ } else {
343+ len = arguments . length ;
344+ // The later should not be the average use case
345+ if ( len !== 0 && ( typeof arguments [ len - 1 ] === 'function' || typeof arguments [ len - 1 ] === 'undefined' ) ) {
346+ len -- ;
347+ callback = arguments [ len ] ;
348+ }
349+ arr = new Array ( len ) ;
350+ for ( ; i < len ; i += 1 ) {
351+ arr [ i ] = arguments [ i ] ;
352+ }
353+ }
354+ var self = this ;
355+ var call_on_write = function ( ) {
356+ self . pub_sub_mode = self . pub_sub_mode || self . command_queue . length + 1 ;
357+ } ;
358+ return this . internal_send_command ( 'psubscribe' , arr , callback , call_on_write ) ;
359+ } ;
360+
361+ Multi . prototype . psubscribe = Multi . prototype . PSUBSCRIBE = function psubscribe ( ) {
362+ var arr ,
363+ len = arguments . length ,
364+ callback ,
365+ i = 0 ;
366+ if ( Array . isArray ( arguments [ 0 ] ) ) {
367+ arr = arguments [ 0 ] ;
368+ callback = arguments [ 1 ] ;
369+ } else {
370+ len = arguments . length ;
371+ // The later should not be the average use case
372+ if ( len !== 0 && ( typeof arguments [ len - 1 ] === 'function' || typeof arguments [ len - 1 ] === 'undefined' ) ) {
373+ len -- ;
374+ callback = arguments [ len ] ;
375+ }
376+ arr = new Array ( len ) ;
377+ for ( ; i < len ; i += 1 ) {
378+ arr [ i ] = arguments [ i ] ;
379+ }
380+ }
381+ var self = this ;
382+ var call_on_write = function ( ) {
383+ self . pub_sub_mode = self . pub_sub_mode || self . command_queue . length + 1 ;
384+ } ;
385+ this . queue . push ( [ 'psubscribe' , arr , callback , call_on_write ] ) ;
386+ return this ;
387+ } ;
388+
389+ RedisClient . prototype . punsubscribe = RedisClient . prototype . PUNSUBSCRIBE = function punsubscribe ( ) {
390+ var arr ,
391+ len = arguments . length ,
392+ callback ,
393+ i = 0 ;
394+ if ( Array . isArray ( arguments [ 0 ] ) ) {
395+ arr = arguments [ 0 ] ;
396+ callback = arguments [ 1 ] ;
397+ } else {
398+ len = arguments . length ;
399+ // The later should not be the average use case
400+ if ( len !== 0 && ( typeof arguments [ len - 1 ] === 'function' || typeof arguments [ len - 1 ] === 'undefined' ) ) {
401+ len -- ;
402+ callback = arguments [ len ] ;
403+ }
404+ arr = new Array ( len ) ;
405+ for ( ; i < len ; i += 1 ) {
406+ arr [ i ] = arguments [ i ] ;
407+ }
408+ }
409+ var self = this ;
410+ var call_on_write = function ( ) {
411+ // Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
412+ self . pub_sub_mode = self . pub_sub_mode || self . command_queue . length + 1 ;
413+ } ;
414+ return this . internal_send_command ( 'punsubscribe' , arr , callback , call_on_write ) ;
415+ } ;
416+
417+ Multi . prototype . punsubscribe = Multi . prototype . PUNSUBSCRIBE = function punsubscribe ( ) {
418+ var arr ,
419+ len = arguments . length ,
420+ callback ,
421+ i = 0 ;
422+ if ( Array . isArray ( arguments [ 0 ] ) ) {
423+ arr = arguments [ 0 ] ;
424+ callback = arguments [ 1 ] ;
425+ } else {
426+ len = arguments . length ;
427+ // The later should not be the average use case
428+ if ( len !== 0 && ( typeof arguments [ len - 1 ] === 'function' || typeof arguments [ len - 1 ] === 'undefined' ) ) {
429+ len -- ;
430+ callback = arguments [ len ] ;
431+ }
432+ arr = new Array ( len ) ;
433+ for ( ; i < len ; i += 1 ) {
434+ arr [ i ] = arguments [ i ] ;
435+ }
436+ }
437+ var self = this ;
438+ var call_on_write = function ( ) {
439+ // Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
440+ self . pub_sub_mode = self . pub_sub_mode || self . command_queue . length + 1 ;
441+ } ;
442+ this . queue . push ( [ 'punsubscribe' , arr , callback , call_on_write ] ) ;
443+ return this ;
444+ } ;
0 commit comments