Fix Symbol.observable polyfill mismatch (#1003)

* Remove spreading store in instrument

* Add subscribe method

* Ignore ESLint error

* Resolve Symbol.observable at store creation time

* Add warning
This commit is contained in:
Nathan Bierema 2022-01-11 22:56:04 -06:00 committed by GitHub
parent aa10c7d918
commit 912e2d132f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 17 deletions

View File

@ -0,0 +1,3 @@
export default function getSymbolObservable() {
return (typeof Symbol === 'function' && Symbol.observable) || '@@observable';
}

View File

@ -1,16 +1,16 @@
import difference from 'lodash/difference';
import union from 'lodash/union';
import isPlainObject from 'lodash/isPlainObject';
import $$observable from './symbol-observable';
import {
Action,
Observable,
Observer,
PreloadedState,
Reducer,
Store,
StoreEnhancer,
StoreEnhancerStoreCreator,
} from 'redux';
import getSymbolObservable from './getSymbolObservable';
export const ActionTypes = {
PERFORM_ACTION: 'PERFORM_ACTION',
@ -903,13 +903,21 @@ export function unliftStore<
return action;
}
return {
...liftedStore,
const $$observable = getSymbolObservable();
if (!($$observable in liftedStore)) {
console.warn(
'Symbol.observable as defined by Redux and Redux DevTools do not match. This could cause your app to behave differently if the DevTools are not loaded. Consider polyfilling Symbol.observable before Redux is imported or avoid polyfilling Symbol.observable altogether.'
);
}
return {
liftedStore,
dispatch,
// eslint-disable-next-line @typescript-eslint/unbound-method
subscribe: liftedStore.subscribe,
getState,
replaceReducer(nextReducer: Reducer<S & NextStateExt, A>) {
@ -923,10 +931,9 @@ export function unliftStore<
);
},
[$$observable](): Observable<S> {
[$$observable]() {
return {
...(liftedStore as any)[$$observable](),
subscribe(observer) {
subscribe(observer: Observer<S>) {
if (typeof observer !== 'object') {
throw new TypeError('Expected the observer to be an object.');
}

View File

@ -1,10 +0,0 @@
declare global {
interface SymbolConstructor {
readonly observable: symbol;
}
}
const $$observable = /* #__PURE__ */ (() =>
(typeof Symbol === 'function' && Symbol.observable) || '@@observable')();
export default $$observable;