1
+
2
+ module . exports = function ( store ) {
3
+
4
+ store . hook = function ( method , pattern , fn ) {
5
+ return store . shareClient . use ( 'after submit' , function ( shareRequest , next ) {
6
+ var backend , collectionName , docName , firstDot , fullPath , matches , op , opData , regExp , relPath , segments , session , snapshot , _i , _len , _ref ;
7
+ opData = shareRequest . opData ;
8
+ if ( opData . del || opData . create ) {
9
+ collectionName = pattern ;
10
+ if ( collectionName !== shareRequest . collection ) {
11
+ return next ( ) ;
12
+ }
13
+ } else {
14
+ firstDot = pattern . indexOf ( '.' ) ;
15
+ if ( firstDot === - 1 ) {
16
+ if ( ! patternToRegExp ( pattern ) . test ( collectionName ) ) {
17
+ return next ( ) ;
18
+ }
19
+ } else {
20
+ collectionName = pattern . slice ( 0 , firstDot ) ;
21
+ if ( collectionName !== shareRequest . collection ) {
22
+ return next ( ) ;
23
+ }
24
+ }
25
+ }
26
+ snapshot = shareRequest . snapshot , docName = shareRequest . docName , backend = shareRequest . backend ;
27
+ session = shareRequest . agent . connectSession ;
28
+ switch ( method ) {
29
+ case 'del' :
30
+ if ( ! opData . del ) {
31
+ return next ( ) ;
32
+ }
33
+ fn ( docName , shareRequest . prev . data , session , backend ) ;
34
+ break ;
35
+ case 'create' :
36
+ if ( ! opData . create ) {
37
+ return next ( ) ;
38
+ }
39
+ fn ( docName , shareRequest . snapshot . data , session , backend ) ;
40
+ break ;
41
+ case 'change' :
42
+ _ref = opData . op ;
43
+ for ( _i = 0 , _len = _ref . length ; _i < _len ; _i ++ ) {
44
+ op = _ref [ _i ] ;
45
+ segments = op . p ;
46
+ if ( op . si || op . sd ) {
47
+ segments = segments . slice ( 0 , - 1 ) ;
48
+ }
49
+ relPath = segments . join ( '.' ) ;
50
+ fullPath = collectionName + '.' + docName + '.' + relPath ;
51
+ regExp = patternToRegExp ( pattern ) ;
52
+ matches = regExp . exec ( fullPath ) ;
53
+ if ( matches ) {
54
+ fn . apply ( null , __slice . call ( matches . slice ( 1 ) ) . concat ( [ lookup ( segments , snapshot . data ) ] , [ op ] , [ session ] , [ backend ] ) ) ;
55
+ }
56
+ }
57
+ }
58
+ return next ( ) ;
59
+ } ) ;
60
+
61
+ } ;
62
+
63
+ store . onQuery = function ( collectionName , source , cb ) {
64
+ return this . shareClient . use ( 'query' , function ( shareRequest , next ) {
65
+ var session ;
66
+ if ( shareRequest . options . backend !== source ) {
67
+ return next ( ) ;
68
+ }
69
+ session = shareRequest . agent . connectSession ;
70
+ shareRequest . query = deepCopy ( shareRequest . query ) ;
71
+ if ( collectionName === '*' ) {
72
+ return cb ( shareRequest . collection , shareRequest . query , session , next ) ;
73
+ } else {
74
+ return cb ( shareRequest . query , session , next ) ;
75
+ }
76
+ } ) ;
77
+ } ;
78
+ return true ;
79
+ } ;
80
+
81
+
82
+ function patternToRegExp ( pattern ) {
83
+ var end ;
84
+ end = pattern . slice ( pattern . length - 2 , pattern . length ) ;
85
+ if ( end === '**' ) {
86
+ pattern = pattern . slice ( 0 , pattern . length - 2 ) ;
87
+ } else {
88
+ end = '' ;
89
+ }
90
+ pattern = pattern . replace ( / \. / g, "\\." ) . replace ( / \* / g, "([^.]*)" ) ;
91
+ return new RegExp ( pattern + ( end ? '.*' : '$' ) ) ;
92
+ } ;
93
+
94
+ function lookup ( segments , doc ) {
95
+ var curr , part , _i , _len ;
96
+ curr = doc ;
97
+ for ( _i = 0 , _len = segments . length ; _i < _len ; _i ++ ) {
98
+ part = segments [ _i ] ;
99
+ if ( curr !== void 0 ) {
100
+ curr = curr [ part ] ;
101
+ }
102
+ }
103
+ return curr ;
104
+ } ;
0 commit comments