1*6dbdd20aSAndroid Build Coastguard Worker// Copyright (C) 2023 The Android Open Source Project 2*6dbdd20aSAndroid Build Coastguard Worker// 3*6dbdd20aSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*6dbdd20aSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*6dbdd20aSAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*6dbdd20aSAndroid Build Coastguard Worker// 7*6dbdd20aSAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*6dbdd20aSAndroid Build Coastguard Worker// 9*6dbdd20aSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*6dbdd20aSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*6dbdd20aSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*6dbdd20aSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*6dbdd20aSAndroid Build Coastguard Worker// limitations under the License. 14*6dbdd20aSAndroid Build Coastguard Worker 15*6dbdd20aSAndroid Build Coastguard Worker// Occasionally libraries require configuration code to be called 16*6dbdd20aSAndroid Build Coastguard Worker// before that library is used. This can become troublesome when the 17*6dbdd20aSAndroid Build Coastguard Worker// library is used in many places through out the code base. To ensure 18*6dbdd20aSAndroid Build Coastguard Worker// a consistent initialization this file exists as a place such code 19*6dbdd20aSAndroid Build Coastguard Worker// can live. It's the first import in the root file of the bundle. 20*6dbdd20aSAndroid Build Coastguard Worker 21*6dbdd20aSAndroid Build Coastguard Worker// We use 'static initializers' (e.g. first import causes 22*6dbdd20aSAndroid Build Coastguard Worker// initialization) rather than allowing the importer control over when 23*6dbdd20aSAndroid Build Coastguard Worker// it happens. This sounds worse on paper but it works a lot better in 24*6dbdd20aSAndroid Build Coastguard Worker// tests where (in JS) there is no global main() you can edit to do the 25*6dbdd20aSAndroid Build Coastguard Worker// initialization. Instead any test where this is a problem can easily 26*6dbdd20aSAndroid Build Coastguard Worker// stick an import at the top of the file. 27*6dbdd20aSAndroid Build Coastguard Worker 28*6dbdd20aSAndroid Build Coastguard Workerimport {enableMapSet, enablePatches, setAutoFreeze} from 'immer'; 29*6dbdd20aSAndroid Build Coastguard Workerimport protobuf from 'protobufjs/minimal'; 30*6dbdd20aSAndroid Build Coastguard Worker 31*6dbdd20aSAndroid Build Coastguard Workerfunction initializeImmer() { 32*6dbdd20aSAndroid Build Coastguard Worker enablePatches(); 33*6dbdd20aSAndroid Build Coastguard Worker enableMapSet(); 34*6dbdd20aSAndroid Build Coastguard Worker 35*6dbdd20aSAndroid Build Coastguard Worker // TODO(primiano): re-enable this, requires fixing some bugs that this bubbles 36*6dbdd20aSAndroid Build Coastguard Worker // up. This is a new feature of immer which freezes object after a produce(). 37*6dbdd20aSAndroid Build Coastguard Worker // Unfortunately we piled up a bunch of bugs where we shallow-copy objects 38*6dbdd20aSAndroid Build Coastguard Worker // from the global state (which is frozen) and later try to update the copies. 39*6dbdd20aSAndroid Build Coastguard Worker // By doing so, we accidentally the local copy of global state, which is 40*6dbdd20aSAndroid Build Coastguard Worker // supposed to be immutable. 41*6dbdd20aSAndroid Build Coastguard Worker setAutoFreeze(true); 42*6dbdd20aSAndroid Build Coastguard Worker} 43*6dbdd20aSAndroid Build Coastguard Worker 44*6dbdd20aSAndroid Build Coastguard Workerfunction initializeProtobuf() { 45*6dbdd20aSAndroid Build Coastguard Worker // Disable Long.js support in protobuf. This seems to be enabled only in tests 46*6dbdd20aSAndroid Build Coastguard Worker // but not in production code. In any case, for now we want casting to number 47*6dbdd20aSAndroid Build Coastguard Worker // accepting the 2**53 limitation. This is consistent with passing 48*6dbdd20aSAndroid Build Coastguard Worker // --force-number in the protobuf.js codegen invocation in //ui/BUILD.gn . 49*6dbdd20aSAndroid Build Coastguard Worker // See also https://github.com/protobufjs/protobuf.js/issues/1253 . 50*6dbdd20aSAndroid Build Coastguard Worker // eslint-disable-next-line @typescript-eslint/no-explicit-any 51*6dbdd20aSAndroid Build Coastguard Worker protobuf.util.Long = undefined as any; 52*6dbdd20aSAndroid Build Coastguard Worker protobuf.configure(); 53*6dbdd20aSAndroid Build Coastguard Worker} 54*6dbdd20aSAndroid Build Coastguard Worker 55*6dbdd20aSAndroid Build Coastguard Workerlet isInitialized = false; 56*6dbdd20aSAndroid Build Coastguard Workerfunction initialize() { 57*6dbdd20aSAndroid Build Coastguard Worker if (isInitialized) { 58*6dbdd20aSAndroid Build Coastguard Worker throw new Error('initialize() should be called exactly once'); 59*6dbdd20aSAndroid Build Coastguard Worker } 60*6dbdd20aSAndroid Build Coastguard Worker initializeImmer(); 61*6dbdd20aSAndroid Build Coastguard Worker initializeProtobuf(); 62*6dbdd20aSAndroid Build Coastguard Worker isInitialized = true; 63*6dbdd20aSAndroid Build Coastguard Worker} 64*6dbdd20aSAndroid Build Coastguard Worker 65*6dbdd20aSAndroid Build Coastguard Worker// JS module semantics ensure this is happens only once. 66*6dbdd20aSAndroid Build Coastguard Workerinitialize(); 67