-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathRequestResponseClientDAO.js
More file actions
149 lines (124 loc) · 4.68 KB
/
RequestResponseClientDAO.js
File metadata and controls
149 lines (124 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @license
* Copyright 2017 The FOAM Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
foam.CLASS({
package: 'foam.dao',
name: 'RequestResponseClientDAO',
extends: 'foam.dao.BaseClientDAO',
requires: [
'foam.lang.Serializable'
],
documentation: `A ClientDAO implementation which publishes its own events upon put/remove.
Suitable for usage against backends that don't support listen(), such as plain HTTP based servers.`,
methods: [
async function put_(x, obj) {
var superMethod = this.SUPER;
await obj.normalizeObj();
var self = this;
return superMethod.call(this, x, obj).then(function(obj) {
self.on.put.pub(obj);
return obj;
});
},
function remove_(x, obj) {
var self = this;
return this.SUPER(null, obj).then(function(o) {
self.on.remove.pub(obj);
return o;
});
},
function find_(x, key) {
return this.SUPER(null, key);
},
function select_(x, sink, skip, limit, order, predicate) {
if ( predicate ) predicate = predicate.partialEval();
if ( predicate === foam.mlang.predicate.True.create() ) predicate = null;
if ( ! skip ) skip = 0;
if ( ! limit ) limit = Number.MAX_SAFE_INTEGER;
// Sometimes its useful to be able to probe the DAO to see what queries will
// be supplied. The setPredicate() method can just throw an exception if it
// doesn't actually want to proceed with the query.
if ( sink.setPredicate ) {
sink.setPredicate(predicate);
}
if ( ! this.Serializable.isInstance(sink) ) {
var self = this;
return this.SUPER(null, foam.dao.ArraySink.create(), skip, limit, order, predicate).then(function(result) {
var items = result.array;
if ( ! sink ) return result;
var sub = foam.lang.FObject.create();
var detached = false;
sub.onDetach(function() { detached = true; });
for ( var i = 0 ; i < items.length ; i++ ) {
if ( detached ) break;
sink.put(items[i], sub);
}
sink.eof();
return sink;
});
}
return this.SUPER(null, sink, skip, limit, order, predicate);
},
function removeAll_(x, skip, limit, order, predicate) {
var self = this;
if ( predicate === foam.mlang.predicate.True.create() ) predicate = null;
if ( ! skip ) skip = 0;
if ( foam.Undefined.isInstance(limit) ) limit = Number.MAX_SAFE_INTEGER;
return this.SUPER(null, skip, limit, order, predicate).then(function() {
self.on.reset.pub();
return;
});
},
function listen_(x, sink, predicate) {
// Avoid the proxy implementation inherited from BaseClientDAO
// and instead use the default implementation from AbstractDAO.
return this.__context__.lookup('foam.dao.AbstractDAO').
prototype.listen_.call(this, x, sink, predicate);
},
{
name: 'cmd_',
code: function cmd_(x, obj) {
var self = this;
var superMethod = this.SUPER;
function processCmd() {
/** Force the DAO to publish a 'reset' notification. **/
if ( foam.dao.DAO.RESET_CMD === obj ) {
self.on.reset.pub();
return true;
}
// ctrl.__subContext__.cSpecDAO.select(ns => { if ( ns.name.indexOf('DAO') == -1 ) return; try { var count = ctrl.__subContext__[ns.name].cmd(foam.dao.DAO.COUNT_LISTENERS_CMD); if ( count ) console.log(ns.name, count); } catch(x) {}});
if ( foam.dao.DAO.COUNT_LISTENERS_CMD === obj ) {
// pub() returns the number of listeners
return self.on.pub('ping');
}
if ( foam.dao.DAO.PURGE_CMD === obj ) {
return obj;
}
return superMethod.call(self, x, obj);
}
if (obj && obj.normalizeObj && typeof obj.normalizeObj === 'function') {
var result = obj.normalizeObj();
}
// Handle both promise and non-promise returns
if ( result && typeof result.then === 'function' ) {
return result.then(processCmd);
} else {
return processCmd();
}
}
}
]
});