|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow strict |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const AUTO_INSTANCE_KEY = -1; |
| 14 | + |
| 15 | +type FlowId = { |
| 16 | + markerId: number, |
| 17 | + instanceKey: number, |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * API for tracking reliability of your user interactions |
| 22 | + * |
| 23 | + * Example: |
| 24 | + * var flowId = UserFlow.newFlowId(QuickLogItentifiersExample.EXAMPLE_EVENT); |
| 25 | + * UserFlow.start(flowId, "user_click"); |
| 26 | + * ... |
| 27 | + * UserFlow.completeWithSuccess(flowId); |
| 28 | + */ |
| 29 | +const UserFlow = { |
| 30 | + /** |
| 31 | + * Creates FlowId from markerId and instanceKey. |
| 32 | + * You will pass FlowId in every other method of UserFlow API. |
| 33 | + * |
| 34 | + * By default, instanceKey will generate unique instance every time you call userFlowGetId with markerId only. |
| 35 | + */ |
| 36 | + newFlowId(markerId: number, instanceKey: number = AUTO_INSTANCE_KEY): FlowId { |
| 37 | + var resolvedInstanceKey = instanceKey; |
| 38 | + if (instanceKey === AUTO_INSTANCE_KEY) { |
| 39 | + if (global.nativeUserFlowNextInstanceKey) { |
| 40 | + resolvedInstanceKey = global.nativeUserFlowNextInstanceKey(); |
| 41 | + } else { |
| 42 | + // There is no JSI methods installed, API won't do anything |
| 43 | + resolvedInstanceKey = 0; |
| 44 | + } |
| 45 | + } |
| 46 | + return { |
| 47 | + markerId: markerId, |
| 48 | + instanceKey: resolvedInstanceKey, |
| 49 | + }; |
| 50 | + }, |
| 51 | + |
| 52 | + start(flowId: FlowId, triggerSource: string): void { |
| 53 | + if (global.nativeUserFlowStart) { |
| 54 | + global.nativeUserFlowStart( |
| 55 | + flowId.markerId, |
| 56 | + flowId.instanceKey, |
| 57 | + triggerSource, |
| 58 | + ); |
| 59 | + } |
| 60 | + }, |
| 61 | + |
| 62 | + annotate( |
| 63 | + flowId: FlowId, |
| 64 | + annotationName: string, |
| 65 | + annotationValue: string, |
| 66 | + ): void { |
| 67 | + if (global.nativeUserFlowAnnotate) { |
| 68 | + global.nativeUserFlowAnnotate( |
| 69 | + flowId.markerId, |
| 70 | + flowId.instanceKey, |
| 71 | + annotationName, |
| 72 | + annotationValue, |
| 73 | + ); |
| 74 | + } |
| 75 | + }, |
| 76 | + |
| 77 | + markPoint(flowId: FlowId, pointName: string): void { |
| 78 | + if (global.nativeUserFlowMarkPoint) { |
| 79 | + global.nativeUserFlowMarkPoint( |
| 80 | + flowId.markerId, |
| 81 | + flowId.instanceKey, |
| 82 | + pointName, |
| 83 | + ); |
| 84 | + } |
| 85 | + }, |
| 86 | + |
| 87 | + completeWithSuccess(flowId: FlowId): void { |
| 88 | + if (global.nativeUserFlowCompleteWithSuccess) { |
| 89 | + global.nativeUserFlowCompleteWithSuccess( |
| 90 | + flowId.markerId, |
| 91 | + flowId.instanceKey, |
| 92 | + ); |
| 93 | + } |
| 94 | + }, |
| 95 | + |
| 96 | + completeWithUnexpectedFail( |
| 97 | + flowId: FlowId, |
| 98 | + reason: string, |
| 99 | + failureLocation: string, |
| 100 | + ): void { |
| 101 | + if (global.nativeUserFlowCompleteWithUnexpectedFail) { |
| 102 | + global.nativeUserFlowCompleteWithUnexpectedFail( |
| 103 | + flowId.markerId, |
| 104 | + flowId.instanceKey, |
| 105 | + reason, |
| 106 | + failureLocation, |
| 107 | + ); |
| 108 | + } |
| 109 | + }, |
| 110 | + |
| 111 | + completeWithExpectedFail( |
| 112 | + flowId: FlowId, |
| 113 | + reason: string, |
| 114 | + failureLocation: string, |
| 115 | + ): void { |
| 116 | + if (global.nativeUserFlowCompleteWithExpectedFail) { |
| 117 | + global.nativeUserFlowCompleteWithExpectedFail( |
| 118 | + flowId.markerId, |
| 119 | + flowId.instanceKey, |
| 120 | + reason, |
| 121 | + failureLocation, |
| 122 | + ); |
| 123 | + } |
| 124 | + }, |
| 125 | + |
| 126 | + cancel(flowId: FlowId, cancelReason: string): void { |
| 127 | + if (global.nativeUserFlowCancel) { |
| 128 | + global.nativeUserFlowCancel( |
| 129 | + flowId.markerId, |
| 130 | + flowId.instanceKey, |
| 131 | + cancelReason, |
| 132 | + ); |
| 133 | + } |
| 134 | + }, |
| 135 | +}; |
| 136 | + |
| 137 | +module.exports = UserFlow; |
0 commit comments