@dxos/echo-db 2.29.2-dev.8c2ad8e5 → 2.29.2-dev.f6ed60b8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/api/database.d.ts +11 -7
- package/dist/src/api/database.d.ts.map +1 -1
- package/dist/src/api/database.js +22 -24
- package/dist/src/api/database.js.map +1 -1
- package/dist/src/api/database.test.js +13 -13
- package/dist/src/api/database.test.js.map +1 -1
- package/dist/src/api/item.d.ts.map +1 -1
- package/dist/src/api/item.js +1 -1
- package/dist/src/api/item.js.map +1 -1
- package/dist/src/api/result-set.d.ts.map +1 -1
- package/dist/src/api/result-set.js +1 -0
- package/dist/src/api/result-set.js.map +1 -1
- package/dist/src/api/selection/index.d.ts +5 -0
- package/dist/src/api/selection/index.d.ts.map +1 -0
- package/dist/src/api/selection/index.js +20 -0
- package/dist/src/api/selection/index.js.map +1 -0
- package/dist/src/api/selection/queries.d.ts +51 -0
- package/dist/src/api/selection/queries.d.ts.map +1 -0
- package/dist/src/api/selection/queries.js +70 -0
- package/dist/src/api/selection/queries.js.map +1 -0
- package/dist/src/api/selection/result.d.ts +50 -0
- package/dist/src/api/selection/result.d.ts.map +1 -0
- package/dist/src/api/selection/result.js +91 -0
- package/dist/src/api/selection/result.js.map +1 -0
- package/dist/src/api/selection/selection.d.ts +96 -0
- package/dist/src/api/selection/selection.d.ts.map +1 -0
- package/dist/src/api/selection/selection.js +164 -0
- package/dist/src/api/selection/selection.js.map +1 -0
- package/dist/src/api/{selection.test.d.ts → selection/selection.test.d.ts} +0 -0
- package/dist/src/api/selection/selection.test.d.ts.map +1 -0
- package/dist/src/api/{selection.test.js → selection/selection.test.js} +46 -44
- package/dist/src/api/selection/selection.test.js.map +1 -0
- package/dist/src/api/selection/util.d.ts +7 -0
- package/dist/src/api/selection/util.d.ts.map +1 -0
- package/dist/src/api/selection/util.js +25 -0
- package/dist/src/api/selection/util.js.map +1 -0
- package/dist/src/echo.test.js +20 -20
- package/dist/src/echo.test.js.map +1 -1
- package/dist/src/halo/contact-manager.js +2 -2
- package/dist/src/halo/contact-manager.js.map +1 -1
- package/dist/src/halo/halo.d.ts +1 -1
- package/dist/src/halo/halo.d.ts.map +1 -1
- package/dist/src/halo/halo.js +2 -2
- package/dist/src/halo/halo.js.map +1 -1
- package/dist/src/halo/preferences.js +6 -6
- package/dist/src/halo/preferences.js.map +1 -1
- package/dist/src/parties/party-core.test.js +3 -3
- package/dist/src/parties/party-core.test.js.map +1 -1
- package/dist/src/parties/party-factory.d.ts.map +1 -1
- package/dist/src/parties/party-factory.js +3 -3
- package/dist/src/parties/party-factory.js.map +1 -1
- package/dist/src/parties/party-internal.js +3 -3
- package/dist/src/parties/party-internal.js.map +1 -1
- package/dist/src/parties/party-manager.js +1 -1
- package/dist/src/parties/party-manager.js.map +1 -1
- package/dist/src/parties/party-manager.test.js +8 -10
- package/dist/src/parties/party-manager.test.js.map +1 -1
- package/dist/src/testing/testing-factories.d.ts +2 -2
- package/dist/src/testing/testing-factories.d.ts.map +1 -1
- package/dist/src/testing/testing-factories.js +1 -1
- package/dist/src/testing/testing-factories.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -17
- package/src/api/database.test.ts +13 -13
- package/src/api/database.ts +30 -30
- package/src/api/item.ts +2 -2
- package/src/api/result-set.ts +1 -0
- package/src/api/selection/index.ts +8 -0
- package/src/api/selection/queries.ts +108 -0
- package/src/api/selection/result.ts +112 -0
- package/src/api/{selection.test.ts → selection/selection.test.ts} +50 -48
- package/src/api/{selection.ts → selection/selection.ts} +30 -231
- package/src/api/selection/util.ts +27 -0
- package/src/echo.test.ts +20 -20
- package/src/halo/contact-manager.ts +2 -2
- package/src/halo/halo.ts +2 -3
- package/src/halo/preferences.ts +6 -6
- package/src/parties/party-core.test.ts +3 -3
- package/src/parties/party-factory.ts +3 -4
- package/src/parties/party-internal.ts +3 -3
- package/src/parties/party-manager.test.ts +8 -10
- package/src/parties/party-manager.ts +1 -1
- package/src/testing/testing-factories.ts +3 -3
- package/dist/src/api/selection.d.ts +0 -183
- package/dist/src/api/selection.d.ts.map +0 -1
- package/dist/src/api/selection.js +0 -308
- package/dist/src/api/selection.js.map +0 -1
- package/dist/src/api/selection.test.d.ts.map +0 -1
- package/dist/src/api/selection.test.js.map +0 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Event } from '@dxos/async';
|
|
2
|
+
import { Database } from '../database';
|
|
3
|
+
import { Entity } from '../entity';
|
|
4
|
+
/**
|
|
5
|
+
* Represents where the selection has started.
|
|
6
|
+
*/
|
|
7
|
+
export declare type SelectionRoot = Database | Entity;
|
|
8
|
+
/**
|
|
9
|
+
* Returned from each stage of the visitor.
|
|
10
|
+
*/
|
|
11
|
+
export declare type SelectionContext<T extends Entity, R> = [entities: T[], result?: R];
|
|
12
|
+
/**
|
|
13
|
+
* Query subscription.
|
|
14
|
+
* Represents a live-query (subscription) that can notify about future updates to the relevant subset of items.
|
|
15
|
+
*/
|
|
16
|
+
export declare class SelectionResult<T extends Entity, R = any> {
|
|
17
|
+
private readonly _execute;
|
|
18
|
+
private readonly _update;
|
|
19
|
+
private readonly _root;
|
|
20
|
+
private readonly _reducer;
|
|
21
|
+
/**
|
|
22
|
+
* Fired when there are updates in the selection.
|
|
23
|
+
* Only update that are relevant to the selection cause the update.
|
|
24
|
+
*/
|
|
25
|
+
readonly update: Event<SelectionResult<T, any>>;
|
|
26
|
+
private _lastResult;
|
|
27
|
+
constructor(_execute: () => SelectionContext<T, R>, _update: Event<Entity[]>, _root: SelectionRoot, _reducer: boolean);
|
|
28
|
+
toString(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Re-run query.
|
|
31
|
+
*/
|
|
32
|
+
refresh(): this;
|
|
33
|
+
/**
|
|
34
|
+
* The root of the selection. Either a database or an item. Must be a stable reference.
|
|
35
|
+
*/
|
|
36
|
+
get root(): SelectionRoot;
|
|
37
|
+
/**
|
|
38
|
+
* Get the result of this selection.
|
|
39
|
+
*/
|
|
40
|
+
get entities(): T[];
|
|
41
|
+
/**
|
|
42
|
+
* Returns the selection or reducer result.
|
|
43
|
+
*/
|
|
44
|
+
get value(): R extends void ? T[] : R;
|
|
45
|
+
/**
|
|
46
|
+
* Return the first element if the set has exactly one element.
|
|
47
|
+
*/
|
|
48
|
+
expectOne(): T;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../../../src/api/selection/result.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAGnC;;GAEG;AACH,oBAAY,aAAa,GAAG,QAAQ,GAAG,MAAM,CAAA;AAE7C;;GAEG;AACH,oBAAY,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,CAAA;AAE/E;;;GAGG;AACH,qBAAa,eAAe,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,GAAG,GAAG;IAUlD,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAZ3B;;;OAGG;IACH,QAAQ,CAAC,MAAM,iCAAmC;IAElD,OAAO,CAAC,WAAW,CAAgC;gBAGhC,QAAQ,EAAE,MAAM,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,EACtC,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,EACxB,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,OAAO;IAiBpC,QAAQ;IAOR;;OAEG;IACH,OAAO;IAMP;;OAEG;IACH,IAAI,IAAI,IAAK,aAAa,CAEzB;IAED;;OAEG;IACH,IAAI,QAAQ,IAAK,CAAC,EAAE,CAOnB;IAED;;OAEG;IACH,IAAI,KAAK,IAAK,CAAC,SAAS,IAAI,GAAG,CAAC,EAAE,GAAG,CAAC,CAOrC;IAED;;OAEG;IACH,SAAS,IAAK,CAAC;CAKhB"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//
|
|
3
|
+
// Copyright 2020 DXOS.org
|
|
4
|
+
//
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.SelectionResult = void 0;
|
|
10
|
+
const assert_1 = __importDefault(require("assert"));
|
|
11
|
+
const async_1 = require("@dxos/async");
|
|
12
|
+
const util_1 = require("./util");
|
|
13
|
+
/**
|
|
14
|
+
* Query subscription.
|
|
15
|
+
* Represents a live-query (subscription) that can notify about future updates to the relevant subset of items.
|
|
16
|
+
*/
|
|
17
|
+
class SelectionResult {
|
|
18
|
+
constructor(_execute, _update, _root, _reducer) {
|
|
19
|
+
this._execute = _execute;
|
|
20
|
+
this._update = _update;
|
|
21
|
+
this._root = _root;
|
|
22
|
+
this._reducer = _reducer;
|
|
23
|
+
/**
|
|
24
|
+
* Fired when there are updates in the selection.
|
|
25
|
+
* Only update that are relevant to the selection cause the update.
|
|
26
|
+
*/
|
|
27
|
+
this.update = new async_1.Event(); // TODO(burdon): Result result object.
|
|
28
|
+
this._lastResult = [[]];
|
|
29
|
+
this.refresh();
|
|
30
|
+
// Re-run if deps change.
|
|
31
|
+
this.update.addEffect(() => _update.on(currentEntities => {
|
|
32
|
+
const [previousEntities] = this._lastResult;
|
|
33
|
+
this.refresh();
|
|
34
|
+
// Filters mutation events only if selection (since we can't reason about deps of call methods).
|
|
35
|
+
const set = new Set([...previousEntities, ...this._lastResult[0]]);
|
|
36
|
+
if (this._reducer || currentEntities.some(entity => set.has(entity))) {
|
|
37
|
+
this.update.emit(this);
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
toString() {
|
|
42
|
+
const [entities] = this._lastResult;
|
|
43
|
+
return `SelectionResult<${JSON.stringify({
|
|
44
|
+
entities: entities.length
|
|
45
|
+
})}>`;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Re-run query.
|
|
49
|
+
*/
|
|
50
|
+
refresh() {
|
|
51
|
+
const [entities, result] = this._execute();
|
|
52
|
+
this._lastResult = [(0, util_1.dedupe)(entities), result];
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The root of the selection. Either a database or an item. Must be a stable reference.
|
|
57
|
+
*/
|
|
58
|
+
get root() {
|
|
59
|
+
return this._root;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Get the result of this selection.
|
|
63
|
+
*/
|
|
64
|
+
get entities() {
|
|
65
|
+
if (!this._lastResult) {
|
|
66
|
+
this.refresh();
|
|
67
|
+
}
|
|
68
|
+
const [entities] = this._lastResult;
|
|
69
|
+
return entities;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns the selection or reducer result.
|
|
73
|
+
*/
|
|
74
|
+
get value() {
|
|
75
|
+
if (!this._lastResult) {
|
|
76
|
+
this.refresh();
|
|
77
|
+
}
|
|
78
|
+
const [entities, value] = this._lastResult;
|
|
79
|
+
return (this._reducer ? value : entities);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Return the first element if the set has exactly one element.
|
|
83
|
+
*/
|
|
84
|
+
expectOne() {
|
|
85
|
+
const entities = this.entities;
|
|
86
|
+
(0, assert_1.default)(entities.length === 1, `Expected one result; got ${entities.length}`);
|
|
87
|
+
return entities[0];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.SelectionResult = SelectionResult;
|
|
91
|
+
//# sourceMappingURL=result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../../../src/api/selection/result.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;;;;AAEF,oDAA4B;AAE5B,uCAAoC;AAIpC,iCAAgC;AAYhC;;;GAGG;AACH,MAAa,eAAe;IAS1B,YACmB,QAAsC,EACtC,OAAwB,EACxB,KAAoB,EACpB,QAAiB;QAHjB,aAAQ,GAAR,QAAQ,CAA8B;QACtC,YAAO,GAAP,OAAO,CAAiB;QACxB,UAAK,GAAL,KAAK,CAAe;QACpB,aAAQ,GAAR,QAAQ,CAAS;QAZpC;;;WAGG;QACM,WAAM,GAAG,IAAI,aAAK,EAAsB,CAAC,CAAC,sCAAsC;QAEjF,gBAAW,GAA2B,CAAC,EAAE,CAAC,CAAC;QAQjD,IAAI,CAAC,OAAO,EAAE,CAAC;QAEf,yBAAyB;QACzB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,eAAe,CAAC,EAAE;YACvD,MAAM,CAAC,gBAAgB,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC;YAC5C,IAAI,CAAC,OAAO,EAAE,CAAC;YAEf,gGAAgG;YAChG,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,gBAAgB,EAAE,GAAG,IAAI,CAAC,WAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACpE,IAAI,IAAI,CAAC,QAAQ,IAAI,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,MAAa,CAAC,CAAC,EAAE;gBAC3E,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACxB;QACH,CAAC,CAAC,CAAC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CAAC,CAAC,CAAC;IACrB,CAAC;CACF;AArFD,0CAqFC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Event } from '@dxos/async';
|
|
2
|
+
import { Entity } from '../entity';
|
|
3
|
+
import { Item } from '../item';
|
|
4
|
+
import { Link } from '../link';
|
|
5
|
+
import { Callable, ItemFilter, LinkFilter, Predicate, QueryOptions, RootFilter } from './queries';
|
|
6
|
+
import { SelectionContext, SelectionResult, SelectionRoot } from './result';
|
|
7
|
+
/**
|
|
8
|
+
* Factory for selector that provides a root set of items.
|
|
9
|
+
* @param itemsProvider
|
|
10
|
+
* @param updateEventProvider
|
|
11
|
+
* @param root
|
|
12
|
+
* @param filter
|
|
13
|
+
* @param value Initial reducer value.
|
|
14
|
+
*/
|
|
15
|
+
export declare const createSelection: <R>(itemsProvider: () => Item[], updateEventProvider: () => Event<Entity[]>, root: SelectionRoot, filter: RootFilter | undefined, value: R) => Selection<Item<any>, R>;
|
|
16
|
+
/**
|
|
17
|
+
* Factory for specific item selector.
|
|
18
|
+
* @param root
|
|
19
|
+
* @param update
|
|
20
|
+
* @param value Initial reducer value.
|
|
21
|
+
*/
|
|
22
|
+
export declare const createItemSelection: <R>(root: Item<any>, update: Event<Entity[]>, value: R) => Selection<Item<any>, R>;
|
|
23
|
+
/**
|
|
24
|
+
* Selections are used to construct database subscriptions.
|
|
25
|
+
* They are [monads](https://www.quora.com/What-are-monads-in-computer-science) that support
|
|
26
|
+
* the functional composition of predicates to traverse the graph.
|
|
27
|
+
* Additionally, selections may be used to create reducers that compute an aggregated value over the traversal.
|
|
28
|
+
*
|
|
29
|
+
* Implementation:
|
|
30
|
+
* Each Selection contains a visitor
|
|
31
|
+
*/
|
|
32
|
+
export declare class Selection<T extends Entity<any>, R = void> {
|
|
33
|
+
private readonly _visitor;
|
|
34
|
+
private readonly _update;
|
|
35
|
+
private readonly _root;
|
|
36
|
+
private readonly _reducer;
|
|
37
|
+
/**
|
|
38
|
+
* @param _visitor Executes the query.
|
|
39
|
+
* @param _update The unfiltered update event.
|
|
40
|
+
* @param _root The root of the selection. Must be a stable reference.
|
|
41
|
+
* @param _reducer
|
|
42
|
+
*/
|
|
43
|
+
constructor(_visitor: (options: QueryOptions) => SelectionContext<T, R>, _update: Event<Entity[]>, _root: SelectionRoot, _reducer?: boolean);
|
|
44
|
+
/**
|
|
45
|
+
* Creates a derrived selection by aplying a mapping function to the result of the current selection.
|
|
46
|
+
*/
|
|
47
|
+
private _createSubSelection;
|
|
48
|
+
/**
|
|
49
|
+
* Finish the selection and return the result.
|
|
50
|
+
*/
|
|
51
|
+
exec(options?: QueryOptions): SelectionResult<T, R>;
|
|
52
|
+
/**
|
|
53
|
+
* @deprecated
|
|
54
|
+
*/
|
|
55
|
+
query(options?: QueryOptions): SelectionResult<T, R>;
|
|
56
|
+
/**
|
|
57
|
+
* The root of the selection. Either a database or an item. Must be a stable reference.
|
|
58
|
+
*/
|
|
59
|
+
get root(): SelectionRoot;
|
|
60
|
+
/**
|
|
61
|
+
* Visitor.
|
|
62
|
+
* @param visitor
|
|
63
|
+
*/
|
|
64
|
+
call(visitor: Callable<T, R>): Selection<T, R>;
|
|
65
|
+
/**
|
|
66
|
+
* Filter entities of this selection.
|
|
67
|
+
* @param filter A filter object or a predicate function.
|
|
68
|
+
*/
|
|
69
|
+
filter(this: Selection<Item<any>, R>, filter: ItemFilter): Selection<Item<any>, R>;
|
|
70
|
+
filter<U extends Entity>(this: Selection<U, R>, filter: Predicate<U>): Selection<U, R>;
|
|
71
|
+
/**
|
|
72
|
+
* Select children of the items in this selection.
|
|
73
|
+
*/
|
|
74
|
+
children(this: Selection<Item<any>, R>, filter?: ItemFilter): Selection<Item<any>, R>;
|
|
75
|
+
/**
|
|
76
|
+
* Select parent of the items in this selection.
|
|
77
|
+
*/
|
|
78
|
+
parent(this: Selection<Item<any>, R>): Selection<Item<any>, R>;
|
|
79
|
+
/**
|
|
80
|
+
* Select links sourcing from the items in this selection.
|
|
81
|
+
*/
|
|
82
|
+
links(this: Selection<Item<any>, R>, filter?: LinkFilter): Selection<Link, R>;
|
|
83
|
+
/**
|
|
84
|
+
* Select links pointing to items in this selection.
|
|
85
|
+
*/
|
|
86
|
+
refs(this: Selection<Item<any>, R>, filter?: LinkFilter): Selection<Link, R>;
|
|
87
|
+
/**
|
|
88
|
+
* Select targets of links in this selection.
|
|
89
|
+
*/
|
|
90
|
+
target(this: Selection<Link, R>, filter?: ItemFilter): Selection<Item<any>, R>;
|
|
91
|
+
/**
|
|
92
|
+
* Select sources of links in this selection.
|
|
93
|
+
*/
|
|
94
|
+
source(this: Selection<Link, R>, filter?: ItemFilter): Selection<Item<any>, R>;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=selection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.d.ts","sourceRoot":"","sources":["../../../../src/api/selection/selection.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAIL,QAAQ,EACR,UAAU,EACV,UAAU,EACV,SAAS,EACT,YAAY,EACZ,UAAU,EACX,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE5E;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,qBAEX,MAAM,IAAI,EAAE,uBAEN,MAAM,MAAM,MAAM,EAAE,CAAC,QACpC,aAAa,UACX,UAAU,GAAG,SAAS,sCAe/B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,YACxB,KAAK,GAAG,CAAC,UACP,MAAM,MAAM,EAAE,CAAC,sCAIxB,CAAC;AAEF;;;;;;;;GAQG;A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EAAE,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAWtF;;OAEG;IACH,MAAM,CAAE,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAO/D;;OAEG;IACH,KAAK,CAAE,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,GAAE,UAAe,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;IAQlF;;OAEG;IACH,IAAI,CAAE,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,GAAE,UAAe,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;IAQjF;;OAEG;IACH,MAAM,CAAE,IAAI,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,GAAE,UAAe,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAQnF;;OAEG;IACH,MAAM,CAAE,IAAI,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,GAAE,UAAe,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;CAOpF"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//
|
|
3
|
+
// Copyright 2020 DXOS.org
|
|
4
|
+
//
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Selection = exports.createItemSelection = exports.createSelection = void 0;
|
|
7
|
+
const queries_1 = require("./queries");
|
|
8
|
+
const result_1 = require("./result");
|
|
9
|
+
/**
|
|
10
|
+
* Factory for selector that provides a root set of items.
|
|
11
|
+
* @param itemsProvider
|
|
12
|
+
* @param updateEventProvider
|
|
13
|
+
* @param root
|
|
14
|
+
* @param filter
|
|
15
|
+
* @param value Initial reducer value.
|
|
16
|
+
*/
|
|
17
|
+
const createSelection = (
|
|
18
|
+
// Provider is called each time the query is executed.
|
|
19
|
+
itemsProvider,
|
|
20
|
+
// TODO(burdon): Replace with direct event handler.
|
|
21
|
+
updateEventProvider, root, filter, value) => {
|
|
22
|
+
const predicate = filter ? (0, queries_1.filterToPredicate)(filter) : () => true;
|
|
23
|
+
// TODO(burdon): Option to filter out system types.
|
|
24
|
+
const visitor = (options) => {
|
|
25
|
+
const items = itemsProvider()
|
|
26
|
+
.filter((0, queries_1.createQueryOptionsFilter)(options))
|
|
27
|
+
.filter(predicate);
|
|
28
|
+
return [items, value];
|
|
29
|
+
};
|
|
30
|
+
return new Selection(visitor, updateEventProvider(), root, value !== undefined);
|
|
31
|
+
};
|
|
32
|
+
exports.createSelection = createSelection;
|
|
33
|
+
/**
|
|
34
|
+
* Factory for specific item selector.
|
|
35
|
+
* @param root
|
|
36
|
+
* @param update
|
|
37
|
+
* @param value Initial reducer value.
|
|
38
|
+
*/
|
|
39
|
+
const createItemSelection = (root, update, value) => {
|
|
40
|
+
return new Selection(() => [[root], value], update, root, value !== undefined);
|
|
41
|
+
};
|
|
42
|
+
exports.createItemSelection = createItemSelection;
|
|
43
|
+
/**
|
|
44
|
+
* Selections are used to construct database subscriptions.
|
|
45
|
+
* They are [monads](https://www.quora.com/What-are-monads-in-computer-science) that support
|
|
46
|
+
* the functional composition of predicates to traverse the graph.
|
|
47
|
+
* Additionally, selections may be used to create reducers that compute an aggregated value over the traversal.
|
|
48
|
+
*
|
|
49
|
+
* Implementation:
|
|
50
|
+
* Each Selection contains a visitor
|
|
51
|
+
*/
|
|
52
|
+
class Selection {
|
|
53
|
+
/**
|
|
54
|
+
* @param _visitor Executes the query.
|
|
55
|
+
* @param _update The unfiltered update event.
|
|
56
|
+
* @param _root The root of the selection. Must be a stable reference.
|
|
57
|
+
* @param _reducer
|
|
58
|
+
*/
|
|
59
|
+
constructor(_visitor, _update, _root, _reducer = false) {
|
|
60
|
+
this._visitor = _visitor;
|
|
61
|
+
this._update = _update;
|
|
62
|
+
this._root = _root;
|
|
63
|
+
this._reducer = _reducer;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates a derrived selection by aplying a mapping function to the result of the current selection.
|
|
67
|
+
*/
|
|
68
|
+
_createSubSelection(map) {
|
|
69
|
+
return new Selection(options => map(this._visitor(options), options), this._update, this._root, this._reducer);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Finish the selection and return the result.
|
|
73
|
+
*/
|
|
74
|
+
exec(options = {}) {
|
|
75
|
+
return this.query(options);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @deprecated
|
|
79
|
+
*/
|
|
80
|
+
// TODO(burdon): Remove.
|
|
81
|
+
query(options = {}) {
|
|
82
|
+
return new result_1.SelectionResult(() => this._visitor(options), this._update, this._root, this._reducer);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The root of the selection. Either a database or an item. Must be a stable reference.
|
|
86
|
+
*/
|
|
87
|
+
get root() {
|
|
88
|
+
return this._root;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Visitor.
|
|
92
|
+
* @param visitor
|
|
93
|
+
*/
|
|
94
|
+
call(visitor) {
|
|
95
|
+
return this._createSubSelection(([items, result]) => [items, visitor(items, result)]);
|
|
96
|
+
}
|
|
97
|
+
filter(filter) {
|
|
98
|
+
const predicate = (0, queries_1.filterToPredicate)(filter);
|
|
99
|
+
return this._createSubSelection(([items, result]) => [items.filter(predicate), result]);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Select children of the items in this selection.
|
|
103
|
+
*/
|
|
104
|
+
children(filter) {
|
|
105
|
+
const predicate = filter ? (0, queries_1.filterToPredicate)(filter) : Boolean;
|
|
106
|
+
return this._createSubSelection(([items, result], options) => [
|
|
107
|
+
items.flatMap(item => Array.from(item._children.values())
|
|
108
|
+
.filter((0, queries_1.createQueryOptionsFilter)(options))
|
|
109
|
+
.filter(predicate)),
|
|
110
|
+
result
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Select parent of the items in this selection.
|
|
115
|
+
*/
|
|
116
|
+
parent() {
|
|
117
|
+
return this._createSubSelection(([items, result], options) => [
|
|
118
|
+
items.flatMap(item => item.parent ? [item.parent].filter((0, queries_1.createQueryOptionsFilter)(options)) : []),
|
|
119
|
+
result
|
|
120
|
+
]);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Select links sourcing from the items in this selection.
|
|
124
|
+
*/
|
|
125
|
+
links(filter = {}) {
|
|
126
|
+
const predicate = (0, queries_1.linkFilterToPredicate)(filter);
|
|
127
|
+
return this._createSubSelection(([items, result], options) => [
|
|
128
|
+
items.flatMap(item => item.links.filter(predicate).filter((0, queries_1.createQueryOptionsFilter)(options))),
|
|
129
|
+
result
|
|
130
|
+
]);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Select links pointing to items in this selection.
|
|
134
|
+
*/
|
|
135
|
+
refs(filter = {}) {
|
|
136
|
+
const predicate = (0, queries_1.linkFilterToPredicate)(filter);
|
|
137
|
+
return this._createSubSelection(([items, result], options) => [
|
|
138
|
+
items.flatMap(item => item.refs.filter(predicate).filter((0, queries_1.createQueryOptionsFilter)(options))),
|
|
139
|
+
result
|
|
140
|
+
]);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Select targets of links in this selection.
|
|
144
|
+
*/
|
|
145
|
+
target(filter = {}) {
|
|
146
|
+
const predicate = (0, queries_1.filterToPredicate)(filter);
|
|
147
|
+
return this._createSubSelection(([links, result], options) => [
|
|
148
|
+
links.flatMap(link => link.target).filter(predicate).filter((0, queries_1.createQueryOptionsFilter)(options)),
|
|
149
|
+
result
|
|
150
|
+
]);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Select sources of links in this selection.
|
|
154
|
+
*/
|
|
155
|
+
source(filter = {}) {
|
|
156
|
+
const predicate = (0, queries_1.filterToPredicate)(filter);
|
|
157
|
+
return this._createSubSelection(([links, result], options) => [
|
|
158
|
+
links.flatMap(link => link.source).filter(predicate).filter((0, queries_1.createQueryOptionsFilter)(options)),
|
|
159
|
+
result
|
|
160
|
+
]);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
exports.Selection = Selection;
|
|
164
|
+
//# sourceMappingURL=selection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.js","sourceRoot":"","sources":["../../../../src/api/selection/selection.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,0BAA0B;AAC1B,EAAE;;;AAOF,uCAUmB;AACnB,qCAA4E;AAE5E;;;;;;;GAOG;AACI,MAAM,eAAe,GAAG;AAC7B,sDAAsD;AACtD,aAA2B;AAC3B,mDAAmD;AACnD,mBAA0C,EAC1C,IAAmB,EACnB,MAA8B,EAC9B,KAAQ,EACiB,EAAE;IAC3B,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,IAAA,2BAAiB,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC;IAElE,mDAAmD;IACnD,MAAM,OAAO,GAAG,CAAC,OAAqB,EAA8B,EAAE;QACpE,MAAM,KAAK,GAAG,aAAa,EAAE;aAC1B,MAAM,CAAC,IAAA,kCAAwB,EAAC,OAAO,CAAC,CAAC;aACzC,MAAM,CAAC,SAAS,CAAC,CAAC;QAErB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,CAAC,CAAC;IAEF,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,mBAAmB,EAAE,EAAE,IAAI,EAAE,KAAK,KAAK,SAAS,CAAC,CAAC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AAE,CAAC;YAC5D,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,IAAA,kCAAwB,EAAC,OAAO,CAAC,CAAC;YAC9F,MAAM;SACP,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,MAAM,CAA4B,SAAqB,EAAE;QACvD,MAAM,SAAS,GAAG,IAAA,2BAAiB,EAAC,MAAM,CAAC,CAAC;QAC5C,OAAO,IAAI,CAAC,mBAAmB,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;YAC5D,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,IAAA,kCAAwB,EAAC,OAAO,CAAC,CAAC;YAC9F,MAAM;SACP,CAAC,CAAC;IACL,CAAC;CACF;AAnID,8BAmIC"}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.test.d.ts","sourceRoot":"","sources":["../../../../src/api/selection/selection.test.ts"],"names":[],"mappings":""}
|
|
@@ -12,14 +12,16 @@ const async_1 = require("@dxos/async");
|
|
|
12
12
|
const crypto_1 = require("@dxos/crypto");
|
|
13
13
|
const model_factory_1 = require("@dxos/model-factory");
|
|
14
14
|
const object_model_1 = require("@dxos/object-model");
|
|
15
|
-
const item_1 = require("
|
|
16
|
-
const link_1 = require("
|
|
15
|
+
const item_1 = require("../item");
|
|
16
|
+
const link_1 = require("../link");
|
|
17
17
|
const selection_1 = require("./selection");
|
|
18
18
|
// Use to prevent ultra-long diffs.
|
|
19
19
|
const ids = (entities) => entities.map(entity => entity.id);
|
|
20
20
|
const modelFactory = new model_factory_1.ModelFactory().registerModel(object_model_1.ObjectModel);
|
|
21
21
|
const createModel = (id) => modelFactory.createModel(object_model_1.ObjectModel.meta.type, id, {}, crypto_1.PublicKey.random());
|
|
22
|
-
const createItem = (id, type, parent) =>
|
|
22
|
+
const createItem = (id, type, parent) => {
|
|
23
|
+
return new item_1.Item(null, id, type, createModel(id), undefined, parent);
|
|
24
|
+
};
|
|
23
25
|
const createLink = (id, type, source, target) => {
|
|
24
26
|
const link = new link_1.Link(null, id, type, createModel(id), {
|
|
25
27
|
sourceId: source.id,
|
|
@@ -31,8 +33,8 @@ const createLink = (id, type, source, target) => {
|
|
|
31
33
|
target._refs.add(link);
|
|
32
34
|
return link;
|
|
33
35
|
};
|
|
34
|
-
const
|
|
35
|
-
const createReducer = (result) => (0, selection_1.
|
|
36
|
+
const createRootSelection = (filter) => (0, selection_1.createSelection)(() => items, () => new async_1.Event(), null, filter, undefined);
|
|
37
|
+
const createReducer = (result) => (0, selection_1.createSelection)(() => items, () => new async_1.Event(), null, undefined, result);
|
|
36
38
|
// TODO(burdon): Use more complex data set (org, person, project, task).
|
|
37
39
|
const ITEM_ORG = 'example:item/org';
|
|
38
40
|
const ITEM_PROJECT = 'example:item/project';
|
|
@@ -68,61 +70,61 @@ const links = [
|
|
|
68
70
|
describe('Selection', () => {
|
|
69
71
|
describe('root', () => {
|
|
70
72
|
(0, mocha_1.it)('all', () => {
|
|
71
|
-
(0, expect_1.default)(
|
|
72
|
-
.
|
|
73
|
+
(0, expect_1.default)(createRootSelection()
|
|
74
|
+
.exec().entities).toHaveLength(items.length);
|
|
73
75
|
});
|
|
74
76
|
(0, mocha_1.it)('by id', () => {
|
|
75
|
-
(0, expect_1.default)(
|
|
76
|
-
.
|
|
77
|
-
(0, expect_1.default)(
|
|
78
|
-
.
|
|
77
|
+
(0, expect_1.default)(createRootSelection({ id: org1.id })
|
|
78
|
+
.exec().entities).toEqual([org1]);
|
|
79
|
+
(0, expect_1.default)(createRootSelection({ id: org2.id })
|
|
80
|
+
.exec().entities).toEqual([org2]);
|
|
79
81
|
});
|
|
80
82
|
(0, mocha_1.it)('single type', () => {
|
|
81
|
-
(0, expect_1.default)(
|
|
82
|
-
.
|
|
83
|
+
(0, expect_1.default)(createRootSelection({ type: ITEM_PROJECT })
|
|
84
|
+
.exec().entities).toHaveLength(3);
|
|
83
85
|
});
|
|
84
86
|
(0, mocha_1.it)('multiple types', () => {
|
|
85
|
-
(0, expect_1.default)(
|
|
86
|
-
.
|
|
87
|
+
(0, expect_1.default)(createRootSelection({ type: [ITEM_ORG, ITEM_PROJECT] })
|
|
88
|
+
.exec().entities).toHaveLength(5);
|
|
87
89
|
});
|
|
88
90
|
});
|
|
89
91
|
describe('filter', () => {
|
|
90
92
|
(0, mocha_1.it)('invalid', () => {
|
|
91
|
-
(0, expect_1.default)(
|
|
92
|
-
.filter({ type: 'dxos:type
|
|
93
|
-
.
|
|
93
|
+
(0, expect_1.default)(createRootSelection()
|
|
94
|
+
.filter({ type: 'dxos:type/invalid' })
|
|
95
|
+
.exec().entities).toHaveLength(0);
|
|
94
96
|
});
|
|
95
97
|
(0, mocha_1.it)('single type', () => {
|
|
96
|
-
(0, expect_1.default)(
|
|
98
|
+
(0, expect_1.default)(createRootSelection()
|
|
97
99
|
.filter({ type: ITEM_PROJECT })
|
|
98
|
-
.
|
|
100
|
+
.exec().entities).toHaveLength(3);
|
|
99
101
|
});
|
|
100
102
|
(0, mocha_1.it)('multiple types', () => {
|
|
101
|
-
(0, expect_1.default)(
|
|
103
|
+
(0, expect_1.default)(createRootSelection()
|
|
102
104
|
.filter({ type: [ITEM_ORG, ITEM_PROJECT] })
|
|
103
|
-
.
|
|
105
|
+
.exec().entities).toHaveLength(5);
|
|
104
106
|
});
|
|
105
107
|
(0, mocha_1.it)('by function', () => {
|
|
106
|
-
(0, expect_1.default)(
|
|
108
|
+
(0, expect_1.default)(createRootSelection()
|
|
107
109
|
.filter(item => item.type === ITEM_ORG)
|
|
108
|
-
.
|
|
110
|
+
.exec().entities).toHaveLength(2);
|
|
109
111
|
});
|
|
110
112
|
});
|
|
111
113
|
describe('children', () => {
|
|
112
114
|
(0, mocha_1.it)('from multiple items', () => {
|
|
113
|
-
(0, expect_1.default)(ids(
|
|
115
|
+
(0, expect_1.default)(ids(createRootSelection()
|
|
114
116
|
.filter({ type: ITEM_ORG })
|
|
115
117
|
.children({ type: ITEM_PROJECT })
|
|
116
|
-
.
|
|
118
|
+
.exec().entities)).toStrictEqual(ids([
|
|
117
119
|
project1,
|
|
118
120
|
project2,
|
|
119
121
|
project3
|
|
120
122
|
]));
|
|
121
123
|
});
|
|
122
124
|
(0, mocha_1.it)('from single item', () => {
|
|
123
|
-
(0, expect_1.default)(ids(
|
|
125
|
+
(0, expect_1.default)(ids(createRootSelection({ id: org1.id })
|
|
124
126
|
.children()
|
|
125
|
-
.
|
|
127
|
+
.exec().entities)).toStrictEqual(ids([
|
|
126
128
|
project1,
|
|
127
129
|
project2,
|
|
128
130
|
person1,
|
|
@@ -132,47 +134,47 @@ describe('Selection', () => {
|
|
|
132
134
|
});
|
|
133
135
|
describe('parent', () => {
|
|
134
136
|
(0, mocha_1.it)('from multiple items', () => {
|
|
135
|
-
(0, expect_1.default)(ids(
|
|
137
|
+
(0, expect_1.default)(ids(createRootSelection()
|
|
136
138
|
.filter({ type: ITEM_PROJECT })
|
|
137
139
|
.parent()
|
|
138
|
-
.
|
|
140
|
+
.exec().entities)).toStrictEqual(ids([
|
|
139
141
|
org1,
|
|
140
142
|
org2
|
|
141
143
|
]));
|
|
142
144
|
});
|
|
143
145
|
(0, mocha_1.it)('from single item', () => {
|
|
144
|
-
(0, expect_1.default)(ids(
|
|
146
|
+
(0, expect_1.default)(ids(createRootSelection({ id: project1.id })
|
|
145
147
|
.parent()
|
|
146
|
-
.
|
|
148
|
+
.exec().entities)).toStrictEqual(ids([
|
|
147
149
|
org1
|
|
148
150
|
]));
|
|
149
151
|
});
|
|
150
152
|
(0, mocha_1.it)('is empty', () => {
|
|
151
|
-
(0, expect_1.default)(
|
|
153
|
+
(0, expect_1.default)(createRootSelection({ id: org1.id })
|
|
152
154
|
.parent()
|
|
153
|
-
.
|
|
155
|
+
.exec().entities).toEqual([]);
|
|
154
156
|
});
|
|
155
157
|
});
|
|
156
158
|
describe('links', () => {
|
|
157
159
|
(0, mocha_1.it)('links from single item', () => {
|
|
158
|
-
(0, expect_1.default)(ids(
|
|
160
|
+
(0, expect_1.default)(ids(createRootSelection({ id: project1.id })
|
|
159
161
|
.links()
|
|
160
162
|
.target()
|
|
161
|
-
.
|
|
163
|
+
.exec().entities)).toStrictEqual(ids([
|
|
162
164
|
person1,
|
|
163
165
|
person2
|
|
164
166
|
]));
|
|
165
167
|
});
|
|
166
168
|
(0, mocha_1.it)('links from multiple items', () => {
|
|
167
|
-
(0, expect_1.default)(
|
|
169
|
+
(0, expect_1.default)(createRootSelection({ type: ITEM_PROJECT })
|
|
168
170
|
.links()
|
|
169
|
-
.
|
|
171
|
+
.exec().entities).toHaveLength(links.length);
|
|
170
172
|
});
|
|
171
173
|
(0, mocha_1.it)('sources', () => {
|
|
172
|
-
(0, expect_1.default)(ids(
|
|
174
|
+
(0, expect_1.default)(ids(createRootSelection({ type: ITEM_PERSON })
|
|
173
175
|
.refs()
|
|
174
176
|
.source()
|
|
175
|
-
.
|
|
177
|
+
.exec().entities)).toStrictEqual(ids([
|
|
176
178
|
project1,
|
|
177
179
|
project2
|
|
178
180
|
]));
|
|
@@ -180,7 +182,7 @@ describe('Selection', () => {
|
|
|
180
182
|
});
|
|
181
183
|
describe('reducer', () => {
|
|
182
184
|
(0, mocha_1.it)('simple reducer', () => {
|
|
183
|
-
const query = createReducer(0).call((items, count) => count + items.length).
|
|
185
|
+
const query = createReducer(0).call((items, count) => count + items.length).exec();
|
|
184
186
|
(0, expect_1.default)(query.value).toEqual(items.length);
|
|
185
187
|
});
|
|
186
188
|
// TODO(burdon): Support nested traverals (context as third arg?)
|
|
@@ -199,16 +201,16 @@ describe('Selection', () => {
|
|
|
199
201
|
return { ...rest, numLinks: numLinks + links.length, stage: 'c' };
|
|
200
202
|
})
|
|
201
203
|
.target()
|
|
202
|
-
.
|
|
204
|
+
.exec();
|
|
203
205
|
(0, expect_1.default)(query.value).toEqual({ numItems: 5, numLinks: 4, stage: 'c' });
|
|
204
206
|
});
|
|
205
207
|
});
|
|
206
208
|
describe('events', () => {
|
|
207
209
|
(0, mocha_1.it)('events get filtered correctly', async () => {
|
|
208
210
|
const update = new async_1.Event();
|
|
209
|
-
const query = (0, selection_1.
|
|
211
|
+
const query = (0, selection_1.createSelection)(() => items, () => update, null, { type: ITEM_ORG }, undefined)
|
|
210
212
|
.children()
|
|
211
|
-
.
|
|
213
|
+
.exec();
|
|
212
214
|
{
|
|
213
215
|
const promise = query.update.waitForCount(1);
|
|
214
216
|
update.emit([project1]);
|