aboutsummaryrefslogtreecommitdiff
path: root/source/storage/Backend.mc
blob: 6596d9cb1799e717c6cdc310c11645bda40362f2 (plain) (blame)
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
// Author:  cirakg & claude ai
// Summary: Key-value persistence behind an interface: Application.Storage on the watch, a dictionary in tests.
//
// Everything the vault persists goes through a Backend, so unit tests can run
// against MemoryBackend (test/storage/) and simulate a full store, while the
// app uses AppStorageBackend. Values are the types Application.Storage
// accepts: numbers, strings, booleans, ByteArrays, arrays and dictionaries.
// Records are single ByteArrays, so their stored size is known exactly.

import Toybox.Application;
import Toybox.Lang;

module Persist {

    // Base class; subclasses override all three methods.
    class Backend {

        function initialize() {
        }

        // The stored value, or null when the key is absent.
        function getValue(key as String) as Application.Storage.ValueType {
            return null;
        }

        // Store a value. Throws when the store is full (the vault turns that
        // into VaultError FULL).
        function setValue(key as String, value as Application.Storage.ValueType) as Void {
        }

        function deleteValue(key as String) as Void {
        }
    }

    // The real store: Application.Storage, private to this app.
    class AppStorageBackend extends Backend {

        function initialize() {
            Backend.initialize();
        }

        function getValue(key as String) as Application.Storage.ValueType {
            return Application.Storage.getValue(key);
        }

        function setValue(key as String, value as Application.Storage.ValueType) as Void {
            Application.Storage.setValue(key, value);
        }

        function deleteValue(key as String) as Void {
            Application.Storage.deleteValue(key);
        }
    }
}