comes-storage 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2026, Rafael Cheruti
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,3 @@
1
+ import { EventSystem } from 'comes';
2
+ import { Storage } from './storage.type';
3
+ export declare const setupStorage: (storageName: string, events: string[], storage?: Storage, es?: EventSystem) => void;
package/build/index.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setupStorage = void 0;
4
+ const comes_1 = require("comes");
5
+ const local_storage_1 = require("./local-storage");
6
+ const setupStorage = (storageName, events, storage, es) => {
7
+ if (!es)
8
+ es = comes_1.es;
9
+ if (!storage)
10
+ storage = new local_storage_1.LocalStorageStorage();
11
+ for (let event of events) {
12
+ const initialMainObj = lastData(storageName, storage, es);
13
+ const initialObj = (storageName === event ? initialMainObj : initialMainObj[event]) || null;
14
+ es.send(event, initialObj);
15
+ es.addInter(event, (id, data) => {
16
+ let obj = lastData(storageName, storage, es);
17
+ if (storageName === id)
18
+ obj = data;
19
+ else
20
+ obj[id] = data;
21
+ storage.save(storageName, obj);
22
+ return data;
23
+ });
24
+ }
25
+ };
26
+ exports.setupStorage = setupStorage;
27
+ const lastData = (storageName, storage, es) => {
28
+ let mainEntry = es.get(storageName);
29
+ let obj = mainEntry.last || {};
30
+ if (!mainEntry.date) {
31
+ obj = storage.load(storageName);
32
+ mainEntry.date = Date.now();
33
+ mainEntry.last = obj;
34
+ }
35
+ return obj;
36
+ };
@@ -0,0 +1,11 @@
1
+ import type { Storage } from './storage.type';
2
+ /**
3
+ * A simple implementation of the Storage interface using the browser's localStorage.
4
+ * This class provides methods to load, save, and retrieve the last saved data for a given address.
5
+ */
6
+ export declare class LocalStorageStorage implements Storage {
7
+ s: globalThis.Storage;
8
+ constructor();
9
+ load(address: string): any;
10
+ save(address: string, data: any): void;
11
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LocalStorageStorage = void 0;
4
+ /**
5
+ * A simple implementation of the Storage interface using the browser's localStorage.
6
+ * This class provides methods to load, save, and retrieve the last saved data for a given address.
7
+ */
8
+ class LocalStorageStorage {
9
+ constructor() {
10
+ this.s = globalThis.localStorage;
11
+ }
12
+ load(address) {
13
+ var _a;
14
+ const str = ((_a = this.s) === null || _a === void 0 ? void 0 : _a.getItem(address)) || '{}';
15
+ try {
16
+ return JSON.parse(str);
17
+ }
18
+ catch (ex) {
19
+ console.error(`Error loading "${address}" from "localStorage"!`, ex);
20
+ return {};
21
+ }
22
+ }
23
+ save(address, data) {
24
+ var _a;
25
+ const str = JSON.stringify(data);
26
+ (_a = this.s) === null || _a === void 0 ? void 0 : _a.setItem(address, str);
27
+ }
28
+ }
29
+ exports.LocalStorageStorage = LocalStorageStorage;
@@ -0,0 +1,15 @@
1
+ export type Storage = {
2
+ /**
3
+ * Must load the data from storage and return it. The data will be sent to the event system as the last value of the event.
4
+ * The event system will call this method when the event is first sent.
5
+ * @param address Address/ID of the event
6
+ * @returns The data loaded from storage
7
+ */
8
+ load: (address: string) => any;
9
+ /**
10
+ * Must save the data to storage. The event system will call this method whenever the event is sent with new data.
11
+ * @param address Address/ID of the event
12
+ * @param data Data to store/save
13
+ */
14
+ save: (address: string, data: any) => void;
15
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "comes-storage",
3
+ "version": "0.0.1",
4
+ "description": "Simple Storage for ComES",
5
+ "keywords": [
6
+ "event",
7
+ "event-system",
8
+ "communication",
9
+ "event-system storage"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git://github.com/rcheruti/comes-storage.git"
14
+ },
15
+ "homepage": "https://github.com/rcheruti/comes-storage#readme",
16
+ "main": "build/index.js",
17
+ "types": "build/index.d.ts",
18
+ "scripts": {
19
+ "dev": "ts-node src/index.ts",
20
+ "build": "rimraf build && tsc",
21
+ "test": "vitest"
22
+ },
23
+ "author": "RCC",
24
+ "license": "ISC",
25
+ "peerDependencies": {
26
+ "comes": ">=0.0.4"
27
+ },
28
+ "devDependencies": {
29
+ "@vitest/coverage-v8": "2.1.3",
30
+ "comes": "^0.0.4",
31
+ "rimraf": "^6.0.1",
32
+ "typescript": "5.6.3",
33
+ "vitest": "2.1.3"
34
+ }
35
+ }