MCPcopy Index your code
hub / github.com/dresende/node-orm2 / ChainInstance

Function ChainInstance

lib/ChainInstance.js:3–89  ·  view source on GitHub ↗
(chain, cb)

Source from the content-addressed store, hash-verified

1module.exports = ChainInstance;
2
3function ChainInstance(chain, cb) {
4 var instances = null;
5 var loading = false;
6 var queue = [];
7
8 var load = function () {
9 loading = true;
10 chain.run(function (err, items) {
11 instances = items;
12
13 return next();
14 });
15 };
16 var promise = function(hwd, next) {
17 return function () {
18 if (!loading) {
19 load();
20 }
21
22 queue.push({ hwd: hwd, args: arguments });
23
24 return calls;
25 };
26 };
27 var next = function () {
28 if (queue.length === 0) return;
29
30 var item = queue.shift();
31
32 item.hwd.apply(calls, item.args);
33 };
34 var calls = {
35 filter: promise(function (cb) {
36 instances = instances.filter(cb);
37
38 return next();
39 }),
40 forEach: promise(function (cb) {
41 instances.forEach(cb);
42
43 return next();
44 }),
45 sort: promise(function (sortFn) {
46 instances.sort(sortFn);
47
48 return next();
49 }),
50 count: promise(function (cb) {
51 cb(instances.length);
52
53 return next();
54 }),
55 get: promise(function (cb) {
56 cb(instances);
57
58 return next();
59 }),
60 save: promise(function (cb) {

Callers

nothing calls this directly

Calls 6

promiseFunction · 0.85
cbFunction · 0.85
saveNextFunction · 0.85
filterMethod · 0.80
sortMethod · 0.80
nextFunction · 0.70

Tested by

no test coverage detected