-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
121 lines (121 loc) · 3.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
function createStore(defaultState, middleware, reducer) {
if (middleware === void 0) { middleware = rxjs_1.identity; }
if (reducer === void 0) { reducer = function (s) { return s; }; }
var mutations = new rxjs_1.Subject();
var stream = new rxjs_1.BehaviorSubject(defaultState);
mutations
.pipe(middleware, operators_1.scan(function (state, mutation) {
return mutation(state);
}, defaultState), operators_1.distinctUntilChanged())
.subscribe(stream);
return {
get stream() {
return stream;
},
get value() {
return stream.value;
},
next: function (m) {
mutations.next(m);
},
dispatch: function (action) {
mutations.next(function (state) { return reducer(state, action); });
},
//we don't use the name `complete` because it will cause store to terminate when you use pattern like .subscribe(store)
destroy: function () {
mutations.complete();
stream.complete();
},
use: function () {
return useObservables(stream)[0];
},
};
}
exports.createStore = createStore;
function useObservables() {
var obs = [];
for (var _i = 0; _i < arguments.length; _i++) {
obs[_i] = arguments[_i];
}
var _a = React.useState(function () {
return obs.map(function (x) { return (x instanceof rxjs_1.BehaviorSubject ? x.value : null); });
}), v = _a[0], setV = _a[1];
React.useEffect(function () {
var subs = obs.map(function (x, i) {
if (x) {
return (x instanceof rxjs_1.BehaviorSubject ? operators_1.skip(1)(x) : x).subscribe(function (x) {
setV(function (value) {
value[i] = x;
return value.slice();
});
});
}
});
return function () {
subs.forEach(function (x) { return x && x.unsubscribe(); });
};
}, obs);
return v;
}
exports.useObservables = useObservables;
function useSink(operation, deps) {
if (deps === void 0) { deps = []; }
var _a = React.useMemo(function () {
var subject = new rxjs_1.Subject();
return [subject, subject.next.bind(subject)];
}, deps), subject = _a[0], next = _a[1];
React.useEffect(function () {
var subscription = operation(subject);
return function () {
subject.complete();
subscription.unsubscribe(); //this is to prevent leak when operation fn contains some operation like combineLatest
};
}, [subject]);
return next;
}
exports.useSink = useSink;
/**
* @deprecated use useObservables
*/
function useObservable(ob) {
var _a = React.useState(null), value = _a[0], setValue = _a[1];
React.useEffect(function () {
var sub = ob.subscribe(setValue);
return sub.unsubscribe.bind(sub);
}, [ob]);
return value;
}
exports.useObservable = useObservable;
/**
* @deprecated use useObservables
*/
function useSource(ob, operator, deps) {
if (operator === void 0) { operator = operators_1.map(function (x) { return x; }); }
if (deps === void 0) { deps = []; }
var selected = React.useMemo(function () {
return ob.pipe(operator, operators_1.distinctUntilChanged(shallowEqual));
}, [ob].concat(deps));
return useObservable(selected);
}
exports.useSource = useSource;
function shallowEqual(a, b) {
if (typeof a !== "object" ||
a === null ||
typeof b !== "object" ||
b === null) {
return a === b;
}
else {
var ka = Object.keys(a);
var kb = Object.keys(b);
if (ka.length !== kb.length)
return false;
return ka.every(function (k) { return a[k] === b[k]; });
}
}
//# sourceMappingURL=index.js.map