• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Type of the onProgressUpdate callback function.
19 */
20export type OnProgressUpdateType = (percentage: number) => void;
21
22/**
23 * Utility functions for working with functions.
24 */
25export class FunctionUtils {
26  /**
27   * A function that does nothing.
28   */
29  static readonly DO_NOTHING = () => {
30    // do nothing
31  };
32
33  /**
34   * A function that does nothing asynchronously.
35   */
36  static readonly DO_NOTHING_ASYNC = (): Promise<void> => {
37    return Promise.resolve();
38  };
39
40  /**
41   * Mixin two objects together.
42   *
43   * This function takes two objects and returns a new object that is the
44   * result of merging the two objects. The properties of the first object
45   * take precedence over the properties of the second object if there are
46   * any conflicts.
47   *
48   * @param a The first object.
49   * @param b The second object.
50   * @return The merged object.
51   */
52  static mixin<T extends object, U extends object>(a: T, b: U): T & U {
53    const ret = {};
54    Object.assign(ret, a);
55    Object.assign(ret, b);
56
57    const assignMethods = (dst: object, src: object) => {
58      for (const methodName of Object.getOwnPropertyNames(
59        Object.getPrototypeOf(src),
60      )) {
61        const method = (src as any)[methodName];
62        (dst as any)[methodName] = method;
63      }
64    };
65
66    assignMethods(ret, a);
67    assignMethods(ret, b);
68
69    return ret as T & U;
70  }
71}
72