1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | require("reflect-metadata");
|
5 | const inversify_1 = require("inversify");
|
6 | const mobx_1 = require("mobx");
|
7 | const mobx_utils_1 = require("mobx-utils");
|
8 | const rxdb_1 = require("rxdb");
|
9 | const ajv_validate_1 = require("rxdb/plugins/ajv-validate");
|
10 | const rxjs_1 = require("rxjs");
|
11 | const rxjs_2 = require("rxjs");
|
12 | const operators_1 = require("rxjs/operators");
|
13 | const util_1 = require("util");
|
14 | const NarwhalSchema_1 = require("./data_engine/NarwhalSchema");
|
15 | const schema_parser_1 = require("./data_engine/schema_parser");
|
16 | const proxied_collection_1 = require("./data_engine/proxied_collection");
|
17 | const predicate_parser_1 = require("./predicate_parser");
|
18 | const TYPES_1 = require("./TYPES");
|
19 | class DataEngineDatabaseContainer {
|
20 | init(db) {
|
21 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
22 | if (db) {
|
23 | this.db = db;
|
24 | }
|
25 | else {
|
26 | }
|
27 | });
|
28 | }
|
29 | }
|
30 | exports.DataEngineDatabaseContainer = DataEngineDatabaseContainer;
|
31 | let EmptyContext = class EmptyContext {
|
32 | context() {
|
33 | return {};
|
34 | }
|
35 | };
|
36 | EmptyContext = tslib_1.__decorate([
|
37 | inversify_1.injectable()
|
38 | ], EmptyContext);
|
39 | exports.EmptyContext = EmptyContext;
|
40 | let SchemaNamespaceHandlerHook = class SchemaNamespaceHandlerHook {
|
41 | handleNamespace(name, version, namespace) {
|
42 | Object.entries(namespace.schema).forEach(([schemaName, schema]) => {
|
43 | this.context.addSchema(name, version, schemaName, schema);
|
44 | });
|
45 | }
|
46 | };
|
47 | tslib_1.__decorate([
|
48 | inversify_1.inject(TYPES_1.CORE_TYPES.NarwhalSchemaContext),
|
49 | tslib_1.__metadata("design:type", schema_parser_1.NarwhalSchemaContext)
|
50 | ], SchemaNamespaceHandlerHook.prototype, "context", void 0);
|
51 | SchemaNamespaceHandlerHook = tslib_1.__decorate([
|
52 | inversify_1.injectable()
|
53 | ], SchemaNamespaceHandlerHook);
|
54 | let NamespaceHandler = class NamespaceHandler {
|
55 | addNamespace(namespace_name, namespace, version = 0) {
|
56 | for (const hook of this.hooks) {
|
57 | hook.handleNamespace(namespace_name, version, namespace);
|
58 | }
|
59 | }
|
60 | };
|
61 | tslib_1.__decorate([
|
62 | inversify_1.multiInject(TYPES_1.CORE_TYPES.NamespaceHandlerHook),
|
63 | tslib_1.__metadata("design:type", Array)
|
64 | ], NamespaceHandler.prototype, "hooks", void 0);
|
65 | NamespaceHandler = tslib_1.__decorate([
|
66 | inversify_1.injectable()
|
67 | ], NamespaceHandler);
|
68 | exports.NamespaceHandler = NamespaceHandler;
|
69 | const getStream = (query) => {
|
70 | return query.$.pipe(operators_1.distinctUntilChanged((left, right) => {
|
71 | if (util_1.isArray(left) && util_1.isArray(right)) {
|
72 | const left_gids = left.map(leftEl => leftEl._rev);
|
73 | const right_gids = right.map(rightEl => rightEl._rev);
|
74 | return left_gids.every((v, i) => v === right_gids[i]) && right_gids.every((v, i) => v === left_gids[i]);
|
75 | }
|
76 | else if (left == null || right == null) {
|
77 | return left === right;
|
78 | }
|
79 | else {
|
80 | return left._rev === right._rev;
|
81 | }
|
82 | }), operators_1.map(val => {
|
83 | try {
|
84 | if (val && !val.query) {
|
85 | Object.defineProperty(val, "query", {
|
86 | value: query,
|
87 | enumerable: false,
|
88 | writable: false,
|
89 | });
|
90 | }
|
91 | }
|
92 | catch (e) {
|
93 | console.warn(e);
|
94 | }
|
95 | return val;
|
96 | }));
|
97 | };
|
98 | exports.container_module_generator = (db) => {
|
99 |
|
100 | function annotateQuery(query) {
|
101 | if (!query.hasOwnProperty("results")) {
|
102 |
|
103 | if (query.op === "find") {
|
104 |
|
105 | const mutableMap = mobx_1.observable.array([], { deep: false });
|
106 | if (!mutableMap.hasOwnProperty("unresolved")) {
|
107 | Object.defineProperty(mutableMap, "unresolved", {
|
108 | get() {
|
109 |
|
110 |
|
111 | const atom = mobx_1.getAtom(this);
|
112 | atom.reportObserved();
|
113 |
|
114 | return this._unresolved;
|
115 | },
|
116 | set(val) {
|
117 |
|
118 | this._unresolved = val;
|
119 | const atom = mobx_1.getAtom(this);
|
120 | atom.reportChanged();
|
121 | },
|
122 | });
|
123 | }
|
124 |
|
125 | mutableMap.unresolved = true;
|
126 |
|
127 | mutableMap.query = query;
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | let qSub;
|
135 | const qStream = getStream(query);
|
136 | mobx_1.onBecomeObserved(mutableMap, () => {
|
137 | qSub = qStream.subscribe({
|
138 | next(arr) {
|
139 | if (arr.length !== mutableMap.length || !mutableMap.every((v, i) => arr[i] === v)) {
|
140 | mutableMap.splice(0, arr.length, ...arr);
|
141 | }
|
142 |
|
143 | mutableMap.unresolved = false;
|
144 | },
|
145 | });
|
146 | });
|
147 | mobx_1.onBecomeUnobserved(mutableMap, () => {
|
148 | if (qSub) {
|
149 | qSub.unsubscribe();
|
150 | }
|
151 | qSub = null;
|
152 | });
|
153 |
|
154 | mobx_1.extendObservable(query, { results: mutableMap });
|
155 | }
|
156 | else if (query.op === "findOne") {
|
157 | const orig = {
|
158 | unresolved: true,
|
159 | query,
|
160 | };
|
161 | let sub;
|
162 | const stream = rxjs_1.merge(getStream(query), rxjs_2.from(query.exec()));
|
163 | const observableResource = mobx_utils_1.fromResource(callback => {
|
164 | sub = stream.subscribe(callback);
|
165 | }, () => {
|
166 | sub.unsubscribe();
|
167 | }, orig);
|
168 | mobx_1.extendObservable(query, {
|
169 | get results() {
|
170 | return observableResource.current();
|
171 | },
|
172 | enumerable: true,
|
173 | });
|
174 | }
|
175 | }
|
176 |
|
177 | const old_clone = query._clone.bind(query);
|
178 | function _clone(...args) {
|
179 | const res = old_clone(...args);
|
180 | annotateQuery(res);
|
181 | return res;
|
182 | }
|
183 |
|
184 | query._clone = _clone.bind(query);
|
185 | }
|
186 | rxdb_1.default.plugin({
|
187 | rxdb: true,
|
188 | hooks: {
|
189 | createRxQuery: (query) => {
|
190 | annotateQuery(query);
|
191 | },
|
192 | },
|
193 | });
|
194 | rxdb_1.default.plugin(ajv_validate_1.default);
|
195 | return new inversify_1.ContainerModule((bind) => {
|
196 | bind(TYPES_1.CORE_TYPES.PredicateParser).to(predicate_parser_1.PredicateParser).inSingletonScope();
|
197 | bind(TYPES_1.CORE_TYPES.NarwhalSchemaContext).to(schema_parser_1.NarwhalSchemaContext).inSingletonScope();
|
198 | bind(TYPES_1.CORE_TYPES.NamespaceHandler).to(NamespaceHandler).inSingletonScope();
|
199 | bind(TYPES_1.CORE_TYPES.NamespaceHandlerHook).to(SchemaNamespaceHandlerHook);
|
200 | bind(TYPES_1.CORE_TYPES.SystemContextComponent).to(EmptyContext).inSingletonScope();
|
201 | bind(TYPES_1.CORE_TYPES.NarwhalSchema).to(NarwhalSchema_1.NarwhalSchema);
|
202 | bind(TYPES_1.CORE_TYPES.NarwhalSchemaFactory).toAutoFactory(TYPES_1.CORE_TYPES.NarwhalSchema);
|
203 | bind(TYPES_1.CORE_TYPES.DataEngineDatabase).toConstantValue(db);
|
204 | bind(TYPES_1.CORE_TYPES.ConcreteProxiedCollection).to(proxied_collection_1.ConcreteProxiedCollection);
|
205 | bind(TYPES_1.CORE_TYPES.ProxiedCollection).to(proxied_collection_1.ProxiedCollection);
|
206 | bind(TYPES_1.CORE_TYPES.ProxiedCollectionFactory).toAutoFactory(TYPES_1.CORE_TYPES.ProxiedCollection);
|
207 | bind(TYPES_1.CORE_TYPES.ConcreteProxiedCollectionFactory).toFactory(context => (schema, collection) => {
|
208 | const proxied_collection = context.container.get(TYPES_1.CORE_TYPES.ConcreteProxiedCollection);
|
209 | proxied_collection.init(schema, collection);
|
210 | return proxied_collection;
|
211 | });
|
212 | });
|
213 | };
|
214 |
|
\ | No newline at end of file |