@guanghechen/observable 5.0.0-alpha.2

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) 2020-2023 guanghechen (https://github.com/guanghechen)
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,74 @@
1
+ <header>
2
+ <h1 align="center">
3
+ <a href="https://github.com/guanghechen/node-scaffolds/tree/release-5.x.x/packages/observable#readme">@guanghechen/observable</a>
4
+ </h1>
5
+ <div align="center">
6
+ <a href="https://www.npmjs.com/package/@guanghechen/observable">
7
+ <img
8
+ alt="Npm Version"
9
+ src="https://img.shields.io/npm/v/@guanghechen/observable.svg"
10
+ />
11
+ </a>
12
+ <a href="https://www.npmjs.com/package/@guanghechen/observable">
13
+ <img
14
+ alt="Npm Download"
15
+ src="https://img.shields.io/npm/dm/@guanghechen/observable.svg"
16
+ />
17
+ </a>
18
+ <a href="https://www.npmjs.com/package/@guanghechen/observable">
19
+ <img
20
+ alt="Npm License"
21
+ src="https://img.shields.io/npm/l/@guanghechen/observable.svg"
22
+ />
23
+ </a>
24
+ <a href="#install">
25
+ <img
26
+ alt="Module formats: cjs, esm"
27
+ src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
28
+ />
29
+ </a>
30
+ <a href="https://github.com/nodejs/node">
31
+ <img
32
+ alt="Node.js Version"
33
+ src="https://img.shields.io/node/v/@guanghechen/observable"
34
+ />
35
+ </a>
36
+ <a href="https://github.com/facebook/jest">
37
+ <img
38
+ alt="Tested with Jest"
39
+ src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
40
+ />
41
+ </a>
42
+ <a href="https://github.com/prettier/prettier">
43
+ <img
44
+ alt="Code Style: prettier"
45
+ src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
46
+ />
47
+ </a>
48
+ </div>
49
+ </header>
50
+ <br/>
51
+
52
+
53
+ A simple observable for subscript/notification pattern.
54
+
55
+
56
+ ## Install
57
+
58
+ ```bash
59
+ npm install --save @guanghechen/observable
60
+ ```
61
+
62
+ * yarn
63
+
64
+ ```bash
65
+ yarn add @guanghechen/observable
66
+ ```
67
+
68
+
69
+ ## Usage
70
+
71
+ * Basic
72
+
73
+
74
+ [homepage]: https://github.com/guanghechen/node-scaffolds/tree/release-5.x.x/packages/observable#readme
@@ -0,0 +1,64 @@
1
+ 'use strict';
2
+
3
+ class Observable {
4
+ _subscribers;
5
+ _equals;
6
+ _value;
7
+ _available;
8
+ constructor(defaultValue, equals) {
9
+ this._subscribers = new Set();
10
+ this._equals = equals ?? ((x, y) => x === y);
11
+ this._value = defaultValue;
12
+ this._available = true;
13
+ }
14
+ get subscribers() {
15
+ return Array.from(this._subscribers.values());
16
+ }
17
+ get disposed() {
18
+ return !this._available;
19
+ }
20
+ dispose() {
21
+ this._available = false;
22
+ this._subscribers.clear();
23
+ }
24
+ getSnapshot = () => {
25
+ return this._value;
26
+ };
27
+ next(value) {
28
+ if (this._available && !this._equals(value, this._value)) {
29
+ this._value = value;
30
+ const subscribers = this.subscribers;
31
+ for (const subscriber of subscribers)
32
+ subscriber.next(value);
33
+ }
34
+ }
35
+ error(error) {
36
+ if (this._available) {
37
+ const subscribers = this.subscribers;
38
+ for (const subscriber of subscribers)
39
+ subscriber.error(error);
40
+ this.dispose();
41
+ }
42
+ }
43
+ complete() {
44
+ if (this._available) {
45
+ const subscribers = this.subscribers;
46
+ for (const subscriber of subscribers)
47
+ subscriber.complete();
48
+ this.dispose();
49
+ }
50
+ }
51
+ subscribe(subscriber) {
52
+ if (!this._subscribers.has(subscriber)) {
53
+ subscriber.next(this.getSnapshot());
54
+ this._subscribers.add(subscriber);
55
+ }
56
+ return {
57
+ unsubscribe: () => {
58
+ this._subscribers.delete(subscriber);
59
+ },
60
+ };
61
+ }
62
+ }
63
+
64
+ exports.Observable = Observable;
@@ -0,0 +1,62 @@
1
+ class Observable {
2
+ _subscribers;
3
+ _equals;
4
+ _value;
5
+ _available;
6
+ constructor(defaultValue, equals) {
7
+ this._subscribers = new Set();
8
+ this._equals = equals ?? ((x, y) => x === y);
9
+ this._value = defaultValue;
10
+ this._available = true;
11
+ }
12
+ get subscribers() {
13
+ return Array.from(this._subscribers.values());
14
+ }
15
+ get disposed() {
16
+ return !this._available;
17
+ }
18
+ dispose() {
19
+ this._available = false;
20
+ this._subscribers.clear();
21
+ }
22
+ getSnapshot = () => {
23
+ return this._value;
24
+ };
25
+ next(value) {
26
+ if (this._available && !this._equals(value, this._value)) {
27
+ this._value = value;
28
+ const subscribers = this.subscribers;
29
+ for (const subscriber of subscribers)
30
+ subscriber.next(value);
31
+ }
32
+ }
33
+ error(error) {
34
+ if (this._available) {
35
+ const subscribers = this.subscribers;
36
+ for (const subscriber of subscribers)
37
+ subscriber.error(error);
38
+ this.dispose();
39
+ }
40
+ }
41
+ complete() {
42
+ if (this._available) {
43
+ const subscribers = this.subscribers;
44
+ for (const subscriber of subscribers)
45
+ subscriber.complete();
46
+ this.dispose();
47
+ }
48
+ }
49
+ subscribe(subscriber) {
50
+ if (!this._subscribers.has(subscriber)) {
51
+ subscriber.next(this.getSnapshot());
52
+ this._subscribers.add(subscriber);
53
+ }
54
+ return {
55
+ unsubscribe: () => {
56
+ this._subscribers.delete(subscriber);
57
+ },
58
+ };
59
+ }
60
+ }
61
+
62
+ export { Observable };
@@ -0,0 +1,38 @@
1
+ interface IDisposable {
2
+ readonly disposed: boolean;
3
+ dispose(): void;
4
+ }
5
+ type IEquals<T> = (x: T, y: T) => boolean;
6
+ interface IUnsubscribable {
7
+ unsubscribe(): void;
8
+ }
9
+ interface ISubscribable<T> {
10
+ subscribe(subscriber: ISubscriber<T>): IUnsubscribable;
11
+ }
12
+ interface ISubscriber<T> {
13
+ next(value: T): void;
14
+ error(err: any): void;
15
+ complete(): void;
16
+ }
17
+
18
+ declare class Observable<T> implements IDisposable, ISubscribable<T>, ISubscriber<T> {
19
+ protected readonly _subscribers: Set<ISubscriber<T>>;
20
+ protected readonly _equals: IEquals<T>;
21
+ protected _value: T;
22
+ protected _available: boolean;
23
+ constructor(defaultValue: T, equals?: IEquals<T>);
24
+ get subscribers(): Array<ISubscriber<T>>;
25
+ get disposed(): boolean;
26
+ dispose(): void;
27
+ getSnapshot: () => T;
28
+ /**
29
+ * 1. Update observable current state.
30
+ * 2. Notify all subscribers if the value is changed.
31
+ */
32
+ next(value: T): void;
33
+ error(error: unknown): void;
34
+ complete(): void;
35
+ subscribe(subscriber: ISubscriber<T>): IUnsubscribable;
36
+ }
37
+
38
+ export { IDisposable, IEquals, ISubscribable, ISubscriber, IUnsubscribable, Observable };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@guanghechen/observable",
3
+ "version": "5.0.0-alpha.2",
4
+ "description": "A simple observable for subscript/notification pattern.",
5
+ "author": {
6
+ "name": "guanghechen",
7
+ "url": "https://github.com/guanghechen/"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/guanghechen/node-scaffolds/tree/release-5.x.x",
12
+ "directory": "packages/observable"
13
+ },
14
+ "homepage": "https://github.com/guanghechen/node-scaffolds/tree/release-5.x.x/packages/observable#readme",
15
+ "keywords": [
16
+ "observable"
17
+ ],
18
+ "type": "module",
19
+ "main": "./lib/cjs/index.cjs",
20
+ "module": "./lib/esm/index.mjs",
21
+ "exports": {
22
+ "import": "./lib/esm/index.mjs",
23
+ "require": "./lib/cjs/index.cjs"
24
+ },
25
+ "types": "lib/types/index.d.ts",
26
+ "source": "src/index.ts",
27
+ "license": "MIT",
28
+ "engines": {
29
+ "node": ">= 16.0.0"
30
+ },
31
+ "files": [
32
+ "lib/",
33
+ "!lib/**/*.map",
34
+ "package.json",
35
+ "CHANGELOG.md",
36
+ "LICENSE",
37
+ "README.md"
38
+ ],
39
+ "scripts": {
40
+ "build": "rimraf lib/ && cross-env NODE_ENV=production rollup -c ../../rollup.config.mjs",
41
+ "prepublishOnly": "yarn build",
42
+ "test": "cross-env TS_NODE_FILES=true NODE_OPTIONS=--experimental-vm-modules jest --config ../../jest.config.mjs --rootDir ."
43
+ },
44
+ "devDependencies": {
45
+ "cross-env": "^7.0.3",
46
+ "jest": "^29.5.0",
47
+ "rimraf": "^5.0.0",
48
+ "rollup": "^3.21.1"
49
+ },
50
+ "gitHead": "708dd6d721059e902c375e628c939b28664f3058"
51
+ }