synstate 5.0.1 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/dist/core/class/observable-base-class.mjs.map +1 -1
- package/dist/core/class/propagation-state.mjs.map +1 -1
- package/dist/core/combine/combine.d.mts.map +1 -1
- package/dist/core/combine/combine.mjs +3 -43
- package/dist/core/combine/combine.mjs.map +1 -1
- package/dist/core/combine/derive.d.mts +63 -0
- package/dist/core/combine/derive.d.mts.map +1 -0
- package/dist/core/combine/derive.mjs +124 -0
- package/dist/core/combine/derive.mjs.map +1 -0
- package/dist/core/combine/index.d.mts +1 -0
- package/dist/core/combine/index.d.mts.map +1 -1
- package/dist/core/combine/index.mjs +1 -0
- package/dist/core/combine/index.mjs.map +1 -1
- package/dist/core/operators/keep-equal-parts.d.mts +1 -3
- package/dist/core/operators/keep-equal-parts.d.mts.map +1 -1
- package/dist/core/operators/keep-equal-parts.mjs +0 -2
- package/dist/core/operators/keep-equal-parts.mjs.map +1 -1
- package/dist/core/operators/map.d.mts +30 -18
- package/dist/core/operators/map.d.mts.map +1 -1
- package/dist/core/operators/map.mjs +24 -10
- package/dist/core/operators/map.mjs.map +1 -1
- package/dist/core/predefined/operators/attach-index.d.mts +4 -2
- package/dist/core/predefined/operators/attach-index.d.mts.map +1 -1
- package/dist/core/predefined/operators/attach-index.mjs +36 -2
- package/dist/core/predefined/operators/attach-index.mjs.map +1 -1
- package/dist/core/predefined/operators/map-to.d.mts +6 -2
- package/dist/core/predefined/operators/map-to.d.mts.map +1 -1
- package/dist/core/predefined/operators/map-to.mjs +5 -1
- package/dist/core/predefined/operators/map-to.mjs.map +1 -1
- package/dist/core/types/observable-family.d.mts +3 -0
- package/dist/core/types/observable-family.d.mts.map +1 -1
- package/dist/core/types/observable-family.mjs.map +1 -1
- package/dist/utils/create-reducer.d.mts +1 -1
- package/dist/utils/create-state.d.mts +1 -1
- package/package.json +12 -10
- package/src/core/class/observable-base-class.mts +2 -2
- package/src/core/class/propagation-state.mts +1 -1
- package/src/core/combine/combine.mts +7 -72
- package/src/core/combine/derive.mts +172 -0
- package/src/core/combine/derive.test.mts +190 -0
- package/src/core/combine/index.mts +1 -0
- package/src/core/operators/keep-equal-parts.mts +1 -3
- package/src/core/operators/map-cut-off.test.mts +215 -0
- package/src/core/operators/map.mts +48 -31
- package/src/core/predefined/operators/attach-index.mts +62 -5
- package/src/core/predefined/operators/map-to.mts +7 -3
- package/src/core/types/observable-family.mts +18 -0
- package/src/utils/create-reducer.mts +1 -1
- package/src/utils/create-state.mts +1 -1
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { Optional, expectType } from 'ts-data-forge';
|
|
2
|
+
import { source } from '../create/index.mjs';
|
|
3
|
+
import { map, withInitialValue } from '../operators/index.mjs';
|
|
4
|
+
import type {
|
|
5
|
+
InitializedObservable,
|
|
6
|
+
InitializedSyncChildObservable,
|
|
7
|
+
Observable,
|
|
8
|
+
SyncChildObservable,
|
|
9
|
+
} from '../types/index.mjs';
|
|
10
|
+
import { derive } from './derive.mjs';
|
|
11
|
+
|
|
12
|
+
describe(derive, () => {
|
|
13
|
+
test('emits the projection of the latest values, once every parent has one', () => {
|
|
14
|
+
const a$ = source<number>();
|
|
15
|
+
|
|
16
|
+
const b$ = source<string>();
|
|
17
|
+
|
|
18
|
+
const label$ = derive([a$, b$], (a, b) => `${b}${a.toString()}`);
|
|
19
|
+
|
|
20
|
+
const mut_history: string[] = [];
|
|
21
|
+
|
|
22
|
+
label$.subscribe((v) => {
|
|
23
|
+
mut_history.push(v);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
a$.next(1);
|
|
27
|
+
|
|
28
|
+
assert.deepStrictEqual(mut_history, []);
|
|
29
|
+
|
|
30
|
+
b$.next('x');
|
|
31
|
+
|
|
32
|
+
a$.next(2);
|
|
33
|
+
|
|
34
|
+
b$.next('y');
|
|
35
|
+
|
|
36
|
+
assert.deepStrictEqual(mut_history, ['x1', 'x2', 'y2']);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('is one observable: a diamond updates it once per write', () => {
|
|
40
|
+
const counter$ = source<number>(0);
|
|
41
|
+
|
|
42
|
+
const doubled$ = counter$.pipe(map((x) => x * 2));
|
|
43
|
+
|
|
44
|
+
const tripled$ = counter$.pipe(map((x) => x * 3));
|
|
45
|
+
|
|
46
|
+
let mut_calls = 0;
|
|
47
|
+
|
|
48
|
+
const sum$ = derive([doubled$, tripled$], (d, t) => {
|
|
49
|
+
mut_calls += 1;
|
|
50
|
+
|
|
51
|
+
return d + t;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const mut_history: number[] = [];
|
|
55
|
+
|
|
56
|
+
sum$.subscribe((v) => {
|
|
57
|
+
mut_history.push(v);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
counter$.next(1);
|
|
61
|
+
|
|
62
|
+
counter$.next(2);
|
|
63
|
+
|
|
64
|
+
assert.deepStrictEqual(mut_history, [0, 5, 10]);
|
|
65
|
+
|
|
66
|
+
assert.strictEqual(mut_calls, 3);
|
|
67
|
+
|
|
68
|
+
assert.deepStrictEqual(sum$.parents, [doubled$, tripled$]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('computes the initial value when every parent has one', () => {
|
|
72
|
+
const a$ = source<number>(1);
|
|
73
|
+
|
|
74
|
+
const b$ = source<number>(2);
|
|
75
|
+
|
|
76
|
+
const sum$ = derive([a$, b$], (a, b) => a + b);
|
|
77
|
+
|
|
78
|
+
assert.deepStrictEqual(sum$.getSnapshot(), Optional.some(3));
|
|
79
|
+
|
|
80
|
+
const c$ = source<number>();
|
|
81
|
+
|
|
82
|
+
const partial$ = derive([a$, c$], (a, c) => a + c);
|
|
83
|
+
|
|
84
|
+
assert.isTrue(Optional.isNone(partial$.getSnapshot()));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('a result equal by Object.is to the one held is not emitted', () => {
|
|
88
|
+
const a$ = source<number>(1);
|
|
89
|
+
|
|
90
|
+
const b$ = source<number>(2);
|
|
91
|
+
|
|
92
|
+
const parity$ = derive([a$, b$], (a, b) => (a + b) % 2);
|
|
93
|
+
|
|
94
|
+
const mut_history: number[] = [];
|
|
95
|
+
|
|
96
|
+
parity$.subscribe((v) => {
|
|
97
|
+
mut_history.push(v);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
a$.next(3);
|
|
101
|
+
|
|
102
|
+
b$.next(4);
|
|
103
|
+
|
|
104
|
+
a$.next(4);
|
|
105
|
+
|
|
106
|
+
assert.deepStrictEqual(mut_history, [1, 0]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('`equals` replaces Object.is', () => {
|
|
110
|
+
const a$ = source<number>(1);
|
|
111
|
+
|
|
112
|
+
const b$ = source<number>(2);
|
|
113
|
+
|
|
114
|
+
const pair$ = derive([a$, b$], (a, b) => ({ total: a + b }), {
|
|
115
|
+
equals: (x, y) => x.total === y.total,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const mut_history: number[] = [];
|
|
119
|
+
|
|
120
|
+
pair$.subscribe(({ total }) => {
|
|
121
|
+
mut_history.push(total);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
a$.next(2);
|
|
125
|
+
|
|
126
|
+
// A new object with the same total: equal by `equals`, not by Object.is.
|
|
127
|
+
a$.next(2);
|
|
128
|
+
|
|
129
|
+
b$.next(1);
|
|
130
|
+
|
|
131
|
+
b$.next(5);
|
|
132
|
+
|
|
133
|
+
assert.deepStrictEqual(mut_history, [3, 4, 3, 7]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('a write from one parent does not reuse a value another update left', () => {
|
|
137
|
+
const a$ = source<number>(1);
|
|
138
|
+
|
|
139
|
+
const b$ = source<number>(10);
|
|
140
|
+
|
|
141
|
+
const xs$ = derive([a$, b$], (...values) => values);
|
|
142
|
+
|
|
143
|
+
const mut_history: (readonly number[])[] = [];
|
|
144
|
+
|
|
145
|
+
xs$.subscribe((v) => {
|
|
146
|
+
mut_history.push(v);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
a$.next(2);
|
|
150
|
+
|
|
151
|
+
b$.next(20);
|
|
152
|
+
|
|
153
|
+
assert.deepStrictEqual(mut_history, [
|
|
154
|
+
[1, 10],
|
|
155
|
+
[2, 10],
|
|
156
|
+
[2, 20],
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
// Each emission is its own array, so a held value is never changed by a
|
|
160
|
+
// later update.
|
|
161
|
+
assert.notStrictEqual(mut_history[0], mut_history[1]);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('types', () => {
|
|
165
|
+
const s1: Observable<1> = source<1>();
|
|
166
|
+
|
|
167
|
+
const s2: InitializedObservable<2> = source<2>().pipe(withInitialValue(2));
|
|
168
|
+
|
|
169
|
+
const p = derive([s1, s2], (a, b) => {
|
|
170
|
+
expectType<typeof a, 1>('=');
|
|
171
|
+
|
|
172
|
+
expectType<typeof b, 2>('=');
|
|
173
|
+
|
|
174
|
+
return 'x' as const;
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
expectType<typeof p, SyncChildObservable<'x', readonly [1, 2]>>('=');
|
|
178
|
+
|
|
179
|
+
const s3: InitializedObservable<3> = source<3>().pipe(withInitialValue(3));
|
|
180
|
+
|
|
181
|
+
const _q = derive([s2, s3], (a, b) => a + b);
|
|
182
|
+
|
|
183
|
+
expectType<
|
|
184
|
+
typeof _q,
|
|
185
|
+
InitializedSyncChildObservable<number, readonly [2, 3]>
|
|
186
|
+
>('=');
|
|
187
|
+
|
|
188
|
+
expect(p).toBeDefined();
|
|
189
|
+
});
|
|
190
|
+
});
|
|
@@ -59,9 +59,7 @@ import type {
|
|
|
59
59
|
*
|
|
60
60
|
* const board$ = message$.pipe(keepEqualParts());
|
|
61
61
|
*
|
|
62
|
-
* const firstCard$ = board
|
|
63
|
-
* .pipe(map((board) => board.cards[0]))
|
|
64
|
-
* .pipe(skipIfNoChange());
|
|
62
|
+
* const firstCard$ = board$.pipe(map((board) => board.cards[0]));
|
|
65
63
|
*
|
|
66
64
|
* const boardHistory: Board[] = [];
|
|
67
65
|
*
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { expectType } from 'ts-data-forge';
|
|
2
|
+
import { combine } from '../combine/index.mjs';
|
|
3
|
+
import { source } from '../create/index.mjs';
|
|
4
|
+
import { mapTo, withIndex } from '../predefined/index.mjs';
|
|
5
|
+
import type { Observable } from '../types/index.mjs';
|
|
6
|
+
import { map } from './map.mjs';
|
|
7
|
+
|
|
8
|
+
describe('map stops a result equal to the one it holds', () => {
|
|
9
|
+
test('a result equal by Object.is is not emitted', () => {
|
|
10
|
+
const a$ = source<number>();
|
|
11
|
+
|
|
12
|
+
const parity$ = a$.pipe(map((x) => x % 2));
|
|
13
|
+
|
|
14
|
+
const mut_history: number[] = [];
|
|
15
|
+
|
|
16
|
+
parity$.subscribe((p) => {
|
|
17
|
+
mut_history.push(p);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
a$.next(1);
|
|
21
|
+
|
|
22
|
+
a$.next(3);
|
|
23
|
+
|
|
24
|
+
a$.next(4);
|
|
25
|
+
|
|
26
|
+
a$.next(6);
|
|
27
|
+
|
|
28
|
+
a$.next(7);
|
|
29
|
+
|
|
30
|
+
assert.deepStrictEqual(mut_history, [1, 0, 1]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('the value computed from an initial value counts as the one held', () => {
|
|
34
|
+
const a$ = source<number>(1);
|
|
35
|
+
|
|
36
|
+
const parity$ = a$.pipe(map((x) => x % 2));
|
|
37
|
+
|
|
38
|
+
const mut_history: number[] = [];
|
|
39
|
+
|
|
40
|
+
parity$.subscribe((p) => {
|
|
41
|
+
mut_history.push(p);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
a$.next(3);
|
|
45
|
+
|
|
46
|
+
a$.next(4);
|
|
47
|
+
|
|
48
|
+
assert.deepStrictEqual(mut_history, [1, 0]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('a node below is not woken when the result is equal', () => {
|
|
52
|
+
const a$ = source<number>(1);
|
|
53
|
+
|
|
54
|
+
const parity$ = a$.pipe(map((x) => x % 2));
|
|
55
|
+
|
|
56
|
+
let mut_calls = 0;
|
|
57
|
+
|
|
58
|
+
const label$ = parity$.pipe(
|
|
59
|
+
map((p) => {
|
|
60
|
+
mut_calls += 1;
|
|
61
|
+
|
|
62
|
+
return p === 0 ? 'even' : 'odd';
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
label$.subscribe(() => {});
|
|
67
|
+
|
|
68
|
+
const callsAfterSetup = mut_calls;
|
|
69
|
+
|
|
70
|
+
a$.next(3);
|
|
71
|
+
|
|
72
|
+
a$.next(5);
|
|
73
|
+
|
|
74
|
+
assert.strictEqual(mut_calls, callsAfterSetup);
|
|
75
|
+
|
|
76
|
+
a$.next(6);
|
|
77
|
+
|
|
78
|
+
assert.strictEqual(mut_calls, callsAfterSetup + 1);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('a node reading a stopped map and another parent still updates once', () => {
|
|
82
|
+
const a$ = source<number>(1);
|
|
83
|
+
|
|
84
|
+
const parity$ = a$.pipe(map((x) => x % 2));
|
|
85
|
+
|
|
86
|
+
const mut_history: string[] = [];
|
|
87
|
+
|
|
88
|
+
combine([a$, parity$]).subscribe(([a, p]) => {
|
|
89
|
+
mut_history.push(`${a.toString()},${p.toString()}`);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
a$.next(3);
|
|
93
|
+
|
|
94
|
+
a$.next(4);
|
|
95
|
+
|
|
96
|
+
assert.deepStrictEqual(mut_history, ['1,1', '3,1', '4,0']);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('NaN equals NaN, and 0 differs from -0, as Object.is says', () => {
|
|
100
|
+
const a$ = source<number>();
|
|
101
|
+
|
|
102
|
+
const r$ = a$.pipe(map((x) => x));
|
|
103
|
+
|
|
104
|
+
const mut_history: number[] = [];
|
|
105
|
+
|
|
106
|
+
r$.subscribe((v) => {
|
|
107
|
+
mut_history.push(v);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
a$.next(Number.NaN);
|
|
111
|
+
|
|
112
|
+
a$.next(Number.NaN);
|
|
113
|
+
|
|
114
|
+
a$.next(0);
|
|
115
|
+
|
|
116
|
+
a$.next(-0);
|
|
117
|
+
|
|
118
|
+
assert.deepStrictEqual(mut_history, [Number.NaN, 0, -0]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('`equals` replaces Object.is', () => {
|
|
122
|
+
const a$ = source<Readonly<{ id: number; name: string }>>();
|
|
123
|
+
|
|
124
|
+
const id$ = a$.pipe(
|
|
125
|
+
map((x) => ({ id: x.id }), { equals: (p, q) => p.id === q.id }),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const mut_history: number[] = [];
|
|
129
|
+
|
|
130
|
+
id$.subscribe(({ id }) => {
|
|
131
|
+
mut_history.push(id);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
a$.next({ id: 1, name: 'a' });
|
|
135
|
+
|
|
136
|
+
a$.next({ id: 1, name: 'b' });
|
|
137
|
+
|
|
138
|
+
a$.next({ id: 2, name: 'b' });
|
|
139
|
+
|
|
140
|
+
assert.deepStrictEqual(mut_history, [1, 2]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('`equals: () => false` emits every result', () => {
|
|
144
|
+
const click$ = source<undefined>();
|
|
145
|
+
|
|
146
|
+
const one$ = click$.pipe(map(() => 1, { equals: () => false }));
|
|
147
|
+
|
|
148
|
+
const mut_history: number[] = [];
|
|
149
|
+
|
|
150
|
+
one$.subscribe((v) => {
|
|
151
|
+
mut_history.push(v);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
click$.next(undefined);
|
|
155
|
+
|
|
156
|
+
click$.next(undefined);
|
|
157
|
+
|
|
158
|
+
assert.deepStrictEqual(mut_history, [1, 1]);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('the mapping function no longer receives an index', () => {
|
|
162
|
+
const a$ = source<number>();
|
|
163
|
+
|
|
164
|
+
// @ts-expect-error `map` passes the value alone; use `withIndex`.
|
|
165
|
+
const r$ = a$.pipe(map((x: number, i: number) => x + i));
|
|
166
|
+
|
|
167
|
+
expect(r$).toBeDefined();
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('operators that turn events into values emit every event', () => {
|
|
172
|
+
test('mapTo emits its value on every event', () => {
|
|
173
|
+
const reset$ = source<undefined>();
|
|
174
|
+
|
|
175
|
+
const action$ = reset$.pipe(mapTo('reset'));
|
|
176
|
+
|
|
177
|
+
expectType<typeof action$, Observable<'reset'>>('=');
|
|
178
|
+
|
|
179
|
+
const mut_history: string[] = [];
|
|
180
|
+
|
|
181
|
+
action$.subscribe((v) => {
|
|
182
|
+
mut_history.push(v);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
reset$.next(undefined);
|
|
186
|
+
|
|
187
|
+
reset$.next(undefined);
|
|
188
|
+
|
|
189
|
+
assert.deepStrictEqual(mut_history, ['reset', 'reset']);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('withIndex counts from 0, and gives -1 to an initial value', () => {
|
|
193
|
+
const a$ = source<string>('init');
|
|
194
|
+
|
|
195
|
+
const indexed$ = a$.pipe(withIndex());
|
|
196
|
+
|
|
197
|
+
assert.deepStrictEqual(indexed$.getSnapshot().value, [-1, 'init']);
|
|
198
|
+
|
|
199
|
+
const mut_history: (readonly [number, string])[] = [];
|
|
200
|
+
|
|
201
|
+
indexed$.subscribe((v) => {
|
|
202
|
+
mut_history.push(v);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
a$.next('x');
|
|
206
|
+
|
|
207
|
+
a$.next('x');
|
|
208
|
+
|
|
209
|
+
assert.deepStrictEqual(mut_history, [
|
|
210
|
+
[-1, 'init'],
|
|
211
|
+
[0, 'x'],
|
|
212
|
+
[1, 'x'],
|
|
213
|
+
]);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// @sumi-expect-error-file banned-syntax/no-this
|
|
6
6
|
// @sumi-expect-error-file functions/prefer-arrow-function
|
|
7
7
|
// @sumi-expect-error-file mutation/no-mutation-without-mut-prefix
|
|
8
|
-
import { Optional,
|
|
8
|
+
import { Optional, expectType } from 'ts-data-forge';
|
|
9
9
|
import { SyncChildObservableClass } from '../class/index.mjs';
|
|
10
10
|
import { source } from '../create/index.mjs';
|
|
11
11
|
import type {
|
|
@@ -18,51 +18,64 @@ import type {
|
|
|
18
18
|
import { withInitialValue } from './with-initial-value.mjs';
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* Transforms each value emitted by the source
|
|
21
|
+
* Transforms each value emitted by the source with a mapping function, and
|
|
22
|
+
* emits the result unless it equals the one already held.
|
|
23
|
+
*
|
|
24
|
+
* The comparison is `Object.is` unless `equals` says otherwise, so a result
|
|
25
|
+
* that did not change wakes nothing below. To pass on every result — an event
|
|
26
|
+
* turned into a value, say — give `equals: () => false`, or use `mapTo`. The
|
|
27
|
+
* mapping function receives the value alone; `withIndex` attaches an index.
|
|
22
28
|
*
|
|
23
29
|
* @template A - The type of values from the source
|
|
24
30
|
* @template B - The type of mapped values
|
|
25
|
-
* @param mapFn - A function that maps each value
|
|
26
|
-
* @
|
|
31
|
+
* @param mapFn - A function that maps each value
|
|
32
|
+
* @param options - `equals` compares a result with the one held
|
|
33
|
+
* @returns An operator that maps values
|
|
27
34
|
*
|
|
28
35
|
* @example
|
|
29
36
|
* ```ts
|
|
30
37
|
* // Timeline:
|
|
31
38
|
* //
|
|
32
|
-
* // num$
|
|
33
|
-
* //
|
|
39
|
+
* // num$ 1 3 4 6 7
|
|
40
|
+
* // parity$ 1 0 1
|
|
34
41
|
* //
|
|
35
42
|
* // Explanation:
|
|
36
|
-
* // -
|
|
37
|
-
* // -
|
|
43
|
+
* // - map transforms each value
|
|
44
|
+
* // - A result equal to the one held is not emitted
|
|
38
45
|
*
|
|
39
|
-
* const num$ = source<
|
|
46
|
+
* const num$ = source<number>();
|
|
40
47
|
*
|
|
41
|
-
* const
|
|
48
|
+
* const parity$ = num$.pipe(map((x) => x % 2));
|
|
42
49
|
*
|
|
43
|
-
* const valueHistory:
|
|
50
|
+
* const valueHistory: number[] = [];
|
|
44
51
|
*
|
|
45
|
-
*
|
|
46
|
-
* valueHistory.push(
|
|
52
|
+
* parity$.subscribe((p) => {
|
|
53
|
+
* valueHistory.push(p);
|
|
47
54
|
* });
|
|
48
55
|
*
|
|
49
|
-
* num$.next(
|
|
56
|
+
* num$.next(1); // 1
|
|
57
|
+
*
|
|
58
|
+
* num$.next(3); // still 1: not emitted
|
|
50
59
|
*
|
|
51
|
-
* num$.next(
|
|
60
|
+
* num$.next(4); // 0
|
|
52
61
|
*
|
|
53
|
-
* num$.next(
|
|
62
|
+
* num$.next(6); // still 0: not emitted
|
|
54
63
|
*
|
|
55
|
-
*
|
|
64
|
+
* num$.next(7); // 1
|
|
65
|
+
*
|
|
66
|
+
* assert.deepStrictEqual(valueHistory, [1, 0, 1]);
|
|
56
67
|
* ```
|
|
57
68
|
*/
|
|
58
69
|
export const map = <A, B>(
|
|
59
|
-
mapFn: (x: A
|
|
70
|
+
mapFn: (x: A) => B,
|
|
71
|
+
options?: Readonly<{ equals?: (x: B, y: B) => boolean }>,
|
|
60
72
|
): KeepInitialValueOperator<A, B> =>
|
|
61
73
|
// @sumi-expect-error banned-syntax/no-unsafe-type-assertion
|
|
62
74
|
((parentObservable) =>
|
|
63
75
|
new MapObservableClass(
|
|
64
76
|
parentObservable,
|
|
65
77
|
mapFn,
|
|
78
|
+
options?.equals ?? Object.is,
|
|
66
79
|
)) as KeepInitialValueOperator<A, B>;
|
|
67
80
|
|
|
68
81
|
/* implementation */
|
|
@@ -71,24 +84,23 @@ class MapObservableClass<A, B>
|
|
|
71
84
|
extends SyncChildObservableClass<B, readonly [A]>
|
|
72
85
|
implements MapOperatorObservable<A, B>
|
|
73
86
|
{
|
|
74
|
-
readonly #mapFn: (x: A
|
|
75
|
-
#
|
|
87
|
+
readonly #mapFn: (x: A) => B;
|
|
88
|
+
readonly #equals: (x: B, y: B) => boolean;
|
|
76
89
|
|
|
77
90
|
constructor(
|
|
78
91
|
parentObservable: Observable<A>,
|
|
79
|
-
mapFn: (x: A
|
|
92
|
+
mapFn: (x: A) => B,
|
|
93
|
+
equals: (x: B, y: B) => boolean,
|
|
80
94
|
) {
|
|
81
95
|
super({
|
|
82
96
|
parents: [parentObservable],
|
|
83
|
-
initialValue: Optional.map(parentObservable.getSnapshot(),
|
|
84
|
-
mapFn(x, -1),
|
|
85
|
-
),
|
|
97
|
+
initialValue: Optional.map(parentObservable.getSnapshot(), mapFn),
|
|
86
98
|
});
|
|
87
99
|
|
|
88
|
-
this.#mut_index = -1;
|
|
89
|
-
|
|
90
100
|
this.#mapFn = mapFn;
|
|
91
101
|
|
|
102
|
+
this.#equals = equals;
|
|
103
|
+
|
|
92
104
|
this.attach();
|
|
93
105
|
}
|
|
94
106
|
|
|
@@ -101,10 +113,15 @@ class MapObservableClass<A, B>
|
|
|
101
113
|
return; // skip update
|
|
102
114
|
}
|
|
103
115
|
|
|
104
|
-
this.#
|
|
105
|
-
|
|
116
|
+
const next = this.#mapFn(sn.value);
|
|
117
|
+
|
|
118
|
+
const held = this.getSnapshot();
|
|
119
|
+
|
|
120
|
+
if (Optional.isSome(held) && this.#equals(held.value, next)) {
|
|
121
|
+
return; // the result did not change
|
|
122
|
+
}
|
|
106
123
|
|
|
107
|
-
this.setNext(
|
|
124
|
+
this.setNext(next, updateToken);
|
|
108
125
|
}
|
|
109
126
|
}
|
|
110
127
|
|
|
@@ -116,7 +133,7 @@ if (import.meta.vitest !== undefined) {
|
|
|
116
133
|
{
|
|
117
134
|
const s: Observable<number> = source<number>();
|
|
118
135
|
|
|
119
|
-
const _d1 = s.pipe(map((x
|
|
136
|
+
const _d1 = s.pipe(map((x) => x + 1));
|
|
120
137
|
|
|
121
138
|
expectType<typeof _d1, Observable<number>>('=');
|
|
122
139
|
}
|
|
@@ -126,7 +143,7 @@ if (import.meta.vitest !== undefined) {
|
|
|
126
143
|
|
|
127
144
|
const m: InitializedObservable<number> = s.pipe(withInitialValue(0));
|
|
128
145
|
|
|
129
|
-
const _d = m.pipe(map((x
|
|
146
|
+
const _d = m.pipe(map((x) => x + 1));
|
|
130
147
|
|
|
131
148
|
expectType<typeof _d, InitializedObservable<number>>('=');
|
|
132
149
|
}
|
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// Sumi bans classes, `this`, method syntax and writes to `this` outside a
|
|
2
|
+
// `mut_` binding; an observable is a class (`ObservableBaseClass` says why),
|
|
3
|
+
// so those rules are waived here (D-63).
|
|
4
|
+
// @sumi-expect-error-file classes/no-class
|
|
5
|
+
// @sumi-expect-error-file banned-syntax/no-this
|
|
6
|
+
// @sumi-expect-error-file functions/prefer-arrow-function
|
|
7
|
+
// @sumi-expect-error-file mutation/no-mutation-without-mut-prefix
|
|
8
|
+
import { Optional, SafeUint, asSafeUint } from 'ts-data-forge';
|
|
9
|
+
import { SyncChildObservableClass } from '../../class/index.mjs';
|
|
10
|
+
import type {
|
|
11
|
+
KeepInitialValueOperator,
|
|
12
|
+
Observable,
|
|
13
|
+
UpdateToken,
|
|
14
|
+
} from '../../types/index.mjs';
|
|
4
15
|
|
|
5
16
|
/**
|
|
6
17
|
* Attaches a sequential index to each emitted value, producing `[index, value]` tuples.
|
|
7
|
-
* Index starts at 0 and increments with each emission
|
|
18
|
+
* Index starts at 0 and increments with each emission; a value the source
|
|
19
|
+
* already holds when this is applied gets -1. Use it where `map` used to
|
|
20
|
+
* receive an index.
|
|
8
21
|
*
|
|
9
22
|
* @template A - The type of values from the source
|
|
10
23
|
* @returns An operator that emits `[index, value]` tuples
|
|
@@ -54,10 +67,54 @@ import type { KeepInitialValueOperator } from '../../types/index.mjs';
|
|
|
54
67
|
export const withIndex = <A,>(): KeepInitialValueOperator<
|
|
55
68
|
A,
|
|
56
69
|
readonly [SafeUint | -1, A]
|
|
57
|
-
> =>
|
|
70
|
+
> =>
|
|
71
|
+
// @sumi-expect-error banned-syntax/no-unsafe-type-assertion
|
|
72
|
+
((parentObservable) =>
|
|
73
|
+
new WithIndexObservableClass(parentObservable)) as KeepInitialValueOperator<
|
|
74
|
+
A,
|
|
75
|
+
readonly [SafeUint | -1, A]
|
|
76
|
+
>;
|
|
58
77
|
|
|
59
78
|
/**
|
|
60
79
|
* Alias for `withIndex`.
|
|
61
80
|
* @see withIndex
|
|
62
81
|
*/
|
|
63
82
|
export const attachIndex = withIndex;
|
|
83
|
+
|
|
84
|
+
/* implementation */
|
|
85
|
+
|
|
86
|
+
class WithIndexObservableClass<A> extends SyncChildObservableClass<
|
|
87
|
+
readonly [SafeUint | -1, A],
|
|
88
|
+
readonly [A]
|
|
89
|
+
> {
|
|
90
|
+
#mut_index: SafeUint | -1;
|
|
91
|
+
|
|
92
|
+
constructor(parentObservable: Observable<A>) {
|
|
93
|
+
super({
|
|
94
|
+
parents: [parentObservable],
|
|
95
|
+
initialValue: Optional.map(
|
|
96
|
+
parentObservable.getSnapshot(),
|
|
97
|
+
(a) => [-1, a] as const,
|
|
98
|
+
),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
this.#mut_index = -1;
|
|
102
|
+
|
|
103
|
+
this.attach();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
override tryUpdate(updateToken: UpdateToken): void {
|
|
107
|
+
const par = this.parents[0];
|
|
108
|
+
|
|
109
|
+
const sn = par.getSnapshot();
|
|
110
|
+
|
|
111
|
+
if (par.updateToken !== updateToken || Optional.isNone(sn)) {
|
|
112
|
+
return; // skip update
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
this.#mut_index =
|
|
116
|
+
this.#mut_index === -1 ? asSafeUint(0) : SafeUint.add(1, this.#mut_index);
|
|
117
|
+
|
|
118
|
+
this.setNext([this.#mut_index, sn.value], updateToken);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -3,7 +3,11 @@ import type { KeepInitialValueOperator } from '../../types/index.mjs';
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Maps all emitted values to a constant value, ignoring the source values.
|
|
6
|
-
*
|
|
6
|
+
* Emits on every source value although the value never changes — where
|
|
7
|
+
* `map(() => value)` would stop every result after the first as equal — so
|
|
8
|
+
* it is the one for turning events into a value. The value's type is
|
|
9
|
+
* inferred as written, as with `as const`: `mapTo('reset')` gives
|
|
10
|
+
* `'reset'`, not `string`.
|
|
7
11
|
*
|
|
8
12
|
* @template A - The type of values from the source
|
|
9
13
|
* @template B - The type of the constant value
|
|
@@ -41,5 +45,5 @@ import type { KeepInitialValueOperator } from '../../types/index.mjs';
|
|
|
41
45
|
* assert.deepStrictEqual(valueHistory, [1, 1, 1]);
|
|
42
46
|
* ```
|
|
43
47
|
*/
|
|
44
|
-
export const mapTo = <A, B>(value: B): KeepInitialValueOperator<A, B> =>
|
|
45
|
-
map(() => value);
|
|
48
|
+
export const mapTo = <A, const B>(value: B): KeepInitialValueOperator<A, B> =>
|
|
49
|
+
map(() => value, { equals: () => false });
|