1 |
|
2 | (function() {
|
3 | var Immutable, Key, NOTIFY, SUBSCRIBE, UNSUBSCRIBE, UPDATE, build_backend, changes_semantic, has_key, is_object, is_string, most, not_null, operation;
|
4 |
|
5 | build_backend = function({changes, view_changes, semantic, get_key, view_key, fromJS}) {
|
6 |
|
7 | return function(sources) {
|
8 | var route, subscriptions_keys, values, view_values;
|
9 |
|
10 | route = sources.filter(operation(UPDATE)).thru(changes_semantic);
|
11 | route.create.forEach(semantic.create);
|
12 | route.update.forEach(semantic.update);
|
13 | route.delete.forEach(semantic.delete);
|
14 |
|
15 |
|
16 |
|
17 | subscriptions_keys = sources.filter(operation(SUBSCRIBE)).map(Key).multicast();
|
18 |
|
19 |
|
20 |
|
21 | values = subscriptions_keys.filter(is_string).map(get_key).chain(most.fromPromise).filter(not_null);
|
22 |
|
23 | view_values = subscriptions_keys.chain(view_key);
|
24 |
|
25 | return most.mergeArray([
|
26 |
|
27 | changes.map(function(msg) {
|
28 | return Immutable.fromJS(msg).merge({
|
29 | op: NOTIFY,
|
30 | value: {
|
31 | rev: msg.doc._rev
|
32 | },
|
33 | doc: fromJS(msg.doc),
|
34 | key: msg.id
|
35 | });
|
36 | }),
|
37 |
|
38 | values.map(function(doc) {
|
39 | return Immutable.Map({
|
40 | op: NOTIFY,
|
41 | id: doc._id,
|
42 | key: doc._id,
|
43 | value: {
|
44 | rev: doc._rev
|
45 | },
|
46 | doc: fromJS(doc)
|
47 | });
|
48 | }),
|
49 |
|
50 | view_changes.map(function(msg) {
|
51 | return Immutable.fromJS(msg).set('op',
|
52 | NOTIFY);
|
53 | }),
|
54 |
|
55 | view_values.map(function(msg) {
|
56 | return Immutable.fromJS(msg).set('op',
|
57 | NOTIFY);
|
58 | })
|
59 | ]);
|
60 | };
|
61 | };
|
62 |
|
63 | module.exports = build_backend;
|
64 |
|
65 | changes_semantic = require('red-rings-semantic');
|
66 |
|
67 | ({operation, Key, is_string, is_object, not_null, has_key} = require('abrasive-ducks-transducers'));
|
68 |
|
69 | Immutable = require('immutable');
|
70 |
|
71 | most = require('most');
|
72 |
|
73 | ({UPDATE, SUBSCRIBE, UNSUBSCRIBE, NOTIFY} = require('red-rings/operations'));
|
74 |
|
75 | }).call(this);
|