DataProvider.mjs (4799B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { LocaleFallbacker } from "./LocaleFallbacker.mjs" 4 import wasm from "./diplomat-wasm.mjs"; 5 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 6 7 8 /** 9 * An ICU4X data provider, capable of loading ICU4X data keys from some source. 10 * 11 * Currently the only source supported is loading from "blob" formatted data from a bytes buffer or the file system. 12 * 13 * If you wish to use ICU4X's builtin "compiled data", use the version of the constructors that do not have `_with_provider` 14 * in their names. 15 * 16 * See the [Rust documentation for `icu_provider`](https://docs.rs/icu_provider/latest/icu_provider/index.html) for more information. 17 */ 18 const DataProvider_box_destroy_registry = new FinalizationRegistry((ptr) => { 19 wasm.icu4x_DataProvider_destroy_mv1(ptr); 20 }); 21 22 export class DataProvider { 23 // Internal ptr reference: 24 #ptr = null; 25 26 // Lifetimes are only to keep dependencies alive. 27 // Since JS won't garbage collect until there are no incoming edges. 28 #selfEdge = []; 29 30 #internalConstructor(symbol, ptr, selfEdge) { 31 if (symbol !== diplomatRuntime.internalConstructor) { 32 console.error("DataProvider is an Opaque type. You cannot call its constructor."); 33 return; 34 } 35 this.#ptr = ptr; 36 this.#selfEdge = selfEdge; 37 38 // Are we being borrowed? If not, we can register. 39 if (this.#selfEdge.length === 0) { 40 DataProvider_box_destroy_registry.register(this, this.#ptr); 41 } 42 43 return this; 44 } 45 get ffiValue() { 46 return this.#ptr; 47 } 48 49 50 /** 51 * Creates a provider that tries the current provider and then, if the current provider 52 * doesn't support the data key, another provider `other`. 53 * 54 * This takes ownership of the `other` provider, leaving an empty provider in its place. 55 * 56 * See the [Rust documentation for `ForkByMarkerProvider`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fork/type.ForkByMarkerProvider.html) for more information. 57 */ 58 forkByMarker(other) { 59 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 60 61 62 const result = wasm.icu4x_DataProvider_fork_by_marker_mv1(diplomatReceive.buffer, this.ffiValue, other.ffiValue); 63 64 try { 65 if (!diplomatReceive.resultFlag) { 66 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 67 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 68 } 69 } 70 71 finally { 72 diplomatReceive.free(); 73 } 74 } 75 76 /** 77 * Same as `fork_by_key` but forks by locale instead of key. 78 * 79 * See the [Rust documentation for `IdentifierNotFoundPredicate`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fork/predicates/struct.IdentifierNotFoundPredicate.html) for more information. 80 */ 81 forkByLocale(other) { 82 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 83 84 85 const result = wasm.icu4x_DataProvider_fork_by_locale_mv1(diplomatReceive.buffer, this.ffiValue, other.ffiValue); 86 87 try { 88 if (!diplomatReceive.resultFlag) { 89 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 90 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 91 } 92 } 93 94 finally { 95 diplomatReceive.free(); 96 } 97 } 98 99 /** 100 * See the [Rust documentation for `new`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.new) for more information. 101 * 102 * Additional information: [1](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html) 103 */ 104 enableLocaleFallbackWith(fallbacker) { 105 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 106 107 108 const result = wasm.icu4x_DataProvider_enable_locale_fallback_with_mv1(diplomatReceive.buffer, this.ffiValue, fallbacker.ffiValue); 109 110 try { 111 if (!diplomatReceive.resultFlag) { 112 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 113 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 114 } 115 } 116 117 finally { 118 diplomatReceive.free(); 119 } 120 } 121 122 constructor(symbol, ptr, selfEdge) { 123 return this.#internalConstructor(...arguments) 124 } 125 }