• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {
16  IDisableTracingResponse,
17  IEnableTracingResponse,
18  IFreeBuffersResponse,
19  IGetTraceStatsResponse,
20  IReadBuffersResponse,
21} from '../protos';
22
23export interface Typed {
24  type: string;
25}
26
27// A type guard that can be used in order to be able to access the property of
28// an object in a checked manner.
29export function hasProperty<T extends object, P extends string>(
30  obj: T,
31  prop: P,
32): obj is T & {[prop in P]: unknown} {
33  return obj.hasOwnProperty(prop);
34}
35
36export function isTyped(obj: object): obj is Typed {
37  return obj.hasOwnProperty('type');
38}
39
40export interface ReadBuffersResponse extends Typed, IReadBuffersResponse {}
41export interface EnableTracingResponse extends Typed, IEnableTracingResponse {}
42export interface GetTraceStatsResponse extends Typed, IGetTraceStatsResponse {}
43export interface FreeBuffersResponse extends Typed, IFreeBuffersResponse {}
44export interface GetCategoriesResponse extends Typed {}
45export interface DisableTracingResponse
46  extends Typed,
47    IDisableTracingResponse {}
48
49export type ConsumerPortResponse =
50  | EnableTracingResponse
51  | ReadBuffersResponse
52  | GetTraceStatsResponse
53  | GetCategoriesResponse
54  | FreeBuffersResponse
55  | DisableTracingResponse;
56
57export function isReadBuffersResponse(obj: Typed): obj is ReadBuffersResponse {
58  return obj.type === 'ReadBuffersResponse';
59}
60
61export function isEnableTracingResponse(
62  obj: Typed,
63): obj is EnableTracingResponse {
64  return obj.type === 'EnableTracingResponse';
65}
66
67export function isGetTraceStatsResponse(
68  obj: Typed,
69): obj is GetTraceStatsResponse {
70  return obj.type === 'GetTraceStatsResponse';
71}
72
73export function isFreeBuffersResponse(obj: Typed): obj is FreeBuffersResponse {
74  return obj.type === 'FreeBuffersResponse';
75}
76
77export function isDisableTracingResponse(
78  obj: Typed,
79): obj is DisableTracingResponse {
80  return obj.type === 'DisableTracingResponse';
81}
82