From 150908ae5500992b441e3e6159f4c5e8a3cc0cfc Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 14 May 2020 00:12:23 +0200 Subject: [PATCH] packages/core: add common Observable type based on TC39 --- packages/core/src/api/index.ts | 1 + packages/core/src/api/types.ts | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 packages/core/src/api/types.ts diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.ts index 0f111b26ba..2eabc38280 100644 --- a/packages/core/src/api/index.ts +++ b/packages/core/src/api/index.ts @@ -18,3 +18,4 @@ export * from './apis'; export * from './app'; export * from './navTargets'; export * from './plugin'; +export * from './types'; diff --git a/packages/core/src/api/types.ts b/packages/core/src/api/types.ts new file mode 100644 index 0000000000..28a6155017 --- /dev/null +++ b/packages/core/src/api/types.ts @@ -0,0 +1,63 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This file contains non-react related core types used throught Backstage. + */ + +/** + * Observer interface for consuming an Observer, see TC39. + */ +export type Observer = { + next?(value: T): void; + error?(error: Error): void; + complete?(): void; +}; + +/** + * Subscription returned when subscribing to an Observable, see TC39. + */ +export type Subscription = { + /** + * CAncels the subscription + */ + unsubscribe(): void; + + /** + * Value indicating whether the subscription is closed. + */ + readonly closed: Boolean; +}; + +/** + * Observable sequence of values and errors, see TC39. + * + * https://github.com/tc39/proposal-observable + * + * This is used as a common return type for observable values and can be created + * using many different observable implementations, such as zen-observable or RxJS 5. + */ +export type Observable = { + /** + * Subscribes to this observable to start receiving new values. + */ + subscribe(observer: Observer): Subscription; + subscribe( + onNext: (value: T) => void, + onError?: (error: Error) => void, + onComplete?: () => void, + ): Subscription; +};