Project

General

Profile

Download (1.2 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / storage_raw.js @ 2bd35bc4

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Basic wrappers for storage API functions.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
7
 * Redistribution terms are gathered in the `copyright' file.
8
 */
9

    
10
/*
11
 * IMPORTS_START
12
 * IMPORT TYPE_PREFIX
13
 * IMPORT browser
14
 * IMPORT is_chrome
15
 * IMPORTS_END
16
 */
17

    
18
async function get(key)
19
{
20
    /* Fix for fact that Chrome does not use promises here */
21
    const promise = is_chrome ?
22
	  new Promise(resolve => chrome.storage.local.get(key, resolve)) :
23
	  browser.storage.local.get(key);
24

    
25
    return (await promise)[key];
26
}
27

    
28
async function set(key_or_object, value)
29
{
30
    const arg = typeof key_or_object === "object" ?
31
	  key_or_object : {[key_or_object]: value};
32
    return browser.storage.local.set(arg);
33
}
34

    
35
async function set_var(name, value)
36
{
37
    return set(TYPE_PREFIX.VAR + name, value);
38
}
39

    
40
async function get_var(name)
41
{
42
    return get(TYPE_PREFIX.VAR + name);
43
}
44

    
45
const on_changed = browser.storage.onChanged || browser.storage.local.onChanged;
46
const listen = cb => on_changed.addListener(cb);
47
const no_listen = cb => on_changed.removeListener(cb);
48

    
49
const raw_storage = {get, set, get_var, set_var, listen, no_listen};
50

    
51
/*
52
 * EXPORTS_START
53
 * EXPORT raw_storage
54
 * EXPORTS_END
55
 */
(15-15/16)