@nativefragments/signals 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Native Fragments contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Native Fragments Signals
2
+
3
+ Optional signal-based reactive state helpers for Native Fragments apps.
4
+
5
+ Core stays dependency-free. Add this package when a component or island needs
6
+ client-side state that updates DOM bindings without a build step.
7
+
8
+ ```sh
9
+ npm i @nativefragments/signals
10
+ ```
11
+
12
+ For no-build browser usage, copy the public helpers into your app:
13
+
14
+ ```sh
15
+ cp node_modules/@nativefragments/signals/public/nativefragments/*.js public/nativefragments/
16
+ ```
17
+
18
+ Then import the browser module:
19
+
20
+ ```js
21
+ import {
22
+ bindText,
23
+ computed,
24
+ effect,
25
+ state
26
+ } from "/nativefragments/signals.js";
27
+
28
+ const count = state(0);
29
+ const doubled = computed(() => count.get() * 2);
30
+
31
+ bindText(document.querySelector("[data-count]"), () => doubled.get());
32
+ document.querySelector("button").addEventListener("click", () => {
33
+ count.set(count.get() + 1);
34
+ });
35
+
36
+ effect(() => {
37
+ console.log("count changed", count.get());
38
+ });
39
+ ```
40
+
41
+ Bundled or server-side code can import from the package:
42
+
43
+ ```js
44
+ import { state, computed } from "@nativefragments/signals";
45
+ ```
46
+
47
+ ## API
48
+
49
+ - `state(initial, options)`: create a writable `Signal.State`.
50
+ - `computed(callback, options)`: create a `Signal.Computed`.
51
+ - `effect(callback)`: run a microtask-batched reactive side effect.
52
+ - `read(value)`: read a raw value, signal, or function.
53
+ - `bindText(node, value)`: bind text content.
54
+ - `bindHTML(element, value)`: bind trusted HTML.
55
+ - `bindAttr(element, name, value)`: bind an attribute.
56
+ - `bindProperty(element, property, value)`: bind an object property.
57
+ - `bindClass(element, name, value)`: toggle a class.
58
+ - `bindStyle(element, name, value)`: bind a style property.
59
+ - `model(element, signal, eventName)`: two-way bind a form value.
60
+
61
+ ## Agent Skill
62
+
63
+ Agents can read the shipped conventions from:
64
+
65
+ ```sh
66
+ node_modules/@nativefragments/signals/skills/nativefragments-signals/SKILL.md
67
+ ```
@@ -0,0 +1,13 @@
1
+ # Third Party Notices
2
+
3
+ This package includes a browser-copyable build of `signal-polyfill` for
4
+ zero-build apps.
5
+
6
+ ## signal-polyfill
7
+
8
+ - Package: `signal-polyfill`
9
+ - Version: `0.2.2`
10
+ - License: Apache-2.0
11
+ - Repository: https://github.com/proposal-signals/signal-polyfill
12
+
13
+ The full license text is available in the `signal-polyfill` npm package.
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@nativefragments/signals",
3
+ "version": "0.1.0",
4
+ "description": "Signal-based reactive state helpers for Native Fragments apps.",
5
+ "type": "module",
6
+ "main": "./src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js",
9
+ "./client/signals.js": "./public/nativefragments/signals.js",
10
+ "./client/signal-polyfill.js": "./public/nativefragments/signal-polyfill.js",
11
+ "./skills/nativefragments-signals": "./skills/nativefragments-signals/SKILL.md"
12
+ },
13
+ "files": [
14
+ "src",
15
+ "public/nativefragments",
16
+ "skills",
17
+ "scripts",
18
+ "third-party",
19
+ "README.md",
20
+ "LICENSE",
21
+ "THIRD_PARTY_NOTICES.md"
22
+ ],
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/somedudeokay/nativefragments-signals.git"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/somedudeokay/nativefragments-signals/issues"
29
+ },
30
+ "homepage": "https://nativefragments.org/docs/signals",
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "check": "find src public/nativefragments scripts -name '*.js' -print0 | xargs -0 -n1 node --check",
36
+ "smoke": "node scripts/smoke.mjs"
37
+ },
38
+ "keywords": [
39
+ "signals",
40
+ "nativefragments",
41
+ "web-components",
42
+ "zero-build",
43
+ "ai-friendly-apps"
44
+ ],
45
+ "dependencies": {
46
+ "signal-polyfill": "^0.2.2"
47
+ },
48
+ "license": "MIT"
49
+ }
@@ -0,0 +1,583 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+ var __accessCheck = (obj, member, msg) => {
8
+ if (!member.has(obj))
9
+ throw TypeError("Cannot " + msg);
10
+ };
11
+ var __privateIn = (member, obj) => {
12
+ if (Object(obj) !== obj)
13
+ throw TypeError('Cannot use the "in" operator on this value');
14
+ return member.has(obj);
15
+ };
16
+ var __privateAdd = (obj, member, value) => {
17
+ if (member.has(obj))
18
+ throw TypeError("Cannot add the same private member more than once");
19
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
20
+ };
21
+ var __privateMethod = (obj, member, method) => {
22
+ __accessCheck(obj, member, "access private method");
23
+ return method;
24
+ };
25
+ /**
26
+ * @license
27
+ * Copyright Google LLC All Rights Reserved.
28
+ *
29
+ * Use of this source code is governed by an MIT-style license that can be
30
+ * found in the LICENSE file at https://angular.io/license
31
+ */
32
+ function defaultEquals(a, b) {
33
+ return Object.is(a, b);
34
+ }
35
+ /**
36
+ * @license
37
+ * Copyright Google LLC All Rights Reserved.
38
+ *
39
+ * Use of this source code is governed by an MIT-style license that can be
40
+ * found in the LICENSE file at https://angular.io/license
41
+ */
42
+ let activeConsumer = null;
43
+ let inNotificationPhase = false;
44
+ let epoch = 1;
45
+ const SIGNAL = /* @__PURE__ */ Symbol("SIGNAL");
46
+ function setActiveConsumer(consumer) {
47
+ const prev = activeConsumer;
48
+ activeConsumer = consumer;
49
+ return prev;
50
+ }
51
+ function getActiveConsumer() {
52
+ return activeConsumer;
53
+ }
54
+ function isInNotificationPhase() {
55
+ return inNotificationPhase;
56
+ }
57
+ const REACTIVE_NODE = {
58
+ version: 0,
59
+ lastCleanEpoch: 0,
60
+ dirty: false,
61
+ producerNode: void 0,
62
+ producerLastReadVersion: void 0,
63
+ producerIndexOfThis: void 0,
64
+ nextProducerIndex: 0,
65
+ liveConsumerNode: void 0,
66
+ liveConsumerIndexOfThis: void 0,
67
+ consumerAllowSignalWrites: false,
68
+ consumerIsAlwaysLive: false,
69
+ producerMustRecompute: () => false,
70
+ producerRecomputeValue: () => {
71
+ },
72
+ consumerMarkedDirty: () => {
73
+ },
74
+ consumerOnSignalRead: () => {
75
+ }
76
+ };
77
+ function producerAccessed(node) {
78
+ if (inNotificationPhase) {
79
+ throw new Error(
80
+ typeof ngDevMode !== "undefined" && ngDevMode ? `Assertion error: signal read during notification phase` : ""
81
+ );
82
+ }
83
+ if (activeConsumer === null) {
84
+ return;
85
+ }
86
+ activeConsumer.consumerOnSignalRead(node);
87
+ const idx = activeConsumer.nextProducerIndex++;
88
+ assertConsumerNode(activeConsumer);
89
+ if (idx < activeConsumer.producerNode.length && activeConsumer.producerNode[idx] !== node) {
90
+ if (consumerIsLive(activeConsumer)) {
91
+ const staleProducer = activeConsumer.producerNode[idx];
92
+ producerRemoveLiveConsumerAtIndex(staleProducer, activeConsumer.producerIndexOfThis[idx]);
93
+ }
94
+ }
95
+ if (activeConsumer.producerNode[idx] !== node) {
96
+ activeConsumer.producerNode[idx] = node;
97
+ activeConsumer.producerIndexOfThis[idx] = consumerIsLive(activeConsumer) ? producerAddLiveConsumer(node, activeConsumer, idx) : 0;
98
+ }
99
+ activeConsumer.producerLastReadVersion[idx] = node.version;
100
+ }
101
+ function producerIncrementEpoch() {
102
+ epoch++;
103
+ }
104
+ function producerUpdateValueVersion(node) {
105
+ if (!node.dirty && node.lastCleanEpoch === epoch) {
106
+ return;
107
+ }
108
+ if (!node.producerMustRecompute(node) && !consumerPollProducersForChange(node)) {
109
+ node.dirty = false;
110
+ node.lastCleanEpoch = epoch;
111
+ return;
112
+ }
113
+ node.producerRecomputeValue(node);
114
+ node.dirty = false;
115
+ node.lastCleanEpoch = epoch;
116
+ }
117
+ function producerNotifyConsumers(node) {
118
+ if (node.liveConsumerNode === void 0) {
119
+ return;
120
+ }
121
+ const prev = inNotificationPhase;
122
+ inNotificationPhase = true;
123
+ try {
124
+ for (const consumer of node.liveConsumerNode) {
125
+ if (!consumer.dirty) {
126
+ consumerMarkDirty(consumer);
127
+ }
128
+ }
129
+ } finally {
130
+ inNotificationPhase = prev;
131
+ }
132
+ }
133
+ function producerUpdatesAllowed() {
134
+ return (activeConsumer == null ? void 0 : activeConsumer.consumerAllowSignalWrites) !== false;
135
+ }
136
+ function consumerMarkDirty(node) {
137
+ var _a;
138
+ node.dirty = true;
139
+ producerNotifyConsumers(node);
140
+ (_a = node.consumerMarkedDirty) == null ? void 0 : _a.call(node.wrapper ?? node);
141
+ }
142
+ function consumerBeforeComputation(node) {
143
+ node && (node.nextProducerIndex = 0);
144
+ return setActiveConsumer(node);
145
+ }
146
+ function consumerAfterComputation(node, prevConsumer) {
147
+ setActiveConsumer(prevConsumer);
148
+ if (!node || node.producerNode === void 0 || node.producerIndexOfThis === void 0 || node.producerLastReadVersion === void 0) {
149
+ return;
150
+ }
151
+ if (consumerIsLive(node)) {
152
+ for (let i = node.nextProducerIndex; i < node.producerNode.length; i++) {
153
+ producerRemoveLiveConsumerAtIndex(node.producerNode[i], node.producerIndexOfThis[i]);
154
+ }
155
+ }
156
+ while (node.producerNode.length > node.nextProducerIndex) {
157
+ node.producerNode.pop();
158
+ node.producerLastReadVersion.pop();
159
+ node.producerIndexOfThis.pop();
160
+ }
161
+ }
162
+ function consumerPollProducersForChange(node) {
163
+ assertConsumerNode(node);
164
+ for (let i = 0; i < node.producerNode.length; i++) {
165
+ const producer = node.producerNode[i];
166
+ const seenVersion = node.producerLastReadVersion[i];
167
+ if (seenVersion !== producer.version) {
168
+ return true;
169
+ }
170
+ producerUpdateValueVersion(producer);
171
+ if (seenVersion !== producer.version) {
172
+ return true;
173
+ }
174
+ }
175
+ return false;
176
+ }
177
+ function producerAddLiveConsumer(node, consumer, indexOfThis) {
178
+ var _a;
179
+ assertProducerNode(node);
180
+ assertConsumerNode(node);
181
+ if (node.liveConsumerNode.length === 0) {
182
+ (_a = node.watched) == null ? void 0 : _a.call(node.wrapper);
183
+ for (let i = 0; i < node.producerNode.length; i++) {
184
+ node.producerIndexOfThis[i] = producerAddLiveConsumer(node.producerNode[i], node, i);
185
+ }
186
+ }
187
+ node.liveConsumerIndexOfThis.push(indexOfThis);
188
+ return node.liveConsumerNode.push(consumer) - 1;
189
+ }
190
+ function producerRemoveLiveConsumerAtIndex(node, idx) {
191
+ var _a;
192
+ assertProducerNode(node);
193
+ assertConsumerNode(node);
194
+ if (typeof ngDevMode !== "undefined" && ngDevMode && idx >= node.liveConsumerNode.length) {
195
+ throw new Error(
196
+ `Assertion error: active consumer index ${idx} is out of bounds of ${node.liveConsumerNode.length} consumers)`
197
+ );
198
+ }
199
+ if (node.liveConsumerNode.length === 1) {
200
+ (_a = node.unwatched) == null ? void 0 : _a.call(node.wrapper);
201
+ for (let i = 0; i < node.producerNode.length; i++) {
202
+ producerRemoveLiveConsumerAtIndex(node.producerNode[i], node.producerIndexOfThis[i]);
203
+ }
204
+ }
205
+ const lastIdx = node.liveConsumerNode.length - 1;
206
+ node.liveConsumerNode[idx] = node.liveConsumerNode[lastIdx];
207
+ node.liveConsumerIndexOfThis[idx] = node.liveConsumerIndexOfThis[lastIdx];
208
+ node.liveConsumerNode.length--;
209
+ node.liveConsumerIndexOfThis.length--;
210
+ if (idx < node.liveConsumerNode.length) {
211
+ const idxProducer = node.liveConsumerIndexOfThis[idx];
212
+ const consumer = node.liveConsumerNode[idx];
213
+ assertConsumerNode(consumer);
214
+ consumer.producerIndexOfThis[idxProducer] = idx;
215
+ }
216
+ }
217
+ function consumerIsLive(node) {
218
+ var _a;
219
+ return node.consumerIsAlwaysLive || (((_a = node == null ? void 0 : node.liveConsumerNode) == null ? void 0 : _a.length) ?? 0) > 0;
220
+ }
221
+ function assertConsumerNode(node) {
222
+ node.producerNode ?? (node.producerNode = []);
223
+ node.producerIndexOfThis ?? (node.producerIndexOfThis = []);
224
+ node.producerLastReadVersion ?? (node.producerLastReadVersion = []);
225
+ }
226
+ function assertProducerNode(node) {
227
+ node.liveConsumerNode ?? (node.liveConsumerNode = []);
228
+ node.liveConsumerIndexOfThis ?? (node.liveConsumerIndexOfThis = []);
229
+ }
230
+ /**
231
+ * @license
232
+ * Copyright Google LLC All Rights Reserved.
233
+ *
234
+ * Use of this source code is governed by an MIT-style license that can be
235
+ * found in the LICENSE file at https://angular.io/license
236
+ */
237
+ function computedGet(node) {
238
+ producerUpdateValueVersion(node);
239
+ producerAccessed(node);
240
+ if (node.value === ERRORED) {
241
+ throw node.error;
242
+ }
243
+ return node.value;
244
+ }
245
+ function createComputed(computation) {
246
+ const node = Object.create(COMPUTED_NODE);
247
+ node.computation = computation;
248
+ const computed = () => computedGet(node);
249
+ computed[SIGNAL] = node;
250
+ return computed;
251
+ }
252
+ const UNSET = /* @__PURE__ */ Symbol("UNSET");
253
+ const COMPUTING = /* @__PURE__ */ Symbol("COMPUTING");
254
+ const ERRORED = /* @__PURE__ */ Symbol("ERRORED");
255
+ const COMPUTED_NODE = /* @__PURE__ */ (() => {
256
+ return {
257
+ ...REACTIVE_NODE,
258
+ value: UNSET,
259
+ dirty: true,
260
+ error: null,
261
+ equal: defaultEquals,
262
+ producerMustRecompute(node) {
263
+ return node.value === UNSET || node.value === COMPUTING;
264
+ },
265
+ producerRecomputeValue(node) {
266
+ if (node.value === COMPUTING) {
267
+ throw new Error("Detected cycle in computations.");
268
+ }
269
+ const oldValue = node.value;
270
+ node.value = COMPUTING;
271
+ const prevConsumer = consumerBeforeComputation(node);
272
+ let newValue;
273
+ let wasEqual = false;
274
+ try {
275
+ newValue = node.computation.call(node.wrapper);
276
+ const oldOk = oldValue !== UNSET && oldValue !== ERRORED;
277
+ wasEqual = oldOk && node.equal.call(node.wrapper, oldValue, newValue);
278
+ } catch (err) {
279
+ newValue = ERRORED;
280
+ node.error = err;
281
+ } finally {
282
+ consumerAfterComputation(node, prevConsumer);
283
+ }
284
+ if (wasEqual) {
285
+ node.value = oldValue;
286
+ return;
287
+ }
288
+ node.value = newValue;
289
+ node.version++;
290
+ }
291
+ };
292
+ })();
293
+ /**
294
+ * @license
295
+ * Copyright Google LLC All Rights Reserved.
296
+ *
297
+ * Use of this source code is governed by an MIT-style license that can be
298
+ * found in the LICENSE file at https://angular.io/license
299
+ */
300
+ function defaultThrowError() {
301
+ throw new Error();
302
+ }
303
+ let throwInvalidWriteToSignalErrorFn = defaultThrowError;
304
+ function throwInvalidWriteToSignalError() {
305
+ throwInvalidWriteToSignalErrorFn();
306
+ }
307
+ /**
308
+ * @license
309
+ * Copyright Google LLC All Rights Reserved.
310
+ *
311
+ * Use of this source code is governed by an MIT-style license that can be
312
+ * found in the LICENSE file at https://angular.io/license
313
+ */
314
+ function createSignal(initialValue) {
315
+ const node = Object.create(SIGNAL_NODE);
316
+ node.value = initialValue;
317
+ const getter = () => {
318
+ producerAccessed(node);
319
+ return node.value;
320
+ };
321
+ getter[SIGNAL] = node;
322
+ return getter;
323
+ }
324
+ function signalGetFn() {
325
+ producerAccessed(this);
326
+ return this.value;
327
+ }
328
+ function signalSetFn(node, newValue) {
329
+ if (!producerUpdatesAllowed()) {
330
+ throwInvalidWriteToSignalError();
331
+ }
332
+ if (!node.equal.call(node.wrapper, node.value, newValue)) {
333
+ node.value = newValue;
334
+ signalValueChanged(node);
335
+ }
336
+ }
337
+ const SIGNAL_NODE = /* @__PURE__ */ (() => {
338
+ return {
339
+ ...REACTIVE_NODE,
340
+ equal: defaultEquals,
341
+ value: void 0
342
+ };
343
+ })();
344
+ function signalValueChanged(node) {
345
+ node.version++;
346
+ producerIncrementEpoch();
347
+ producerNotifyConsumers(node);
348
+ }
349
+ /**
350
+ * @license
351
+ * Copyright 2024 Bloomberg Finance L.P.
352
+ *
353
+ * Licensed under the Apache License, Version 2.0 (the "License");
354
+ * you may not use this file except in compliance with the License.
355
+ * You may obtain a copy of the License at
356
+ *
357
+ * http://www.apache.org/licenses/LICENSE-2.0
358
+ *
359
+ * Unless required by applicable law or agreed to in writing, software
360
+ * distributed under the License is distributed on an "AS IS" BASIS,
361
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
362
+ * See the License for the specific language governing permissions and
363
+ * limitations under the License.
364
+ */
365
+ const NODE = Symbol("node");
366
+ var Signal;
367
+ ((Signal2) => {
368
+ var _a, _brand, brand_fn, _b, _brand2, brand_fn2;
369
+ class State {
370
+ constructor(initialValue, options = {}) {
371
+ __privateAdd(this, _brand);
372
+ __publicField(this, _a);
373
+ const ref = createSignal(initialValue);
374
+ const node = ref[SIGNAL];
375
+ this[NODE] = node;
376
+ node.wrapper = this;
377
+ if (options) {
378
+ const equals = options.equals;
379
+ if (equals) {
380
+ node.equal = equals;
381
+ }
382
+ node.watched = options[Signal2.subtle.watched];
383
+ node.unwatched = options[Signal2.subtle.unwatched];
384
+ }
385
+ }
386
+ get() {
387
+ if (!(0, Signal2.isState)(this))
388
+ throw new TypeError("Wrong receiver type for Signal.State.prototype.get");
389
+ return signalGetFn.call(this[NODE]);
390
+ }
391
+ set(newValue) {
392
+ if (!(0, Signal2.isState)(this))
393
+ throw new TypeError("Wrong receiver type for Signal.State.prototype.set");
394
+ if (isInNotificationPhase()) {
395
+ throw new Error("Writes to signals not permitted during Watcher callback");
396
+ }
397
+ const ref = this[NODE];
398
+ signalSetFn(ref, newValue);
399
+ }
400
+ }
401
+ _a = NODE;
402
+ _brand = new WeakSet();
403
+ brand_fn = function() {
404
+ };
405
+ Signal2.isState = (s) => typeof s === "object" && __privateIn(_brand, s);
406
+ Signal2.State = State;
407
+ class Computed {
408
+ // Create a Signal which evaluates to the value returned by the callback.
409
+ // Callback is called with this signal as the parameter.
410
+ constructor(computation, options) {
411
+ __privateAdd(this, _brand2);
412
+ __publicField(this, _b);
413
+ const ref = createComputed(computation);
414
+ const node = ref[SIGNAL];
415
+ node.consumerAllowSignalWrites = true;
416
+ this[NODE] = node;
417
+ node.wrapper = this;
418
+ if (options) {
419
+ const equals = options.equals;
420
+ if (equals) {
421
+ node.equal = equals;
422
+ }
423
+ node.watched = options[Signal2.subtle.watched];
424
+ node.unwatched = options[Signal2.subtle.unwatched];
425
+ }
426
+ }
427
+ get() {
428
+ if (!(0, Signal2.isComputed)(this))
429
+ throw new TypeError("Wrong receiver type for Signal.Computed.prototype.get");
430
+ return computedGet(this[NODE]);
431
+ }
432
+ }
433
+ _b = NODE;
434
+ _brand2 = new WeakSet();
435
+ brand_fn2 = function() {
436
+ };
437
+ Signal2.isComputed = (c) => typeof c === "object" && __privateIn(_brand2, c);
438
+ Signal2.Computed = Computed;
439
+ ((subtle2) => {
440
+ var _a2, _brand3, brand_fn3, _assertSignals, assertSignals_fn;
441
+ function untrack(cb) {
442
+ let output;
443
+ let prevActiveConsumer = null;
444
+ try {
445
+ prevActiveConsumer = setActiveConsumer(null);
446
+ output = cb();
447
+ } finally {
448
+ setActiveConsumer(prevActiveConsumer);
449
+ }
450
+ return output;
451
+ }
452
+ subtle2.untrack = untrack;
453
+ function introspectSources(sink) {
454
+ var _a3;
455
+ if (!(0, Signal2.isComputed)(sink) && !(0, Signal2.isWatcher)(sink)) {
456
+ throw new TypeError("Called introspectSources without a Computed or Watcher argument");
457
+ }
458
+ return ((_a3 = sink[NODE].producerNode) == null ? void 0 : _a3.map((n) => n.wrapper)) ?? [];
459
+ }
460
+ subtle2.introspectSources = introspectSources;
461
+ function introspectSinks(signal) {
462
+ var _a3;
463
+ if (!(0, Signal2.isComputed)(signal) && !(0, Signal2.isState)(signal)) {
464
+ throw new TypeError("Called introspectSinks without a Signal argument");
465
+ }
466
+ return ((_a3 = signal[NODE].liveConsumerNode) == null ? void 0 : _a3.map((n) => n.wrapper)) ?? [];
467
+ }
468
+ subtle2.introspectSinks = introspectSinks;
469
+ function hasSinks(signal) {
470
+ if (!(0, Signal2.isComputed)(signal) && !(0, Signal2.isState)(signal)) {
471
+ throw new TypeError("Called hasSinks without a Signal argument");
472
+ }
473
+ const liveConsumerNode = signal[NODE].liveConsumerNode;
474
+ if (!liveConsumerNode)
475
+ return false;
476
+ return liveConsumerNode.length > 0;
477
+ }
478
+ subtle2.hasSinks = hasSinks;
479
+ function hasSources(signal) {
480
+ if (!(0, Signal2.isComputed)(signal) && !(0, Signal2.isWatcher)(signal)) {
481
+ throw new TypeError("Called hasSources without a Computed or Watcher argument");
482
+ }
483
+ const producerNode = signal[NODE].producerNode;
484
+ if (!producerNode)
485
+ return false;
486
+ return producerNode.length > 0;
487
+ }
488
+ subtle2.hasSources = hasSources;
489
+ class Watcher {
490
+ // When a (recursive) source of Watcher is written to, call this callback,
491
+ // if it hasn't already been called since the last `watch` call.
492
+ // No signals may be read or written during the notify.
493
+ constructor(notify) {
494
+ __privateAdd(this, _brand3);
495
+ __privateAdd(this, _assertSignals);
496
+ __publicField(this, _a2);
497
+ let node = Object.create(REACTIVE_NODE);
498
+ node.wrapper = this;
499
+ node.consumerMarkedDirty = notify;
500
+ node.consumerIsAlwaysLive = true;
501
+ node.consumerAllowSignalWrites = false;
502
+ node.producerNode = [];
503
+ this[NODE] = node;
504
+ }
505
+ // Add these signals to the Watcher's set, and set the watcher to run its
506
+ // notify callback next time any signal in the set (or one of its dependencies) changes.
507
+ // Can be called with no arguments just to reset the "notified" state, so that
508
+ // the notify callback will be invoked again.
509
+ watch(...signals) {
510
+ if (!(0, Signal2.isWatcher)(this)) {
511
+ throw new TypeError("Called unwatch without Watcher receiver");
512
+ }
513
+ __privateMethod(this, _assertSignals, assertSignals_fn).call(this, signals);
514
+ const node = this[NODE];
515
+ node.dirty = false;
516
+ const prev = setActiveConsumer(node);
517
+ for (const signal of signals) {
518
+ producerAccessed(signal[NODE]);
519
+ }
520
+ setActiveConsumer(prev);
521
+ }
522
+ // Remove these signals from the watched set (e.g., for an effect which is disposed)
523
+ unwatch(...signals) {
524
+ if (!(0, Signal2.isWatcher)(this)) {
525
+ throw new TypeError("Called unwatch without Watcher receiver");
526
+ }
527
+ __privateMethod(this, _assertSignals, assertSignals_fn).call(this, signals);
528
+ const node = this[NODE];
529
+ assertConsumerNode(node);
530
+ for (let i = node.producerNode.length - 1; i >= 0; i--) {
531
+ if (signals.includes(node.producerNode[i].wrapper)) {
532
+ producerRemoveLiveConsumerAtIndex(node.producerNode[i], node.producerIndexOfThis[i]);
533
+ const lastIdx = node.producerNode.length - 1;
534
+ node.producerNode[i] = node.producerNode[lastIdx];
535
+ node.producerIndexOfThis[i] = node.producerIndexOfThis[lastIdx];
536
+ node.producerNode.length--;
537
+ node.producerIndexOfThis.length--;
538
+ node.nextProducerIndex--;
539
+ if (i < node.producerNode.length) {
540
+ const idxConsumer = node.producerIndexOfThis[i];
541
+ const producer = node.producerNode[i];
542
+ assertProducerNode(producer);
543
+ producer.liveConsumerIndexOfThis[idxConsumer] = i;
544
+ }
545
+ }
546
+ }
547
+ }
548
+ // Returns the set of computeds in the Watcher's set which are still yet
549
+ // to be re-evaluated
550
+ getPending() {
551
+ if (!(0, Signal2.isWatcher)(this)) {
552
+ throw new TypeError("Called getPending without Watcher receiver");
553
+ }
554
+ const node = this[NODE];
555
+ return node.producerNode.filter((n) => n.dirty).map((n) => n.wrapper);
556
+ }
557
+ }
558
+ _a2 = NODE;
559
+ _brand3 = new WeakSet();
560
+ brand_fn3 = function() {
561
+ };
562
+ _assertSignals = new WeakSet();
563
+ assertSignals_fn = function(signals) {
564
+ for (const signal of signals) {
565
+ if (!(0, Signal2.isComputed)(signal) && !(0, Signal2.isState)(signal)) {
566
+ throw new TypeError("Called watch/unwatch without a Computed or State argument");
567
+ }
568
+ }
569
+ };
570
+ Signal2.isWatcher = (w) => __privateIn(_brand3, w);
571
+ subtle2.Watcher = Watcher;
572
+ function currentComputed() {
573
+ var _a3;
574
+ return (_a3 = getActiveConsumer()) == null ? void 0 : _a3.wrapper;
575
+ }
576
+ subtle2.currentComputed = currentComputed;
577
+ subtle2.watched = Symbol("watched");
578
+ subtle2.unwatched = Symbol("unwatched");
579
+ })(Signal2.subtle || (Signal2.subtle = {}));
580
+ })(Signal || (Signal = {}));
581
+ export {
582
+ Signal
583
+ };
@@ -0,0 +1,119 @@
1
+ import { Signal } from "./signal-polyfill.js";
2
+
3
+ let needsEnqueue = true;
4
+
5
+ const effectWatcher = new Signal.subtle.Watcher(() => {
6
+ if (!needsEnqueue) return;
7
+ needsEnqueue = false;
8
+ queueMicrotask(processPending);
9
+ });
10
+
11
+ const report = (error) => {
12
+ if (typeof globalThis.reportError === "function") {
13
+ globalThis.reportError(error);
14
+ } else {
15
+ setTimeout(() => {
16
+ throw error;
17
+ });
18
+ }
19
+ };
20
+
21
+ function processPending() {
22
+ needsEnqueue = true;
23
+ const errors = [];
24
+
25
+ for (const signal of effectWatcher.getPending()) {
26
+ try {
27
+ signal.get();
28
+ } catch (error) {
29
+ errors.push(error);
30
+ }
31
+ }
32
+
33
+ effectWatcher.watch();
34
+ for (const error of errors) report(error);
35
+ }
36
+
37
+ export const state = (initial, options) => new Signal.State(initial, options);
38
+
39
+ export const computed = (callback, options) => new Signal.Computed(callback, options);
40
+
41
+ export const isSignal = (value) =>
42
+ Signal.isState(value) || Signal.isComputed(value);
43
+
44
+ export const read = (value) => {
45
+ if (isSignal(value)) return value.get();
46
+ if (typeof value === "function") return value();
47
+ return value;
48
+ };
49
+
50
+ export const effect = (callback) => {
51
+ let cleanup;
52
+
53
+ const computation = new Signal.Computed(() => {
54
+ if (typeof cleanup === "function") cleanup();
55
+ cleanup = callback();
56
+ });
57
+
58
+ effectWatcher.watch(computation);
59
+ computation.get();
60
+
61
+ return () => {
62
+ effectWatcher.unwatch(computation);
63
+ if (typeof cleanup === "function") cleanup();
64
+ cleanup = undefined;
65
+ };
66
+ };
67
+
68
+ export const bindText = (node, value) =>
69
+ effect(() => {
70
+ node.textContent = read(value) ?? "";
71
+ });
72
+
73
+ export const bindHTML = (element, value) =>
74
+ effect(() => {
75
+ element.innerHTML = read(value) ?? "";
76
+ });
77
+
78
+ export const bindAttr = (element, name, value) =>
79
+ effect(() => {
80
+ const next = read(value);
81
+ if (next === false || next === null || next === undefined) {
82
+ element.removeAttribute(name);
83
+ } else {
84
+ element.setAttribute(name, next === true ? "" : String(next));
85
+ }
86
+ });
87
+
88
+ export const bindProperty = (element, property, value) =>
89
+ effect(() => {
90
+ element[property] = read(value);
91
+ });
92
+
93
+ export const bindClass = (element, name, value) =>
94
+ effect(() => {
95
+ element.classList.toggle(name, Boolean(read(value)));
96
+ });
97
+
98
+ export const bindStyle = (element, name, value) =>
99
+ effect(() => {
100
+ const next = read(value);
101
+ if (next === false || next === null || next === undefined) {
102
+ element.style.removeProperty(name);
103
+ } else {
104
+ element.style.setProperty(name, String(next));
105
+ }
106
+ });
107
+
108
+ export const model = (element, signal, eventName = "input") => {
109
+ const stopBinding = bindProperty(element, "value", signal);
110
+ const update = () => signal.set(element.value);
111
+ element.addEventListener(eventName, update);
112
+
113
+ return () => {
114
+ stopBinding();
115
+ element.removeEventListener(eventName, update);
116
+ };
117
+ };
118
+
119
+ export { Signal };
@@ -0,0 +1,54 @@
1
+ import {
2
+ bindAttr,
3
+ bindClass,
4
+ computed,
5
+ effect,
6
+ state,
7
+ } from "../src/index.js";
8
+
9
+ const count = state(1);
10
+ const doubled = computed(() => count.get() * 2);
11
+ let latest = 0;
12
+
13
+ const stop = effect(() => {
14
+ latest = doubled.get();
15
+ });
16
+
17
+ if (latest !== 2) throw new Error("initial effect failed");
18
+ count.set(3);
19
+ await new Promise((resolve) => setTimeout(resolve, 0));
20
+ if (latest !== 6) throw new Error("reactive effect failed");
21
+ stop();
22
+
23
+ const attributes = new Map();
24
+ const element = {
25
+ classList: {
26
+ values: new Set(),
27
+ toggle(name, enabled) {
28
+ if (enabled) this.values.add(name);
29
+ else this.values.delete(name);
30
+ },
31
+ },
32
+ removeAttribute(name) {
33
+ attributes.delete(name);
34
+ },
35
+ setAttribute(name, value) {
36
+ attributes.set(name, value);
37
+ },
38
+ };
39
+
40
+ const enabled = state(false);
41
+ const stopClass = bindClass(element, "enabled", enabled);
42
+ enabled.set(true);
43
+ await new Promise((resolve) => setTimeout(resolve, 0));
44
+ if (!element.classList.values.has("enabled")) throw new Error("class binding failed");
45
+ stopClass();
46
+
47
+ const label = state("Save");
48
+ const stopAttr = bindAttr(element, "aria-label", label);
49
+ label.set("Saved");
50
+ await new Promise((resolve) => setTimeout(resolve, 0));
51
+ if (attributes.get("aria-label") !== "Saved") throw new Error("attr binding failed");
52
+ stopAttr();
53
+
54
+ console.log("signals smoke ok");
@@ -0,0 +1,67 @@
1
+ ---
2
+ name: nativefragments-signals
3
+ description: Add reactive state to Native Fragments apps with signal-polyfill and no build step.
4
+ ---
5
+
6
+ # Native Fragments Signals Skill
7
+
8
+ Use this skill when a Native Fragments app needs client-side reactive state.
9
+
10
+ ## Rules
11
+
12
+ - Keep `@nativefragments/core` dependency-free. Use this package only where
13
+ client-side reactivity is needed.
14
+ - Prefer `state()` for writable values and `computed()` for derived values.
15
+ - Use DOM binding helpers instead of rerendering whole component trees.
16
+ - Keep effects small. They run immediately and rerun in a microtask when a
17
+ signal read by the effect changes.
18
+ - For first-paint UI, keep server-rendered HTML canonical and use signals only
19
+ to hydrate interactive islands.
20
+
21
+ ## Browser Setup
22
+
23
+ Copy the browser files into the app's public helpers:
24
+
25
+ ```sh
26
+ cp node_modules/@nativefragments/signals/public/nativefragments/*.js public/nativefragments/
27
+ ```
28
+
29
+ Import from the public path:
30
+
31
+ ```js
32
+ import { bindText, computed, state } from "/nativefragments/signals.js";
33
+ ```
34
+
35
+ ## Component Pattern
36
+
37
+ ```js
38
+ import { bindText, computed, state } from "/nativefragments/signals.js";
39
+ import { shadow, sheet } from "/nativefragments/component.js";
40
+
41
+ const styles = sheet(`
42
+ :host { display: inline-grid; gap: 0.5rem; }
43
+ `);
44
+
45
+ class CounterButton extends HTMLElement {
46
+ connectedCallback() {
47
+ const count = state(0);
48
+ const label = computed(() => `Count ${count.get()}`);
49
+ const root = shadow(this, {
50
+ styles: [styles],
51
+ html: `<button type="button"><span data-label></span></button>`
52
+ });
53
+
54
+ bindText(root.querySelector("[data-label]"), label);
55
+ root.querySelector("button").addEventListener("click", () => {
56
+ count.set(count.get() + 1);
57
+ });
58
+ }
59
+ }
60
+
61
+ customElements.define("counter-button", CounterButton);
62
+ ```
63
+
64
+ ## Cleanup
65
+
66
+ Bindings return cleanup functions. Call them in `disconnectedCallback()` when a
67
+ component can be removed and reinserted.
package/src/index.js ADDED
@@ -0,0 +1,217 @@
1
+ import { Signal } from "signal-polyfill";
2
+
3
+ let needsEnqueue = true;
4
+
5
+ const effectWatcher = new Signal.subtle.Watcher(() => {
6
+ if (!needsEnqueue) return;
7
+ needsEnqueue = false;
8
+ queueMicrotask(processPending);
9
+ });
10
+
11
+ const report = (error) => {
12
+ if (typeof globalThis.reportError === "function") {
13
+ globalThis.reportError(error);
14
+ } else {
15
+ setTimeout(() => {
16
+ throw error;
17
+ });
18
+ }
19
+ };
20
+
21
+ function processPending() {
22
+ needsEnqueue = true;
23
+ const errors = [];
24
+
25
+ for (const signal of effectWatcher.getPending()) {
26
+ try {
27
+ signal.get();
28
+ } catch (error) {
29
+ errors.push(error);
30
+ }
31
+ }
32
+
33
+ effectWatcher.watch();
34
+ for (const error of errors) report(error);
35
+ }
36
+
37
+ /**
38
+ * Create a writable signal.
39
+ *
40
+ * @template T
41
+ * @param {T} initial Initial value.
42
+ * @param {object} [options] Signal options passed to `Signal.State`.
43
+ * @returns {Signal.State<T>} Writable signal.
44
+ */
45
+ export const state = (initial, options) => new Signal.State(initial, options);
46
+
47
+ /**
48
+ * Create a computed signal.
49
+ *
50
+ * @template T
51
+ * @param {() => T} callback Computation callback.
52
+ * @param {object} [options] Signal options passed to `Signal.Computed`.
53
+ * @returns {Signal.Computed<T>} Computed signal.
54
+ */
55
+ export const computed = (callback, options) => new Signal.Computed(callback, options);
56
+
57
+ /**
58
+ * Check whether a value is a signal state or computed.
59
+ *
60
+ * @param {unknown} value Value to check.
61
+ * @returns {boolean} Whether the value is a signal.
62
+ */
63
+ export const isSignal = (value) =>
64
+ Signal.isState(value) || Signal.isComputed(value);
65
+
66
+ /**
67
+ * Read a raw value, signal, or value function.
68
+ *
69
+ * @template T
70
+ * @param {T | { get(): T } | (() => T)} value Value source.
71
+ * @returns {T} Current value.
72
+ */
73
+ export const read = (value) => {
74
+ if (isSignal(value)) return value.get();
75
+ if (typeof value === "function") return value();
76
+ return value;
77
+ };
78
+
79
+ /**
80
+ * Run a reactive side effect.
81
+ *
82
+ * The callback runs immediately. Any signals read during the callback are
83
+ * tracked, and the callback reruns in a microtask when they change.
84
+ *
85
+ * @param {() => void | (() => void)} callback Effect callback.
86
+ * @returns {() => void} Cleanup function.
87
+ */
88
+ export const effect = (callback) => {
89
+ let cleanup;
90
+
91
+ const computation = new Signal.Computed(() => {
92
+ if (typeof cleanup === "function") cleanup();
93
+ cleanup = callback();
94
+ });
95
+
96
+ effectWatcher.watch(computation);
97
+ computation.get();
98
+
99
+ return () => {
100
+ effectWatcher.unwatch(computation);
101
+ if (typeof cleanup === "function") cleanup();
102
+ cleanup = undefined;
103
+ };
104
+ };
105
+
106
+ /**
107
+ * Bind text content to a reactive value.
108
+ *
109
+ * @param {Node} node Target node.
110
+ * @param {unknown | (() => unknown)} value Reactive value.
111
+ * @returns {() => void} Cleanup function.
112
+ */
113
+ export const bindText = (node, value) =>
114
+ effect(() => {
115
+ node.textContent = read(value) ?? "";
116
+ });
117
+
118
+ /**
119
+ * Bind inner HTML to a reactive value.
120
+ *
121
+ * Use only with trusted HTML.
122
+ *
123
+ * @param {Element} element Target element.
124
+ * @param {unknown | (() => unknown)} value Reactive HTML.
125
+ * @returns {() => void} Cleanup function.
126
+ */
127
+ export const bindHTML = (element, value) =>
128
+ effect(() => {
129
+ element.innerHTML = read(value) ?? "";
130
+ });
131
+
132
+ /**
133
+ * Bind an attribute to a reactive value.
134
+ *
135
+ * `false`, `null`, and `undefined` remove the attribute. `true` writes a
136
+ * boolean attribute.
137
+ *
138
+ * @param {Element} element Target element.
139
+ * @param {string} name Attribute name.
140
+ * @param {unknown | (() => unknown)} value Reactive value.
141
+ * @returns {() => void} Cleanup function.
142
+ */
143
+ export const bindAttr = (element, name, value) =>
144
+ effect(() => {
145
+ const next = read(value);
146
+ if (next === false || next === null || next === undefined) {
147
+ element.removeAttribute(name);
148
+ } else {
149
+ element.setAttribute(name, next === true ? "" : String(next));
150
+ }
151
+ });
152
+
153
+ /**
154
+ * Bind an element property to a reactive value.
155
+ *
156
+ * @param {object} element Target object or element.
157
+ * @param {string} property Property name.
158
+ * @param {unknown | (() => unknown)} value Reactive value.
159
+ * @returns {() => void} Cleanup function.
160
+ */
161
+ export const bindProperty = (element, property, value) =>
162
+ effect(() => {
163
+ element[property] = read(value);
164
+ });
165
+
166
+ /**
167
+ * Toggle a class from a reactive value.
168
+ *
169
+ * @param {Element} element Target element.
170
+ * @param {string} name Class name.
171
+ * @param {unknown | (() => unknown)} value Reactive value.
172
+ * @returns {() => void} Cleanup function.
173
+ */
174
+ export const bindClass = (element, name, value) =>
175
+ effect(() => {
176
+ element.classList.toggle(name, Boolean(read(value)));
177
+ });
178
+
179
+ /**
180
+ * Bind a CSS custom property or style property to a reactive value.
181
+ *
182
+ * @param {HTMLElement} element Target element.
183
+ * @param {string} name Style property name.
184
+ * @param {unknown | (() => unknown)} value Reactive value.
185
+ * @returns {() => void} Cleanup function.
186
+ */
187
+ export const bindStyle = (element, name, value) =>
188
+ effect(() => {
189
+ const next = read(value);
190
+ if (next === false || next === null || next === undefined) {
191
+ element.style.removeProperty(name);
192
+ } else {
193
+ element.style.setProperty(name, String(next));
194
+ }
195
+ });
196
+
197
+ /**
198
+ * Bind an input-like element's value to a writable signal.
199
+ *
200
+ * @param {HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement} element
201
+ * Form control.
202
+ * @param {{ get(): string, set(value: string): void }} signal Writable signal.
203
+ * @param {string} [eventName="input"] Event name used to update the signal.
204
+ * @returns {() => void} Cleanup function.
205
+ */
206
+ export const model = (element, signal, eventName = "input") => {
207
+ const stopBinding = bindProperty(element, "value", signal);
208
+ const update = () => signal.set(element.value);
209
+ element.addEventListener(eventName, update);
210
+
211
+ return () => {
212
+ stopBinding();
213
+ element.removeEventListener(eventName, update);
214
+ };
215
+ };
216
+
217
+ export { Signal };
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.