@blockquote-web-components/blockquote-controller-rxjs 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) 2022 blockquote-controller-rxjs
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,66 @@
1
+ # BlockquoteControllerRxjs
2
+
3
+ ![Lit](https://img.shields.io/badge/lit-2.0.0-blue)
4
+
5
+ `BlockquoteControllerRxjs` is a [Reactive Controller](https://lit.dev/docs/composition/controllers/#content)
6
+
7
+ - Original idea by [Adrian Fâciu](https://github.com/adrianfaciu/rx-lit)
8
+ - [observables-litelement](https://adrianfaciu.dev/posts/observables-litelement/)
9
+
10
+ `BlockquoteControllerRxjs` provides a subscribe method to which pass the property we want to
11
+ assign values to and the Observable we want to subscribe.
12
+
13
+ - It works with [reactive properties](https://lit.dev/docs/components/properties/) and no-reactive properties
14
+ - It ignores calls on the same property with the same Observable
15
+ - It unsubscribes from old observable if called again on the same property with different Observable
16
+ - It unsubscribes when the component is removed
17
+
18
+ ## Usage
19
+
20
+ ```js
21
+ class BlockquoteControllerRxjsDemo extends LitElement {
22
+ static get is() {
23
+ return 'blockquote-controller-rxjs-demo';
24
+ }
25
+
26
+ static get properties() {
27
+ return {
28
+ _pos: {
29
+ type: Object,
30
+ attribute: false,
31
+ },
32
+ };
33
+ }
34
+
35
+ constructor() {
36
+ super();
37
+ this.rx = new BlockquoteControllerRxjs(this);
38
+ this._pos = { x: 0, y: 0 };
39
+ this.values$ = fromEvent(window, 'mousemove').pipe(
40
+ map(({ clientX, clientY }) => ({ x: clientX, y: clientY })),
41
+ );
42
+ }
43
+
44
+ connectedCallback() {
45
+ super.connectedCallback();
46
+
47
+ // Property and Observable.
48
+ this.rx.subscribe('_pos', this.values$);
49
+ }
50
+
51
+ render() {
52
+ return html`
53
+ <p>The mouse is at:</p>
54
+ <pre>
55
+ x: ${this._pos.x}
56
+ y: ${this._pos.y}
57
+ </pre
58
+ >
59
+ `;
60
+ }
61
+ }
62
+ ```
63
+
64
+ ## Exports
65
+
66
+ - BlockquoteControllerRxjs
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export { BlockquoteControllerRxjs } from './src/BlockquoteControllerRxjs.js';
package/package.json ADDED
@@ -0,0 +1,155 @@
1
+ {
2
+ "name": "@blockquote-web-components/blockquote-controller-rxjs",
3
+ "version": "1.0.0",
4
+ "description": "Webcomponent blockquote-controller-rxjs following open-wc recommendations",
5
+ "keywords": [
6
+ "lit",
7
+ "web-component",
8
+ "lit-element"
9
+ ],
10
+ "license": "MIT",
11
+ "author": "blockquote-controller-rxjs",
12
+ "main": "index.js",
13
+ "module": "index.js",
14
+ "files": [
15
+ "/define/",
16
+ "/src/",
17
+ "index.js",
18
+ "!/**/*.scss"
19
+ ],
20
+ "scripts": {
21
+ "analyze": "cem analyze --litelement --globs \"{src,define}/**/*.{js,ts}\" \"index.js\"",
22
+ "analyze:doc": "npm run analyze && npx web-component-analyzer \"{src,define}/**/*.{js,ts}\" \"index.js\" --outFile README.md",
23
+ "build": "echo \"This is not a TypeScript project, so no need to build.\"",
24
+ "dev:vite": "vite build",
25
+ "format": "npm run format:eslint && npm run format:prettier && npm run format:stylelint",
26
+ "format:eslint": "eslint \"**/*.{js,ts,html}\" --fix --ignore-path .eslintignore",
27
+ "format:prettier": "prettier \"**/*.{js,ts,json,html,md}\" --write --ignore-path .eslintignore",
28
+ "format:stylelint": "stylelint \"**/*.{scss,css}\" --fix --allow-empty-input --ignore-path .eslintignore",
29
+ "postinstall": "npm run sort:package",
30
+ "lint": "npm run lint:eslint && npm run lint:prettier && npm run lint:stylelint",
31
+ "lint:eslint": "eslint \"**/*.{js,ts,html}\" --ignore-path .eslintignore",
32
+ "lint:prettier": "prettier \"**/*.{js,ts,json,html,md}\" --check --ignore-path .eslintignore",
33
+ "lint:stylelint": "stylelint \"**/*.{scss,css}\" --allow-empty-input --ignore-path .eslintignore",
34
+ "preview:vite": "vite preview",
35
+ "sass:watch": "sass-style-template",
36
+ "sort:package": "npx sort-package-json",
37
+ "start": "concurrently -k -r \"npm:sass:watch\" \"npm:vite\"",
38
+ "start:wds": "concurrently -k -r \"npm:sass:watch\" \"npm:wds\"",
39
+ "test": "wtr --coverage",
40
+ "test:watch": "wtr --watch",
41
+ "vite": "vite",
42
+ "wds": "web-dev-server"
43
+ },
44
+ "husky": {
45
+ "hooks": {
46
+ "pre-commit": "lint-staged"
47
+ }
48
+ },
49
+ "lint-staged": {
50
+ "**/*.{js,ts,html}": [
51
+ "npm run format:eslint"
52
+ ],
53
+ "**/*.{js,ts,json,html,md}": [
54
+ "npm run format:prettier"
55
+ ],
56
+ "**/*.{scss,css}": [
57
+ "npm run format:stylelint"
58
+ ]
59
+ },
60
+ "prettier": {
61
+ "arrowParens": "avoid",
62
+ "printWidth": 100,
63
+ "singleQuote": true,
64
+ "trailingComma": "all",
65
+ "overrides": [
66
+ {
67
+ "files": "*.{scss,css}",
68
+ "options": {
69
+ "singleQuote": false
70
+ }
71
+ }
72
+ ]
73
+ },
74
+ "eslintConfig": {
75
+ "parserOptions": {
76
+ "ecmaVersion": "latest"
77
+ },
78
+ "plugins": [
79
+ "log-filenames"
80
+ ],
81
+ "extends": [
82
+ "prettier",
83
+ "@open-wc"
84
+ ],
85
+ "rules": {
86
+ "class-methods-use-this": "off",
87
+ "object-curly-newline": "off",
88
+ "import/extensions": [
89
+ "error",
90
+ "always",
91
+ {
92
+ "ignorePackages": true
93
+ }
94
+ ],
95
+ "import/no-extraneous-dependencies": [
96
+ "error",
97
+ {
98
+ "devDependencies": [
99
+ "**/test/**/*.{js,ts}",
100
+ "**/*.config.{js,mjs,cjs}",
101
+ "**/*.conf.{js,mjs,cjs}"
102
+ ]
103
+ }
104
+ ],
105
+ "import/no-unresolved": "off",
106
+ "import/prefer-default-export": "off"
107
+ }
108
+ },
109
+ "stylelint": {
110
+ "extends": "stylelint-config-standard-scss",
111
+ "rules": {
112
+ "custom-property-pattern": null,
113
+ "max-line-length": null
114
+ }
115
+ },
116
+ "dependencies": {
117
+ "lit": "^2.0.2",
118
+ "rxjs": "^7.5.5"
119
+ },
120
+ "devDependencies": {
121
+ "@blockquote/rollup-plugin-total-bundlesize": "^1.0.0",
122
+ "@blockquote/sass-style-template": "^2.0.0",
123
+ "@blockquote/test-runner-mocha-style-reporter": "^1.0.0",
124
+ "@custom-elements-manifest/analyzer": "^0.5.3",
125
+ "@custom-elements-manifest/to-markdown": "^0.1.0",
126
+ "@open-wc/eslint-config": "^7.0.0",
127
+ "@open-wc/testing": "^3.0.3",
128
+ "@ungap/global-this": "^0.4.4",
129
+ "@web/dev-server": "^0.1.25",
130
+ "@web/rollup-plugin-html": "^1.10.1",
131
+ "@web/test-runner": "^0.13.23",
132
+ "@web/test-runner-playwright": "^0.8.8",
133
+ "@webcomponents/shadycss": "^1.11.0",
134
+ "@webcomponents/webcomponentsjs": "^2.6.0",
135
+ "concurrently": "^7.0.0",
136
+ "eslint": "^8.0.0",
137
+ "eslint-config-prettier": "^8.3.0",
138
+ "eslint-plugin-log-filenames": "^1.0.4",
139
+ "husky": "^4.3.8",
140
+ "lint-staged": "^12.0.0",
141
+ "prettier": "^2.4.1",
142
+ "rollup-plugin-copy": "^3.4.0",
143
+ "rollup-plugin-minify-html-literals": "^1.2.6",
144
+ "rollup-plugin-summary": "^1.3.0",
145
+ "sort-package-json": "^1.52.0",
146
+ "stylelint": "^14.0.0",
147
+ "stylelint-config-standard-scss": "^3.0.0",
148
+ "tiny-array-flat-polyfill": "^0.2.1",
149
+ "vite": "^2.7.0"
150
+ },
151
+ "publishConfig": {
152
+ "access": "public"
153
+ },
154
+ "customElements": "custom-elements.json"
155
+ }
@@ -0,0 +1,109 @@
1
+ import { Subject, isObservable } from 'rxjs';
2
+ import { takeUntil } from 'rxjs/operators';
3
+
4
+ const unsubscribe = Symbol('unsubscribe');
5
+ const subscriptions = Symbol('subscriptions');
6
+
7
+ /**
8
+ # BlockquoteControllerRxjs
9
+
10
+ ![Lit](https://img.shields.io/badge/lit-2.0.0-blue)
11
+
12
+ `BlockquoteControllerRxjs` is a Reactive Controller.
13
+
14
+ - Original idea by [Adrian Fâciu](https://github.com/adrianfaciu/rx-lit)
15
+ - [observables-litelement](https://adrianfaciu.dev/posts/observables-litelement/)
16
+
17
+ `BlockquoteControllerRxjs` provides a subscribe method to which pass the property we want to
18
+ assign values to and the Observable we want to subscribe.
19
+
20
+ - It works with [reactive properties](https://lit.dev/docs/components/properties/) and no-reactive properties
21
+ - It ignores calls on the same property with the same Observable
22
+ - It unsubscribes from old observable if called again on the same property with different Observable
23
+ - It unsubscribes when the component is removed
24
+
25
+ ## Usage
26
+
27
+ ```js
28
+ class BlockquoteControllerRxjsDemo extends LitElement {
29
+ static get is() {
30
+ return 'blockquote-controller-rxjs-demo';
31
+ }
32
+
33
+ static get properties() {
34
+ return {
35
+ _pos: {
36
+ type: Object,
37
+ attribute: false,
38
+ },
39
+ };
40
+ }
41
+
42
+ constructor() {
43
+ super();
44
+ this.rx = new BlockquoteControllerRxjs(this);
45
+ this._pos = { x: 0, y: 0 };
46
+ this.values$ = fromEvent(window, 'mousemove').pipe(
47
+ map(({ clientX, clientY }) => ({ x: clientX, y: clientY })),
48
+ );
49
+ }
50
+
51
+ connectedCallback() {
52
+ super.connectedCallback();
53
+
54
+ // Property and Observable.
55
+ this.rx.subscribe('_pos', this.values$);
56
+ }
57
+
58
+ render() {
59
+ return html`
60
+ <p>The mouse is at:</p>
61
+ <pre>
62
+ x: ${this._pos.x}
63
+ y: ${this._pos.y}
64
+ </pre
65
+ >
66
+ `;
67
+ }
68
+ }
69
+ ```
70
+
71
+ ## Exports
72
+
73
+ - BlockquoteControllerRxjs
74
+
75
+ */
76
+ export class BlockquoteControllerRxjs {
77
+ constructor(host) {
78
+ this[unsubscribe] = new Subject();
79
+ this[subscriptions] = new Map();
80
+ (this.host = host).addController(this);
81
+ }
82
+
83
+ subscribe(propKey, stream$) {
84
+ if (!isObservable(stream$)) {
85
+ throw new Error('Invalid Observable!');
86
+ }
87
+ const existingSubscription = this[subscriptions].get(propKey);
88
+ if (existingSubscription) {
89
+ if (existingSubscription?.stream$ === stream$) {
90
+ return stream$;
91
+ }
92
+ existingSubscription?.subscription?.unsubscribe();
93
+ }
94
+
95
+ // eslint-disable-next-line arrow-parens
96
+ const subscription = stream$.pipe(takeUntil(this[unsubscribe])).subscribe(res => {
97
+ this.host[propKey] = res;
98
+ this.host.requestUpdate();
99
+ });
100
+
101
+ this[subscriptions].set(propKey, { stream$, subscription });
102
+
103
+ return stream$;
104
+ }
105
+
106
+ hostDisconnected() {
107
+ this[unsubscribe].next();
108
+ }
109
+ }