Skip to content

Commit eec2bd7

Browse files
committed
change require API
1 parent 7b222c5 commit eec2bd7

File tree

2 files changed

+76
-72
lines changed

2 files changed

+76
-72
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ First you should require the package and init it:
99
server.js
1010

1111
```js
12-
// ...
13-
14-
var shareDbHooks = require('sharedb-hooks');
1512

1613
// Add 'hook' and 'onQuery' functions to the store
17-
shareDbHooks(store);
14+
derby.use(require('sharedb-hooks'));
15+
or
16+
racer.use(require('sharedb-hooks'));
1817

1918
// ...
2019
```

index.js

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,88 @@
11

2-
module.exports = function(store) {
3-
4-
store.hook = function(method, pattern, fn) {
5-
var emitter = store.backend || store.shareClient;
2+
module.exports = function(racer) {
3+
racer.on('store', function(store) {
4+
store.hook = hook.bind(store);
5+
store.onQuery = onQuery.bind(store);
6+
});
7+
};
68

7-
emitter.use('after submit', function(shareRequest, next) {
8-
var collectionName, firstDot, fullPath, matches, regExp, relPath, segments, op;
9+
function onQuery(collectionName, cb) {
10+
var store = this;
11+
var emitter = store.backend || store.shareClient;
912

10-
var opData = shareRequest.opData || shareRequest.op;
13+
emitter.use('query', function (shareRequest, next) {
1114

12-
if (opData.del || opData.create) {
13-
collectionName = pattern;
14-
if (collectionName !== shareRequest.collection) return next();
15-
} else {
16-
firstDot = pattern.indexOf('.');
17-
if (firstDot === -1) {
18-
if (!patternToRegExp(pattern).test(shareRequest.collection)) return next();
19-
} else {
20-
collectionName = pattern.slice(0, firstDot);
21-
if (collectionName !== shareRequest.collection) return next();
22-
}
23-
}
15+
var session = shareRequest.agent.connectSession;
2416

25-
var snapshot = shareRequest.snapshot;
26-
var docName = shareRequest.docName || shareRequest.id;
27-
var backend = shareRequest.backend;
28-
var session = shareRequest.agent.connectSession;
29-
30-
switch (method) {
31-
case 'del':
32-
if (!opData.del) return next();
33-
fn(docName, shareRequest, session, backend);
34-
break;
35-
case 'create':
36-
if (!opData.create) return next();
37-
fn(docName, shareRequest.snapshot.data, session, backend);
38-
break;
39-
case 'change':
40-
var ops = opData.op;
41-
if (ops) {
42-
for (var i = 0; i < ops.length; i++) {
43-
op = ops[i];
44-
segments = op.p;
45-
if (op.si || op.sd) segments = segments.slice(0, -1);
46-
47-
relPath = segments.join('.');
48-
fullPath = collectionName + '.' + docName + '.' + relPath;
49-
regExp = patternToRegExp(pattern);
50-
matches = regExp.exec(fullPath);
51-
if (matches) {
52-
fn.apply(null, Array.prototype.slice.call(matches.slice(1)).concat([lookup(segments, snapshot.data)], [op], [session], [backend]));
53-
}
54-
}
55-
}
56-
}
57-
next();
58-
});
17+
if (collectionName === '*') {
18+
return cb(shareRequest.collection, shareRequest.query, session, next);
19+
}
5920

60-
};
21+
if (shareRequest.collection !== collectionName) return next();
6122

62-
store.onQuery = function(collectionName, cb) {
63-
var emitter = store.backend || store.shareClient;
23+
cb(shareRequest.query, session, next);
6424

65-
emitter.use('query', function(shareRequest, next) {
25+
});
26+
}
6627

67-
var session = shareRequest.agent.connectSession;
6828

69-
if (collectionName === '*') {
70-
return cb(shareRequest.collection, shareRequest.query, session, next);
71-
}
29+
function hook(method, pattern, fn) {
30+
var store = this;
31+
var emitter = store.backend || store.shareClient;
7232

73-
if (shareRequest.collection !== collectionName) return next();
33+
emitter.use('after submit', function (shareRequest, next) {
34+
var collectionName, firstDot, fullPath, matches, regExp, relPath, segments, op;
7435

75-
cb(shareRequest.query, session, next);
36+
var opData = shareRequest.opData || shareRequest.op;
7637

77-
});
78-
};
38+
if (opData.del || opData.create) {
39+
collectionName = pattern;
40+
if (collectionName !== shareRequest.collection) return next();
41+
} else {
42+
firstDot = pattern.indexOf('.');
43+
if (firstDot === -1) {
44+
if (!patternToRegExp(pattern).test(shareRequest.collection)) return next();
45+
} else {
46+
collectionName = pattern.slice(0, firstDot);
47+
if (collectionName !== shareRequest.collection) return next();
48+
}
49+
}
7950

80-
};
51+
var snapshot = shareRequest.snapshot;
52+
var docName = shareRequest.docName || shareRequest.id;
53+
var backend = shareRequest.backend;
54+
var session = shareRequest.agent.connectSession;
55+
56+
switch (method) {
57+
case 'del':
58+
if (!opData.del) return next();
59+
fn(docName, shareRequest, session, backend);
60+
break;
61+
case 'create':
62+
if (!opData.create) return next();
63+
fn(docName, shareRequest.snapshot.data, session, backend);
64+
break;
65+
case 'change':
66+
var ops = opData.op;
67+
if (ops) {
68+
for (var i = 0; i < ops.length; i++) {
69+
op = ops[i];
70+
segments = op.p;
71+
if (op.si || op.sd) segments = segments.slice(0, -1);
72+
73+
relPath = segments.join('.');
74+
fullPath = collectionName + '.' + docName + '.' + relPath;
75+
regExp = patternToRegExp(pattern);
76+
matches = regExp.exec(fullPath);
77+
if (matches) {
78+
fn.apply(null, Array.prototype.slice.call(matches.slice(1)).concat([lookup(segments, snapshot.data)], [op], [session], [backend]));
79+
}
80+
}
81+
}
82+
}
83+
next();
84+
});
85+
}
8186

8287

8388
function patternToRegExp(pattern) {
@@ -90,7 +95,7 @@ function patternToRegExp(pattern) {
9095
}
9196
pattern = pattern.replace(/\./g, "\\.").replace(/\*/g, "([^.]*)");
9297
return new RegExp(pattern + (end ? '.*' : '$'));
93-
};
98+
}
9499

95100
function lookup(segments, doc) {
96101
var curr, part, _i, _len;
@@ -102,4 +107,4 @@ function lookup(segments, doc) {
102107
}
103108
}
104109
return curr;
105-
};
110+
}

0 commit comments

Comments
 (0)