@jdeighan/coffee-utils 4.1.30 → 4.1.31
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/package.json +2 -1
- package/src/DataStores.coffee +157 -0
- package/src/DataStores.js +197 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.1.
|
|
4
|
+
"version": "4.1.31",
|
|
5
5
|
"description": "A set of utility functions for CoffeeScript",
|
|
6
6
|
"main": "coffee_utils.js",
|
|
7
7
|
"exports": {
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"./debug": "./src/debug_utils.js",
|
|
17
17
|
"./svelte": "./src/svelte_utils.js",
|
|
18
18
|
"./test": "./src/UnitTester.js",
|
|
19
|
+
"./store": "./src/DataStores.js",
|
|
19
20
|
"./package.json": "./package.json"
|
|
20
21
|
},
|
|
21
22
|
"engines": {
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# DataStores.coffee
|
|
2
|
+
|
|
3
|
+
import pathlib from 'path'
|
|
4
|
+
import {writable, readable, get} from 'svelte/store'
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
assert, undef, pass, error, localStore, isEmpty,
|
|
8
|
+
} from '@jdeighan/coffee-utils'
|
|
9
|
+
import {log} from '@jdeighan/coffee-utils/log'
|
|
10
|
+
import {
|
|
11
|
+
withExt, slurp, barf, newerDestFileExists,
|
|
12
|
+
} from '@jdeighan/coffee-utils/fs'
|
|
13
|
+
import {isTAML, taml} from '@jdeighan/string-input/taml'
|
|
14
|
+
|
|
15
|
+
# ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
export class WritableDataStore
|
|
18
|
+
|
|
19
|
+
constructor: (value=undef) ->
|
|
20
|
+
@store = writable value
|
|
21
|
+
|
|
22
|
+
subscribe: (callback) ->
|
|
23
|
+
return @store.subscribe(callback)
|
|
24
|
+
|
|
25
|
+
set: (value) ->
|
|
26
|
+
@store.set(value)
|
|
27
|
+
|
|
28
|
+
update: (func) ->
|
|
29
|
+
@store.update(func)
|
|
30
|
+
|
|
31
|
+
# ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
export class LocalStorageDataStore extends WritableDataStore
|
|
34
|
+
|
|
35
|
+
constructor: (@masterKey, defValue=undef) ->
|
|
36
|
+
|
|
37
|
+
# --- CoffeeScript forces us to call super first
|
|
38
|
+
# so we can't get the localStorage value first
|
|
39
|
+
super defValue
|
|
40
|
+
value = localStore(@masterKey)
|
|
41
|
+
if value?
|
|
42
|
+
@set value
|
|
43
|
+
|
|
44
|
+
# --- I'm assuming that when update() is called,
|
|
45
|
+
# set() will also be called
|
|
46
|
+
|
|
47
|
+
set: (value) ->
|
|
48
|
+
if ! value?
|
|
49
|
+
error "LocalStorageStore.set(): cannont set to undef"
|
|
50
|
+
super value
|
|
51
|
+
localStore @masterKey, value
|
|
52
|
+
|
|
53
|
+
update: (func) ->
|
|
54
|
+
super func
|
|
55
|
+
localStore @masterKey, get(@store)
|
|
56
|
+
|
|
57
|
+
# ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
export class PropsDataStore extends LocalStorageDataStore
|
|
60
|
+
|
|
61
|
+
constructor: (masterKey) ->
|
|
62
|
+
super masterKey, {}
|
|
63
|
+
|
|
64
|
+
setProp: (name, value) ->
|
|
65
|
+
if ! name?
|
|
66
|
+
error "PropStore.setProp(): empty key"
|
|
67
|
+
@update (hPrefs) ->
|
|
68
|
+
hPrefs[name] = value
|
|
69
|
+
return hPrefs
|
|
70
|
+
|
|
71
|
+
# ---------------------------------------------------------------------------
|
|
72
|
+
|
|
73
|
+
export class ReadableDataStore
|
|
74
|
+
|
|
75
|
+
constructor: () ->
|
|
76
|
+
@store = readable null, (set) ->
|
|
77
|
+
@setter = set # store the setter function
|
|
78
|
+
@start() # call your start() method
|
|
79
|
+
return () => @stop() # return function capable of stopping
|
|
80
|
+
|
|
81
|
+
subscribe: (callback) ->
|
|
82
|
+
return @store.subscribe(callback)
|
|
83
|
+
|
|
84
|
+
start: () ->
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
stop: () ->
|
|
88
|
+
pass
|
|
89
|
+
|
|
90
|
+
# ---------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
export class DateTimeDataStore extends ReadableDataStore
|
|
93
|
+
|
|
94
|
+
start: () ->
|
|
95
|
+
# --- We need to store this interval for use in stop() later
|
|
96
|
+
@interval = setInterval(() ->
|
|
97
|
+
@setter new Date()
|
|
98
|
+
, 1000)
|
|
99
|
+
|
|
100
|
+
stop: () ->
|
|
101
|
+
clearInterval @interval
|
|
102
|
+
|
|
103
|
+
# ---------------------------------------------------------------------------
|
|
104
|
+
|
|
105
|
+
export class MousePosDataStore extends ReadableDataStore
|
|
106
|
+
|
|
107
|
+
start: () ->
|
|
108
|
+
# --- We need to store this handler for use in stop() later
|
|
109
|
+
@mouseMoveHandler = (e) ->
|
|
110
|
+
@setter {
|
|
111
|
+
x: e.clientX,
|
|
112
|
+
y: e.clientY,
|
|
113
|
+
}
|
|
114
|
+
document.body.addEventListener('mousemove', @mouseMoveHandler)
|
|
115
|
+
|
|
116
|
+
stop: () ->
|
|
117
|
+
document.body.removeEventListener('mousemove', @mouseMoveHandler)
|
|
118
|
+
|
|
119
|
+
# ---------------------------------------------------------------------------
|
|
120
|
+
|
|
121
|
+
export class TAMLDataStore extends WritableDataStore
|
|
122
|
+
|
|
123
|
+
constructor: (str) ->
|
|
124
|
+
|
|
125
|
+
super taml(str)
|
|
126
|
+
|
|
127
|
+
# ---------------------------------------------------------------------------
|
|
128
|
+
# UTILITIES
|
|
129
|
+
# ---------------------------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
export brewTamlStr = (code, stub) ->
|
|
132
|
+
|
|
133
|
+
return """
|
|
134
|
+
import {TAMLDataStore} from '@jdeighan/starbucks/stores';
|
|
135
|
+
|
|
136
|
+
export let #{stub} = new TAMLDataStore(`#{code}`);
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
# ---------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
export brewTamlFile = (srcPath, destPath=undef, hOptions={}) ->
|
|
142
|
+
# --- taml => js
|
|
143
|
+
# Valid Options:
|
|
144
|
+
# force
|
|
145
|
+
|
|
146
|
+
if ! destPath?
|
|
147
|
+
destPath = withExt(srcPath, '.js', {removeLeadingUnderScore:true})
|
|
148
|
+
if hOptions.force || ! newerDestFileExists(srcPath, destPath)
|
|
149
|
+
hInfo = pathlib.parse(destPath)
|
|
150
|
+
stub = hInfo.name
|
|
151
|
+
|
|
152
|
+
tamlCode = slurp(srcPath)
|
|
153
|
+
jsCode = brewTamlStr(tamlCode, stub)
|
|
154
|
+
barf destPath, jsCode
|
|
155
|
+
return
|
|
156
|
+
|
|
157
|
+
# ---------------------------------------------------------------------------
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// Generated by CoffeeScript 2.6.1
|
|
2
|
+
// DataStores.coffee
|
|
3
|
+
import pathlib from 'path';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
writable,
|
|
7
|
+
readable,
|
|
8
|
+
get
|
|
9
|
+
} from 'svelte/store';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
assert,
|
|
13
|
+
undef,
|
|
14
|
+
pass,
|
|
15
|
+
error,
|
|
16
|
+
localStore,
|
|
17
|
+
isEmpty
|
|
18
|
+
} from '@jdeighan/coffee-utils';
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
log
|
|
22
|
+
} from '@jdeighan/coffee-utils/log';
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
withExt,
|
|
26
|
+
slurp,
|
|
27
|
+
barf,
|
|
28
|
+
newerDestFileExists
|
|
29
|
+
} from '@jdeighan/coffee-utils/fs';
|
|
30
|
+
|
|
31
|
+
import {
|
|
32
|
+
isTAML,
|
|
33
|
+
taml
|
|
34
|
+
} from '@jdeighan/string-input/taml';
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
export var WritableDataStore = class WritableDataStore {
|
|
38
|
+
constructor(value = undef) {
|
|
39
|
+
this.store = writable(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
subscribe(callback) {
|
|
43
|
+
return this.store.subscribe(callback);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
set(value) {
|
|
47
|
+
return this.store.set(value);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
update(func) {
|
|
51
|
+
return this.store.update(func);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
export var LocalStorageDataStore = class LocalStorageDataStore extends WritableDataStore {
|
|
58
|
+
constructor(masterKey1, defValue = undef) {
|
|
59
|
+
var value;
|
|
60
|
+
super(defValue);
|
|
61
|
+
this.masterKey = masterKey1;
|
|
62
|
+
value = localStore(this.masterKey);
|
|
63
|
+
if (value != null) {
|
|
64
|
+
this.set(value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// --- I'm assuming that when update() is called,
|
|
69
|
+
// set() will also be called
|
|
70
|
+
set(value) {
|
|
71
|
+
if (value == null) {
|
|
72
|
+
error("LocalStorageStore.set(): cannont set to undef");
|
|
73
|
+
}
|
|
74
|
+
super.set(value);
|
|
75
|
+
return localStore(this.masterKey, value);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
update(func) {
|
|
79
|
+
super.update(func);
|
|
80
|
+
return localStore(this.masterKey, get(this.store));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
export var PropsDataStore = class PropsDataStore extends LocalStorageDataStore {
|
|
87
|
+
constructor(masterKey) {
|
|
88
|
+
super(masterKey, {});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
setProp(name, value) {
|
|
92
|
+
if (name == null) {
|
|
93
|
+
error("PropStore.setProp(): empty key");
|
|
94
|
+
}
|
|
95
|
+
return this.update(function(hPrefs) {
|
|
96
|
+
hPrefs[name] = value;
|
|
97
|
+
return hPrefs;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
export var ReadableDataStore = class ReadableDataStore {
|
|
105
|
+
constructor() {
|
|
106
|
+
this.store = readable(null, function(set) {
|
|
107
|
+
this.setter = set; // store the setter function
|
|
108
|
+
this.start(); // call your start() method
|
|
109
|
+
return () => {
|
|
110
|
+
return this.stop(); // return function capable of stopping
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
subscribe(callback) {
|
|
116
|
+
return this.store.subscribe(callback);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
start() {
|
|
120
|
+
return pass;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
stop() {
|
|
124
|
+
return pass;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
export var DateTimeDataStore = class DateTimeDataStore extends ReadableDataStore {
|
|
131
|
+
start() {
|
|
132
|
+
// --- We need to store this interval for use in stop() later
|
|
133
|
+
return this.interval = setInterval(function() {
|
|
134
|
+
return this.setter(new Date(), 1000);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
stop() {
|
|
139
|
+
return clearInterval(this.interval);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// ---------------------------------------------------------------------------
|
|
145
|
+
export var MousePosDataStore = class MousePosDataStore extends ReadableDataStore {
|
|
146
|
+
start() {
|
|
147
|
+
// --- We need to store this handler for use in stop() later
|
|
148
|
+
this.mouseMoveHandler = function(e) {
|
|
149
|
+
return this.setter({
|
|
150
|
+
x: e.clientX,
|
|
151
|
+
y: e.clientY
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
return document.body.addEventListener('mousemove', this.mouseMoveHandler);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
stop() {
|
|
158
|
+
return document.body.removeEventListener('mousemove', this.mouseMoveHandler);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
export var TAMLDataStore = class TAMLDataStore extends WritableDataStore {
|
|
165
|
+
constructor(str) {
|
|
166
|
+
super(taml(str));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// ---------------------------------------------------------------------------
|
|
172
|
+
// UTILITIES
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
export var brewTamlStr = function(code, stub) {
|
|
175
|
+
return `import {TAMLDataStore} from '@jdeighan/starbucks/stores';
|
|
176
|
+
|
|
177
|
+
export let ${stub} = new TAMLDataStore(\`${code}\`);`;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// ---------------------------------------------------------------------------
|
|
181
|
+
export var brewTamlFile = function(srcPath, destPath = undef, hOptions = {}) {
|
|
182
|
+
var hInfo, jsCode, stub, tamlCode;
|
|
183
|
+
if (destPath == null) {
|
|
184
|
+
destPath = withExt(srcPath, '.js', {
|
|
185
|
+
removeLeadingUnderScore: true
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (hOptions.force || !newerDestFileExists(srcPath, destPath)) {
|
|
189
|
+
hInfo = pathlib.parse(destPath);
|
|
190
|
+
stub = hInfo.name;
|
|
191
|
+
tamlCode = slurp(srcPath);
|
|
192
|
+
jsCode = brewTamlStr(tamlCode, stub);
|
|
193
|
+
barf(destPath, jsCode);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// ---------------------------------------------------------------------------
|