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.SimpleSinkBase = void 0;
|
---|
11 | const rxjs_1 = require("rxjs");
|
---|
12 | const operators_1 = require("rxjs/operators");
|
---|
13 | const exception_1 = require("../exception/exception");
|
---|
14 | const action_1 = require("../tree/action");
|
---|
15 | const Noop = function () { };
|
---|
16 | class SimpleSinkBase {
|
---|
17 | constructor() {
|
---|
18 | this.preCommitAction = Noop;
|
---|
19 | this.postCommitAction = Noop;
|
---|
20 | this.preCommit = Noop;
|
---|
21 | this.postCommit = Noop;
|
---|
22 | }
|
---|
23 | _fileAlreadyExistException(path) {
|
---|
24 | throw new exception_1.FileAlreadyExistException(path);
|
---|
25 | }
|
---|
26 | _fileDoesNotExistException(path) {
|
---|
27 | throw new exception_1.FileDoesNotExistException(path);
|
---|
28 | }
|
---|
29 | _validateOverwriteAction(action) {
|
---|
30 | return this._validateFileExists(action.path).pipe(operators_1.map((b) => {
|
---|
31 | if (!b) {
|
---|
32 | this._fileDoesNotExistException(action.path);
|
---|
33 | }
|
---|
34 | }));
|
---|
35 | }
|
---|
36 | _validateCreateAction(action) {
|
---|
37 | return this._validateFileExists(action.path).pipe(operators_1.map((b) => {
|
---|
38 | if (b) {
|
---|
39 | this._fileAlreadyExistException(action.path);
|
---|
40 | }
|
---|
41 | }));
|
---|
42 | }
|
---|
43 | _validateRenameAction(action) {
|
---|
44 | return this._validateFileExists(action.path).pipe(operators_1.map((b) => {
|
---|
45 | if (!b) {
|
---|
46 | this._fileDoesNotExistException(action.path);
|
---|
47 | }
|
---|
48 | }), operators_1.mergeMap(() => this._validateFileExists(action.to)), operators_1.map((b) => {
|
---|
49 | if (b) {
|
---|
50 | this._fileAlreadyExistException(action.to);
|
---|
51 | }
|
---|
52 | }));
|
---|
53 | }
|
---|
54 | _validateDeleteAction(action) {
|
---|
55 | return this._validateFileExists(action.path).pipe(operators_1.map((b) => {
|
---|
56 | if (!b) {
|
---|
57 | this._fileDoesNotExistException(action.path);
|
---|
58 | }
|
---|
59 | }));
|
---|
60 | }
|
---|
61 | validateSingleAction(action) {
|
---|
62 | switch (action.kind) {
|
---|
63 | case 'o':
|
---|
64 | return this._validateOverwriteAction(action);
|
---|
65 | case 'c':
|
---|
66 | return this._validateCreateAction(action);
|
---|
67 | case 'r':
|
---|
68 | return this._validateRenameAction(action);
|
---|
69 | case 'd':
|
---|
70 | return this._validateDeleteAction(action);
|
---|
71 | default:
|
---|
72 | throw new action_1.UnknownActionException(action);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | commitSingleAction(action) {
|
---|
76 | return rxjs_1.concat(this.validateSingleAction(action), new rxjs_1.Observable((observer) => {
|
---|
77 | let committed = null;
|
---|
78 | switch (action.kind) {
|
---|
79 | case 'o':
|
---|
80 | committed = this._overwriteFile(action.path, action.content);
|
---|
81 | break;
|
---|
82 | case 'c':
|
---|
83 | committed = this._createFile(action.path, action.content);
|
---|
84 | break;
|
---|
85 | case 'r':
|
---|
86 | committed = this._renameFile(action.path, action.to);
|
---|
87 | break;
|
---|
88 | case 'd':
|
---|
89 | committed = this._deleteFile(action.path);
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | if (committed) {
|
---|
93 | committed.subscribe(observer);
|
---|
94 | }
|
---|
95 | else {
|
---|
96 | observer.complete();
|
---|
97 | }
|
---|
98 | })).pipe(operators_1.ignoreElements());
|
---|
99 | }
|
---|
100 | commit(tree) {
|
---|
101 | const actions = rxjs_1.from(tree.actions);
|
---|
102 | return rxjs_1.concat(this.preCommit() || rxjs_1.of(null), rxjs_1.defer(() => actions).pipe(operators_1.concatMap((action) => {
|
---|
103 | const maybeAction = this.preCommitAction(action);
|
---|
104 | if (rxjs_1.isObservable(maybeAction) || isPromiseLike(maybeAction)) {
|
---|
105 | return maybeAction;
|
---|
106 | }
|
---|
107 | return rxjs_1.of(maybeAction || action);
|
---|
108 | }), operators_1.concatMap((action) => {
|
---|
109 | return rxjs_1.concat(this.commitSingleAction(action).pipe(operators_1.ignoreElements()), rxjs_1.of(action));
|
---|
110 | }), operators_1.concatMap((action) => this.postCommitAction(action) || rxjs_1.of(null))), rxjs_1.defer(() => this._done()), rxjs_1.defer(() => this.postCommit() || rxjs_1.of(null))).pipe(operators_1.ignoreElements());
|
---|
111 | }
|
---|
112 | }
|
---|
113 | exports.SimpleSinkBase = SimpleSinkBase;
|
---|
114 | function isPromiseLike(value) {
|
---|
115 | return !!value && typeof value.then === 'function';
|
---|
116 | }
|
---|