@coaction/yjs 1.0.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) 2024 Michael Lin
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/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # @coaction/yjs
2
+
3
+ ![Node CI](https://github.com/unadlib/coaction/workflows/Node%20CI/badge.svg)
4
+ [![npm](https://img.shields.io/npm/v/@coaction/yjs.svg)](https://www.npmjs.com/package/@coaction/yjs)
5
+ ![license](https://img.shields.io/npm/l/@coaction/yjs)
6
+
7
+ A Coaction integration tool for Yjs.
8
+
9
+ ## Installation
10
+
11
+ You can install it via npm, yarn or pnpm.
12
+
13
+ ```sh
14
+ npm install coaction @coaction/yjs
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { create } from 'coaction';
21
+ import { yjs } from '@coaction/yjs';
22
+
23
+ const store = create(
24
+ (set) => ({
25
+ count: 0,
26
+ increment() {
27
+ set((draft) => {
28
+ draft.count += 1;
29
+ });
30
+ }
31
+ }),
32
+ {
33
+ middlewares: [yjs()]
34
+ }
35
+ );
36
+ ```
37
+
38
+ ## Documentation
39
+
40
+ You can find the documentation [here](https://github.com/unadlib/coaction).
@@ -0,0 +1,18 @@
1
+ import { Store, Middleware } from 'coaction';
2
+ import * as Y from 'yjs';
3
+ export * from 'yjs';
4
+
5
+ type YjsBindingOptions = {
6
+ doc?: Y.Doc;
7
+ key?: string;
8
+ };
9
+ type YjsBinding<T extends object> = {
10
+ doc: Y.Doc;
11
+ map: Y.Map<any>;
12
+ syncNow: () => void;
13
+ destroy: () => void;
14
+ };
15
+ declare const bindYjs: <T extends object>(store: Store<T>, options?: YjsBindingOptions) => YjsBinding<T>;
16
+ declare const yjs: <T extends object>(options?: YjsBindingOptions) => Middleware<T>;
17
+
18
+ export { type YjsBinding, type YjsBindingOptions, bindYjs, yjs };
@@ -0,0 +1,18 @@
1
+ import { Store, Middleware } from 'coaction';
2
+ import * as Y from 'yjs';
3
+ export * from 'yjs';
4
+
5
+ type YjsBindingOptions = {
6
+ doc?: Y.Doc;
7
+ key?: string;
8
+ };
9
+ type YjsBinding<T extends object> = {
10
+ doc: Y.Doc;
11
+ map: Y.Map<any>;
12
+ syncNow: () => void;
13
+ destroy: () => void;
14
+ };
15
+ declare const bindYjs: <T extends object>(store: Store<T>, options?: YjsBindingOptions) => YjsBinding<T>;
16
+ declare const yjs: <T extends object>(options?: YjsBindingOptions) => Middleware<T>;
17
+
18
+ export { type YjsBinding, type YjsBindingOptions, bindYjs, yjs };
package/dist/index.js ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // index.ts
32
+ var index_exports = {};
33
+ __export(index_exports, {
34
+ bindYjs: () => bindYjs,
35
+ yjs: () => yjs
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+
39
+ // src/index.ts
40
+ var src_exports = {};
41
+ __export(src_exports, {
42
+ bindYjs: () => bindYjs,
43
+ yjs: () => yjs
44
+ });
45
+ var Y = __toESM(require("yjs"));
46
+ __reExport(src_exports, require("yjs"));
47
+ var ORIGIN = /* @__PURE__ */ Symbol("coaction-yjs");
48
+ function clone(value) {
49
+ if (typeof structuredClone === "function") {
50
+ return structuredClone(value);
51
+ }
52
+ return JSON.parse(JSON.stringify(value));
53
+ }
54
+ var bindYjs = (store, options = {}) => {
55
+ if (store.share === "client") {
56
+ throw new Error("Yjs binding is not supported in client store mode.");
57
+ }
58
+ const doc = options.doc ?? new Y.Doc();
59
+ const key = options.key ?? `coaction:${store.name}`;
60
+ const map = doc.getMap(key);
61
+ const syncNow = () => {
62
+ const pureState = clone(store.getPureState());
63
+ doc.transact(() => {
64
+ map.set("state", pureState);
65
+ }, ORIGIN);
66
+ };
67
+ const syncFromYjs = () => {
68
+ const incoming = map.get("state");
69
+ if (typeof incoming !== "object" || incoming === null) {
70
+ return;
71
+ }
72
+ store.setState(clone(incoming));
73
+ };
74
+ if (map.has("state")) {
75
+ syncFromYjs();
76
+ } else {
77
+ syncNow();
78
+ }
79
+ const observer = (event) => {
80
+ if (event.transaction.origin === ORIGIN) {
81
+ return;
82
+ }
83
+ if (event.keysChanged.has("state")) {
84
+ syncFromYjs();
85
+ }
86
+ };
87
+ map.observe(observer);
88
+ const unsubscribe = store.subscribe(() => {
89
+ syncNow();
90
+ });
91
+ return {
92
+ doc,
93
+ map,
94
+ syncNow,
95
+ destroy: () => {
96
+ unsubscribe();
97
+ map.unobserve(observer);
98
+ if (!options.doc) {
99
+ doc.destroy();
100
+ }
101
+ }
102
+ };
103
+ };
104
+ var yjs = (options = {}) => (store) => {
105
+ const binding = bindYjs(store, options);
106
+ const baseDestroy = store.destroy;
107
+ store.destroy = () => {
108
+ binding.destroy();
109
+ baseDestroy();
110
+ };
111
+ return store;
112
+ };
113
+
114
+ // index.ts
115
+ __reExport(index_exports, src_exports, module.exports);
116
+ // Annotate the CommonJS export names for ESM import in node:
117
+ 0 && (module.exports = {
118
+ bindYjs,
119
+ yjs
120
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,107 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+
19
+ // index.ts
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ bindYjs: () => bindYjs,
23
+ yjs: () => yjs
24
+ });
25
+
26
+ // src/index.ts
27
+ var src_exports = {};
28
+ __export(src_exports, {
29
+ bindYjs: () => bindYjs,
30
+ yjs: () => yjs
31
+ });
32
+ __reExport(src_exports, yjs_star);
33
+ import * as Y from "yjs";
34
+ import * as yjs_star from "yjs";
35
+ var ORIGIN = /* @__PURE__ */ Symbol("coaction-yjs");
36
+ function clone(value) {
37
+ if (typeof structuredClone === "function") {
38
+ return structuredClone(value);
39
+ }
40
+ return JSON.parse(JSON.stringify(value));
41
+ }
42
+ var bindYjs = (store, options = {}) => {
43
+ if (store.share === "client") {
44
+ throw new Error("Yjs binding is not supported in client store mode.");
45
+ }
46
+ const doc = options.doc ?? new Y.Doc();
47
+ const key = options.key ?? `coaction:${store.name}`;
48
+ const map = doc.getMap(key);
49
+ const syncNow = () => {
50
+ const pureState = clone(store.getPureState());
51
+ doc.transact(() => {
52
+ map.set("state", pureState);
53
+ }, ORIGIN);
54
+ };
55
+ const syncFromYjs = () => {
56
+ const incoming = map.get("state");
57
+ if (typeof incoming !== "object" || incoming === null) {
58
+ return;
59
+ }
60
+ store.setState(clone(incoming));
61
+ };
62
+ if (map.has("state")) {
63
+ syncFromYjs();
64
+ } else {
65
+ syncNow();
66
+ }
67
+ const observer = (event) => {
68
+ if (event.transaction.origin === ORIGIN) {
69
+ return;
70
+ }
71
+ if (event.keysChanged.has("state")) {
72
+ syncFromYjs();
73
+ }
74
+ };
75
+ map.observe(observer);
76
+ const unsubscribe = store.subscribe(() => {
77
+ syncNow();
78
+ });
79
+ return {
80
+ doc,
81
+ map,
82
+ syncNow,
83
+ destroy: () => {
84
+ unsubscribe();
85
+ map.unobserve(observer);
86
+ if (!options.doc) {
87
+ doc.destroy();
88
+ }
89
+ }
90
+ };
91
+ };
92
+ var yjs = (options = {}) => (store) => {
93
+ const binding = bindYjs(store, options);
94
+ const baseDestroy = store.destroy;
95
+ store.destroy = () => {
96
+ binding.destroy();
97
+ baseDestroy();
98
+ };
99
+ return store;
100
+ };
101
+
102
+ // index.ts
103
+ __reExport(index_exports, src_exports);
104
+ export {
105
+ bindYjs,
106
+ yjs
107
+ };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@coaction/yjs",
3
+ "version": "1.0.0",
4
+ "description": "A Coaction integration tool for Yjs",
5
+ "keywords": [
6
+ "state",
7
+ "coaction",
8
+ "yjs",
9
+ "crdt"
10
+ ],
11
+ "authors": [
12
+ "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
13
+ ],
14
+ "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-yjs#readme",
15
+ "license": "MIT",
16
+ "main": "dist/index.js",
17
+ "module": "dist/index.mjs",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.js",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "types": "dist/index.d.ts",
28
+ "sideEffects": false,
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/unadlib/coaction.git",
35
+ "directory": "packages/coaction-yjs"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/unadlib/coaction/issues"
39
+ },
40
+ "peerDependencies": {
41
+ "coaction": "^0.2.0",
42
+ "yjs": "^13.6.0"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "coaction": {
46
+ "optional": true
47
+ },
48
+ "yjs": {
49
+ "optional": true
50
+ }
51
+ },
52
+ "devDependencies": {
53
+ "coaction": "^0.2.0",
54
+ "yjs": "^13.6.27"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public",
58
+ "provenance": true
59
+ },
60
+ "author": "unadlib",
61
+ "scripts": {
62
+ "clean": "rm -rf dist",
63
+ "test": "vitest run test",
64
+ "build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
65
+ "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
66
+ }
67
+ }