@xtia/jel 0.6.0 → 0.6.2

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { DomEntity, ElementClassDescriptor, ElementDescriptor, DOMContent, DomHelper, StyleAccessor, JelEntity } from "./internal/types";
2
2
  export { $ } from "./internal/element";
3
3
  export { createEntity } from "./internal/util";
4
- export { createEventSource } from "./internal/emitter";
4
+ export { createEventSource, interval } from "./internal/emitter";
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { $ } from "./internal/element";
2
2
  export { createEntity } from "./internal/util";
3
- export { createEventSource } from "./internal/emitter";
3
+ export { createEventSource, interval } from "./internal/emitter";
@@ -73,7 +73,6 @@ export declare class EventEmitter<T> extends Emitter<T> {
73
73
  debounce(ms: number): EventEmitter<T>;
74
74
  throttle(ms: number): EventEmitter<T>;
75
75
  batch(ms: number): Emitter<T[]>;
76
- filter(check: (value: T) => boolean): EventEmitter<T>;
77
76
  /**
78
77
  * **Experimental**: May change in future revisions
79
78
  * Note: potential leak - This link will remain subscribed to the parent
@@ -92,7 +91,6 @@ export declare class EventEmitter<T> extends Emitter<T> {
92
91
  * @returns
93
92
  */
94
93
  take(limit: number): EventEmitter<T>;
95
- tap(cb: Handler<T>): EventEmitter<T>;
96
94
  /**
97
95
  * **Experimental**: May change in future revisions
98
96
  * Note: potential leak - This link will remain subscribed to the notifier
@@ -101,6 +99,46 @@ export declare class EventEmitter<T> extends Emitter<T> {
101
99
  * @returns
102
100
  */
103
101
  takeUntil(notifier: Emitter<any>): Emitter<T>;
102
+ /**
103
+ * Creates a chainable emitter that immediately emits a value to every new subscriber,
104
+ * then forwards parent emissions
105
+ * @param value
106
+ * @returns A new emitter that emits a value to new subscribers and forwards all values from the parent
107
+ */
108
+ immediate(value: T): EventEmitter<T>;
109
+ cached(): EventEmitter<T>;
110
+ /**
111
+ * Creates a chainable emitter that applies arbitrary transformation to values emitted by its parent
112
+ * @param mapFunc
113
+ * @returns Listenable: emits transformed values
114
+ */
115
+ map<R>(mapFunc: (value: T) => R): EventEmitter<R>;
116
+ /**
117
+ * Creates a chainable emitter that selectively forwards emissions along the chain
118
+ * @param check Function that takes an emitted value and returns true if the emission should be forwarded along the chain
119
+ * @returns Listenable: emits values that pass the filter
120
+ */
121
+ filter(check: (value: T) => boolean): EventEmitter<T>;
122
+ /**
123
+ * Creates a chainable emitter that discards emitted values that are the same as the last value emitted by the new emitter
124
+ * @param compare Optional function that takes the previous and next values and returns true if they should be considered equal
125
+ *
126
+ * If no `compare` function is provided, values will be compared via `===`
127
+ * @returns Listenable: emits non-repeating values
128
+ */
129
+ dedupe(compare?: (a: T, b: T) => boolean): EventEmitter<T>;
130
+ /**
131
+ * Creates a chainable emitter that mirrors emissions from the parent emitter, invoking the provided callback `cb` as a side effect for each emission.
132
+ *
133
+ * The callback `cb` is called exactly once per parent emission, regardless of how many listeners are attached to the returned emitter.
134
+ * All listeners attached to the returned emitter receive the same values as the parent emitter.
135
+ *
136
+ * *Note*, the side effect `cb` is only invoked when there is at least one listener attached to the returned emitter
137
+ *
138
+ * @param cb A function to be called as a side effect for each value emitted by the parent emitter.
139
+ * @returns A new emitter that forwards all values from the parent, invoking `cb` as a side effect.
140
+ */
141
+ tap(cb: Handler<T>): EventEmitter<T>;
104
142
  }
105
143
  /**
106
144
  * Creates a linked Emitter and emit() pair
@@ -141,4 +179,7 @@ export declare function createListenable<T>(onAddFirst?: () => void, onRemoveLas
141
179
  listen: (fn: (v: T) => void) => UnsubscribeFunc;
142
180
  emit: (value: T) => void;
143
181
  };
182
+ export declare function interval(t: number | {
183
+ asMilliseconds: number;
184
+ }): EventEmitter<number>;
144
185
  export {};
@@ -152,10 +152,6 @@ export class EventEmitter extends Emitter {
152
152
  });
153
153
  return new EventEmitter(listen);
154
154
  }
155
- filter(check) {
156
- const listen = this.transform((value, emit) => check(value) && emit(value));
157
- return new EventEmitter(listen);
158
- }
159
155
  /**
160
156
  * **Experimental**: May change in future revisions
161
157
  * Note: potential leak - This link will remain subscribed to the parent
@@ -211,13 +207,6 @@ export class EventEmitter extends Emitter {
211
207
  });
212
208
  return new EventEmitter(listen);
213
209
  }
214
- tap(cb) {
215
- const listen = this.transform((value, emit) => {
216
- cb(value);
217
- emit(value);
218
- });
219
- return new EventEmitter(listen);
220
- }
221
210
  /**
222
211
  * **Experimental**: May change in future revisions
223
212
  * Note: potential leak - This link will remain subscribed to the notifier
@@ -236,6 +225,90 @@ export class EventEmitter extends Emitter {
236
225
  });
237
226
  return new EventEmitter(listen);
238
227
  }
228
+ /**
229
+ * Creates a chainable emitter that immediately emits a value to every new subscriber,
230
+ * then forwards parent emissions
231
+ * @param value
232
+ * @returns A new emitter that emits a value to new subscribers and forwards all values from the parent
233
+ */
234
+ immediate(value) {
235
+ return new EventEmitter(handle => {
236
+ handle(value);
237
+ return this.onListen(handle);
238
+ });
239
+ }
240
+ cached() {
241
+ let cache = null;
242
+ let unsub = null;
243
+ const { listen, emit } = createListenable(() => {
244
+ unsub = this.onListen((value => {
245
+ cache = { value };
246
+ emit(value);
247
+ }));
248
+ }, () => {
249
+ unsub();
250
+ });
251
+ return new EventEmitter(handler => {
252
+ if (cache)
253
+ handler(cache.value);
254
+ return listen(handler);
255
+ });
256
+ }
257
+ /**
258
+ * Creates a chainable emitter that applies arbitrary transformation to values emitted by its parent
259
+ * @param mapFunc
260
+ * @returns Listenable: emits transformed values
261
+ */
262
+ map(mapFunc) {
263
+ const listen = this.transform((value, emit) => emit(mapFunc(value)));
264
+ return new EventEmitter(listen);
265
+ }
266
+ /**
267
+ * Creates a chainable emitter that selectively forwards emissions along the chain
268
+ * @param check Function that takes an emitted value and returns true if the emission should be forwarded along the chain
269
+ * @returns Listenable: emits values that pass the filter
270
+ */
271
+ filter(check) {
272
+ const listen = this.transform((value, emit) => check(value) && emit(value));
273
+ return new EventEmitter(listen);
274
+ }
275
+ /**
276
+ * Creates a chainable emitter that discards emitted values that are the same as the last value emitted by the new emitter
277
+ * @param compare Optional function that takes the previous and next values and returns true if they should be considered equal
278
+ *
279
+ * If no `compare` function is provided, values will be compared via `===`
280
+ * @returns Listenable: emits non-repeating values
281
+ */
282
+ dedupe(compare) {
283
+ let previous = null;
284
+ const listen = this.transform((value, emit) => {
285
+ if (!previous || (compare
286
+ ? !compare(previous.value, value)
287
+ : (previous.value !== value))) {
288
+ emit(value);
289
+ previous = { value };
290
+ }
291
+ });
292
+ return new EventEmitter(listen);
293
+ }
294
+ /**
295
+ * Creates a chainable emitter that mirrors emissions from the parent emitter, invoking the provided callback `cb` as a side effect for each emission.
296
+ *
297
+ * The callback `cb` is called exactly once per parent emission, regardless of how many listeners are attached to the returned emitter.
298
+ * All listeners attached to the returned emitter receive the same values as the parent emitter.
299
+ *
300
+ * *Note*, the side effect `cb` is only invoked when there is at least one listener attached to the returned emitter
301
+ *
302
+ * @param cb A function to be called as a side effect for each value emitted by the parent emitter.
303
+ * @returns A new emitter that forwards all values from the parent, invoking `cb` as a side effect.
304
+ */
305
+ tap(cb) {
306
+ const listen = this.transform((value, emit) => {
307
+ cb(value);
308
+ emit(value);
309
+ });
310
+ return new EventEmitter(listen);
311
+ }
239
312
  }
240
313
  /**
241
314
  * Creates a linked Emitter and emit() pair
@@ -298,3 +371,13 @@ export function createListenable(onAddFirst, onRemoveLast) {
298
371
  emit: (value) => handlers.forEach(h => h.fn(value)),
299
372
  };
300
373
  }
374
+ export function interval(t) {
375
+ let intervalId = null;
376
+ let idx = 0;
377
+ const { emit, listen } = createListenable(() => {
378
+ intervalId = setInterval(() => {
379
+ emit(idx++);
380
+ }, typeof t == "number" ? t : t.asMilliseconds);
381
+ }, () => clearInterval(intervalId));
382
+ return new EventEmitter(listen);
383
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtia/jel",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "repository": {
5
5
  "url": "https://github.com/tiadrop/jel-ts",
6
6
  "type": "github"