|
5 | 5 | /*#ifndef(UMD)*/
|
6 | 6 | "use strict";
|
7 | 7 | /*global _gpfDefine*/ // Shortcut for gpf.define
|
8 |
| -/*global _gpfEmptyFunc*/ // An empty function |
9 |
| -/*global _gpfStreamSecureInstallProgressFlag*/ // Install the progress flag used by _gpfStreamSecureRead and Write |
10 |
| -/*global _gpfStreamSecureRead*/ // Generate a wrapper to secure multiple calls to stream#read |
11 |
| -/*global _gpfStreamSecureWrite*/ // Generates a wrapper to secure multiple calls to stream#write |
| 8 | +/*global _GpfStreamAbtsractOperator*/ // gpf.stream.AbstractOperator |
12 | 9 | /*exported _GpfStreamFilter*/ // gpf.stream.Filter
|
13 | 10 | /*#endif*/
|
14 | 11 |
|
15 | 12 | var
|
16 | 13 | _GpfStreamFilter = _gpfDefine({
|
17 | 14 | $class: "gpf.stream.Filter",
|
| 15 | + $extend: _GpfStreamAbtsractOperator, |
18 | 16 |
|
19 | 17 | /**
|
20 | 18 | * Filter stream
|
|
30 | 28 | this._filter = filter;
|
31 | 29 | },
|
32 | 30 |
|
33 |
| - //region internal handling |
34 |
| - |
35 |
| - /** |
36 |
| - * Promise used to wait for data |
37 |
| - * @type {Promise} |
38 |
| - * @since 0.2.4 |
39 |
| - */ |
40 |
| - _dataInPromise: undefined, |
41 |
| - |
42 |
| - /** |
43 |
| - * Resolve function of _dataInPromise |
44 |
| - * @type {Function} |
45 |
| - * @since 0.2.4 |
46 |
| - */ |
47 |
| - _dataInResolve: _gpfEmptyFunc, |
48 |
| - |
49 |
| - /** |
50 |
| - * Resolve function of _writeData's Promise |
51 |
| - * @type {Function} |
52 |
| - * @since 0.2.4 |
53 |
| - */ |
54 |
| - _dataOutResolve: _gpfEmptyFunc, |
55 |
| - |
56 |
| - /** |
57 |
| - * Reject function of _writeData's Promise |
58 |
| - * @type {Function} |
59 |
| - * @since 0.2.4 |
60 |
| - */ |
61 |
| - _dataOutReject: _gpfEmptyFunc, |
62 |
| - |
63 |
| - /** |
64 |
| - * Wait until data was written to this stream |
65 |
| - * |
66 |
| - * @return {Promise} Resolved when a data as been written to this stream |
67 |
| - * @since 0.2.4 |
68 |
| - */ |
69 |
| - _waitForData: function () { |
70 |
| - var me = this; |
71 |
| - if (!me._dataInPromise) { |
72 |
| - me._dataInPromise = new Promise(function (resolve) { |
73 |
| - me._dataInResolve = resolve; |
74 |
| - }).then(function (data) { |
75 |
| - delete me._dataInPromise; |
76 |
| - delete me._dataInResolve; |
77 |
| - return data; |
78 |
| - }); |
79 |
| - } |
80 |
| - return me._dataInPromise; |
81 |
| - }, |
82 |
| - |
83 |
| - /** |
84 |
| - * Waits for the read API to write it out |
85 |
| - * |
86 |
| - * @param {*} data Data to write |
87 |
| - * @return {Promise} Resolved when write operation has been done on output |
88 |
| - * @since 0.2.4 |
89 |
| - */ |
90 |
| - _writeData: function (data) { |
91 |
| - var me = this; |
92 |
| - me._waitForData(); |
93 |
| - me._dataInResolve(data); |
94 |
| - return new Promise(function (resolve, reject) { |
95 |
| - me._dataOutResolve = resolve; |
96 |
| - me._dataOutReject = reject; |
97 |
| - }).then(function (value) { |
98 |
| - delete me._dataOutResolve; |
99 |
| - delete me._dataOutReject; |
100 |
| - return value; |
101 |
| - }, function (reason) { |
102 |
| - delete me._dataOutResolve; |
103 |
| - delete me._dataOutReject; |
104 |
| - return Promise.reject(reason); |
105 |
| - }); |
106 |
| - }, |
107 |
| - |
108 |
| - //endregion |
109 |
| - |
110 |
| - //region gpf.interfaces.IReadableStream |
111 |
| - |
112 |
| - /** |
113 |
| - * @gpf:sameas gpf.interfaces.IReadableStream#read |
114 |
| - * @since 0.2.4 |
115 |
| - */ |
116 |
| - read: _gpfStreamSecureRead(function (output) { |
117 |
| - var me = this; //eslint-disable-line no-invalid-this |
118 |
| - return me._waitForData() |
119 |
| - .then(function (data) { |
120 |
| - if (undefined !== data) { |
121 |
| - return output.write(data).then(me._dataOutResolve, me._dataOutReject); |
122 |
| - } |
123 |
| - me._dataOutResolve(); |
124 |
| - return Promise.resolve(); // Nothing to write |
125 |
| - }); |
126 |
| - }), |
127 |
| - |
128 |
| - //endregion |
129 |
| - |
130 |
| - //region gpf.interfaces.IWritableStream |
131 |
| - |
132 |
| - /** |
133 |
| - * @gpf:sameas gpf.interfaces.IWritableStream#write |
134 |
| - * @since 0.2.4 |
135 |
| - */ |
136 |
| - write: _gpfStreamSecureWrite(function (data) { |
137 |
| - var me = this; //eslint-disable-line no-invalid-this |
138 |
| - if (me._filter(data)) { |
139 |
| - return me._writeData(data); |
140 |
| - } |
141 |
| - return Promise.resolve(); |
142 |
| - }), |
143 |
| - |
144 |
| - //endregion |
145 |
| - |
146 |
| - //region gpf.interfaces.IFlushableStream |
147 |
| - |
148 |
| - /** |
149 |
| - * @gpf:sameas gpf.interfaces.IFlushableStream#flush |
150 |
| - * @since 0.2.4 |
151 |
| - */ |
152 |
| - flush: function () { |
153 |
| - if (this._dataInPromise) { |
154 |
| - return this._writeData(); |
| 31 | + _process: function (data) { |
| 32 | + if (this._filter(data)) { |
| 33 | + return this._writeData(data); |
155 | 34 | }
|
156 | 35 | return Promise.resolve();
|
157 | 36 | }
|
158 | 37 |
|
159 |
| - //endregion |
160 |
| - |
161 | 38 | });
|
162 |
| - |
163 |
| -_gpfStreamSecureInstallProgressFlag(_GpfStreamFilter); |
0 commit comments