tagu-tagu 1.0.7 → 2.0.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": "1.0.7",
3
+ "version": "2.0.0",
4
4
  "description": "A lightweight helper for vanilla `HTMLElement`, with reactivity.",
5
5
  "keywords": [
6
6
  "vanilla",
package/src/Modify.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type DataRecord, initializeData } from "./data/data";
1
+ import { type DataRecord, initializeData, waitForData } from "./data/data";
2
2
  import { Binding } from "./data/useBinding";
3
3
  import { type ChildType, initializeChildBlock } from "./initializeChildBlock";
4
4
  import { FromStates, State } from "./State";
@@ -65,7 +65,7 @@ function applyStringOrStateOrBinding(
65
65
  initialize: (text: string) => void,
66
66
  ) {
67
67
  if (value instanceof Binding) {
68
- initializeData(element, {
68
+ waitForData(element, {
69
69
  [value.key]: (data: any) => {
70
70
  const isState = data instanceof State;
71
71
  const stringOrState = isState
package/src/data/data.ts CHANGED
@@ -81,45 +81,21 @@ function resolveCallbacksByData(
81
81
  }
82
82
  }
83
83
 
84
- export type DataRecord = Record<string, DataCallback | any>;
84
+ export type DataRecord = Record<string, any>;
85
85
  export const nodeData = new NodeData();
86
86
  export function initializeData(element: Node, data: DataRecord | undefined) {
87
- nodeData.addCallbacks(element, extractCallbackRecord(data));
88
- nodeData.setDataRecord(element, extractDataValueRecord(data));
87
+ nodeData.setDataRecord(element, data);
89
88
  }
90
89
 
91
90
  export function extractCallbackRecord(
92
- record: DataRecord | undefined,
91
+ record: Record<string, DataCallback> | undefined,
93
92
  ): Record<string, DataCallback[]> | undefined {
94
- return extractRecordFromDataRecord(
95
- record,
96
- (value) => typeof value === "function",
97
- (callback) => [callback],
98
- );
99
- }
100
-
101
- export function extractDataValueRecord(
102
- record: DataRecord | undefined,
103
- ): Record<string, any> | undefined {
104
- return extractRecordFromDataRecord(
105
- record,
106
- (value) => typeof value !== "function",
107
- );
108
- }
109
-
110
- function extractRecordFromDataRecord(
111
- record: DataRecord | undefined,
112
- predicate: (value: any) => boolean,
113
- map = (value: any) => value,
114
- ) {
115
93
  if (!record) return;
116
94
 
117
95
  const result = {} as Record<string, any>;
118
96
  for (const key in record) {
119
97
  const value = record[key];
120
- if (predicate(value)) {
121
- result[key] = map(value);
122
- }
98
+ result[key] = [value];
123
99
  }
124
100
 
125
101
  if (!Object.keys(result).length) return;
@@ -127,16 +103,6 @@ function extractRecordFromDataRecord(
127
103
  return result;
128
104
  }
129
105
 
130
- export function createDescendantCallbacks(
131
- record: Record<string, DataCallback> | undefined,
132
- ) {
133
- return extractRecordFromDataRecord(
134
- record,
135
- () => true,
136
- (value) => [value],
137
- );
138
- }
139
-
140
106
  export function appendCallbacksRecord(
141
107
  record1: Record<string, DataCallback[]>,
142
108
  record2: Record<string, DataCallback[]> | undefined,
@@ -158,3 +124,10 @@ export function findData(node: Node | null, key: string) {
158
124
  }
159
125
  return findData(node.parentElement, key);
160
126
  }
127
+
128
+ export function waitForData(
129
+ node: Node,
130
+ callbackRecord: Record<string, (data: any) => void>,
131
+ ) {
132
+ nodeData.addCallbacks(node, extractCallbackRecord(callbackRecord));
133
+ }