tagu-tagu 2.0.1 → 2.1.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tagu-tagu",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "A lightweight helper for vanilla `HTMLElement`, with reactivity.",
5
5
  "keywords": [
6
6
  "vanilla",
package/src/Modify.ts CHANGED
@@ -11,6 +11,7 @@ type EventListenerType<
11
11
  type EventListenerRecord<TEventType2Event> = {
12
12
  [TEventType in keyof TEventType2Event]?:
13
13
  | EventListenerType<TEventType2Event, TEventType>
14
+ | Binding
14
15
  | {
15
16
  listener: EventListenerType<TEventType2Event, TEventType>;
16
17
  options: boolean | AddEventListenerOptions;
@@ -170,6 +171,13 @@ function initializeEventListeners<TEventType2Event>(
170
171
  if (!listener) continue;
171
172
  if (typeof listener === "function") {
172
173
  element.addEventListener(eventName, listener as EventListener);
174
+ } else if (listener instanceof Binding) {
175
+ waitForData(element, {
176
+ [listener.key]: (data) => {
177
+ const func = listener.map(data);
178
+ element.addEventListener(eventName, func);
179
+ },
180
+ });
173
181
  } else {
174
182
  element.addEventListener(
175
183
  eventName,
@@ -1,10 +1,15 @@
1
- export function useBinding<T>(key: string, map: (value: T) => string) {
1
+ export function useBinding<T>(key: string): Binding<T, T>;
2
+ export function useBinding<T, TResult>(
3
+ key: string,
4
+ map: (value: T) => TResult,
5
+ ): Binding<T, TResult>;
6
+ export function useBinding<T>(key: string, map = (value: T) => value) {
2
7
  return new Binding(key, map);
3
8
  }
4
9
 
5
- export class Binding<T = any> {
10
+ export class Binding<T = any, TResult = any> {
6
11
  constructor(
7
12
  public key: string,
8
- public map: (value: T) => string,
13
+ public map: (value: T) => TResult,
9
14
  ) {}
10
15
  }