patron-oop 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +8 -0
- package/dist/patron.min.js +1 -0
- package/examples/elegant_objects.html +124 -0
- package/examples/reactive.html +36 -0
- package/package.json +6 -2
- package/rollup.config.js +6 -0
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
4
4
|
|
5
|
+
## [1.2.0](https://github.com/kosukhin/patron/compare/v1.1.0...v1.2.0) (2024-10-13)
|
6
|
+
|
7
|
+
|
8
|
+
### Features
|
9
|
+
|
10
|
+
* **main:** сделал минификацию при сборке ролап ([53294a4](https://github.com/kosukhin/patron/commit/53294a470a3b31fb81643b629b83a94b8122a746))
|
11
|
+
* **main:** сделать пример использования библиотеки ([34232b8](https://github.com/kosukhin/patron/commit/34232b8fd020d42ab070eac375dd1e1bd9d7b16f))
|
12
|
+
|
5
13
|
## 1.1.0 (2024-10-12)
|
6
14
|
|
7
15
|
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";var e=Object.defineProperty,t=(t,i,s)=>((t,i,s)=>i in t?e(t,i,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[i]=s)(t,"symbol"!=typeof i?i+"":i,s);const i=new Map;class s{constructor(e){this.initiator=e,t(this,"patrons",new Set),t(this,"receive"),i.set(this,this.patrons);let s=null;const r=(e,t)=>{this.patrons.forEach((i=>{this.sendValueToGuest(e,i,t)}))};this.receive=(e,t)=>{const i=()=>{i===s&&r(e,t)};return s=i,queueMicrotask(i),this}}add(e){return e.introduction&&"patron"===e.introduction()&&this.patrons.add(e),this}remove(e){return this.patrons.delete(e),this}distribute(e,t){return this.add(t),this.sendValueToGuest(e,t,{}),this}sendValueToGuest(e,t,i){t.receive(e,{...i,data:{...i?.data??{},initiator:this.initiator,pool:this}})}}var r=Object.defineProperty,o=(e,t,i)=>((e,t,i)=>t in e?r(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i)(e,t+"",i);class n{constructor(e,t=null,i=null){this.defaultValue=t,this.theCache=i,o(this,"pool"),this.pool=new s(e)}receive(e,t){return this.theCache=e,this.pool.receive(e,t),this}receiving(e){return null!==this.theCache?e.receive(this.theCache):null!==this.defaultValue&&e.receive(this.defaultValue),this.pool.add(e),this}}class h{constructor(e){this.receiver=e}receive(e,t){return this.receiver(e,t),this}}var c=Object.defineProperty,a=(e,t,i)=>((e,t,i)=>t in e?c(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i)(e,"symbol"!=typeof t?t+"":t,i);class u{constructor(e){a(this,"guests",new Set),a(this,"patronPool"),this.patronPool=new s(e)}receive(e,t){return this.deliverToGuests(e,t),this.patronPool.receive(e,t),this}add(e){return e.introduction&&"guest"!==e.introduction()||this.guests.add(e),this.patronPool.add(e),this}remove(e){return this.guests.delete(e),this.patronPool.remove(e),this}distribute(e,t){return this.add(t),this.receive(e),this}deliverToGuests(e,t){this.guests.forEach((i=>{i.receive(e,t)})),this.guests.clear()}}class l{constructor(e,t){this.baseGuest=e,this.middleFn=t}introduction(){return this.baseGuest.introduction?this.baseGuest.introduction():"guest"}receive(e,t){return this.middleFn(e,t),this}}var d=Object.defineProperty,v=(e,t,i)=>((e,t,i)=>t in e?d(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i)(e,"symbol"!=typeof t?t+"":t,i);var p=Object.defineProperty,b=(e,t,i)=>((e,t,i)=>t in e?p(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i)(e,t+"",i);var f=Object.defineProperty,w=(e,t,i)=>((e,t,i)=>t in e?f(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i)(e,t+"",i);exports.Cache=n,exports.Chain=class{constructor(){v(this,"theChain"),v(this,"keysKnown",new Set),v(this,"keysFilled",new Set),v(this,"filledChainPool",new u(this)),this.theChain=new n(this,{})}resultArray(e){return this.filledChainPool.add(new l(e,(e=>Object.values(e)))),this.isChainFilled()&&this.theChain.receiving(new h((e=>{this.filledChainPool.receive(Object.values(e))}))),this}result(e){return this.isChainFilled()?(this.filledChainPool.add(e),this.theChain.receiving(new h((e=>{this.filledChainPool.receive(e)})))):this.filledChainPool.add(e),this}receiveKey(e){return this.keysKnown.add(e),new h((t=>{queueMicrotask((()=>{this.theChain.receiving(new h((i=>{this.keysFilled.add(e);const s={...i,[e]:t};this.theChain.receive(s),this.isChainFilled()&&this.filledChainPool.receive(s)})))}))}))}isChainFilled(){return this.keysFilled.size>0&&this.keysFilled.size===this.keysKnown.size}},exports.Factory=class{constructor(e){this.constructorFn=e}create(...e){return new this.constructorFn(...e)}},exports.FactoryDynamic=class{constructor(e){this.creationFn=e}create(...e){return this.creationFn(...e)}},exports.FactoryWithFactories=class{constructor(e,t={}){this.constructorFn=e,this.factories=t}create(...e){return new this.constructorFn(...e,this.factories)}},exports.Guest=h,exports.GuestAware=class{constructor(e){this.guestReceiver=e}receiving(e){return this.guestReceiver(e),e}},exports.GuestCast=class{constructor(e,t){this.sourceGuest=e,this.targetGuest=t}introduction(){return this.sourceGuest.introduction?this.sourceGuest.introduction():"guest"}receive(e,t){return this.targetGuest.receive(e,t),this}},exports.GuestInTheMiddle=l,exports.GuestPool=u,exports.GuestSync=class{constructor(e){this.theValue=e}receive(e){return this.theValue=e,this}value(){return this.theValue}},exports.Patron=class{constructor(e){this.willBePatron=e}introduction(){return"patron"}receive(e,t){return this.willBePatron.receive(e,t),this}},exports.PatronOnce=class{constructor(e){this.baseGuest=e,b(this,"received",!1)}introduction(){return"patron"}receive(e,t){this.received||this.baseGuest.receive(e,t);const i=t?.data;return i?.pool&&i.pool.remove(this),this}},exports.PatronPool=s,exports.Source=class{constructor(e){this.sourceDocument=e,w(this,"pool",new s(this))}receive(e){return this.sourceDocument=e,this.pool.receive(this.sourceDocument),this}receiving(e){return this.pool.distribute(this.sourceDocument,e),this}},exports.removePatronFromPools=e=>{i.forEach((t=>{t.delete(e)}))};
|
@@ -0,0 +1,124 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="en">
|
3
|
+
<head>
|
4
|
+
<meta charset="UTF-8">
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
+
<title>Document</title>
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<h1>Elegant Objects</h1>
|
10
|
+
<p>
|
11
|
+
Композиция объектов описывает поведение
|
12
|
+
</p>
|
13
|
+
<p>
|
14
|
+
Умножение: num1 * num2 = result
|
15
|
+
</p>
|
16
|
+
<div>
|
17
|
+
<label>
|
18
|
+
num1:
|
19
|
+
<input type="number" class="input-1" />
|
20
|
+
</label>
|
21
|
+
</div>
|
22
|
+
<div>
|
23
|
+
<label>
|
24
|
+
num2:
|
25
|
+
<input type="number" class="input-2" />
|
26
|
+
</label>
|
27
|
+
</div>
|
28
|
+
<div>
|
29
|
+
result = <b class="result"></b>
|
30
|
+
</div>
|
31
|
+
<script type="module">
|
32
|
+
import {Guest, Source, Patron, Chain, GuestCast, GuestInTheMiddle} from 'https://cdn.jsdelivr.net/npm/patron-oop@1.1.0/dist/patron.mjs';
|
33
|
+
|
34
|
+
// Presentation of text in document
|
35
|
+
class TextInDocument {
|
36
|
+
bindTo(selector) {
|
37
|
+
const element = document.querySelector(selector);
|
38
|
+
return new Guest((value) => {
|
39
|
+
element.innerText = value;
|
40
|
+
});
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
// Presentation of input with relation to source value
|
45
|
+
class Input {
|
46
|
+
#source;
|
47
|
+
|
48
|
+
constructor(source) {
|
49
|
+
this.#source = source;
|
50
|
+
}
|
51
|
+
|
52
|
+
bindTo(selector) {
|
53
|
+
const el = document.querySelector(selector);
|
54
|
+
this.#source.receiving(new Patron(new Guest((value) => {
|
55
|
+
el.value = value;
|
56
|
+
})));
|
57
|
+
el.addEventListener('keyup', (e) => {
|
58
|
+
this.receive(e.target.value);
|
59
|
+
});
|
60
|
+
el.addEventListener('change', (e) => {
|
61
|
+
this.receive(e.target.value);
|
62
|
+
});
|
63
|
+
return this;
|
64
|
+
}
|
65
|
+
|
66
|
+
receiving(guest) {
|
67
|
+
this.#source.receiving(guest);
|
68
|
+
return this;
|
69
|
+
}
|
70
|
+
|
71
|
+
receive(value) {
|
72
|
+
this.#source.receive(value);
|
73
|
+
return this;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
// Number presentation
|
78
|
+
class NumberOf {
|
79
|
+
#source;
|
80
|
+
|
81
|
+
constructor(source) {
|
82
|
+
this.#source = source;
|
83
|
+
}
|
84
|
+
|
85
|
+
number(guest) {
|
86
|
+
this.#source.receiving(guest);
|
87
|
+
return this;
|
88
|
+
}
|
89
|
+
|
90
|
+
receive(value) {
|
91
|
+
this.#source.receive(value);
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
// Multiplication
|
96
|
+
class Mul {
|
97
|
+
#num1;
|
98
|
+
#num2;
|
99
|
+
|
100
|
+
constructor(num1, num2) {
|
101
|
+
this.#num1 = num1;
|
102
|
+
this.#num2 = num2;
|
103
|
+
}
|
104
|
+
|
105
|
+
result(guest) {
|
106
|
+
const chain = new Chain();
|
107
|
+
this.#num1.number(new GuestCast(guest, chain.receiveKey('num1')));
|
108
|
+
this.#num2.number(new GuestCast(guest, chain.receiveKey('num2')));
|
109
|
+
chain.result(new GuestInTheMiddle(guest, ({num1, num2}) => {
|
110
|
+
guest.receive(num1 * num2)
|
111
|
+
}));
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
window.input1 = new Input(new Source(2)).bindTo('.input-1');
|
116
|
+
window.input2 = new Input(new Source(2)).bindTo('.input-2');
|
117
|
+
const multiplication = new Mul(
|
118
|
+
new NumberOf(window.input1),
|
119
|
+
new NumberOf(window.input2)
|
120
|
+
)
|
121
|
+
multiplication.result(new Patron(new TextInDocument().bindTo('.result')));
|
122
|
+
</script>
|
123
|
+
</body>
|
124
|
+
</html>
|
@@ -0,0 +1,36 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="en">
|
3
|
+
<head>
|
4
|
+
<meta charset="UTF-8">
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
+
<title>Reactive Patron</title>
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<h1>Reactive example</h1>
|
10
|
+
<div>
|
11
|
+
<label>
|
12
|
+
Поле ввода:
|
13
|
+
<input onkeyup="input.receive(this.value)">
|
14
|
+
</label>
|
15
|
+
</div>
|
16
|
+
<div>
|
17
|
+
Результат ввода: <span class="result"></span>
|
18
|
+
</div>
|
19
|
+
<script type="module">
|
20
|
+
import {Guest, Source, Patron} from 'https://cdn.jsdelivr.net/npm/patron-oop@1.1.0/dist/patron.mjs';
|
21
|
+
|
22
|
+
class Text {
|
23
|
+
constructor(selector) {
|
24
|
+
this.element = document.querySelector(selector);
|
25
|
+
}
|
26
|
+
|
27
|
+
receive(value) {
|
28
|
+
this.element.innerText = value;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
window.input = new Source(1);
|
33
|
+
input.receiving(new Patron(new Text('.result')));
|
34
|
+
</script>
|
35
|
+
</body>
|
36
|
+
</html>
|
package/package.json
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
{
|
2
2
|
"name": "patron-oop",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.2.0",
|
4
4
|
"description": "",
|
5
5
|
"type": "module",
|
6
6
|
"main": "dist/patron.js",
|
7
7
|
"module": "dist/patron.mjs",
|
8
8
|
"typings": "dist/patron.d.ts",
|
9
|
+
"keywords": [
|
10
|
+
"oop"
|
11
|
+
],
|
9
12
|
"scripts": {
|
10
13
|
"build": "rollup -c",
|
11
14
|
"lint": "eslint src",
|
@@ -13,7 +16,7 @@
|
|
13
16
|
"test": "vitest",
|
14
17
|
"cz": "git add . && git cz",
|
15
18
|
"test-debug": "env DEBUG=app:* vitest",
|
16
|
-
"release": "standard-version --no-verify"
|
19
|
+
"release": "npm run build && standard-version --no-verify"
|
17
20
|
},
|
18
21
|
"repository": {
|
19
22
|
"type": "git",
|
@@ -23,6 +26,7 @@
|
|
23
26
|
"devDependencies": {
|
24
27
|
"@eslint/eslintrc": "^3.1.0",
|
25
28
|
"@eslint/js": "^9.12.0",
|
29
|
+
"@rollup/plugin-terser": "^0.4.4",
|
26
30
|
"@typescript-eslint/eslint-plugin": "^8.8.1",
|
27
31
|
"@typescript-eslint/parser": "^8.8.1",
|
28
32
|
"cz-customizable": "^7.0.0",
|
package/rollup.config.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import dts from "rollup-plugin-dts";
|
2
2
|
import esbuild from "rollup-plugin-esbuild";
|
3
|
+
import terser from "@rollup/plugin-terser";
|
3
4
|
|
4
5
|
const name = "dist/patron";
|
5
6
|
|
@@ -23,6 +24,11 @@ export default [
|
|
23
24
|
format: "es",
|
24
25
|
sourcemap: true,
|
25
26
|
},
|
27
|
+
{
|
28
|
+
file: `${name}.min.js`,
|
29
|
+
format: "cjs",
|
30
|
+
plugins: [terser()],
|
31
|
+
},
|
26
32
|
],
|
27
33
|
}),
|
28
34
|
bundle({
|