summaryrefslogtreecommitdiff
path: root/addons/point_of_sale/static/src/js/utils.js
blob: 7aa7b35e93d9459c473595cb842cc0213210b9db (plain)
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
odoo.define('point_of_sale.utils', function (require) {
    'use strict';

    const { EventBus } = owl.core;

    function getFileAsText(file) {
        return new Promise((resolve, reject) => {
            if (!file) {
                reject();
            } else {
                const reader = new FileReader();
                reader.addEventListener('load', function () {
                    resolve(reader.result);
                });
                reader.addEventListener('abort', reject);
                reader.addEventListener('error', reject);
                reader.readAsText(file);
            }
        });
    }

    /**
     * This global variable is used by nextFrame to store the timer and
     * be able to cancel it before another request for animation frame.
     */
    let timer = null;

    /**
     * Wait for the next animation frame to finish.
     */
    const nextFrame = () => {
        return new Promise((resolve) => {
            cancelAnimationFrame(timer);
            timer = requestAnimationFrame(() => {
                resolve();
            });
        });
    };

    function isRpcError(error) {
        return (
            !(error instanceof Error) &&
            error.message &&
            [100, 200, 404, -32098].includes(error.message.code)
        );
    }

    return { getFileAsText, nextFrame, isRpcError, posbus: new EventBus() };
});