@super-line/store-memory 0.5.0

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ memoryStoreClient: () => memoryStoreClient,
24
+ memoryStoreServer: () => memoryStoreServer
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_core = require("@super-line/core");
28
+ var randomId = () => Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2);
29
+ function memoryStoreServer() {
30
+ const resources = /* @__PURE__ */ new Map();
31
+ const listeners = /* @__PURE__ */ new Set();
32
+ const get = (id) => {
33
+ const r = resources.get(id);
34
+ if (!r) throw new import_core.SuperLineError("NOT_FOUND", `No resource: ${id}`);
35
+ return r;
36
+ };
37
+ return {
38
+ clustering: "relay",
39
+ read(id) {
40
+ return resources.get(id);
41
+ },
42
+ create(id, data, accessRules) {
43
+ if (resources.has(id)) throw new import_core.SuperLineError("CONFLICT", `Resource already exists: ${id}`);
44
+ resources.set(id, { id, accessRules, data });
45
+ },
46
+ apply(change) {
47
+ const r = get(change.id);
48
+ r.data = change.update;
49
+ for (const cb of listeners) cb(change);
50
+ },
51
+ setAccess(id, accessRules) {
52
+ get(id).accessRules = accessRules;
53
+ },
54
+ delete(id) {
55
+ resources.delete(id);
56
+ },
57
+ list() {
58
+ return [...resources.keys()];
59
+ },
60
+ onChange(cb) {
61
+ listeners.add(cb);
62
+ return () => listeners.delete(cb);
63
+ }
64
+ };
65
+ }
66
+ var LwwReplica = class {
67
+ constructor(id, origin) {
68
+ this.id = id;
69
+ this.origin = origin;
70
+ }
71
+ id;
72
+ origin;
73
+ value = void 0;
74
+ listeners = /* @__PURE__ */ new Set();
75
+ getSnapshot() {
76
+ return this.value;
77
+ }
78
+ subscribe(cb) {
79
+ this.listeners.add(cb);
80
+ return () => this.listeners.delete(cb);
81
+ }
82
+ notify() {
83
+ for (const cb of this.listeners) cb();
84
+ }
85
+ seed(snapshot) {
86
+ this.value = snapshot;
87
+ this.notify();
88
+ }
89
+ set(data) {
90
+ if (Object.is(this.value, data)) return null;
91
+ this.value = data;
92
+ this.notify();
93
+ return { id: this.id, update: data, origin: this.origin };
94
+ }
95
+ update(partial) {
96
+ const base = typeof this.value === "object" && this.value !== null ? this.value : {};
97
+ return this.set({ ...base, ...partial });
98
+ }
99
+ applyRemote(change) {
100
+ if (change.origin === this.origin) return;
101
+ this.value = change.update;
102
+ this.notify();
103
+ }
104
+ };
105
+ function memoryStoreClient(opts) {
106
+ const origin = opts?.origin ?? randomId();
107
+ return {
108
+ origin,
109
+ open(id) {
110
+ return new LwwReplica(id, origin);
111
+ }
112
+ };
113
+ }
114
+ // Annotate the CommonJS export names for ESM import in node:
115
+ 0 && (module.exports = {
116
+ memoryStoreClient,
117
+ memoryStoreServer
118
+ });
@@ -0,0 +1,17 @@
1
+ import { ClientStore, ServerStore } from '@super-line/core';
2
+
3
+ /**
4
+ * The in-memory, last-writer-wins **server half**. Holds Resources in a `Map`; a write replaces the
5
+ * whole `data`. `clustering: 'relay'` — it does no networking; super-line core relays its Changes across
6
+ * nodes and feeds remote Changes back in via {@link ServerStore.apply}.
7
+ */
8
+ declare function memoryStoreServer(): ServerStore;
9
+ /**
10
+ * The in-memory, last-writer-wins **client half**. Each opened Resource is a plain reactive value cell;
11
+ * a write replaces it and emits a full-value Change. `origin` is one per client instance.
12
+ */
13
+ declare function memoryStoreClient(opts?: {
14
+ origin?: string;
15
+ }): ClientStore;
16
+
17
+ export { memoryStoreClient, memoryStoreServer };
@@ -0,0 +1,17 @@
1
+ import { ClientStore, ServerStore } from '@super-line/core';
2
+
3
+ /**
4
+ * The in-memory, last-writer-wins **server half**. Holds Resources in a `Map`; a write replaces the
5
+ * whole `data`. `clustering: 'relay'` — it does no networking; super-line core relays its Changes across
6
+ * nodes and feeds remote Changes back in via {@link ServerStore.apply}.
7
+ */
8
+ declare function memoryStoreServer(): ServerStore;
9
+ /**
10
+ * The in-memory, last-writer-wins **client half**. Each opened Resource is a plain reactive value cell;
11
+ * a write replaces it and emits a full-value Change. `origin` is one per client instance.
12
+ */
13
+ declare function memoryStoreClient(opts?: {
14
+ origin?: string;
15
+ }): ClientStore;
16
+
17
+ export { memoryStoreClient, memoryStoreServer };
package/dist/index.js ADDED
@@ -0,0 +1,92 @@
1
+ // src/index.ts
2
+ import { SuperLineError } from "@super-line/core";
3
+ var randomId = () => Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2);
4
+ function memoryStoreServer() {
5
+ const resources = /* @__PURE__ */ new Map();
6
+ const listeners = /* @__PURE__ */ new Set();
7
+ const get = (id) => {
8
+ const r = resources.get(id);
9
+ if (!r) throw new SuperLineError("NOT_FOUND", `No resource: ${id}`);
10
+ return r;
11
+ };
12
+ return {
13
+ clustering: "relay",
14
+ read(id) {
15
+ return resources.get(id);
16
+ },
17
+ create(id, data, accessRules) {
18
+ if (resources.has(id)) throw new SuperLineError("CONFLICT", `Resource already exists: ${id}`);
19
+ resources.set(id, { id, accessRules, data });
20
+ },
21
+ apply(change) {
22
+ const r = get(change.id);
23
+ r.data = change.update;
24
+ for (const cb of listeners) cb(change);
25
+ },
26
+ setAccess(id, accessRules) {
27
+ get(id).accessRules = accessRules;
28
+ },
29
+ delete(id) {
30
+ resources.delete(id);
31
+ },
32
+ list() {
33
+ return [...resources.keys()];
34
+ },
35
+ onChange(cb) {
36
+ listeners.add(cb);
37
+ return () => listeners.delete(cb);
38
+ }
39
+ };
40
+ }
41
+ var LwwReplica = class {
42
+ constructor(id, origin) {
43
+ this.id = id;
44
+ this.origin = origin;
45
+ }
46
+ id;
47
+ origin;
48
+ value = void 0;
49
+ listeners = /* @__PURE__ */ new Set();
50
+ getSnapshot() {
51
+ return this.value;
52
+ }
53
+ subscribe(cb) {
54
+ this.listeners.add(cb);
55
+ return () => this.listeners.delete(cb);
56
+ }
57
+ notify() {
58
+ for (const cb of this.listeners) cb();
59
+ }
60
+ seed(snapshot) {
61
+ this.value = snapshot;
62
+ this.notify();
63
+ }
64
+ set(data) {
65
+ if (Object.is(this.value, data)) return null;
66
+ this.value = data;
67
+ this.notify();
68
+ return { id: this.id, update: data, origin: this.origin };
69
+ }
70
+ update(partial) {
71
+ const base = typeof this.value === "object" && this.value !== null ? this.value : {};
72
+ return this.set({ ...base, ...partial });
73
+ }
74
+ applyRemote(change) {
75
+ if (change.origin === this.origin) return;
76
+ this.value = change.update;
77
+ this.notify();
78
+ }
79
+ };
80
+ function memoryStoreClient(opts) {
81
+ const origin = opts?.origin ?? randomId();
82
+ return {
83
+ origin,
84
+ open(id) {
85
+ return new LwwReplica(id, origin);
86
+ }
87
+ };
88
+ }
89
+ export {
90
+ memoryStoreClient,
91
+ memoryStoreServer
92
+ };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@super-line/store-memory",
3
+ "version": "0.5.0",
4
+ "type": "module",
5
+ "description": "In-memory last-writer-wins Store for super-line — the zero-dependency default Store pair.",
6
+ "license": "MIT",
7
+ "author": "Mert",
8
+ "keywords": [
9
+ "super-line",
10
+ "store",
11
+ "memory",
12
+ "in-memory",
13
+ "lww"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/mertdogar/super-line.git",
18
+ "directory": "packages/store-memory"
19
+ },
20
+ "homepage": "https://mertdogar.github.io/super-line/",
21
+ "bugs": "https://github.com/mertdogar/super-line/issues",
22
+ "main": "./dist/index.cjs",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "import": {
28
+ "types": "./dist/index.d.ts",
29
+ "default": "./dist/index.js"
30
+ },
31
+ "require": {
32
+ "types": "./dist/index.d.cts",
33
+ "default": "./dist/index.cjs"
34
+ }
35
+ }
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "sideEffects": false,
41
+ "engines": {
42
+ "node": ">=18"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "dependencies": {
48
+ "@super-line/core": "^0.5.0"
49
+ },
50
+ "scripts": {
51
+ "build": "tsup"
52
+ }
53
+ }