plug-code 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 +21 -0
- package/package.json +28 -0
- package/src/contexts/pipeline.tsx +4 -0
- package/src/core/plcAPI.tsx +157 -0
- package/src/core/plcPipeline.tsx +81 -0
- package/src/core/plcStore.tsx +94 -0
- package/src/plug-code.tsx +67 -0
- package/src/types/api.ts +2 -0
- package/src/types/features.ts +7 -0
- package/src/types/general.ts +2 -0
- package/src/types/pipeline.ts +3 -0
- package/src/types/store.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Alan Mendez
|
|
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/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "plug-code",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/AlaunS/plug-code.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"type": "commonjs",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/AlaunS/plug-code/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/AlaunS/plug-code#readme",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"immer": "^11.1.3",
|
|
23
|
+
"react": "^19.2.3"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/react": "^19.2.7"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { produce } from "immer";
|
|
2
|
+
import type { ObjectType } from "../types/general";
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { PlcPipeline } from "./plcPipeline";
|
|
5
|
+
import type { PlcStore } from "./plcStore";
|
|
6
|
+
import type { transformerType } from "../types/api";
|
|
7
|
+
|
|
8
|
+
export class PlcAPI<S extends ObjectType> {
|
|
9
|
+
private store: PlcStore<S>
|
|
10
|
+
private pipeline: PlcPipeline<S>
|
|
11
|
+
private substores = new Map<string, any>()
|
|
12
|
+
private transformers: transformerType[] = []
|
|
13
|
+
|
|
14
|
+
constructor(store: PlcStore<S>) {
|
|
15
|
+
this.store = store
|
|
16
|
+
this.pipeline = new PlcPipeline(store)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
register(slot: string, node: () => React.ReactNode): void;
|
|
20
|
+
register<K extends string>(slot: string, node: (data: any) => React.ReactNode, dependencyKey: K): void;
|
|
21
|
+
register(slot: string, node: (data?: any) => React.ReactNode, dependencyKey?: string) {
|
|
22
|
+
if (dependencyKey) {
|
|
23
|
+
const ConnectedWrapper = () => {
|
|
24
|
+
const [data, setData] = useState(() => this.substores.get(dependencyKey));
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const unsubscribe = this.store.subscribe(dependencyKey as any, () => {
|
|
28
|
+
setData(this.substores.get(dependencyKey));
|
|
29
|
+
});
|
|
30
|
+
return unsubscribe;
|
|
31
|
+
}, []);
|
|
32
|
+
|
|
33
|
+
return node(data);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
this.store.batch(() => {
|
|
37
|
+
this.pipeline.register(slot, () => <ConnectedWrapper />);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.store.batch(() => {
|
|
42
|
+
this.pipeline.register(slot, node as () => React.ReactNode);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
scope<T = any>(key: string & "root") {
|
|
48
|
+
return {
|
|
49
|
+
get: (): T => this.getData(key),
|
|
50
|
+
|
|
51
|
+
update: (updater: (draft: T) => void) => {
|
|
52
|
+
this.update(key, updater);
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
connect: (renderer: (data: T) => React.ReactNode) => {
|
|
56
|
+
return this.connect(key, renderer);
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
render: (slotName: string) => {
|
|
60
|
+
return this.connect(key, (localData) => {
|
|
61
|
+
return this.pipeline.render(slotName, localData);
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
receive: (context: any = {}) => {
|
|
66
|
+
const currentData = this.getData(key);
|
|
67
|
+
return this.receive(currentData, context);
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
root: this
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
connect<T = any>(key: string, renderer: (data: T) => React.ReactNode): React.FC {
|
|
75
|
+
return () => {
|
|
76
|
+
const [data, setData] = useState<T>(() => this.substores.get(key));
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
const unsubscribe = this.store.subscribe(key as any, () => {
|
|
80
|
+
setData(this.substores.get(key));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return () => {
|
|
84
|
+
unsubscribe();
|
|
85
|
+
};
|
|
86
|
+
}, []);
|
|
87
|
+
|
|
88
|
+
if (data === undefined) return null;
|
|
89
|
+
return <>{renderer(data)}</>;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
wrap(slot: string, fn: (next: () => React.ReactNode) => () => React.ReactNode) {
|
|
94
|
+
this.store.batch(() => {
|
|
95
|
+
this.pipeline.wrap(slot, fn)
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
after(slot: string, node: () => React.ReactNode) {
|
|
100
|
+
this.store.batch(() => {
|
|
101
|
+
this.pipeline.register(slot, node)
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
render(slot: string) {
|
|
106
|
+
return this.pipeline.render(slot)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
invalidate(slot?: string) {
|
|
110
|
+
this.pipeline.invalidate(slot)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
send(id: string, fn: (data: any, context: any) => any, priority: number) {
|
|
114
|
+
this.transformers.push({ id, priority, fn });
|
|
115
|
+
this.transformers.sort((a, b) => a.priority - b.priority);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
receive(initialData: any, context: any = {}) {
|
|
119
|
+
let currentData = initialData;
|
|
120
|
+
|
|
121
|
+
for (const transformer of this.transformers) {
|
|
122
|
+
try {
|
|
123
|
+
currentData = transformer.fn(currentData, context);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error(`[Pipeline] Error en transformador '${transformer.id}':`, error);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return currentData;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
getData(key: string): any {
|
|
133
|
+
return this.substores.get(key)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
subscribe(listener: () => void) {
|
|
137
|
+
return this.store.subscribe(listener);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
createData<K extends string, T>(key: K, initialState: T) {
|
|
141
|
+
if (this.substores.has(key)) return
|
|
142
|
+
this.substores.set(key, initialState)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
update<K extends keyof S>(key: string & "root", updater: (draft: any) => void, slot?: string) {
|
|
146
|
+
const sub = this.substores.get(key)
|
|
147
|
+
if (!sub) return
|
|
148
|
+
|
|
149
|
+
const newSub = produce(sub, updater)
|
|
150
|
+
this.substores.set(key, newSub)
|
|
151
|
+
|
|
152
|
+
this.store.set(key as K, newSub as S[K])
|
|
153
|
+
if (slot) {
|
|
154
|
+
this.invalidate(slot)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ScopeContext } from "../contexts/pipeline";
|
|
3
|
+
import type { ObjectType } from "../types/general";
|
|
4
|
+
import type { ScheduledSlot, Slot } from "../types/pipeline";
|
|
5
|
+
import type { PlcStore } from "./plcStore";
|
|
6
|
+
|
|
7
|
+
export class PlcPipeline<S extends ObjectType> {
|
|
8
|
+
private slots = new Map<string, Slot[]>()
|
|
9
|
+
private store: PlcStore<S>
|
|
10
|
+
private cache = new Map<string, React.ReactNode[]>()
|
|
11
|
+
private scheduleQueue: ScheduledSlot[] = []
|
|
12
|
+
|
|
13
|
+
constructor(store: PlcStore<S>) {
|
|
14
|
+
this.store = store
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
register(slot: string, fn: Slot, priority: number = 0) {
|
|
18
|
+
if (!this.slots.has(slot)) this.slots.set(slot, [])
|
|
19
|
+
this.slots.get(slot)!.push(fn)
|
|
20
|
+
|
|
21
|
+
this.scheduleQueue.push({ slot, fn, priority })
|
|
22
|
+
this.store.set(`slot:${slot}`, Math.random() as any)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
wrap(slot: string, wrapper: (next: Slot) => Slot, priority: number = 0) {
|
|
26
|
+
const current = this.slots.get(slot) || []
|
|
27
|
+
this.slots.set(
|
|
28
|
+
slot,
|
|
29
|
+
current.map(fn => wrapper(fn))
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
current.forEach(fn => this.scheduleQueue.push({ slot, fn, priority }))
|
|
33
|
+
this.store.set(`slot:${slot}`, Math.random() as any)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
render(slot: string, contextData?: any) {
|
|
37
|
+
if (this.scheduleQueue.length > 0) {
|
|
38
|
+
this.scheduleQueue.sort((a, b) => b.priority - a.priority);
|
|
39
|
+
|
|
40
|
+
const slotsToUpdate = new Set<string>();
|
|
41
|
+
this.scheduleQueue.forEach(item => slotsToUpdate.add(item.slot));
|
|
42
|
+
|
|
43
|
+
slotsToUpdate.forEach(slotName => {
|
|
44
|
+
this.regenerateCache(slotName);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
this.scheduleQueue = [];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!this.cache.has(slot) && this.slots.has(slot)) {
|
|
51
|
+
this.regenerateCache(slot);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const content = this.cache.get(slot);
|
|
55
|
+
|
|
56
|
+
if (contextData !== undefined) {
|
|
57
|
+
return (
|
|
58
|
+
<ScopeContext.Provider value={contextData}>
|
|
59
|
+
{content}
|
|
60
|
+
</ScopeContext.Provider>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return content;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
invalidate(slot?: string) {
|
|
68
|
+
if (slot) {
|
|
69
|
+
this.cache.delete(slot)
|
|
70
|
+
} else {
|
|
71
|
+
this.cache.clear()
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private regenerateCache(slot: string) {
|
|
76
|
+
const nodes = this.slots.get(slot)?.map((fn, i) => (
|
|
77
|
+
<React.Fragment key={i}>{fn()}</React.Fragment>
|
|
78
|
+
)) || [];
|
|
79
|
+
this.cache.set(slot, nodes);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { produce, type Draft } from "immer";
|
|
2
|
+
import type { ObjectType } from "../types/general";
|
|
3
|
+
import type { Listener } from "../types/store";
|
|
4
|
+
|
|
5
|
+
export class PlcStore<S extends ObjectType> {
|
|
6
|
+
private state: S;
|
|
7
|
+
private listeners = new Set<Listener<S>>();
|
|
8
|
+
private batchQueue = new Set<keyof S | undefined>();
|
|
9
|
+
private debug: boolean;
|
|
10
|
+
|
|
11
|
+
private isBatching = false
|
|
12
|
+
|
|
13
|
+
constructor(initial: S, debug: boolean) {
|
|
14
|
+
debug = false;
|
|
15
|
+
|
|
16
|
+
this.state = initial;
|
|
17
|
+
this.debug = debug;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get<K extends keyof S>(key: K): S[K] {
|
|
21
|
+
return this.state[key]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getState(): S {
|
|
25
|
+
return this.state
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
set<K extends keyof S>(key: K, value: S[K]) {
|
|
29
|
+
if (this.state[key] === value) return
|
|
30
|
+
|
|
31
|
+
const oldValue = this.state[key]
|
|
32
|
+
this.state = produce(this.state, draft => {
|
|
33
|
+
(draft as any)[key] = value
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
if (this.debug) {
|
|
37
|
+
console.log(`[Store][set] ${String(key)}:`, { oldValue, newValue: value })
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if (this.isBatching) {
|
|
42
|
+
this.batchQueue.add(key)
|
|
43
|
+
} else {
|
|
44
|
+
this.emit(key)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
batch(updater: (draft: Draft<S>) => void) {
|
|
49
|
+
this.isBatching = true
|
|
50
|
+
|
|
51
|
+
const nextState = produce(this.state, draft => {
|
|
52
|
+
updater(draft)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
if (nextState === this.state) {
|
|
56
|
+
this.isBatching = false
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.state = nextState
|
|
61
|
+
this.isBatching = false
|
|
62
|
+
|
|
63
|
+
this.batchQueue.forEach(key => this.emit(key))
|
|
64
|
+
this.batchQueue.clear()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
subscribe(listener: () => void): () => void
|
|
68
|
+
subscribe<K extends keyof S>(key: K, listener: () => void): () => void
|
|
69
|
+
subscribe<K extends keyof S>(keyOrListener: K | (() => void), maybeListener?: () => void): () => void {
|
|
70
|
+
let listenerObj: Listener<S>
|
|
71
|
+
|
|
72
|
+
if (typeof keyOrListener === "function") {
|
|
73
|
+
listenerObj = { callback: keyOrListener }
|
|
74
|
+
} else {
|
|
75
|
+
listenerObj = { key: keyOrListener, callback: maybeListener! }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.listeners.add(listenerObj)
|
|
79
|
+
return () => {
|
|
80
|
+
this.listeners.delete(listenerObj)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private emit(changedKey?: keyof S) {
|
|
85
|
+
this.listeners.forEach(l => {
|
|
86
|
+
if (!l.key || l.key === changedKey) {
|
|
87
|
+
if (this.debug) {
|
|
88
|
+
console.log(`[Store][emit] key: ${String(changedKey)}`, l)
|
|
89
|
+
}
|
|
90
|
+
l.callback()
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { enableMapSet } from "immer";
|
|
2
|
+
import type { ObjectType } from "./types/general";
|
|
3
|
+
import type { FeatureType } from "./types/features";
|
|
4
|
+
import { PlcAPI } from "./core/plcAPI";
|
|
5
|
+
import { useEffect, useMemo, useState } from "react";
|
|
6
|
+
import { PlcStore } from "./core/plcStore";
|
|
7
|
+
|
|
8
|
+
enableMapSet()
|
|
9
|
+
export function createPlugAndCode<S extends ObjectType>(features: FeatureType<S>[]) {
|
|
10
|
+
const FeatureHost = ({ api, children }: { api: PlcAPI<any>, children?: React.ReactNode }) => (
|
|
11
|
+
<>
|
|
12
|
+
{/* Renderizamos el slot principal */}
|
|
13
|
+
{api.render("root")}
|
|
14
|
+
{children}
|
|
15
|
+
</>
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
function SystemPlcRoot({ api, children }: { api: PlcAPI<any>, children?: React.ReactNode }) {
|
|
19
|
+
if (!api) return null;
|
|
20
|
+
return (
|
|
21
|
+
<FeatureHost api={api}>
|
|
22
|
+
{children}
|
|
23
|
+
</FeatureHost>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function useSystemPlc<T extends object>(initialProps: T) {
|
|
28
|
+
|
|
29
|
+
const api = useMemo(() => {
|
|
30
|
+
const store = new PlcStore({}, true);
|
|
31
|
+
const api = new PlcAPI(store);
|
|
32
|
+
|
|
33
|
+
api.createData("root", initialProps);
|
|
34
|
+
|
|
35
|
+
for (const feature of features) {
|
|
36
|
+
feature.setup?.(api as any);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return api;
|
|
40
|
+
}, []);
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
api.update("root", (draft: any) => {
|
|
44
|
+
Object.assign(draft, initialProps);
|
|
45
|
+
});
|
|
46
|
+
}, [initialProps, api]);
|
|
47
|
+
|
|
48
|
+
const useSelector = <Result,>(selector: (state: any) => Result): Result => {
|
|
49
|
+
const [snap, setSnap] = useState(() => selector(api.getData("root")));
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
return api.subscribe(() => {
|
|
53
|
+
const fullState = api.getData("root");
|
|
54
|
+
setSnap(selector(fullState));
|
|
55
|
+
});
|
|
56
|
+
}, [selector]);
|
|
57
|
+
|
|
58
|
+
return snap;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
api,
|
|
63
|
+
useSelector
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { useSystemPlc, SystemPlcRoot }
|
|
67
|
+
}
|
package/src/types/api.ts
ADDED