polfan-server-js-client 0.2.54 → 0.2.56

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.
@@ -1,9 +1,7 @@
1
1
  import { EventTarget, ObservableInterface } from "./EventTarget";
2
2
  export declare class IndexedCollection<KeyT, ValueT> {
3
3
  protected _items: Map<KeyT, ValueT>;
4
- protected _mutationCounter: number;
5
4
  constructor(items?: [key: KeyT, value: ValueT][]);
6
- get mutationCounter(): number;
7
5
  get items(): Map<KeyT, ValueT>;
8
6
  get length(): number;
9
7
  set(...items: [KeyT, ValueT][]): void;
@@ -12,6 +10,7 @@ export declare class IndexedCollection<KeyT, ValueT> {
12
10
  delete(...ids: KeyT[]): void;
13
11
  deleteAll(): void;
14
12
  findBy(field: keyof ValueT, valueToFind: any, limit?: number): IndexedCollection<KeyT, ValueT>;
13
+ shallowCopy(): IndexedCollection<KeyT, ValueT>;
15
14
  }
16
15
  export declare class IndexedObjectCollection<T> {
17
16
  readonly id: keyof T | ((item: T) => any);
@@ -19,7 +18,6 @@ export declare class IndexedObjectCollection<T> {
19
18
  constructor(id: keyof T | ((item: T) => any), items?: T[]);
20
19
  get items(): T[];
21
20
  get length(): number;
22
- get mutationCounter(): number;
23
21
  set(...items: T[]): void;
24
22
  get(id: any): T | undefined;
25
23
  getAt(index: number): T | undefined;
@@ -27,6 +25,7 @@ export declare class IndexedObjectCollection<T> {
27
25
  delete(...ids: any[]): void;
28
26
  deleteAll(): void;
29
27
  findBy(field: keyof T, valueToFind: any, limit?: number): IndexedObjectCollection<T>;
28
+ shallowCopy(): IndexedObjectCollection<T>;
30
29
  protected getId(item: T): any;
31
30
  }
32
31
  interface ObservableCollectionEvent<KeyT> {
@@ -39,6 +38,7 @@ export declare class ObservableIndexedCollection<KeyT, ValueT> extends IndexedCo
39
38
  set(...items: [KeyT, ValueT][]): void;
40
39
  delete(...ids: KeyT[]): void;
41
40
  deleteAll(): void;
41
+ shallowCopy(): ObservableIndexedCollection<KeyT, ValueT>;
42
42
  on(eventName: 'change', handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this;
43
43
  once(eventName: 'change', handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this;
44
44
  off(eventName: string, handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this;
@@ -50,6 +50,7 @@ export declare class ObservableIndexedObjectCollection<T> extends IndexedObjectC
50
50
  set(...items: T[]): void;
51
51
  delete(...ids: string[]): void;
52
52
  deleteAll(): void;
53
+ shallowCopy(): IndexedObjectCollection<T>;
53
54
  on(eventName: 'change', handler: (ev?: ObservableCollectionEvent<string>) => void): this;
54
55
  once(eventName: 'change', handler: (ev?: ObservableCollectionEvent<string>) => void): this;
55
56
  off(eventName: string, handler: (ev?: ObservableCollectionEvent<string>) => void): this;
@@ -1,6 +1,6 @@
1
1
  import { Message, Topic } from "../types/src";
2
2
  import { ChatStateTracker } from "./ChatStateTracker";
3
- import { ObservableIndexedObjectCollection } from "../IndexedObjectCollection";
3
+ import { IndexedObjectCollection, ObservableIndexedObjectCollection } from "../IndexedObjectCollection";
4
4
  export declare enum WindowState {
5
5
  /**
6
6
  * The latest messages (those received live) are available in the history window, history has not been fetched.
@@ -31,9 +31,10 @@ export declare abstract class TraversableRemoteCollection<T> extends ObservableI
31
31
  * Null for unlimited.
32
32
  */
33
33
  limit: number | null;
34
- private currentState;
35
- private fetchingState;
36
34
  oldestId: string;
35
+ protected currentState: WindowState;
36
+ protected fetchingState: WindowState;
37
+ abstract shallowCopy(): IndexedObjectCollection<T>;
37
38
  get hasLatest(): boolean;
38
39
  get hasOldest(): boolean;
39
40
  resetToLatest(): Promise<void>;
@@ -60,6 +61,7 @@ export declare class TopicHistoryWindow extends TraversableRemoteCollection<Mess
60
61
  readonly WindowState: typeof WindowState;
61
62
  private traverseLock;
62
63
  constructor(roomId: string, topicId: string, tracker: ChatStateTracker);
64
+ shallowCopy(): IndexedObjectCollection<Message>;
63
65
  get isTraverseLocked(): boolean;
64
66
  setTraverseLock(lock: boolean): Promise<void>;
65
67
  resetToLatest(): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polfan-server-js-client",
3
- "version": "0.2.54",
3
+ "version": "0.2.56",
4
4
  "description": "JavaScript client library for handling communication with Polfan chat server.",
5
5
  "author": "Jarosław Żak",
6
6
  "license": "MIT",
@@ -2,16 +2,11 @@ import {EventTarget, ObservableInterface} from "./EventTarget";
2
2
 
3
3
  export class IndexedCollection<KeyT, ValueT> {
4
4
  protected _items: Map<KeyT, ValueT> = new Map();
5
- protected _mutationCounter: number = 0;
6
5
 
7
6
  public constructor(items: [key: KeyT, value: ValueT][] = []) {
8
7
  this.set(...items);
9
8
  }
10
9
 
11
- public get mutationCounter(): number {
12
- return this._mutationCounter;
13
- }
14
-
15
10
  public get items(): Map<KeyT, ValueT> {
16
11
  return this._items;
17
12
  }
@@ -21,7 +16,6 @@ export class IndexedCollection<KeyT, ValueT> {
21
16
  }
22
17
 
23
18
  public set(...items: [KeyT, ValueT][]): void {
24
- this._mutationCounter++;
25
19
  for (const item of items) {
26
20
  this._items.set(item[0], item[1]);
27
21
  }
@@ -39,12 +33,10 @@ export class IndexedCollection<KeyT, ValueT> {
39
33
  for (const id of ids) {
40
34
  this.items.delete(id);
41
35
  }
42
- this._mutationCounter++;
43
36
  }
44
37
 
45
38
  public deleteAll(): void {
46
39
  this.items.clear();
47
- this._mutationCounter++;
48
40
  }
49
41
 
50
42
  public findBy(field: keyof ValueT, valueToFind: any, limit: number = null): IndexedCollection<KeyT, ValueT> {
@@ -60,6 +52,12 @@ export class IndexedCollection<KeyT, ValueT> {
60
52
  }
61
53
  return result;
62
54
  }
55
+
56
+ public shallowCopy(): IndexedCollection<KeyT, ValueT> {
57
+ const copy = new IndexedCollection<KeyT, ValueT>();
58
+ copy._items = this._items;
59
+ return copy;
60
+ }
63
61
  }
64
62
 
65
63
  export class IndexedObjectCollection<T> {
@@ -81,10 +79,6 @@ export class IndexedObjectCollection<T> {
81
79
  return this._items.length;
82
80
  }
83
81
 
84
- public get mutationCounter(): number {
85
- return this._items.mutationCounter;
86
- }
87
-
88
82
  public set(...items: T[]): void {
89
83
  this._items.set(...(items.map(item => [this.getId(item), item] as [string, T])));
90
84
  }
@@ -122,6 +116,12 @@ export class IndexedObjectCollection<T> {
122
116
  return result;
123
117
  }
124
118
 
119
+ public shallowCopy(): IndexedObjectCollection<T> {
120
+ const copy = new IndexedObjectCollection<T>(this.id);
121
+ copy._items = this._items;
122
+ return copy;
123
+ }
124
+
125
125
  protected getId(item: T): any {
126
126
  return typeof this.id === 'function' ? this.id(item) : item[this.id];
127
127
  }
@@ -163,6 +163,13 @@ export class ObservableIndexedCollection<KeyT, ValueT> extends IndexedCollection
163
163
  }
164
164
  }
165
165
 
166
+ public shallowCopy(): ObservableIndexedCollection<KeyT, ValueT> {
167
+ const copy = new ObservableIndexedCollection<KeyT, ValueT>();
168
+ copy.eventTarget = this.eventTarget;
169
+ copy._items = this._items;
170
+ return copy;
171
+ }
172
+
166
173
  public on(eventName: 'change', handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this {
167
174
  this.eventTarget.on(eventName, handler);
168
175
  return this;
@@ -213,6 +220,13 @@ export class ObservableIndexedObjectCollection<T> extends IndexedObjectCollectio
213
220
  }
214
221
  }
215
222
 
223
+ public shallowCopy(): IndexedObjectCollection<T> {
224
+ const copy = new ObservableIndexedObjectCollection<T>(this.id);
225
+ copy.eventTarget = this.eventTarget;
226
+ copy._items = this._items;
227
+ return copy;
228
+ }
229
+
216
230
  public on(eventName: 'change', handler: (ev?: ObservableCollectionEvent<string>) => void): this {
217
231
  this.eventTarget.on(eventName, handler);
218
232
  return this;
@@ -1,6 +1,6 @@
1
1
  import {Message, NewMessage, Session, Topic} from "../types/src";
2
2
  import {ChatStateTracker} from "./ChatStateTracker";
3
- import {ObservableIndexedObjectCollection} from "../IndexedObjectCollection";
3
+ import {IndexedObjectCollection, ObservableIndexedObjectCollection} from "../IndexedObjectCollection";
4
4
 
5
5
  export enum WindowState {
6
6
  /**
@@ -40,10 +40,13 @@ export abstract class TraversableRemoteCollection<T> extends ObservableIndexedOb
40
40
  */
41
41
  public limit: number | null = 50;
42
42
 
43
- private currentState: WindowState = WindowState.LIVE;
44
- private fetchingState: WindowState = undefined;
45
43
  public oldestId: string = null;
46
44
 
45
+ protected currentState: WindowState = WindowState.LIVE;
46
+ protected fetchingState: WindowState = undefined;
47
+
48
+ public abstract shallowCopy(): IndexedObjectCollection<T>;
49
+
47
50
  public get hasLatest(): boolean {
48
51
  return [WindowState.LATEST, WindowState.LIVE].includes(this.state);
49
52
  }
@@ -199,6 +202,16 @@ export class TopicHistoryWindow extends TraversableRemoteCollection<Message> {
199
202
  this.tracker.client.on('NewMessage', ev => this.handleNewMessage(ev));
200
203
  }
201
204
 
205
+ public shallowCopy(): IndexedObjectCollection<Message> {
206
+ const copy = new TopicHistoryWindow(this.roomId, this.topicId, this.tracker);
207
+ copy._items = this._items;
208
+ copy.limit = this.limit;
209
+ copy.currentState = this.currentState;
210
+ copy.fetchingState = this.fetchingState;
211
+ copy.oldestId = this.oldestId;
212
+ return copy;
213
+ }
214
+
202
215
  public get isTraverseLocked(): boolean {
203
216
  return this.traverseLock;
204
217
  }
@@ -1,4 +1,5 @@
1
- import {TraversableRemoteCollection, WindowState} from "../src/state-tracker/TopicHistoryWindow";
1
+ import { IndexedObjectCollection } from "../src";
2
+ import {TopicHistoryWindow, TraversableRemoteCollection, WindowState} from "../src/state-tracker/TopicHistoryWindow";
2
3
 
3
4
  interface SimpleMessage {
4
5
  id: number;
@@ -18,6 +19,10 @@ const messages: SimpleMessage[] = [
18
19
  ];
19
20
 
20
21
  class TestableHistoryWindow extends TraversableRemoteCollection<SimpleMessage> {
22
+ public shallowCopy(): IndexedObjectCollection<SimpleMessage> {
23
+ throw new Error('Method not implemented.');
24
+ }
25
+
21
26
  public constructor() {
22
27
  super('id');
23
28
  }