UNPKG

8.65 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
6
7var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8
9exports.check = check;
10exports.hasOwn = hasOwn;
11exports.remove = remove;
12exports.deferred = deferred;
13exports.arrayOfDeffered = arrayOfDeffered;
14exports.delay = delay;
15exports.createMockTask = createMockTask;
16exports.autoInc = autoInc;
17exports.makeIterator = makeIterator;
18exports.log = log;
19exports.deprecate = deprecate;
20var sym = exports.sym = function sym(id) {
21 return '@@redux-saga/' + id;
22};
23
24var TASK = exports.TASK = sym('TASK');
25var HELPER = exports.HELPER = sym('HELPER');
26var MATCH = exports.MATCH = sym('MATCH');
27var CANCEL = exports.CANCEL = sym('CANCEL_PROMISE');
28var SAGA_ACTION = exports.SAGA_ACTION = sym('SAGA_ACTION');
29var SELF_CANCELLATION = exports.SELF_CANCELLATION = sym('SELF_CANCELLATION');
30var konst = exports.konst = function konst(v) {
31 return function () {
32 return v;
33 };
34};
35var kTrue = exports.kTrue = konst(true);
36var kFalse = exports.kFalse = konst(false);
37var noop = exports.noop = function noop() {};
38var ident = exports.ident = function ident(v) {
39 return v;
40};
41
42function check(value, predicate, error) {
43 if (!predicate(value)) {
44 log('error', 'uncaught at check', error);
45 throw new Error(error);
46 }
47}
48
49var hasOwnProperty = Object.prototype.hasOwnProperty;
50function hasOwn(object, property) {
51 return is.notUndef(object) && hasOwnProperty.call(object, property);
52}
53
54var is = exports.is = {
55 undef: function undef(v) {
56 return v === null || v === undefined;
57 },
58 notUndef: function notUndef(v) {
59 return v !== null && v !== undefined;
60 },
61 func: function func(f) {
62 return typeof f === 'function';
63 },
64 number: function number(n) {
65 return typeof n === 'number';
66 },
67 string: function string(s) {
68 return typeof s === 'string';
69 },
70 array: Array.isArray,
71 object: function object(obj) {
72 return obj && !is.array(obj) && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object';
73 },
74 promise: function promise(p) {
75 return p && is.func(p.then);
76 },
77 iterator: function iterator(it) {
78 return it && is.func(it.next) && is.func(it.throw);
79 },
80 iterable: function iterable(it) {
81 return it && is.func(Symbol) ? is.func(it[Symbol.iterator]) : is.array(it);
82 },
83 task: function task(t) {
84 return t && t[TASK];
85 },
86 observable: function observable(ob) {
87 return ob && is.func(ob.subscribe);
88 },
89 buffer: function buffer(buf) {
90 return buf && is.func(buf.isEmpty) && is.func(buf.take) && is.func(buf.put);
91 },
92 pattern: function pattern(pat) {
93 return pat && (is.string(pat) || (typeof pat === 'undefined' ? 'undefined' : _typeof(pat)) === 'symbol' || is.func(pat) || is.array(pat));
94 },
95 channel: function channel(ch) {
96 return ch && is.func(ch.take) && is.func(ch.close);
97 },
98 helper: function helper(it) {
99 return it && it[HELPER];
100 },
101 stringableFunc: function stringableFunc(f) {
102 return is.func(f) && hasOwn(f, 'toString');
103 }
104};
105
106var object = exports.object = {
107 assign: function assign(target, source) {
108 for (var i in source) {
109 if (hasOwn(source, i)) {
110 target[i] = source[i];
111 }
112 }
113 }
114};
115
116function remove(array, item) {
117 var index = array.indexOf(item);
118 if (index >= 0) {
119 array.splice(index, 1);
120 }
121}
122
123var array = exports.array = {
124 from: function from(obj) {
125 var arr = Array(obj.length);
126 for (var i in obj) {
127 if (hasOwn(obj, i)) {
128 arr[i] = obj[i];
129 }
130 }
131 return arr;
132 }
133};
134
135function deferred() {
136 var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
137
138 var def = _extends({}, props);
139 var promise = new Promise(function (resolve, reject) {
140 def.resolve = resolve;
141 def.reject = reject;
142 });
143 def.promise = promise;
144 return def;
145}
146
147function arrayOfDeffered(length) {
148 var arr = [];
149 for (var i = 0; i < length; i++) {
150 arr.push(deferred());
151 }
152 return arr;
153}
154
155function delay(ms) {
156 var val = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
157
158 var timeoutId = void 0;
159 var promise = new Promise(function (resolve) {
160 timeoutId = setTimeout(function () {
161 return resolve(val);
162 }, ms);
163 });
164
165 promise[CANCEL] = function () {
166 return clearTimeout(timeoutId);
167 };
168
169 return promise;
170}
171
172function createMockTask() {
173 var _ref;
174
175 var running = true;
176 var _result = void 0,
177 _error = void 0;
178
179 return _ref = {}, _ref[TASK] = true, _ref.isRunning = function isRunning() {
180 return running;
181 }, _ref.result = function result() {
182 return _result;
183 }, _ref.error = function error() {
184 return _error;
185 }, _ref.setRunning = function setRunning(b) {
186 return running = b;
187 }, _ref.setResult = function setResult(r) {
188 return _result = r;
189 }, _ref.setError = function setError(e) {
190 return _error = e;
191 }, _ref;
192}
193
194function autoInc() {
195 var seed = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
196
197 return function () {
198 return ++seed;
199 };
200}
201
202var uid = exports.uid = autoInc();
203
204var kThrow = function kThrow(err) {
205 throw err;
206};
207var kReturn = function kReturn(value) {
208 return { value: value, done: true };
209};
210function makeIterator(next) {
211 var thro = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : kThrow;
212 var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
213 var isHelper = arguments[3];
214
215 var iterator = { name: name, next: next, throw: thro, return: kReturn };
216
217 if (isHelper) {
218 iterator[HELPER] = true;
219 }
220 if (typeof Symbol !== 'undefined') {
221 iterator[Symbol.iterator] = function () {
222 return iterator;
223 };
224 }
225 return iterator;
226}
227
228/**
229 Print error in a useful way whether in a browser environment
230 (with expandable error stack traces), or in a node.js environment
231 (text-only log output)
232 **/
233function log(level, message) {
234 var error = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
235
236 /*eslint-disable no-console*/
237 if (typeof window === 'undefined') {
238 console.log('redux-saga ' + level + ': ' + message + '\n' + (error && error.stack || error));
239 } else {
240 console[level](message, error);
241 }
242}
243
244function deprecate(fn, deprecationWarning) {
245 return function () {
246 if (process.env.NODE_ENV === 'development') log('warn', deprecationWarning);
247 return fn.apply(undefined, arguments);
248 };
249}
250
251var updateIncentive = exports.updateIncentive = function updateIncentive(deprecated, preferred) {
252 return deprecated + ' has been deprecated in favor of ' + preferred + ', please update your code';
253};
254
255var internalErr = exports.internalErr = function internalErr(err) {
256 return new Error('\n redux-saga: Error checking hooks detected an inconsistent state. This is likely a bug\n in redux-saga code and not yours. Thanks for reporting this in the project\'s github repo.\n Error: ' + err + '\n');
257};
258
259var createSetContextWarning = exports.createSetContextWarning = function createSetContextWarning(ctx, props) {
260 return (ctx ? ctx + '.' : '') + 'setContext(props): argument ' + props + ' is not a plain object';
261};
262
263var wrapSagaDispatch = exports.wrapSagaDispatch = function wrapSagaDispatch(dispatch) {
264 return function (action) {
265 return dispatch(Object.defineProperty(action, SAGA_ACTION, { value: true }));
266 };
267};
268
269var cloneableGenerator = exports.cloneableGenerator = function cloneableGenerator(generatorFunc) {
270 return function () {
271 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
272 args[_key] = arguments[_key];
273 }
274
275 var history = [];
276 var gen = generatorFunc.apply(undefined, args);
277 return {
278 next: function next(arg) {
279 history.push(arg);
280 return gen.next(arg);
281 },
282 clone: function clone() {
283 var clonedGen = cloneableGenerator(generatorFunc).apply(undefined, args);
284 history.forEach(function (arg) {
285 return clonedGen.next(arg);
286 });
287 return clonedGen;
288 },
289 return: function _return(value) {
290 return gen.return(value);
291 },
292 throw: function _throw(exception) {
293 return gen.throw(exception);
294 }
295 };
296 };
297};
\No newline at end of file