1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.SyncDelegateHost = exports.SynchronousDelegateExpectedException = void 0;
|
---|
11 | const exception_1 = require("../../exception");
|
---|
12 | class SynchronousDelegateExpectedException extends exception_1.BaseException {
|
---|
13 | constructor() {
|
---|
14 | super(`Expected a synchronous delegate but got an asynchronous one.`);
|
---|
15 | }
|
---|
16 | }
|
---|
17 | exports.SynchronousDelegateExpectedException = SynchronousDelegateExpectedException;
|
---|
18 | /**
|
---|
19 | * Implement a synchronous-only host interface (remove the Observable parts).
|
---|
20 | */
|
---|
21 | class SyncDelegateHost {
|
---|
22 | constructor(_delegate) {
|
---|
23 | this._delegate = _delegate;
|
---|
24 | if (!_delegate.capabilities.synchronous) {
|
---|
25 | throw new SynchronousDelegateExpectedException();
|
---|
26 | }
|
---|
27 | }
|
---|
28 | _doSyncCall(observable) {
|
---|
29 | let completed = false;
|
---|
30 | let result = undefined;
|
---|
31 | let errorResult = undefined;
|
---|
32 | // Perf note: this is not using an observer object to avoid a performance penalty in RxJS.
|
---|
33 | // See https://github.com/ReactiveX/rxjs/pull/5646 for details.
|
---|
34 | observable.subscribe((x) => (result = x), (err) => (errorResult = err), () => (completed = true));
|
---|
35 | if (errorResult !== undefined) {
|
---|
36 | throw errorResult;
|
---|
37 | }
|
---|
38 | if (!completed) {
|
---|
39 | throw new SynchronousDelegateExpectedException();
|
---|
40 | }
|
---|
41 | // The non-null operation is to work around `void` type. We don't allow to return undefined
|
---|
42 | // but ResultT could be void, which is undefined in JavaScript, so this doesn't change the
|
---|
43 | // behaviour.
|
---|
44 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
---|
45 | return result;
|
---|
46 | }
|
---|
47 | get capabilities() {
|
---|
48 | return this._delegate.capabilities;
|
---|
49 | }
|
---|
50 | get delegate() {
|
---|
51 | return this._delegate;
|
---|
52 | }
|
---|
53 | write(path, content) {
|
---|
54 | return this._doSyncCall(this._delegate.write(path, content));
|
---|
55 | }
|
---|
56 | read(path) {
|
---|
57 | return this._doSyncCall(this._delegate.read(path));
|
---|
58 | }
|
---|
59 | delete(path) {
|
---|
60 | return this._doSyncCall(this._delegate.delete(path));
|
---|
61 | }
|
---|
62 | rename(from, to) {
|
---|
63 | return this._doSyncCall(this._delegate.rename(from, to));
|
---|
64 | }
|
---|
65 | list(path) {
|
---|
66 | return this._doSyncCall(this._delegate.list(path));
|
---|
67 | }
|
---|
68 | exists(path) {
|
---|
69 | return this._doSyncCall(this._delegate.exists(path));
|
---|
70 | }
|
---|
71 | isDirectory(path) {
|
---|
72 | return this._doSyncCall(this._delegate.isDirectory(path));
|
---|
73 | }
|
---|
74 | isFile(path) {
|
---|
75 | return this._doSyncCall(this._delegate.isFile(path));
|
---|
76 | }
|
---|
77 | // Some hosts may not support stat.
|
---|
78 | stat(path) {
|
---|
79 | const result = this._delegate.stat(path);
|
---|
80 | if (result) {
|
---|
81 | return this._doSyncCall(result);
|
---|
82 | }
|
---|
83 | else {
|
---|
84 | return null;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | watch(path, options) {
|
---|
88 | return this._delegate.watch(path, options);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | exports.SyncDelegateHost = SyncDelegateHost;
|
---|