Skip to content

Commit 97e7bb3

Browse files
committed
support and(live()) operations
1 parent c143df2 commit 97e7bb3

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

operators.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,12 @@ function extractMeta(orig) {
251251
}
252252

253253
function and(...args) {
254-
const rhs = args
255-
.map((arg) => (typeof arg === 'function' ? arg() : arg))
256-
.filter((arg) => !!arg)
257-
return (ops) => {
254+
return (ops, isSpecialOps) => {
255+
const rhs = args
256+
.map((arg) => (typeof arg === 'function' ? arg(ops, true) : arg))
257+
.filter((arg) => !!arg)
258258
const res =
259-
ops && ops.type
259+
ops && ops.type && !isSpecialOps
260260
? {
261261
type: 'AND',
262262
data: [ops, ...rhs],
@@ -273,12 +273,12 @@ function and(...args) {
273273
}
274274

275275
function or(...args) {
276-
const rhs = args
277-
.map((arg) => (typeof arg === 'function' ? arg() : arg))
278-
.filter((arg) => !!arg)
279-
return (ops) => {
276+
return (ops, isSpecialOps) => {
277+
const rhs = args
278+
.map((arg) => (typeof arg === 'function' ? arg(ops, true) : arg))
279+
.filter((arg) => !!arg)
280280
const res =
281-
ops && ops.type
281+
ops && ops.type && !isSpecialOps
282282
? {
283283
type: 'OR',
284284
data: [ops, ...rhs],
@@ -294,6 +294,8 @@ function or(...args) {
294294
}
295295
}
296296

297+
// The following are "special operators": they only update meta
298+
297299
function live(opts) {
298300
if (opts && opts.old) return (ops) => updateMeta(ops, 'live', 'liveAndOld')
299301
else return (ops) => updateMeta(ops, 'live', 'liveOnly')

test/operators.js

+25
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,31 @@ prepareAndRunTest('support live operations', dir, (t, db, raf) => {
10331033
})
10341034
})
10351035

1036+
prepareAndRunTest('support live inside and', dir, (t, db, raf) => {
1037+
const msg = { type: 'post', text: 'Testing!' }
1038+
let state = validate.initial()
1039+
state = validate.appendNew(state, null, alice, msg, Date.now())
1040+
state = validate.appendNew(state, null, bob, msg, Date.now() + 1)
1041+
1042+
addMsg(state.queue[0].value, raf, (e1, msg1) => {
1043+
let i = 0
1044+
query(
1045+
fromDB(db),
1046+
and(live({ old: true }), slowEqual('value.content.type', 'post')),
1047+
toPullStream(),
1048+
pull.drain((msg) => {
1049+
if (i++ == 0) {
1050+
t.equal(msg.value.author, alice.id)
1051+
addMsg(state.queue[1].value, raf, (e2, msg2) => {})
1052+
} else {
1053+
t.equal(msg.value.author, bob.id)
1054+
t.end()
1055+
}
1056+
})
1057+
)
1058+
})
1059+
})
1060+
10361061
prepareAndRunTest('support live with not', dir, (t, db, raf) => {
10371062
const msg = { type: 'post', text: 'Testing!' }
10381063
let state = validate.initial()

0 commit comments

Comments
 (0)