@dra2020/baseclient 1.0.82 → 1.0.85

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/dist/csv/csv.d.ts CHANGED
@@ -1,4 +1,13 @@
1
1
  import * as Util from '../util/all';
2
+ export declare class ParseMany {
3
+ buf: Uint8Array;
4
+ n: number;
5
+ one: ParseOne;
6
+ constructor(coder: Util.Coder, buf: Uint8Array);
7
+ get length(): number;
8
+ get fields(): string[];
9
+ next(): boolean;
10
+ }
2
11
  export declare class ParseOne {
3
12
  coder: Util.Coder;
4
13
  fields: string[];
@@ -12,6 +21,7 @@ export declare class ParseOne {
12
21
  force: boolean;
13
22
  constructor(coder: Util.Coder, line?: string);
14
23
  set(line: string): void;
24
+ setBuf(buf: Uint8Array): void;
15
25
  get length(): number;
16
26
  pushtok(): void;
17
27
  parse(): void;
package/dist/fsm/fsm.d.ts CHANGED
@@ -3,6 +3,7 @@ export declare const FSM_STARTING: number;
3
3
  export declare const FSM_PENDING: number;
4
4
  export declare const FSM_DONE: number;
5
5
  export declare const FSM_ERROR: number;
6
+ export declare const FSM_CANCEL: number;
6
7
  export declare const FSM_RELEASED: number;
7
8
  export declare const FSM_CUSTOM1: number;
8
9
  export declare const FSM_CUSTOM2: number;
@@ -44,6 +45,7 @@ export declare class Fsm {
44
45
  get ready(): boolean;
45
46
  get iserror(): boolean;
46
47
  get isDependentError(): boolean;
48
+ cancel(): void;
47
49
  setDependentError(): void;
48
50
  clearDependentError(): void;
49
51
  get ticked(): boolean;
@@ -113,6 +113,7 @@ export interface SessionUpdateProps {
113
113
  accessUpdate?: AccessMap;
114
114
  restore?: string;
115
115
  revision?: Revision;
116
+ colors?: string;
116
117
  }
117
118
  export interface SessionsIndex {
118
119
  [key: string]: SessionProps;
package/docs/fsm.md CHANGED
@@ -191,14 +191,18 @@ An `Fsm` can be reused and transition from `ready` to not `ready` or `done` to n
191
191
 
192
192
  ### Cancellation
193
193
 
194
- There is no explicit cancellation mechanism.
194
+ The standard member function `cancel` will set the state to `FSM_CANCEL` which is considered a done state as
195
+ well as an error state (and will set the dependentError flag on any waiting state machines).
195
196
 
196
- By convention, a sub-class should override the `end` member function to allow external cancellation or completion.
197
- By default this function sets the `Fsm` to `FSM_DONE` but can optionally take the `FSM_ERROR` state
198
- to indicate error.
197
+ By convention, a sub-class should override the `cancel` member function to allow external cancellation
198
+ if it needs to do more internal cleanup than set the state to `FSM_CANCEL`.
199
199
 
200
- Whether calling `end` or setting the state explicitly to `FSM_DONE` or `FSM_ERROR` from external to
201
- the state machine makes semantic sense depends on the specific properties and semantics of the computation.
200
+ Additionally, it can use the `end` member function (by convention) to complete the state machine if that
201
+ makes sense and does not wanted to be treated as an error.
202
+ The class should override the `end` member function if it needs to do cleanup and finalization internally.
203
+
204
+ If a class internally is implemented with callbacks or promises, by convention it should check `if this.done`
205
+ when the callback or promise completes to check whether it may have been externally canceled while awaiting.
202
206
 
203
207
  ### FsmOnDone
204
208
 
package/lib/csv/csv.ts CHANGED
@@ -23,6 +23,56 @@ function isWhite(c: number): boolean
23
23
  return c === Space || c === Newline || c === Tab || c == CR;
24
24
  }
25
25
 
26
+ // Keep calling next() to retrieve next parsed line. Returns false when done. Empty lines are ignored.
27
+
28
+ export class ParseMany
29
+ {
30
+ buf: Uint8Array;
31
+ n: number;
32
+ one: ParseOne;
33
+
34
+ constructor(coder: Util.Coder, buf: Uint8Array)
35
+ {
36
+ this.buf = buf;
37
+ this.n = 0;
38
+ this.one = new ParseOne(coder);
39
+ }
40
+
41
+ get length(): number { return this.one.length }
42
+ get fields(): string[] { return this.one.fields }
43
+
44
+ next(): boolean
45
+ {
46
+ // Move past any leading CRLF
47
+ while (this.n < this.buf.length)
48
+ {
49
+ let c = this.buf[this.n];
50
+ if (c == CR || c == Newline)
51
+ this.n++;
52
+ else
53
+ break;
54
+ }
55
+
56
+ let s = this.n;
57
+ while (this.n < this.buf.length)
58
+ {
59
+ let c = this.buf[this.n];
60
+ if (c == CR || c == Newline)
61
+ break;
62
+ else
63
+ this.n++;
64
+ }
65
+
66
+ if (s != this.n)
67
+ {
68
+ this.one.setBuf(this.buf.subarray(s, this.n));
69
+ return true;
70
+ }
71
+ else
72
+ return false;
73
+ }
74
+ }
75
+
26
76
  export class ParseOne
27
77
  {
28
78
  coder: Util.Coder;
@@ -47,9 +97,15 @@ export class ParseOne
47
97
 
48
98
  set(line: string): void
49
99
  {
100
+ this.setBuf(Util.s2u8(this.coder, line));
101
+ }
102
+
103
+ setBuf(buf: Uint8Array): void
104
+ {
105
+ this.buf = buf;
50
106
  this.fields = [];
51
- this.buf = Util.s2u8(this.coder, line);
52
- this.tok = new Uint8Array(new ArrayBuffer(this.buf.length));
107
+ if (!this.tok || this.tok.length < this.buf.length)
108
+ this.tok = new Uint8Array(new ArrayBuffer(this.buf.length));
53
109
  this.n = 0;
54
110
  this.toklen = 0;
55
111
  this.infield = false;
package/lib/fsm/fsm.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  // Shared libraries
2
2
  import * as Util from '../util/all';
3
3
 
4
- // States
4
+ // States (note these are no longer bit flags - most uses create custom values incrementally from CUSTOM1)
5
5
  export const FSM_STARTING: number = 0;
6
6
  export const FSM_PENDING: number = 1<<0;
7
7
  export const FSM_DONE: number = 1<<1;
8
8
  export const FSM_ERROR: number = 1<<2;
9
+ export const FSM_CANCEL: number = 5;
9
10
  export const FSM_RELEASED: number = 1<<3;
10
11
  export const FSM_CUSTOM1: number = 1<<4;
11
12
  export const FSM_CUSTOM2: number = 1<<5;
@@ -22,7 +23,7 @@ let doLater: any = global && global.setImmediate ? setImmediate : (cb: any) => {
22
23
 
23
24
  function FsmDone(s: number): boolean
24
25
  {
25
- return (s === FSM_DONE || s === FSM_ERROR || s === FSM_RELEASED);
26
+ return (s === FSM_DONE || s === FSM_ERROR || s === FSM_RELEASED || s === FSM_CANCEL);
26
27
  }
27
28
 
28
29
  function FsmStateToString(state: number): string
@@ -151,7 +152,7 @@ export class Fsm
151
152
 
152
153
  get iserror(): boolean
153
154
  {
154
- return (this.state === FSM_ERROR);
155
+ return (this.state === FSM_ERROR || this.state === FSM_CANCEL);
155
156
  }
156
157
 
157
158
  get isDependentError(): boolean
@@ -159,6 +160,12 @@ export class Fsm
159
160
  return this.dependentError;
160
161
  }
161
162
 
163
+ cancel(): void
164
+ {
165
+ // Override if you need to do more than marking complete
166
+ this.setState(FSM_CANCEL);
167
+ }
168
+
162
169
  setDependentError(): void
163
170
  {
164
171
  this.dependentError = true;
@@ -120,7 +120,7 @@ export interface SessionProps
120
120
  xprops?: { [prop: string]: string };
121
121
  groups: any; // DT.GroupsMapIndex
122
122
  xid?: string; // external ID
123
- colors?: string; // cached district colors (specific to OT redistricting)
123
+ colors?: string; // cached district colors (specific to OT for redistricting)
124
124
  }
125
125
 
126
126
  // True to add, False to remove
@@ -141,6 +141,7 @@ export interface SessionUpdateProps
141
141
  accessUpdate?: AccessMap;
142
142
  restore?: string; // Revision ID
143
143
  revision?: Revision; // If ID is empty, snap a new revision, otherwise label it.
144
+ colors?: string; // cached district colors (specific to OT for redistricting)
144
145
  }
145
146
 
146
147
  export interface SessionsIndex
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.82",
3
+ "version": "1.0.85",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",