@kizmann/pico-js 2.0.4 → 2.0.6

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,169 +1,147 @@
1
- import { Arr, Hash, Mix } from "../index.esm.js";
1
+ import { Arr, Hash, Mix, Obj } from "../index.esm.js";
2
2
 
3
3
  export class PicoRunner
4
4
  {
5
- static $idler = {
6
- native: {}, debounce: {}, throttle: {}
7
- };
5
+ static $idler = null;
8
6
 
9
- static $timer = {
10
- date: 0, func: null
11
- };
7
+ static $timer = 0;
12
8
 
13
9
  static $buffer = [];
14
10
 
15
11
  /**
16
- * Run callback after delay (id)
12
+ * Clear timer or call function
17
13
  *
18
- * @example Run.timeout(() => {}, 100) // => "t-..."
14
+ * @example Run.clear(timer)
19
15
  *
20
- * @param {function} fn Callback to run
21
- * @param {number} [delay] Delay ms
22
- * @param {string|null} [index] Timer id
23
- * @returns {string} Timer id
16
+ * @param {any} timer Timer ID, array of IDs, or function
17
+ * @returns {typeof PicoRunner} Static class
24
18
  */
25
- static timeout(fn, delay = 0, index = null)
19
+ static clear(timer)
26
20
  {
27
- let idler = PicoRunner.$idler.native;
21
+ if ( Mix.isArr(timer) ) {
22
+ return Arr.each(timer, (t) => this.clear(t), this);
23
+ }
28
24
 
29
- if ( index == null ) {
30
- index = Hash.make(12);
25
+ if ( Mix.isFunc(timer) ) {
26
+ return (timer(), this);
31
27
  }
32
28
 
33
- idler[(index = 't-' + index)] = setInterval(() => {
34
- fn();
35
- }, delay);
29
+ clearTimeout(timer);
30
+ clearInterval(timer);
36
31
 
37
- return index;
32
+ return this;
38
33
  }
39
34
 
40
35
  /**
41
- * Run callback on interval (id)
36
+ * Request idle callback with fallback
42
37
  *
43
- * @example Run.interval(() => {}, 250) // => "i-..."
44
- *
45
- * @param {function} fn Callback to run
46
- * @param {number} [intval] Interval ms
47
- * @param {string|null} [index] Timer id
48
- * @returns {string} Timer id
38
+ * @param {function} cb Callback function
39
+ * @returns {typeof PicoRunner} Static class
49
40
  */
50
- static interval(fn, intval = 0, index = null)
41
+ static tryin(cb)
51
42
  {
52
- let idler = PicoRunner.$idler.native;
53
-
54
- if ( index == null ) {
55
- index = Hash.make(12);
43
+ try {
44
+ requestIdleCallback(cb);
45
+ } catch (e) {
46
+ //
56
47
  }
57
48
 
58
- idler[(index = 'i-' + index)] = setInterval(() => {
59
- fn();
60
- }, intval);
61
-
62
- return index;
49
+ return this;
63
50
  }
64
51
 
65
52
  /**
66
- * Clear timer(s) by id
53
+ * Wait for condition to be true
67
54
  *
68
- * @example Run.clear("i-abc") // => Run
69
- * @example Run.clear(["t-a","i-b"]) // => Run
55
+ * @example Run.wait(() => window.foo, 10, 100)
70
56
  *
71
- * @param {string|Array<string>} index Timer id(s)
72
- * @param {string} [scope] Idler scope key
73
- * @returns {typeof PicoRunner} Runner class
57
+ * @param {function} fn Condition function
58
+ * @param {number} [intval] Interval ms
59
+ * @param {number} [limit] Max iterations
60
+ * @returns {function} Clear function
74
61
  */
75
- static clear(index, scope = 'native')
62
+ static wait(fn, intval = 0, limit = 500)
76
63
  {
77
- if ( Mix.isArr(index) ) {
78
- return (Arr.each(index, (e) => this.clear(e, scope)), this);
79
- }
80
-
81
- let idler = PicoRunner.$idler[scope];
64
+ let idler, timer;
82
65
 
83
- if ( /^i-/.test(index) === false ) {
84
- clearInterval(idler[index]);
85
- }
66
+ timer = setTimeout(() => {
67
+ this.clear([idler, timer]);
68
+ }, intval * limit);
86
69
 
87
- if ( /^t-/.test(index) === false ) {
88
- clearTimeout(idler[index]);
89
- }
70
+ idler = setInterval(() => {
71
+ if ( fn() ) this.clear([timer, idler]);
72
+ }, intval);
90
73
 
91
- return this;
74
+ return () => clearInterval(idler);
92
75
  }
93
76
 
94
77
  /**
95
- * Poll until callback is true
78
+ * Run function in next animation frame
96
79
  *
97
- * @example Run.wait(() => ready, 50) // polls
80
+ * @example Run.frame(cb)
98
81
  *
99
- * @param {function} fn Condition callback
100
- * @param {number} [intval] Poll interval ms
101
- * @param {number} [limit] Max poll count
102
- * @returns {void} No return value
82
+ * @param {function} fn Callback function
83
+ * @param {any} [...args] Callback arguments
84
+ * @returns {function} Noop clear function
103
85
  */
104
- static wait(fn, intval = 0, limit = 500)
86
+ static frame(fn, ...args)
105
87
  {
106
- let idler, timer;
107
-
108
- timer = this.timeout(() => {
109
- this.clear([idler, timer]);
110
- }, intval * limit);
88
+ requestAnimationFrame(() => {
89
+ fn(...args)
90
+ });
111
91
 
112
- idler = this.interval(() => {
113
- if ( fn() ) this.clear([idler, timer]);
114
- }, intval);
92
+ return () => null;
115
93
  }
116
94
 
117
95
  /**
118
- * Run callback in next frame
96
+ * Run function when browser is idle
119
97
  *
120
- * @example Run.frame(() => {}) // => Run
98
+ * @example Run.idle(cb)
121
99
  *
122
- * @param {function} fn Callback to run
123
- * @param {...any} args Callback args
124
- * @returns {typeof PicoRunner} Runner class
100
+ * @param {function} fn Callback function
101
+ * @param {any} [...args] Callback arguments
102
+ * @returns {function} Noop clear function
125
103
  */
126
- static frame(fn, ...args)
104
+ static idle(fn, ...args)
127
105
  {
128
- requestAnimationFrame(function() {
129
- fn(...args);
106
+ requestIdleCallback(() => {
107
+ fn(...args)
130
108
  });
131
109
 
132
- return this;
110
+ return () => null;
133
111
  }
134
112
 
135
113
  /**
136
- * Run callback async soon
114
+ * Run function asynchronously
137
115
  *
138
- * @example Run.async(() => {}) // => Run
116
+ * @example Run.async(cb)
139
117
  *
140
- * @param {function} fn Callback to run
141
- * @param {...any} args Callback args
142
- * @returns {typeof PicoRunner} Runner class
118
+ * @param {function} fn Callback function
119
+ * @param {any} [...args] Callback arguments
120
+ * @returns {function} Noop clear function
143
121
  */
144
122
  static async(fn, ...args)
145
123
  {
146
124
  setTimeout(() => {
147
- fn(...args);
125
+ fn(...args)
148
126
  });
149
127
 
150
- return this;
128
+ return () => null;
151
129
  }
152
130
 
153
131
  /**
154
- * Run callback after delay
132
+ * Run function after delay
155
133
  *
156
- * @example const cancel = Run.delay(() => {}, 50)
134
+ * @example Run.delay(cb, 100)
157
135
  *
158
- * @param {function} fn Callback to run
136
+ * @param {function} fn Callback function
159
137
  * @param {number} [delay] Delay ms
160
- * @param {...any} args Callback args
161
- * @returns {function} Cancel function
138
+ * @param {any} [...args] Callback arguments
139
+ * @returns {function} Clear function
162
140
  */
163
141
  static delay(fn, delay = 0, ...args)
164
142
  {
165
143
  let idler = setTimeout(() => {
166
- this.async(fn, ...args);
144
+ fn(...args);
167
145
  }, delay);
168
146
 
169
147
  return () => clearTimeout(idler);
@@ -176,24 +154,19 @@ export class PicoRunner
176
154
  *
177
155
  * @param {function} cb Callback to run
178
156
  * @param {number} [timeout] Wait ms
179
- * @param {string|null} [index] Debounce id
180
157
  * @returns {function} Debounced fn
181
158
  */
182
- static debounce(cb, timeout = 100, index = null)
159
+ static debounce(cb, timeout = 100)
183
160
  {
184
- let idler = PicoRunner.$idler.debounce;
185
-
186
- if ( index == null ) {
187
- index = Hash.make(12);
188
- }
161
+ let idler = null;
189
162
 
190
163
  return (...args) => {
191
164
 
192
- if ( idler[index] ) {
193
- clearTimeout(idler[index]);
165
+ if ( idler ) {
166
+ clearTimeout(idler);
194
167
  }
195
168
 
196
- idler[index] = setTimeout(() => {
169
+ idler = setTimeout(() => {
197
170
  this.frame(cb, ...args);
198
171
  }, timeout);
199
172
  };
@@ -206,24 +179,19 @@ export class PicoRunner
206
179
  *
207
180
  * @param {function} cb Callback to run
208
181
  * @param {number} [timeout] Wait ms
209
- * @param {string|null} [index] Throttle id
210
182
  * @returns {function} Throttled fn
211
183
  */
212
- static throttle(cb, timeout = 100, index = null)
184
+ static throttle(cb, timeout = 100)
213
185
  {
214
- let queued, idler = PicoRunner.$idler.throttle;
215
-
216
- if ( index == null ) {
217
- index = Hash.make(12);
218
- }
186
+ let queued, idler = null;
219
187
 
220
188
  return (...args) => {
221
189
 
222
- if ( idler[index] ) {
223
- clearTimeout(idler[index]);
190
+ if ( idler ) {
191
+ clearTimeout(idler);
224
192
  }
225
193
 
226
- idler[index] = setTimeout(() => {
194
+ idler = setTimeout(() => {
227
195
  queued = false;
228
196
  }, timeout);
229
197
 
@@ -231,7 +199,7 @@ export class PicoRunner
231
199
  return;
232
200
  }
233
201
 
234
- (this.frame(cb, ...args), queued = true)
202
+ (this.frame(cb, ...args), queued = true);
235
203
  };
236
204
  }
237
205
 
@@ -265,18 +233,28 @@ export class PicoRunner
265
233
  *
266
234
  * @param {function} cb Callback to run
267
235
  * @param {string} key Buffer key
268
- * @param {number} [order] Sort order
236
+ * @param {number} [priority] Sort priority
269
237
  * @returns {function} Buffered handler
270
238
  */
271
- static framebuffer(cb, key, order = 1000)
239
+ static framebuffer(cb, key, priority = 1000)
272
240
  {
241
+ const item = {
242
+ key, cb, priority, args: [], active: false
243
+ };
244
+
245
+ Arr.add(this.$buffer, item);
246
+
247
+ let fn = () => {
248
+ this.runFramebuffer();
249
+ };
250
+
273
251
  return (e, ...args) => {
274
252
 
275
- Arr.replace(PicoRunner.$buffer, {
276
- key, cb, order, args: [e, ...args], active: true
277
- }, { key });
253
+ Obj.assign(item, {
254
+ args: [e, ...args], active: true
255
+ });
278
256
 
279
- (e.preventDefault(), PicoRunner.runFramebuffer());
257
+ (e.preventDefault(), fn());
280
258
  };
281
259
  }
282
260
 
@@ -289,23 +267,24 @@ export class PicoRunner
289
267
  */
290
268
  static runFramebuffer()
291
269
  {
292
- if ( this.$timer.func ) {
293
- this.$timer.func();
270
+ if ( this.$idler ) {
271
+ clearTimeout(this.$idler);
294
272
  }
295
273
 
296
- this.$timer.func = this.delay(() => {
274
+ this.$idler = setTimeout(() => {
275
+ console.log('delayed runFramebuffer')
297
276
  this.runFramebuffer();
298
277
  }, 50);
299
278
 
300
- if ( Date.now() - this.$timer.date <= 50 ) {
279
+ if ( Date.now() - this.$timer < 50 ) {
301
280
  return;
302
281
  }
303
282
 
304
- if ( this.$timer.func ) {
305
- this.$timer.func();
283
+ if ( this.$idler ) {
284
+ clearTimeout(this.$idler);
306
285
  }
307
286
 
308
- this.$timer.date = Date.now();
287
+ this.$timer = Date.now();
309
288
 
310
289
  let buffer = Arr.filter(this.$buffer, {
311
290
  active: true
@@ -315,10 +294,10 @@ export class PicoRunner
315
294
  return;
316
295
  }
317
296
 
318
- this.frame(() => {
319
- Arr.each(Arr.sort(buffer, 'order'), (item) => {
320
- item.cb(...item.args); item.active = false;
321
- });
297
+ buffer = Arr.sort(buffer, 'priority');
298
+
299
+ Arr.each(buffer.reverse(), (item) => {
300
+ item.cb(...item.args); item.active = false;
322
301
  });
323
302
  }
324
303
 
@@ -2,7 +2,7 @@
2
2
  * @memberof PicoDom
3
3
  */
4
4
  export class PicoDomEventStatic {
5
- static events: any[];
5
+ static $events: any[];
6
6
  }
7
7
  /**
8
8
  * @memberof PicoDom
@@ -59,6 +59,15 @@ export class PicoDomEventInstance {
59
59
  * @returns {PicoDom} Current instance
60
60
  */
61
61
  off(event: any, selector?: string, options?: any): PicoDom;
62
+ /**
63
+ * Stop listening to event with specific options
64
+ *
65
+ * @example Dom.find("div").optoff({ id: "my-id" })
66
+ *
67
+ * @param {any} [options] Listener options
68
+ * @returns {PicoDom} Current instance
69
+ */
70
+ optoff(options?: any): PicoDom;
62
71
  /**
63
72
  * Listen to event once
64
73
  *
@@ -134,6 +134,17 @@ export class PicoArray {
134
134
  * @returns {any} Mapped array
135
135
  */
136
136
  static each(value: any, cb: Function, retval?: any): any;
137
+ /**
138
+ * Iterate over object keys
139
+ *
140
+ * @example Arr.eachObj({a:1}, (v,k) => v+1) // => [2]
141
+ *
142
+ * @param {any} value Input object
143
+ * @param {function} cb Iterate callback
144
+ * @param {any} [retval] Return value override
145
+ * @returns {any} Mapped values or retval
146
+ */
147
+ static eachObj(value: any, cb: Function, retval?: any): any;
137
148
  /**
138
149
  * Map values in place (mutates)
139
150
  *
@@ -166,6 +177,16 @@ export class PicoArray {
166
177
  * @returns {Array<string>} Matching keys
167
178
  */
168
179
  static filterIndex(value: any, filter?: any): Array<string>;
180
+ /**
181
+ * Remove values matching filter (mutates)
182
+ *
183
+ * @example Arr.filterRemove([1,2], 2) // => [1]
184
+ *
185
+ * @param {any} value Input list
186
+ * @param {any} [filter] Filter spec
187
+ * @returns {any} Mutated list
188
+ */
189
+ static filterRemove(value: any, filter?: any): any;
169
190
  /**
170
191
  * Filter values by filter
171
192
  *
@@ -455,26 +476,26 @@ export namespace PicoArray {
455
476
  /**
456
477
  * @see PicoArray.sortPrim
457
478
  */
458
- function sortString(...args: any[]): any;
479
+ function sortString(...args: any[]): any[];
459
480
  /**
460
481
  * @see PicoArray.append
461
482
  */
462
- function push(...args: any[]): any;
483
+ function push(...args: any[]): any[];
463
484
  /**
464
485
  * @see PicoArray.merge
465
486
  */
466
- function concat(...args: any[]): any;
487
+ function concat(...args: any[]): any[];
467
488
  /**
468
489
  * @see PicoArray.matches
469
490
  */
470
- function equal(...args: any[]): any;
491
+ function equal(...args: any[]): boolean;
471
492
  /**
472
493
  * @see PicoArray.diff
473
494
  */
474
- function diffrence(...args: any[]): any;
495
+ function diffrence(...args: any[]): any[];
475
496
  /**
476
497
  * @see PicoArray.isect
477
498
  */
478
- function intersect(...args: any[]): any;
499
+ function intersect(...args: any[]): any[];
479
500
  }
480
501
  export default PicoArray;
@@ -457,7 +457,7 @@ export namespace PicoMixed {
457
457
  function isPlain(...args: any[]): boolean;
458
458
  function float(...args: any[]): any;
459
459
  function delay(...args: any[]): Function;
460
- function async(...args: any[]): typeof Run;
460
+ function async(...args: any[]): Function;
461
461
  function debounce(...args: any[]): Function;
462
462
  function throttle(...args: any[]): Function;
463
463
  function framerate(...args: any[]): Function;
@@ -468,4 +468,3 @@ export namespace PicoMixed {
468
468
  }
469
469
  export default Mix;
470
470
  import { Mix } from "../index.esm.js";
471
- import { Run } from "../index.esm.js";
@@ -229,6 +229,8 @@ export class PicoObject {
229
229
  * @returns {boolean} True if matches
230
230
  */
231
231
  static matches(value: any, search: any): boolean;
232
+ static sort(obj: any, key: any): any[];
233
+ static sortString(obj: any, key: any): any[];
232
234
  }
233
235
  export namespace PicoObject {
234
236
  /**
@@ -243,13 +245,5 @@ export namespace PicoObject {
243
245
  * @see PicoArray.findIndex
244
246
  */
245
247
  function findIndex(...args: any[]): number;
246
- /**
247
- * @see PicoArray.sort
248
- */
249
- function sort(...args: any[]): any[];
250
- /**
251
- * @see PicoArray.sortDeep
252
- */
253
- function sortString(...args: any[]): any[];
254
248
  }
255
249
  export default PicoObject;