evstream 1.0.1 → 1.0.3
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/.husky/pre-commit +1 -0
- package/.me/dump.ts +102 -0
- package/.prettierignore +15 -0
- package/.prettierrc +8 -0
- package/dist/adapters/pub-sub.d.ts +14 -0
- package/dist/adapters/pub-sub.js +66 -0
- package/dist/adapters/redis.d.ts +3 -1
- package/dist/adapters/redis.js +13 -5
- package/dist/extensions/state-manager.d.ts +21 -0
- package/dist/extensions/state-manager.js +88 -0
- package/dist/state.d.ts +1 -1
- package/dist/state.js +1 -1
- package/dist/utils.js +5 -5
- package/package.json +77 -60
- package/readme.md +844 -674
- package/src/adapters/pub-sub.ts +88 -0
- package/src/adapters/redis.ts +120 -0
- package/src/errors.ts +25 -0
- package/src/extensions/state-manager.ts +186 -0
- package/src/index.ts +28 -0
- package/src/manager.ts +171 -0
- package/src/message.ts +19 -0
- package/src/state.ts +84 -0
- package/src/stream.ts +122 -0
- package/src/types.ts +56 -0
- package/src/utils.ts +29 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npm run lint-staged
|
package/.me/dump.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { EvState, EvStreamManager } from 'evstream'
|
|
2
|
+
import { EvRedisAdapter } from 'evstream/adapter/redis'
|
|
3
|
+
import type Redis from 'ioredis'
|
|
4
|
+
|
|
5
|
+
type SharedStateMessage =
|
|
6
|
+
| { action: 'create'; key: string; initialValue: any }
|
|
7
|
+
| { action: 'remove'; key: string }
|
|
8
|
+
|
|
9
|
+
export class EvCustomSharedState {
|
|
10
|
+
private states = new Map<string, EvState>()
|
|
11
|
+
private redis: Redis
|
|
12
|
+
private pub: Redis
|
|
13
|
+
private manager: EvStreamManager
|
|
14
|
+
private adapter: EvRedisAdapter
|
|
15
|
+
private controlChannel = 'ev:shared-state'
|
|
16
|
+
|
|
17
|
+
constructor(options: {
|
|
18
|
+
redis: Redis
|
|
19
|
+
publisher?: Redis
|
|
20
|
+
manager: EvStreamManager
|
|
21
|
+
adapter: EvRedisAdapter
|
|
22
|
+
}) {
|
|
23
|
+
this.redis = options.redis
|
|
24
|
+
this.pub = options.publisher ?? options.redis
|
|
25
|
+
this.manager = options.manager
|
|
26
|
+
this.adapter = options.adapter
|
|
27
|
+
|
|
28
|
+
this.init()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private async init() {
|
|
32
|
+
await this.redis.subscribe(this.controlChannel)
|
|
33
|
+
|
|
34
|
+
this.redis.on('message', (_, raw) => {
|
|
35
|
+
try {
|
|
36
|
+
const msg = JSON.parse(raw) as SharedStateMessage
|
|
37
|
+
this.handleMessage(msg)
|
|
38
|
+
} catch {
|
|
39
|
+
/* ignore malformed messages */
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private handleMessage(msg: SharedStateMessage) {
|
|
45
|
+
if (msg.action === 'create') {
|
|
46
|
+
this.createLocalState(msg.key, msg.initialValue)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (msg.action === 'remove') {
|
|
50
|
+
this.removeLocalState(msg.key)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private createLocalState(key: string, initialValue: any) {
|
|
55
|
+
if (this.states.has(key)) return
|
|
56
|
+
|
|
57
|
+
const state = new EvState({
|
|
58
|
+
channel: key,
|
|
59
|
+
initialValue,
|
|
60
|
+
manager: this.manager,
|
|
61
|
+
adapter: this.adapter,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
this.states.set(key, state)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private removeLocalState(key: string) {
|
|
68
|
+
const state = this.states.get(key)
|
|
69
|
+
if (!state) return
|
|
70
|
+
|
|
71
|
+
// EvState has no destroy API, so we simply stop tracking it
|
|
72
|
+
this.states.delete(key)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* ================= PUBLIC API ================= */
|
|
76
|
+
|
|
77
|
+
async create(key: string, initialValue: any) {
|
|
78
|
+
await this.pub.publish(
|
|
79
|
+
this.controlChannel,
|
|
80
|
+
JSON.stringify({ action: 'create', key, initialValue })
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async remove(key: string) {
|
|
85
|
+
await this.pub.publish(
|
|
86
|
+
this.controlChannel,
|
|
87
|
+
JSON.stringify({ action: 'remove', key })
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get(key: string): EvState | undefined {
|
|
92
|
+
return this.states.get(key)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
has(key: string): boolean {
|
|
96
|
+
return this.states.has(key)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
keys(): string[] {
|
|
100
|
+
return [...this.states.keys()]
|
|
101
|
+
}
|
|
102
|
+
}
|
package/.prettierignore
ADDED
package/.prettierrc
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RedisOptions } from 'ioredis';
|
|
2
|
+
interface EvRedisPubSubOptions {
|
|
3
|
+
subject?: string;
|
|
4
|
+
options: RedisOptions;
|
|
5
|
+
onMessage?: (message: Object) => void;
|
|
6
|
+
}
|
|
7
|
+
export declare class EvRedisPubSub {
|
|
8
|
+
#private;
|
|
9
|
+
constructor({ options, subject, onMessage }: EvRedisPubSubOptions);
|
|
10
|
+
private init;
|
|
11
|
+
send(msg: Object): Promise<void>;
|
|
12
|
+
onMessage(callback: (msg: Object) => void): void;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
11
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
12
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
13
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
14
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
15
|
+
};
|
|
16
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
17
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
18
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
19
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
|
+
};
|
|
21
|
+
var _EvRedisPubSub_options, _EvRedisPubSub_subject, _EvRedisPubSub_pub, _EvRedisPubSub_sub, _EvRedisPubSub_instanceId, _EvRedisPubSub_onMessage;
|
|
22
|
+
import Redis from 'ioredis';
|
|
23
|
+
import { uid } from '../utils.js';
|
|
24
|
+
export class EvRedisPubSub {
|
|
25
|
+
constructor({ options, subject, onMessage }) {
|
|
26
|
+
_EvRedisPubSub_options.set(this, void 0);
|
|
27
|
+
_EvRedisPubSub_subject.set(this, void 0);
|
|
28
|
+
_EvRedisPubSub_pub.set(this, void 0);
|
|
29
|
+
_EvRedisPubSub_sub.set(this, void 0);
|
|
30
|
+
_EvRedisPubSub_instanceId.set(this, void 0);
|
|
31
|
+
_EvRedisPubSub_onMessage.set(this, void 0);
|
|
32
|
+
__classPrivateFieldSet(this, _EvRedisPubSub_pub, new Redis(options), "f");
|
|
33
|
+
__classPrivateFieldSet(this, _EvRedisPubSub_sub, new Redis(options), "f");
|
|
34
|
+
__classPrivateFieldSet(this, _EvRedisPubSub_onMessage, onMessage, "f");
|
|
35
|
+
__classPrivateFieldSet(this, _EvRedisPubSub_instanceId, uid({ prefix: subject, counter: Math.random() }), "f");
|
|
36
|
+
__classPrivateFieldSet(this, _EvRedisPubSub_subject, subject, "f");
|
|
37
|
+
this.init();
|
|
38
|
+
}
|
|
39
|
+
init() {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
__classPrivateFieldGet(this, _EvRedisPubSub_pub, "f").on('error', (err) => { });
|
|
42
|
+
__classPrivateFieldGet(this, _EvRedisPubSub_sub, "f").on('error', (err) => { });
|
|
43
|
+
yield __classPrivateFieldGet(this, _EvRedisPubSub_sub, "f").subscribe(__classPrivateFieldGet(this, _EvRedisPubSub_subject, "f"));
|
|
44
|
+
__classPrivateFieldGet(this, _EvRedisPubSub_sub, "f").on('message', (_, raw) => {
|
|
45
|
+
try {
|
|
46
|
+
const msg = JSON.parse(raw);
|
|
47
|
+
console.log("Incoming Message");
|
|
48
|
+
console.log(msg);
|
|
49
|
+
if ((msg === null || msg === void 0 ? void 0 : msg.uid) !== __classPrivateFieldGet(this, _EvRedisPubSub_instanceId, "f")) {
|
|
50
|
+
__classPrivateFieldGet(this, _EvRedisPubSub_onMessage, "f").call(this, msg === null || msg === void 0 ? void 0 : msg.msg);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (_a) { }
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
send(msg) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
yield __classPrivateFieldGet(this, _EvRedisPubSub_pub, "f").publish(__classPrivateFieldGet(this, _EvRedisPubSub_subject, "f"), JSON.stringify({ uid: __classPrivateFieldGet(this, _EvRedisPubSub_instanceId, "f"), msg }));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
onMessage(callback) {
|
|
63
|
+
__classPrivateFieldSet(this, _EvRedisPubSub_onMessage, callback, "f");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
_EvRedisPubSub_options = new WeakMap(), _EvRedisPubSub_subject = new WeakMap(), _EvRedisPubSub_pub = new WeakMap(), _EvRedisPubSub_sub = new WeakMap(), _EvRedisPubSub_instanceId = new WeakMap(), _EvRedisPubSub_onMessage = new WeakMap();
|
package/dist/adapters/redis.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RedisOptions } from 'ioredis';
|
|
2
|
+
import { EvRedisPubSub } from './pub-sub.js';
|
|
2
3
|
import { EvStateAdapter } from '../types.js';
|
|
3
|
-
|
|
4
|
+
declare class EvRedisAdapter implements EvStateAdapter {
|
|
4
5
|
#private;
|
|
5
6
|
constructor(options?: RedisOptions);
|
|
6
7
|
publish(channel: string, message: any): Promise<void>;
|
|
@@ -8,3 +9,4 @@ export declare class EvRedisAdapter implements EvStateAdapter {
|
|
|
8
9
|
unsubscribe(channel: string): Promise<void>;
|
|
9
10
|
quit(): void;
|
|
10
11
|
}
|
|
12
|
+
export { EvRedisPubSub, EvRedisAdapter };
|
package/dist/adapters/redis.js
CHANGED
|
@@ -18,33 +18,40 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
18
18
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
19
19
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
20
|
};
|
|
21
|
-
var _EvRedisAdapter_pub, _EvRedisAdapter_sub, _EvRedisAdapter_listeners;
|
|
21
|
+
var _EvRedisAdapter_pub, _EvRedisAdapter_sub, _EvRedisAdapter_listeners, _EvRedisAdapter_instanceId;
|
|
22
22
|
import Redis from 'ioredis';
|
|
23
|
-
|
|
23
|
+
import { EvRedisPubSub } from './pub-sub.js';
|
|
24
|
+
import { uid } from '../utils.js';
|
|
25
|
+
class EvRedisAdapter {
|
|
24
26
|
constructor(options) {
|
|
25
27
|
_EvRedisAdapter_pub.set(this, void 0);
|
|
26
28
|
_EvRedisAdapter_sub.set(this, void 0);
|
|
27
29
|
_EvRedisAdapter_listeners.set(this, void 0);
|
|
30
|
+
_EvRedisAdapter_instanceId.set(this, void 0);
|
|
28
31
|
__classPrivateFieldSet(this, _EvRedisAdapter_pub, new Redis(options), "f");
|
|
29
32
|
__classPrivateFieldSet(this, _EvRedisAdapter_sub, new Redis(options), "f");
|
|
30
33
|
__classPrivateFieldSet(this, _EvRedisAdapter_listeners, new Map(), "f");
|
|
34
|
+
__classPrivateFieldSet(this, _EvRedisAdapter_instanceId, uid({ counter: Math.ceil(Math.random() * 100) }), "f");
|
|
31
35
|
__classPrivateFieldGet(this, _EvRedisAdapter_sub, "f").on('message', (channel, message) => {
|
|
32
36
|
const handlers = __classPrivateFieldGet(this, _EvRedisAdapter_listeners, "f").get(channel);
|
|
33
37
|
if (handlers) {
|
|
34
38
|
let parsed;
|
|
39
|
+
let canHandle;
|
|
35
40
|
try {
|
|
36
41
|
parsed = JSON.parse(message);
|
|
42
|
+
canHandle = (parsed === null || parsed === void 0 ? void 0 : parsed.id) !== __classPrivateFieldGet(this, _EvRedisAdapter_instanceId, "f");
|
|
37
43
|
}
|
|
38
44
|
catch (_a) {
|
|
39
45
|
return;
|
|
40
46
|
}
|
|
41
|
-
|
|
47
|
+
if (canHandle)
|
|
48
|
+
handlers.forEach((handler) => handler(parsed === null || parsed === void 0 ? void 0 : parsed.message));
|
|
42
49
|
}
|
|
43
50
|
});
|
|
44
51
|
}
|
|
45
52
|
publish(channel, message) {
|
|
46
53
|
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
-
yield __classPrivateFieldGet(this, _EvRedisAdapter_pub, "f").publish(channel, JSON.stringify(message));
|
|
54
|
+
yield __classPrivateFieldGet(this, _EvRedisAdapter_pub, "f").publish(channel, JSON.stringify({ id: __classPrivateFieldGet(this, _EvRedisAdapter_instanceId, "f"), message: message }));
|
|
48
55
|
});
|
|
49
56
|
}
|
|
50
57
|
subscribe(channel, onMessage) {
|
|
@@ -67,4 +74,5 @@ export class EvRedisAdapter {
|
|
|
67
74
|
__classPrivateFieldGet(this, _EvRedisAdapter_sub, "f").quit();
|
|
68
75
|
}
|
|
69
76
|
}
|
|
70
|
-
_EvRedisAdapter_pub = new WeakMap(), _EvRedisAdapter_sub = new WeakMap(), _EvRedisAdapter_listeners = new WeakMap();
|
|
77
|
+
_EvRedisAdapter_pub = new WeakMap(), _EvRedisAdapter_sub = new WeakMap(), _EvRedisAdapter_listeners = new WeakMap(), _EvRedisAdapter_instanceId = new WeakMap();
|
|
78
|
+
export { EvRedisPubSub, EvRedisAdapter };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { EvStreamManager } from '../manager.js';
|
|
2
|
+
import type { EvRedisAdapter } from '../adapters/redis.js';
|
|
3
|
+
import type { EvRedisPubSub } from '../adapters/pub-sub.js';
|
|
4
|
+
import { EvState } from '../state.js';
|
|
5
|
+
interface EvStateManagerOptions {
|
|
6
|
+
manager: EvStreamManager;
|
|
7
|
+
adapter?: EvRedisAdapter;
|
|
8
|
+
pubsub?: EvRedisPubSub;
|
|
9
|
+
}
|
|
10
|
+
export declare class EvStateManager<S extends Record<string, any>> {
|
|
11
|
+
#private;
|
|
12
|
+
constructor({ manager, adapter, pubsub }: EvStateManagerOptions);
|
|
13
|
+
private createLocalState;
|
|
14
|
+
private removeLocalState;
|
|
15
|
+
createState<K extends keyof S>(key: K, initialValue: S[K]): EvState<S[K]>;
|
|
16
|
+
getState<K extends keyof S>(key: K): EvState<S[K]> | undefined;
|
|
17
|
+
hasState<K extends keyof S>(key: K): boolean;
|
|
18
|
+
removeState<K extends keyof S>(key: K): void;
|
|
19
|
+
private pubSubCallback;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _EvStateManager_states, _EvStateManager_manager, _EvStateManager_adapter, _EvStateManager_pubsub;
|
|
13
|
+
import { EvState } from '../state.js';
|
|
14
|
+
export class EvStateManager {
|
|
15
|
+
constructor({ manager, adapter, pubsub }) {
|
|
16
|
+
// 🔑 Internally everything is string-based (Redis-safe)
|
|
17
|
+
_EvStateManager_states.set(this, new Map());
|
|
18
|
+
_EvStateManager_manager.set(this, void 0);
|
|
19
|
+
_EvStateManager_adapter.set(this, void 0);
|
|
20
|
+
_EvStateManager_pubsub.set(this, void 0);
|
|
21
|
+
__classPrivateFieldSet(this, _EvStateManager_manager, manager, "f");
|
|
22
|
+
__classPrivateFieldSet(this, _EvStateManager_adapter, adapter, "f");
|
|
23
|
+
__classPrivateFieldSet(this, _EvStateManager_pubsub, pubsub, "f");
|
|
24
|
+
this.pubSubCallback = this.pubSubCallback.bind(this);
|
|
25
|
+
if (__classPrivateFieldGet(this, _EvStateManager_pubsub, "f")) {
|
|
26
|
+
__classPrivateFieldGet(this, _EvStateManager_pubsub, "f").onMessage(this.pubSubCallback);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
createLocalState(channel, initialValue) {
|
|
30
|
+
const state = new EvState({
|
|
31
|
+
channel,
|
|
32
|
+
initialValue,
|
|
33
|
+
manager: __classPrivateFieldGet(this, _EvStateManager_manager, "f"),
|
|
34
|
+
adapter: __classPrivateFieldGet(this, _EvStateManager_adapter, "f"),
|
|
35
|
+
});
|
|
36
|
+
__classPrivateFieldGet(this, _EvStateManager_states, "f").set(channel, state);
|
|
37
|
+
return state;
|
|
38
|
+
}
|
|
39
|
+
removeLocalState(channel) {
|
|
40
|
+
__classPrivateFieldGet(this, _EvStateManager_states, "f").delete(channel);
|
|
41
|
+
}
|
|
42
|
+
createState(key, initialValue) {
|
|
43
|
+
var _a;
|
|
44
|
+
const channel = String(key);
|
|
45
|
+
if (__classPrivateFieldGet(this, _EvStateManager_states, "f").has(channel)) {
|
|
46
|
+
return __classPrivateFieldGet(this, _EvStateManager_states, "f").get(channel);
|
|
47
|
+
}
|
|
48
|
+
const state = this.createLocalState(channel, initialValue);
|
|
49
|
+
(_a = __classPrivateFieldGet(this, _EvStateManager_pubsub, "f")) === null || _a === void 0 ? void 0 : _a.send({
|
|
50
|
+
type: 'create',
|
|
51
|
+
channel,
|
|
52
|
+
initialValue,
|
|
53
|
+
});
|
|
54
|
+
return state;
|
|
55
|
+
}
|
|
56
|
+
getState(key) {
|
|
57
|
+
return __classPrivateFieldGet(this, _EvStateManager_states, "f").get(String(key));
|
|
58
|
+
}
|
|
59
|
+
hasState(key) {
|
|
60
|
+
return __classPrivateFieldGet(this, _EvStateManager_states, "f").has(String(key));
|
|
61
|
+
}
|
|
62
|
+
removeState(key) {
|
|
63
|
+
var _a;
|
|
64
|
+
const channel = String(key);
|
|
65
|
+
this.removeLocalState(channel);
|
|
66
|
+
(_a = __classPrivateFieldGet(this, _EvStateManager_pubsub, "f")) === null || _a === void 0 ? void 0 : _a.send({
|
|
67
|
+
type: 'remove',
|
|
68
|
+
channel,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
pubSubCallback(msg) {
|
|
72
|
+
if (!msg || typeof msg.channel !== 'string')
|
|
73
|
+
return;
|
|
74
|
+
switch (msg.type) {
|
|
75
|
+
case 'create': {
|
|
76
|
+
if (!__classPrivateFieldGet(this, _EvStateManager_states, "f").has(msg.channel)) {
|
|
77
|
+
this.createLocalState(msg.channel, msg.initialValue);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case 'remove': {
|
|
82
|
+
this.removeLocalState(msg.channel);
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
_EvStateManager_states = new WeakMap(), _EvStateManager_manager = new WeakMap(), _EvStateManager_adapter = new WeakMap(), _EvStateManager_pubsub = new WeakMap();
|
package/dist/state.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ type EvSetState<T> = (val: T) => T;
|
|
|
5
5
|
*/
|
|
6
6
|
export declare class EvState<T> {
|
|
7
7
|
#private;
|
|
8
|
-
constructor({ channel, initialValue, manager, key, adapter }: EvStateOptions<T>);
|
|
8
|
+
constructor({ channel, initialValue, manager, key, adapter, }: EvStateOptions<T>);
|
|
9
9
|
/**
|
|
10
10
|
* Returns the current state value.
|
|
11
11
|
*/
|
package/dist/state.js
CHANGED
|
@@ -16,7 +16,7 @@ const { isEqual } = loadash;
|
|
|
16
16
|
* EvState holds a reactive state and broadcasts updates to a channel using EvStreamManager.
|
|
17
17
|
*/
|
|
18
18
|
export class EvState {
|
|
19
|
-
constructor({ channel, initialValue, manager, key, adapter }) {
|
|
19
|
+
constructor({ channel, initialValue, manager, key, adapter, }) {
|
|
20
20
|
_EvState_instances.add(this);
|
|
21
21
|
_EvState_value.set(this, void 0);
|
|
22
22
|
_EvState_channel.set(this, void 0);
|
package/dist/utils.js
CHANGED
|
@@ -6,21 +6,21 @@
|
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
8
|
export function safeJsonParse(val) {
|
|
9
|
-
if (typeof val ===
|
|
9
|
+
if (typeof val === 'string') {
|
|
10
10
|
return val;
|
|
11
11
|
}
|
|
12
|
-
if (typeof val ===
|
|
12
|
+
if (typeof val === 'object') {
|
|
13
13
|
try {
|
|
14
14
|
return JSON.stringify(val);
|
|
15
15
|
}
|
|
16
16
|
catch (error) {
|
|
17
|
-
return
|
|
17
|
+
return '';
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
-
return
|
|
20
|
+
return '';
|
|
21
21
|
}
|
|
22
22
|
export function uid(opts) {
|
|
23
23
|
const now = Date.now().toString(36);
|
|
24
24
|
const rand = Math.random().toString(26).substring(2, 10);
|
|
25
|
-
return `${(opts === null || opts === void 0 ? void 0 : opts.prefix) ? `${opts === null || opts === void 0 ? void 0 : opts.prefix}-` :
|
|
25
|
+
return `${(opts === null || opts === void 0 ? void 0 : opts.prefix) ? `${opts === null || opts === void 0 ? void 0 : opts.prefix}-` : ''}${now}-${rand}-${opts === null || opts === void 0 ? void 0 : opts.counter}`;
|
|
26
26
|
}
|
package/package.json
CHANGED
|
@@ -1,60 +1,77 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "evstream",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "A simple and easy to implement server sent event library for express.js",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"sse",
|
|
7
|
+
"server-sent-events",
|
|
8
|
+
"event-source"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/kisshan13/evstream#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/kisshan13/evstream/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/kisshan13/evstream.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Kishan Sharma",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts"
|
|
27
|
+
},
|
|
28
|
+
"./adapter/redis": {
|
|
29
|
+
"import": "./dist/adapters/redis.js",
|
|
30
|
+
"types": "./dist/adapters/redis.d.ts"
|
|
31
|
+
},
|
|
32
|
+
"./extensions/state-manager": {
|
|
33
|
+
"import": "./dist/extensions/state-manager.js",
|
|
34
|
+
"types": "./dist/extensions/state-manager.d.ts"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"lint-staged": {
|
|
38
|
+
"*.{js,jsx,ts,tsx,cjs,mjs}": [
|
|
39
|
+
"prettier --write"
|
|
40
|
+
],
|
|
41
|
+
"*.{json,md,yml,yaml}": [
|
|
42
|
+
"prettier --write"
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"dev": "tsc --watch",
|
|
47
|
+
"clean": "rimraf ./dist",
|
|
48
|
+
"build": "rimraf ./dist && tsc --incremental false",
|
|
49
|
+
"prepare": "husky",
|
|
50
|
+
"lint-staged": "lint-staged",
|
|
51
|
+
"format": "prettier --write \"**/*.{js,jsx,ts,tsx}\""
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=17.8.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/ioredis": "^5.0.0",
|
|
58
|
+
"@types/lodash": "^4.17.19",
|
|
59
|
+
"@types/node": "^24.0.7",
|
|
60
|
+
"@typescript-eslint/eslint-plugin": "^8.35.0",
|
|
61
|
+
"@typescript-eslint/parser": "^8.35.0",
|
|
62
|
+
"eslint": "^9.30.0",
|
|
63
|
+
"eslint-config-prettier": "^10.1.5",
|
|
64
|
+
"eslint-plugin-import": "^2.32.0",
|
|
65
|
+
"eslint-plugin-prettier": "^5.5.1",
|
|
66
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
67
|
+
"ioredis": "^5.5.0",
|
|
68
|
+
"prettier": "^3.7.4",
|
|
69
|
+
"rimraf": "^6.0.1",
|
|
70
|
+
"typescript": "^5.8.3",
|
|
71
|
+
"husky": "^9.1.7",
|
|
72
|
+
"lint-staged": "^16.2.7"
|
|
73
|
+
},
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"lodash": "^4.17.21"
|
|
76
|
+
}
|
|
77
|
+
}
|