Skip to content

Commit

Permalink
Inline the symbol-observable polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Apr 2, 2021
1 parent b882d9a commit 0d7d94d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
8 changes: 6 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="symbol-observable" />

/**
* An *action* is a plain object that represents an intention to change the
* state. Actions are the only way to get data into the store. Any data,
Expand Down Expand Up @@ -224,6 +222,12 @@ export interface Unsubscribe {
(): void
}

declare global {
interface SymbolConstructor {
readonly observable: symbol;
}
}

/**
* A minimal observable of state changes.
* For more information, see the observable proposal:
Expand Down
2 changes: 1 addition & 1 deletion src/createStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import $$observable from 'symbol-observable'
import $$observable from './utils/symbol-observable'

import ActionTypes from './utils/actionTypes'
import isPlainObject from './utils/isPlainObject'
Expand Down
3 changes: 3 additions & 0 deletions src/utils/symbol-observable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Inlined version of the `symbol-observable` polyfill
export default (() =>
(typeof Symbol === 'function' && Symbol.observable) || '@@observable')()
7 changes: 5 additions & 2 deletions test/createStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import {
import * as reducers from './helpers/reducers'
import { from } from 'rxjs'
import { map } from 'rxjs/operators'
import $$observable from 'symbol-observable'
import $$observable from '../src/utils/symbol-observable'

describe('createStore', () => {
it('exposes the public API', () => {
const store = createStore(combineReducers(reducers))
const methods = Object.keys(store)

// Since switching to internal Symbol.observable impl, it will show up as a key in node env
// So we filter it out
const methods = Object.keys(store).filter(key => key !== $$observable)

expect(methods.length).toBe(4)
expect(methods).toContain('subscribe')
expect(methods).toContain('dispatch')
Expand Down
3 changes: 2 additions & 1 deletion test/typescript/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
Unsubscribe,
Observer,
} from 'redux'
import 'symbol-observable'
// @ts-ignore
import $$observable from '../src/utils/symbol-observable'

type BrandedString = string & { _brand: 'type' }
const brandedString = 'a string' as BrandedString
Expand Down

0 comments on commit 0d7d94d

Please sign in to comment.