|
| 1 | +/** |
| 2 | + * ------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. |
| 4 | + * See License in the project root for license information. |
| 5 | + * ------------------------------------------------------------------------------------------- |
| 6 | + */ |
| 7 | +import { type ParseNodeFactory, ParseNodeFactoryRegistry, type SerializationWriterFactory, SerializationWriterFactoryRegistry } from "./serialization"; |
| 8 | +import { BackingStoreParseNodeFactory, BackingStoreSerializationWriterProxyFactory } from "./store"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Registers the default serializer to the registry. |
| 12 | + * @param serializationWriterFactoryRegistry The serialization writer factory registry to register the default serializer to. |
| 13 | + * @param type the class of the factory to be registered. |
| 14 | + */ |
| 15 | +export function registerDefaultSerializer(serializationWriterFactoryRegistry: SerializationWriterFactoryRegistry, type: new () => SerializationWriterFactory): void { |
| 16 | + if (!type) throw new Error("Type is required"); |
| 17 | + const serializer = new type(); |
| 18 | + serializationWriterFactoryRegistry.contentTypeAssociatedFactories.set(serializer.getValidContentType(), serializer); |
| 19 | +} |
| 20 | +/** |
| 21 | + * Registers the default deserializer to the registry. |
| 22 | + * @param parseNodeFactoryRegistry The parse node factory registry to register the default deserializer to. |
| 23 | + * @param type the class of the factory to be registered. |
| 24 | + */ |
| 25 | +export function registerDefaultDeserializer(parseNodeFactoryRegistry: ParseNodeFactoryRegistry, type: new () => ParseNodeFactory): void { |
| 26 | + if (!type) throw new Error("Type is required"); |
| 27 | + const deserializer = new type(); |
| 28 | + parseNodeFactoryRegistry.contentTypeAssociatedFactories.set(deserializer.getValidContentType(), deserializer); |
| 29 | +} |
| 30 | +/** |
| 31 | + * Enables the backing store on default serialization writers and the given serialization writer. |
| 32 | + * @param serializationWriterFactoryRegistry The serialization writer factory registry to enable the backing store on. |
| 33 | + * @param parseNodeFactoryRegistry The parse node factory registry to enable the backing store on. |
| 34 | + * @param original The serialization writer to enable the backing store on. |
| 35 | + * @returns A new serialization writer with the backing store enabled. |
| 36 | + */ |
| 37 | +export function enableBackingStoreForSerializationWriterFactory(serializationWriterFactoryRegistry: SerializationWriterFactoryRegistry, parseNodeFactoryRegistry: ParseNodeFactoryRegistry, original: SerializationWriterFactory): SerializationWriterFactory { |
| 38 | + if (!original) throw new Error("Original must be specified"); |
| 39 | + let result = original; |
| 40 | + if (original instanceof SerializationWriterFactoryRegistry) { |
| 41 | + enableBackingStoreForSerializationRegistry(original); |
| 42 | + } else { |
| 43 | + result = new BackingStoreSerializationWriterProxyFactory(original); |
| 44 | + } |
| 45 | + enableBackingStoreForSerializationRegistry(serializationWriterFactoryRegistry); |
| 46 | + enableBackingStoreForParseNodeRegistry(parseNodeFactoryRegistry); |
| 47 | + return result; |
| 48 | +} |
| 49 | +/** |
| 50 | + * Enables the backing store on default parse node factories and the given parse node factory. |
| 51 | + * @param parseNodeFactoryRegistry The parse node factory registry to enable the backing store on. |
| 52 | + * @param original The parse node factory to enable the backing store on. |
| 53 | + * @returns A new parse node factory with the backing store enabled. |
| 54 | + */ |
| 55 | +export function enableBackingStoreForParseNodeFactory(parseNodeFactoryRegistry: ParseNodeFactoryRegistry, original: ParseNodeFactory): ParseNodeFactory { |
| 56 | + if (!original) throw new Error("Original must be specified"); |
| 57 | + let result = original; |
| 58 | + if (original instanceof ParseNodeFactoryRegistry) { |
| 59 | + enableBackingStoreForParseNodeRegistry(original); |
| 60 | + } else { |
| 61 | + result = new BackingStoreParseNodeFactory(original); |
| 62 | + } |
| 63 | + enableBackingStoreForParseNodeRegistry(parseNodeFactoryRegistry); |
| 64 | + return result; |
| 65 | +} |
| 66 | +/** |
| 67 | + * Enables the backing store on the given parse node factory registry. |
| 68 | + * @param registry The parse node factory registry to enable the backing store on. |
| 69 | + */ |
| 70 | +function enableBackingStoreForParseNodeRegistry(registry: ParseNodeFactoryRegistry): void { |
| 71 | + for (const [k, v] of registry.contentTypeAssociatedFactories) { |
| 72 | + if (!(v instanceof BackingStoreParseNodeFactory || v instanceof ParseNodeFactoryRegistry)) { |
| 73 | + registry.contentTypeAssociatedFactories.set(k, new BackingStoreParseNodeFactory(v)); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +/** |
| 78 | + * Enables the backing store on the given serialization factory registry. |
| 79 | + * @param registry The serialization factory registry to enable the backing store on. |
| 80 | + */ |
| 81 | +function enableBackingStoreForSerializationRegistry(registry: SerializationWriterFactoryRegistry): void { |
| 82 | + for (const [k, v] of registry.contentTypeAssociatedFactories) { |
| 83 | + if (!(v instanceof BackingStoreSerializationWriterProxyFactory || v instanceof SerializationWriterFactoryRegistry)) { |
| 84 | + registry.contentTypeAssociatedFactories.set(k, new BackingStoreSerializationWriterProxyFactory(v)); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments