@arcanejs/protocol 0.1.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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Sam Lanning
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,43 @@
1
+ # `@arcanejs/diff`
2
+
3
+ [![NPM Version](https://img.shields.io/npm/v/%40arcanejs%2Fdiff)](https://www.npmjs.com/package/@arcanejs/diff)
4
+
5
+ This package provides an easy way to:
6
+
7
+ - Create diffs by comparing objects
8
+ - Update objects by applying diffs
9
+
10
+ This library is written in TypeScript,
11
+ and produces diffs that are type-safe,
12
+ and can only be applied to objects that match the type
13
+ of the objects being compared.
14
+
15
+ This package is part of the
16
+ [`arcanejs` project](https://github.com/arcanejs/arcanejs#arcanejs),
17
+ and is used to maintain a copy of a JSON tree in downstream clients in real-time
18
+ via websockets.
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import { diffJson, Diff } from '@arcanejs/diff/diff';
24
+ import { patchJson } from '@arcanejs/diff/patch';
25
+
26
+ type E = {
27
+ foo: string;
28
+ bar?: number[];
29
+ };
30
+
31
+ const a: E = { foo: 'bar' };
32
+ const b: E = { foo: 'baz', bar: [1] };
33
+
34
+ const diffA: Diff<E> = diffJson(a, b);
35
+
36
+ const resultA = patchJson(a, diffA);
37
+
38
+ console.log(resultB); // { foo: 'baz', bar: [1] }
39
+
40
+ const c = { baz: 'foo' };
41
+
42
+ const resultB = patchJson(c, diffA); // TypeScript Type Error: Property 'baz' is missing in type '{ foo: string; bar?: number[] | undefined; }' but required in type '{ baz: string; }'
43
+ ```
@@ -0,0 +1,124 @@
1
+ import { Diff } from '@arcanejs/diff';
2
+ import { GroupComponentStyle } from './styles';
3
+ type BaseComponent = {
4
+ key: number;
5
+ };
6
+ export type ButtonComponent = BaseComponent & {
7
+ component: 'button';
8
+ text: string;
9
+ icon?: string;
10
+ state: {
11
+ state: 'normal' | 'pressed';
12
+ } | {
13
+ state: 'error';
14
+ error: string;
15
+ };
16
+ };
17
+ export type GroupCollapsedState = 'open' | 'closed';
18
+ export type DefaultGroupCollapsedState = GroupCollapsedState | 'auto';
19
+ export type GroupHeaderComponent = BaseComponent & {
20
+ component: 'group-header';
21
+ children: Component[];
22
+ };
23
+ export type GroupComponent = BaseComponent & GroupComponentStyle & {
24
+ component: 'group';
25
+ title?: string;
26
+ children: Component[];
27
+ headers?: GroupHeaderComponent[];
28
+ labels?: Array<{
29
+ text: string;
30
+ }>;
31
+ editableTitle: boolean;
32
+ /**
33
+ * If set, allows the group to be collapsed,
34
+ * by default set to the given state
35
+ */
36
+ defaultCollapsibleState?: DefaultGroupCollapsedState;
37
+ };
38
+ export type LabelComponent = BaseComponent & {
39
+ component: 'label';
40
+ bold?: boolean;
41
+ text: string;
42
+ };
43
+ export type RectComponent = BaseComponent & {
44
+ component: 'rect';
45
+ color: string;
46
+ };
47
+ export type SliderButtonComponent = BaseComponent & {
48
+ component: 'slider_button';
49
+ min: number;
50
+ max: number;
51
+ step: number;
52
+ value: number | null;
53
+ };
54
+ export type SwitchComponent = BaseComponent & {
55
+ component: 'switch';
56
+ state: 'on' | 'off';
57
+ };
58
+ export type TabComponent = BaseComponent & {
59
+ component: 'tab';
60
+ name: string;
61
+ child?: Component;
62
+ };
63
+ export type TabsComponent = BaseComponent & {
64
+ component: 'tabs';
65
+ tabs: TabComponent[];
66
+ };
67
+ export type TextInputComponent = BaseComponent & {
68
+ component: 'text-input';
69
+ value: string;
70
+ };
71
+ export type TimelineState = {
72
+ state: 'playing';
73
+ totalTimeMillis: number;
74
+ effectiveStartTime: number;
75
+ speed: number;
76
+ } | {
77
+ state: 'stopped';
78
+ totalTimeMillis: number;
79
+ currentTimeMillis: number;
80
+ };
81
+ export type TimelineComponent = BaseComponent & {
82
+ component: 'timeline';
83
+ state: TimelineState;
84
+ title?: string;
85
+ subtitles?: string[];
86
+ source?: {
87
+ name: string;
88
+ };
89
+ };
90
+ export type Component = ButtonComponent | GroupHeaderComponent | GroupComponent | LabelComponent | RectComponent | SliderButtonComponent | SwitchComponent | TabComponent | TabsComponent | TextInputComponent | TimelineComponent;
91
+ export type SendTreeMsg = {
92
+ type: 'tree-full';
93
+ root: GroupComponent;
94
+ };
95
+ export type UpdateTreeMsg = {
96
+ type: 'tree-diff';
97
+ diff: Diff<GroupComponent>;
98
+ };
99
+ export type ServerMessage = SendTreeMsg | UpdateTreeMsg;
100
+ export type BaseClientComponentMessage = {
101
+ type: 'component-message';
102
+ componentKey: number;
103
+ };
104
+ export type ButtonPressMessage = BaseClientComponentMessage & {
105
+ component: 'button';
106
+ };
107
+ export type GroupTitleChangeMessage = BaseClientComponentMessage & {
108
+ component: 'group';
109
+ title: string;
110
+ };
111
+ export type SliderButtonUpdateMessage = BaseClientComponentMessage & {
112
+ component: 'slider_button';
113
+ value: number;
114
+ };
115
+ export type SwitchToggleMessage = BaseClientComponentMessage & {
116
+ component: 'switch';
117
+ };
118
+ export type TextInputUpdateMessage = BaseClientComponentMessage & {
119
+ component: 'text-input';
120
+ value: string;
121
+ };
122
+ export type ClientComponentMessage = ButtonPressMessage | GroupTitleChangeMessage | SliderButtonUpdateMessage | SwitchToggleMessage | TextInputUpdateMessage;
123
+ export type ClientMessage = ClientComponentMessage;
124
+ export {};
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Styling options for the [[Group]] component
3
+ *
4
+ * Default Styling: [[GROUP_DEFAULT_STYLE]]
5
+ */
6
+ export type GroupComponentStyle = {
7
+ /**
8
+ * In which way should child components of this group be organized?
9
+ */
10
+ direction: 'horizontal' | 'vertical';
11
+ /**
12
+ * If true, when the group runs out of vertical or horizontal space, child
13
+ * components will be wrapped, and start to be arranged on additional columns
14
+ * or rows.
15
+ */
16
+ wrap?: boolean;
17
+ /**
18
+ * If true, this group will have a border and a different color background
19
+ * to its parent.
20
+ *
21
+ * This allows you to add a distinctive border between components,
22
+ * without needing to set a header, add header components,
23
+ * or make it collapsible.
24
+ */
25
+ border?: true;
26
+ };
27
+ /**
28
+ * Styling options for the [[Label]] component
29
+ *
30
+ * Default Styling: [[LABEL_DEFAULT_STYLE]]
31
+ */
32
+ export type LabelComponentStyle = {
33
+ /**
34
+ * If true, make the text of this label bold
35
+ */
36
+ bold?: boolean;
37
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@arcanejs/protocol",
3
+ "version": "0.1.1",
4
+ "private": false,
5
+ "description": "The JSON protocol types for the @arcanejs Toolkit",
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "Sam Lanning",
9
+ "url": "https://samlanning.com"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/arcanejs/arcanejs.git"
14
+ },
15
+ "exports": {
16
+ ".": {
17
+ "@arcanejs/source": "./src/index.ts",
18
+ "types": "./dist/index.d.ts"
19
+ },
20
+ "./styles": {
21
+ "@arcanejs/source": "./src/styles.ts",
22
+ "types": "./dist/styles.d.ts"
23
+ }
24
+ },
25
+ "devDependencies": {
26
+ "@types/eslint": "^8.56.5",
27
+ "@types/jest": "^29.5.12",
28
+ "eslint": "^8.57.0",
29
+ "typescript": "^5.3.3",
30
+ "@arcanejs/diff": "^0.3.1",
31
+ "@arcanejs/typescript-config": "^0.0.0",
32
+ "@arcanejs/eslint-config": "^0.0.0"
33
+ },
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "scripts": {
41
+ "lint": "eslint . --max-warnings 0",
42
+ "build": "rm -rf dist && tsc"
43
+ }
44
+ }