Skip to content

Commit

Permalink
Encapsulate ContextLoader creation inside Converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Aug 13, 2024
1 parent 8a0f1b0 commit c0656a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
16 changes: 11 additions & 5 deletions lib/Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,33 @@ export class Converter {
* `context`, `url`, `none`, and any JSON-LD type, each of which maps to
* another map of values of that type to their associated CBOR-LD integer
* values.
* @param {ContextLoader} options.contextLoader - The interface used to
* load JSON-LD contexts.
* @param {documentLoaderFunction} options.documentLoader -The document
* loader to use when resolving JSON-LD Context URLs.
* @param {boolean} [options.legacy=false] - True if legacy mode is in
* effect, false if not.
*/
constructor({
strategy, typeTable, contextLoader, legacy = false
strategy, typeTable, documentLoader, legacy = false
} = {}) {
this.strategy = strategy;
// FIXME: use strategy.typeTable
this.typeTable = typeTable;
this.contextLoader = contextLoader;
this.legacy = legacy;
this.initialActiveCtx = new ActiveContext({contextLoader});

// FIXME: expose differently
this.typeTableEncodedAsBytes = legacy ?
LEGACY_TYPE_TABLE_ENCODED_AS_BYTES : TYPE_TABLE_ENCODED_AS_BYTES;

// FIXME: eliminate cyclical reference
strategy.converter = this;
// FIXME: expose differently
this.reverseTypeTable = strategy.reverseTypeTable;

const contextLoader = new ContextLoader({
documentLoader, buildReverseMap: !!this.reverseTypeTable
});
this.contextLoader = contextLoader;
this.initialActiveCtx = new ActiveContext({contextLoader});
}

/**
Expand Down
5 changes: 1 addition & 4 deletions lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import * as cborg from 'cborg';
import {createLegacyTypeTable, createTypeTable} from './tables.js';
import {CborldError} from './CborldError.js';
import {ContextLoader} from './ContextLoader.js';
import {Converter} from './Converter.js';
import {Decompressor} from './Decompressor.js';
import {inspect} from './util.js';
Expand Down Expand Up @@ -97,9 +96,7 @@ function _createConverter({
// decompress CBOR-LD => JSON-LD
strategy: new Decompressor({typeTable}),
typeTable,
// FIXME: consider passing `documentLoader` and have converted determine
// this based on a strategy API
contextLoader: new ContextLoader({documentLoader, buildReverseMap: true}),
documentLoader,
// FIXME: try to eliminate need for legacy flag
legacy: isLegacy
});
Expand Down
3 changes: 1 addition & 2 deletions lib/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {createLegacyTypeTable, createTypeTable} from './tables.js';
import {CborldEncoder} from './codecs/CborldEncoder.js';
import {CborldError} from './CborldError.js';
import {Compressor} from './Compressor.js';
import {ContextLoader} from './ContextLoader.js';
import {Converter} from './Converter.js';
import {inspect} from './util.js';

Expand Down Expand Up @@ -169,7 +168,7 @@ function _createConverter({
// compress JSON-LD => CBOR-LD
strategy: new Compressor({typeTable}),
typeTable,
contextLoader: new ContextLoader({documentLoader}),
documentLoader,
// FIXME: try to eliminate need for legacy flag
legacy: isLegacy
});
Expand Down

0 comments on commit c0656a5

Please sign in to comment.