@reactive-cache/map 1.0.0 → 1.4.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.
@@ -0,0 +1,43 @@
1
+ import { Observable } from "rxjs"
2
+
3
+ export type ArgType<T> = Observable<T> | Promise<T> | T
4
+ export type __ReactiveMapType__ = ReactiveMap<any, any> | WeakReactiveMap<any, any> | ConstantReactiveMap<any, any> | ConstantWeakReactiveMap<any, any>
5
+
6
+ export class ReactiveMap<K extends string | number | symbol, V> {
7
+ public readonly name: string
8
+ public constructor(name: string, machine: (value: K) => ArgType<V>, params?: { constant?: boolean })
9
+ public get(key: K): Observable<V>
10
+ public next(key: K, value: V): void
11
+ public reset(key: K): void
12
+ public delete(key: K): void
13
+ public update(key: K): Observable<V>
14
+ public has(key: K): boolean
15
+ public clear(): void
16
+ }
17
+
18
+ export class WeakReactiveMap<K extends object, V> {
19
+ public readonly name: string
20
+ public constructor(name: string, machine: (value: K) => ArgType<V>, params?: { constant?: boolean })
21
+ public get(key: K): Observable<V>
22
+ public next(key: K, value: V): void
23
+ public reset(key: K): void
24
+ public delete(key: K): void
25
+ public update(key: K): Observable<V>
26
+ public has(key: K): boolean
27
+ }
28
+
29
+ export class ConstantReactiveMap<K extends string | number | symbol, V> {
30
+ public readonly name: string
31
+ public constructor(name: string, machine: (value: K) => ArgType<V>)
32
+ public get(key: K): Observable<V>
33
+ public delete(key: K): void
34
+ public clear(): void
35
+ }
36
+
37
+ export class ConstantWeakReactiveMap<K extends object, V> {
38
+ public readonly name: string
39
+ public constructor(name: string, machine: (value: K) => ArgType<V>)
40
+ public get(key: K): Observable<V>
41
+ public delete(key: K): void
42
+ public clear(): void
43
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactive-cache/map",
3
- "version": "1.0.0",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "npx tsc && tsx watch src/test.ts && echo 'all tests are complete!'",
package/.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
@@ -1,7 +0,0 @@
1
- import { BehaviorSubject } from "rxjs";
2
- export class NamedBehaviorSubject extends BehaviorSubject {
3
- constructor(initialValue, name) {
4
- super(initialValue);
5
- this.name = name;
6
- }
7
- }
package/src/index.ts DELETED
@@ -1,203 +0,0 @@
1
- import {
2
- __createReactiveCache__,
3
- ConstantReactiveCacheObservable,
4
- ReactiveCacheObservable,
5
- __REACTIVE_CACHE_WINDOW_PROP_NAME__,
6
- ReactiveCacheObservableParameters
7
- } from "@reactive-cache/core"
8
- import {Observable, Subject} from "rxjs";
9
-
10
- export type ArgType<T> = Observable<T> | Promise<T> | T
11
- export type __ReactiveMapType__ = ReactiveMap<any, any> | WeakReactiveMap<any, any> | ConstantReactiveMap<any, any> | ConstantWeakReactiveMap<any, any>
12
-
13
- const __REACTIVE_MAPS_LIST__: __ReactiveMapType__[] = []
14
- const __REACTIVE_MAPS_ON_UPDATE_MAP__: WeakMap<__ReactiveMapType__, Observable<any>> = new WeakMap()
15
- const __REACTIVE_MAPS_LIST_UPDATE_OBSERVABLE__: Subject<void> = new Subject<void>()
16
-
17
- try {
18
- const WINDOW = this || global || window
19
- if(WINDOW && typeof WINDOW === 'object') {
20
- // @ts-ignore
21
- window.addEventListener('load', () => {
22
- // @ts-ignore
23
- if(!WINDOW.__REACTIVE_CACHE_WINDOW_PROP_NAME__) {
24
- // @ts-ignore
25
- WINDOW.__REACTIVE_CACHE_WINDOW_PROP_NAME__ = {}
26
- }
27
- // @ts-ignore
28
- WINDOW.__REACTIVE_CACHE_WINDOW_PROP_NAME__['__REACTIVE_MAPS_LIST__'] = __REACTIVE_MAPS_LIST__
29
- // @ts-ignore
30
- WINDOW.__REACTIVE_CACHE_WINDOW_PROP_NAME__['__REACTIVE_MAPS_LIST_UPDATE_OBSERVABLE__'] = __REACTIVE_MAPS_LIST_UPDATE_OBSERVABLE__
31
- // @ts-ignore
32
- WINDOW.__REACTIVE_CACHE_WINDOW_PROP_NAME__['__REACTIVE_MAPS_ON_UPDATE_MAP__'] = __REACTIVE_MAPS_ON_UPDATE_MAP__
33
- })
34
- }
35
- } catch(_ignored) {}
36
-
37
- export class ReactiveMap<K extends string | number | symbol, V> {
38
- private readonly map = new Map<K, ReactiveCacheObservable<V>>()
39
- private readonly onUpdateSubject = new Subject<V | Symbol>()
40
- public constructor(public readonly name: string, private readonly machine: (value: K) => ArgType<V>, params?: { constant?: boolean }) {
41
- __REACTIVE_MAPS_LIST__.push(this)
42
- __REACTIVE_MAPS_LIST_UPDATE_OBSERVABLE__.next()
43
- __REACTIVE_MAPS_ON_UPDATE_MAP__.set(this, this.onUpdateSubject)
44
- }
45
-
46
- public get(key: K): Observable<V> {
47
- const value = this.map.get(key)
48
- if (value) {
49
- return value
50
- }
51
- const { rc } = __createReactiveCache__(this.machine(key), {}, (v) => {
52
- this.onUpdateSubject.next(v)
53
- })
54
- this.map.set(key, rc as ReactiveCacheObservable<V>)
55
- return rc
56
- }
57
-
58
- public next(key: K, value: V): void {
59
- const subject = this.map.get(key)
60
- if (subject) {
61
- subject.next(value)
62
- }
63
- }
64
-
65
- public reset(key: K): void {
66
- const subject = this.map.get(key)
67
- if (subject) {
68
- subject.resetState()
69
- }
70
- }
71
-
72
- public delete(key: K): void {
73
- this.map.delete(key)
74
- }
75
-
76
- public update(key: K): Observable<V> {
77
- const subject = this.map.get(key)
78
- if (subject) {
79
- return subject.update()
80
- }
81
- return this.get(key)
82
- }
83
-
84
- public has(key: K): boolean {
85
- return this.map.has(key)
86
- }
87
-
88
- public clear(): void {
89
- this.map.clear()
90
- }
91
- }
92
-
93
- export class WeakReactiveMap<K extends object, V> {
94
- private readonly map = new WeakMap<K, ReactiveCacheObservable<V>>()
95
- private readonly onUpdateSubject = new Subject<V | Symbol>()
96
- public constructor(public readonly name: string, private readonly machine: (value: K) => ArgType<V>, params?: { constant?: boolean }) {
97
- __REACTIVE_MAPS_LIST__.push(this)
98
- __REACTIVE_MAPS_LIST_UPDATE_OBSERVABLE__.next()
99
- __REACTIVE_MAPS_ON_UPDATE_MAP__.set(this, this.onUpdateSubject)
100
- }
101
-
102
- public get(key: K): Observable<V> {
103
- const value = this.map.get(key)
104
- if (value) {
105
- return value
106
- }
107
- const { rc } = __createReactiveCache__(this.machine(key), {}, (v) => {
108
- this.onUpdateSubject.next(v)
109
- })
110
- this.map.set(key, rc as ReactiveCacheObservable<V>)
111
- return rc
112
- }
113
-
114
- public next(key: K, value: V): void {
115
- const subject = this.map.get(key)
116
- if (subject) {
117
- subject.next(value)
118
- }
119
- }
120
-
121
- public reset(key: K): void {
122
- const subject = this.map.get(key)
123
- if (subject) {
124
- subject.resetState()
125
- }
126
- }
127
-
128
- public delete(key: K): void {
129
- this.map.delete(key)
130
- }
131
-
132
- public update(key: K): Observable<V> {
133
- const subject = this.map.get(key)
134
- if (subject) {
135
- return subject.update()
136
- }
137
- return this.get(key)
138
- }
139
-
140
- public has(key: K): boolean {
141
- return this.map.has(key)
142
- }
143
- }
144
-
145
- export class ConstantReactiveMap<K extends string | number | symbol, V> {
146
- private readonly map = new Map<K, ConstantReactiveCacheObservable<V>>()
147
- private readonly onUpdateSubject = new Subject<V | Symbol>()
148
- public constructor(public readonly name: string, private readonly machine: (value: K) => ArgType<V>) {
149
- __REACTIVE_MAPS_LIST__.push(this)
150
- __REACTIVE_MAPS_LIST_UPDATE_OBSERVABLE__.next()
151
- __REACTIVE_MAPS_ON_UPDATE_MAP__.set(this, this.onUpdateSubject)
152
- }
153
-
154
- public get(key: K): Observable<V> {
155
- const value = this.map.get(key)
156
- if (value) {
157
- return value
158
- }
159
- const { rc } = __createReactiveCache__(this.machine(key), { constant: true }, (v) => {
160
- this.onUpdateSubject.next(v)
161
- })
162
- this.map.set(key, rc as ConstantReactiveCacheObservable<V>)
163
- return rc
164
- }
165
-
166
- public delete(key: K): void {
167
- this.map.delete(key)
168
- }
169
-
170
- public clear(): void {
171
- this.map.clear()
172
- }
173
- }
174
-
175
- export class ConstantWeakReactiveMap<K extends object, V> {
176
- private readonly map = new Map<K, ConstantReactiveCacheObservable<V>>()
177
- private readonly onUpdateSubject = new Subject<V | Symbol>()
178
- public constructor(public readonly name: string, private readonly machine: (value: K) => ArgType<V>) {
179
- __REACTIVE_MAPS_LIST__.push(this)
180
- __REACTIVE_MAPS_LIST_UPDATE_OBSERVABLE__.next()
181
- __REACTIVE_MAPS_ON_UPDATE_MAP__.set(this, this.onUpdateSubject)
182
- }
183
-
184
- public get(key: K): Observable<V> {
185
- const value = this.map.get(key)
186
- if (value) {
187
- return value
188
- }
189
- const { rc } = __createReactiveCache__(this.machine(key), { constant: true }, (v) => {
190
- this.onUpdateSubject.next(v)
191
- })
192
- this.map.set(key, rc as ConstantReactiveCacheObservable<V>)
193
- return rc
194
- }
195
-
196
- public delete(key: K): void {
197
- this.map.delete(key)
198
- }
199
-
200
- public clear(): void {
201
- this.map.clear()
202
- }
203
- }
@@ -1,7 +0,0 @@
1
- import { BehaviorSubject } from "rxjs";
2
-
3
- export class NamedBehaviorSubject<T> extends BehaviorSubject<T> {
4
- public constructor(initialValue: T, public readonly name: string) {
5
- super(initialValue);
6
- }
7
- }
package/src/test.ts DELETED
@@ -1,22 +0,0 @@
1
- import {ReactiveMap} from "./index.js";
2
- import {Observable} from "rxjs";
3
-
4
- const map = new ReactiveMap<string, string>('map', (key) => 'Hello ' + key);
5
-
6
- map.get('world').subscribe(console.log);
7
- map.next('world', 'Hello world!');
8
- map.get('world').subscribe(console.log);
9
- console.log(map.has('world'));
10
-
11
- const map2 = new ReactiveMap<string, string>('testmap', (key) => new Observable<string>(sub => {
12
- console.log('fetch called')
13
- setTimeout(() => {
14
- sub.next('Hello ' + key + '_' + Math.random());
15
- sub.complete();
16
- }, 1000)
17
- }));
18
-
19
- map2.get('world').subscribe(console.log);
20
- console.log(map2.has('world'));
21
- console.log(map2.has('world2'));
22
- map2.update('world').subscribe(console.log);