msaltnet / T.Viewer

Cross Platform Tizen Log Viewer
https://blog.msalt.net/313
MIT License
16 stars 7 forks source link

Make LogListener singleton #5

Closed msaltnet closed 4 years ago

msaltnet commented 4 years ago

listener 호출 수정 필요.

LogListener

var instance;
export default class {
    constructor (ipcRenderer) {
        if (instance) return instance;

        this.ipcRenderer = ipcRenderer;
        this.LISTEN_REQUEST_CHANNEL = "register-listener";
        this.LISTEN_LOG_CHANNEL = "log-listen";
        this.textDecoder = new TextDecoder("utf-8");
        this.listenerList = [];
        this._listener = (event, arg) => {
            if (this.listener)
                this.listener(this.textDecoder.decode(arg));
        };
    }

    startListen() {
        this.ipcRenderer.send(this.LISTEN_REQUEST_CHANNEL, this.LISTEN_LOG_CHANNEL)
        this.ipcRenderer.on(this.LISTEN_LOG_CHANNEL, this._listener);
    }

    stopListen() {
        this.ipcRenderer.send(this.LISTEN_REQUEST_CHANNEL, null)
        this.ipcRenderer.removeListener(this.LISTEN_LOG_CHANNEL, this._listener)
    }

    registerListener(listener) {
        console.log('register ' + this.LISTEN_REQUEST_CHANNEL);
        let isNeedStart = this.listenerList.length == 0 ? true : false;
        this.listenerList.push(listener);
        if (isNeedStart)
            this.startListen();
    }

    unregisterListener(listener) {
        console.log('unregister ' + this.LISTEN_LOG_CHANNEL);
        for (let i = 0; i < this.listenerList.length; i++) {
            if (listener == this.listenerList[i]) {
                this.listenerList.splice(i, 1);
                break;
            }
        }

        if (this.listenerList.length == 0)
            this.stopListen();
    }
}

it's test

    it('should send requet for listen after call startListen', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        logListener.startListen();

        expect(mockIpcRenderer.send).toBeCalledWith(logListener.LISTEN_REQUEST_CHANNEL, logListener.LISTEN_LOG_CHANNEL);
    })

    it('should register listener for listen after call startListen', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        logListener.startListen();

        expect(mockIpcRenderer.on).toBeCalledWith(logListener.LISTEN_LOG_CHANNEL, logListener._listener);
    })

    it('should send requet to stop listen after call stopListen', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        logListener.stopListen();

        expect(mockIpcRenderer.send).toBeCalledWith(logListener.LISTEN_REQUEST_CHANNEL, null);
    })

    it('should remove listener after call stopListen', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        logListener.stopListen();

        expect(mockIpcRenderer.removeListener).toBeCalledWith(logListener.LISTEN_LOG_CHANNEL, logListener._listener);
    })

    it('should have listener in listenerList after registerListener', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        const mockLisnter = () => {};

        logListener.registerListener(mockLisnter)
        expect(logListener.listenerList.includes(mockLisnter)).toEqual(true);
    })

    it('should call startListen when listenerList is empty', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        const mockLisnter = () => {};
        expect(logListener.listenerList.length == 0).toEqual(true);
        logListener.startListen = jest.fn();

        logListener.registerListener(mockLisnter)
        expect(logListener.startListen).toBeCalled();
    })

    it('should NOT call startListen when listenerList is NOT empty', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        const mockLisnter = () => {};
        logListener.listenerList.push(mockLisnter);
        expect(logListener.listenerList.length != 0).toEqual(true);
        logListener.startListen = jest.fn();

        logListener.registerListener(mockLisnter)
        expect(logListener.startListen).not.toBeCalled();
    })

    it('should NOT have listener in listenerList after unregisterListener', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        const mockLisnter = () => {};
        logListener.listenerList.push(mockLisnter);
        logListener.unregisterListener(mockLisnter)
        expect(logListener.listenerList.includes(mockLisnter)).toEqual(false);
    })

    it('should call stopListen when listenerList length is empty', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        const mockLisnter = () => {};
        expect(logListener.listenerList.length == 0).toEqual(true);
        logListener.stopListen = jest.fn();

        logListener.unregisterListener(mockLisnter)
        expect(logListener.stopListen).toBeCalled();
    })

    it('should NOT call stopListen when listenerList is NOT empty', () => {
        const mockIpcRenderer = {
            on: jest.fn(),
            send: jest.fn(),
            removeListener: jest.fn(),
        }
        const logListener = new LogListener(mockIpcRenderer);
        const mockLisnter = () => {};
        logListener.listenerList.push(mockLisnter);
        expect(logListener.listenerList.length != 0).toEqual(true);
        logListener.stopListen = jest.fn();

        logListener.registerListener(mockLisnter)
        expect(logListener.stopListen).not.toBeCalled();
    })
})
msaltnet commented 4 years ago

bee0d4f