Skip to content

Commit 4fb7a44

Browse files
pinyintimdorr
authored andcommitted
Add types for current Observable interpolation interface (#3067)
* Add types about Observable interpolation * Run through Prettier * Also run through Prettier
1 parent 9827e2d commit 4fb7a44

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

index.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference types="symbol-observable" />
2+
13
/**
24
* An *action* is a plain object that represents an intention to change the
35
* state. Actions are the only way to get data into the store. Any data,
@@ -129,6 +131,32 @@ export interface Unsubscribe {
129131
(): void
130132
}
131133

134+
/**
135+
* A minimal observable of state changes.
136+
* For more information, see the observable proposal:
137+
* https://github.com/tc39/proposal-observable
138+
*/
139+
export type Observable<T> = {
140+
/**
141+
* The minimal observable subscription method.
142+
* @param {Object} observer Any object that can be used as an observer.
143+
* The observer object should have a `next` method.
144+
* @returns {subscription} An object with an `unsubscribe` method that can
145+
* be used to unsubscribe the observable from the store, and prevent further
146+
* emission of values from the observable.
147+
*/
148+
subscribe: (observer: Observer<T>) => { unsubscribe: Unsubscribe }
149+
[Symbol.observable](): Observable<T>
150+
}
151+
152+
/**
153+
* An Observer is used to receive data from an Observable, and is supplied as
154+
* an argument to subscribe.
155+
*/
156+
export type Observer<T> = {
157+
next?(value: T): void
158+
}
159+
132160
/**
133161
* A store is an object that holds the application's state tree.
134162
* There should only be a single store in a Redux app, as the composition
@@ -209,6 +237,14 @@ export interface Store<S = any, A extends Action = AnyAction> {
209237
* @param nextReducer The reducer for the store to use instead.
210238
*/
211239
replaceReducer(nextReducer: Reducer<S, A>): void
240+
241+
/**
242+
* Interoperability point for observable/reactive libraries.
243+
* @returns {observable} A minimal observable of state changes.
244+
* For more information, see the observable proposal:
245+
* https://github.com/tc39/proposal-observable
246+
*/
247+
[Symbol.observable](): Observable<S>
212248
}
213249

214250
export type DeepPartial<T> = {

test/typescript/store.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
StoreEnhancer,
77
StoreCreator,
88
StoreEnhancerStoreCreator,
9-
Unsubscribe
9+
Unsubscribe,
10+
Observer
1011
} from 'redux'
12+
import 'symbol-observable'
1113

1214
type State = {
1315
a: 'a'
@@ -101,3 +103,15 @@ unsubscribe()
101103
const newReducer: Reducer<State> = reducer
102104

103105
store.replaceReducer(newReducer)
106+
107+
/* observable */
108+
109+
let observable = store[Symbol.observable]()
110+
observable = observable[Symbol.observable]()
111+
const observer: Observer<State> = {
112+
next(state: State) {
113+
console.log('current state:', state)
114+
}
115+
}
116+
const unsubscribeFromObservable = observable.subscribe(observer).unsubscribe
117+
unsubscribeFromObservable()

0 commit comments

Comments
 (0)