|
| 1 | +/** @license for symbol-observable |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) |
| 5 | + * Copyright (c) Ben Lesh <ben@benlesh.com> |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +function symbolObservablePonyfill(root) { |
| 26 | + var result; |
| 27 | + var Symbol = root.Symbol; |
| 28 | + |
| 29 | + if (typeof Symbol === 'function') { |
| 30 | + if (Symbol.observable) { |
| 31 | + result = Symbol.observable; |
| 32 | + } else { |
| 33 | + result = Symbol('observable'); |
| 34 | + Symbol.observable = result; |
| 35 | + } |
| 36 | + } else { |
| 37 | + result = '@@observable'; |
| 38 | + } |
| 39 | + |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +/* global window */ |
| 44 | + |
| 45 | +var root; |
| 46 | + |
| 47 | +if (typeof self !== 'undefined') { |
| 48 | + root = self; |
| 49 | +} else if (typeof window !== 'undefined') { |
| 50 | + root = window; |
| 51 | +} else if (typeof global !== 'undefined') { |
| 52 | + root = global; |
| 53 | +} else if (typeof module !== 'undefined') { |
| 54 | + root = module; |
| 55 | +} else { |
| 56 | + root = Function('return this')(); |
| 57 | +} |
| 58 | + |
| 59 | +var $observable = symbolObservablePonyfill(root); |
| 60 | + |
| 61 | +/** @license |
| 62 | + * Copyright 2016 - present The Material Motion Authors. All Rights Reserved. |
| 63 | + * |
| 64 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 65 | + * use this file except in compliance with the License. You may obtain a copy |
| 66 | + * of the License at |
| 67 | + * |
| 68 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 69 | + * |
| 70 | + * Unless required by applicable law or agreed to in writing, software |
| 71 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 72 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 73 | + * License for the specific language governing permissions and limitations |
| 74 | + * under the License. |
| 75 | + */ |
| 76 | +/** |
| 77 | + * TypeScript is a pain to use with polymorphic types unless you wrap them in a |
| 78 | + * function that returns a single type. So, that's what this is. |
| 79 | + * |
| 80 | + * If you give it an observer, you get back that observer. If you give it an |
| 81 | + * anonymous function, you get back that anonymous function wrapped in an |
| 82 | + * observer. |
| 83 | + */ |
| 84 | +function wrapWithObserver(listener) { |
| 85 | + if (typeof listener === 'function') { |
| 86 | + return { |
| 87 | + next: listener |
| 88 | + }; |
| 89 | + } |
| 90 | + else { |
| 91 | + return listener; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +/** |
| 97 | + * `Observable` is a standard interface that's useful for modeling multiple, |
| 98 | + * asynchronous events. |
| 99 | + * |
| 100 | + * `IndefiniteObservable` is a minimalist implementation of a subset of the TC39 |
| 101 | + * Observable proposal. It is indefinite because it will never call `complete` |
| 102 | + * or `error` on the provided observer. |
| 103 | + */ |
| 104 | +class IndefiniteObservable { |
| 105 | + /** |
| 106 | + * The provided function should receive an observer and connect that |
| 107 | + * observer's `next` method to an event source (for instance, |
| 108 | + * `element.addEventListener('click', observer.next)`). |
| 109 | + * |
| 110 | + * It must return a function that will disconnect the observer from the event |
| 111 | + * source. |
| 112 | + */ |
| 113 | + constructor(connect) { |
| 114 | + this._connect = connect; |
| 115 | + } |
| 116 | + /** |
| 117 | + * `subscribe` uses the function supplied to the constructor to connect an |
| 118 | + * observer to an event source. Each observer is connected independently: |
| 119 | + * each call to `subscribe` calls `connect` with the new observer. |
| 120 | + * |
| 121 | + * To disconnect the observer from the event source, call `unsubscribe` on the |
| 122 | + * returned subscription. |
| 123 | + * |
| 124 | + * Note: `subscribe` accepts either a function or an object with a |
| 125 | + * next method. |
| 126 | + */ |
| 127 | + subscribe(observerOrNext) { |
| 128 | + // For simplicity's sake, `subscribe` accepts `next` either as either an |
| 129 | + // anonymous function or wrapped in an object (the observer). Since |
| 130 | + // `connect` always expects to receive an observer, wrap any loose |
| 131 | + // functions in an object. |
| 132 | + const observer = wrapWithObserver(observerOrNext); |
| 133 | + let disconnect = this._connect(observer); |
| 134 | + return { |
| 135 | + unsubscribe() { |
| 136 | + if (disconnect) { |
| 137 | + disconnect(); |
| 138 | + disconnect = undefined; |
| 139 | + } |
| 140 | + } |
| 141 | + }; |
| 142 | + } |
| 143 | + /** |
| 144 | + * Tells other libraries that know about observables that we are one. |
| 145 | + * |
| 146 | + * https://github.com/tc39/proposal-observable#observable |
| 147 | + */ |
| 148 | + [$observable]() { |
| 149 | + return this; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +export { wrapWithObserver, IndefiniteObservable }; |
| 156 | +export default IndefiniteObservable; |
0 commit comments