All files / src/Ice ObjectAdapterFactory.js

100% Statements 65/65
100% Branches 14/14
100% Functions 4/4
100% Lines 65/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 6641x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 67x 67x 67x 67x 67x 41x 41x 65x 65x 65x 65x 65x 14x 14x 65x 41x 41x 31x 31x 31x 21x 21x 21x 21x 21x 21x 21x 21x 21x 29x 10x 1x 1x 9x 9x 9x 27x 29x 4x 4x 31x 41x 41x 26x 26x 26x 26x 26x 26x 26x 5x 5x 26x 41x  
// Copyright (c) ZeroC, Inc.
 
import { ObjectAdapter } from "./ObjectAdapter.js";
import { AlreadyRegisteredException } from "./LocalExceptions.js";
import { generateUUID } from "./UUID.js";
 
//
// Only for use by Instance.
//
export class ObjectAdapterFactory {
    constructor(instance, communicator) {
        this._instance = instance;
        this._communicator = communicator;
        this._adapters = [];
        this._adapterNamesInUse = [];
    }
 
    destroy() {
        // We cannot directly iterate over this._adapters because ObjectAdapter.destroy() will remove the
        // adapter from the list of adapters by calling removeObjectAdapter. We use slice() to create a
        // shallow copy of the array to avoid modifying the array during iteration.
        const adapters = this._adapters.slice();
        for (const adapter of adapters) {
            adapter.destroy();
        }
    }
 
    createObjectAdapter(name, router, promise) {
        let adapter = null;
        try {
            if (name.length === 0) {
                adapter = new ObjectAdapter(
                    this._instance,
                    this._communicator,
                    this,
                    generateUUID(),
                    null,
                    true,
                    promise,
                );
            } else {
                if (this._adapterNamesInUse.indexOf(name) !== -1) {
                    throw new AlreadyRegisteredException("object adapter", name);
                }
                adapter = new ObjectAdapter(this._instance, this._communicator, this, name, router, false, promise);
                this._adapterNamesInUse.push(name);
            }
            this._adapters.push(adapter);
        } catch (ex) {
            promise.reject(ex);
        }
    }
 
    removeObjectAdapter(adapter) {
        let n = this._adapters.indexOf(adapter);
        if (n !== -1) {
            this._adapters.splice(n, 1);
        }
 
        n = this._adapterNamesInUse.indexOf(adapter.getName());
        if (n !== -1) {
            this._adapterNamesInUse.splice(n, 1);
        }
    }
}