@esportsplus/reactivity 0.1.17 → 0.1.19

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,9 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SIGNAL = exports.ROOT = exports.EFFECT = exports.DISPOSED = exports.DIRTY = exports.COMPUTED = exports.CLEAN = exports.CHECK = void 0;
1
4
  const CLEAN = 0;
5
+ exports.CLEAN = CLEAN;
2
6
  const CHECK = 1;
7
+ exports.CHECK = CHECK;
3
8
  const DIRTY = 2;
9
+ exports.DIRTY = DIRTY;
4
10
  const DISPOSED = 3;
11
+ exports.DISPOSED = DISPOSED;
5
12
  const COMPUTED = 0;
13
+ exports.COMPUTED = COMPUTED;
6
14
  const EFFECT = 1;
15
+ exports.EFFECT = EFFECT;
7
16
  const ROOT = 2;
17
+ exports.ROOT = ROOT;
8
18
  const SIGNAL = 3;
9
- export { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL };
19
+ exports.SIGNAL = SIGNAL;
package/build/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export { default as macro } from './macro';
2
2
  export { default as resource } from './resource';
3
3
  export { default as reactive } from './reactive';
4
4
  export { computed, dispose, effect, root, signal } from './signal';
5
+ export * from './constants';
5
6
  export * from './types';
package/build/index.js CHANGED
@@ -1,5 +1,34 @@
1
- export { default as macro } from './macro';
2
- export { default as resource } from './resource';
3
- export { default as reactive } from './reactive';
4
- export { computed, dispose, effect, root, signal } from './signal';
5
- export * from './types';
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.signal = exports.root = exports.effect = exports.dispose = exports.computed = exports.reactive = exports.resource = exports.macro = void 0;
21
+ var macro_1 = require("./macro");
22
+ Object.defineProperty(exports, "macro", { enumerable: true, get: function () { return __importDefault(macro_1).default; } });
23
+ var resource_1 = require("./resource");
24
+ Object.defineProperty(exports, "resource", { enumerable: true, get: function () { return __importDefault(resource_1).default; } });
25
+ var reactive_1 = require("./reactive");
26
+ Object.defineProperty(exports, "reactive", { enumerable: true, get: function () { return __importDefault(reactive_1).default; } });
27
+ var signal_1 = require("./signal");
28
+ Object.defineProperty(exports, "computed", { enumerable: true, get: function () { return signal_1.computed; } });
29
+ Object.defineProperty(exports, "dispose", { enumerable: true, get: function () { return signal_1.dispose; } });
30
+ Object.defineProperty(exports, "effect", { enumerable: true, get: function () { return signal_1.effect; } });
31
+ Object.defineProperty(exports, "root", { enumerable: true, get: function () { return signal_1.root; } });
32
+ Object.defineProperty(exports, "signal", { enumerable: true, get: function () { return signal_1.signal; } });
33
+ __exportStar(require("./constants"), exports);
34
+ __exportStar(require("./types"), exports);
package/build/macro.js CHANGED
@@ -1,17 +1,22 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { computed } from './signal';
3
- class Macro extends CustomFunction {
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const custom_function_1 = __importDefault(require("@esportsplus/custom-function"));
7
+ const signal_1 = require("./signal");
8
+ class Macro extends custom_function_1.default {
4
9
  #factory;
5
10
  constructor(fn, options = {}) {
6
11
  super((...args) => {
7
12
  return this.#factory.get()(...args);
8
13
  });
9
- this.#factory = computed(fn, options);
14
+ this.#factory = (0, signal_1.computed)(fn, options);
10
15
  }
11
16
  dispose() {
12
17
  this.#factory.dispose();
13
18
  }
14
19
  }
15
- export default (fn, options = {}) => {
20
+ exports.default = (fn, options = {}) => {
16
21
  return new Macro(fn, options);
17
22
  };
@@ -1,9 +1,12 @@
1
- import { dispose, signal } from '../signal';
2
- import { ReactiveObject } from './object';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReactiveObjectArray = exports.ReactiveArray = void 0;
4
+ const signal_1 = require("../signal");
5
+ const object_1 = require("./object");
3
6
  function factory(data, options = {}) {
4
7
  let signals = [];
5
8
  for (let i = 0, n = data.length; i < n; i++) {
6
- signals.push(new ReactiveObject(data[i], options));
9
+ signals.push(new object_1.ReactiveObject(data[i], options));
7
10
  }
8
11
  return signals;
9
12
  }
@@ -14,7 +17,7 @@ class ReactiveArray extends Array {
14
17
  #signal;
15
18
  constructor(data) {
16
19
  super(...data);
17
- this.#signal = signal(false);
20
+ this.#signal = (0, signal_1.signal)(false);
18
21
  }
19
22
  set length(n) {
20
23
  if (n > this.length) {
@@ -99,6 +102,7 @@ class ReactiveArray extends Array {
99
102
  return length;
100
103
  }
101
104
  }
105
+ exports.ReactiveArray = ReactiveArray;
102
106
  class ReactiveObjectArray extends ReactiveArray {
103
107
  #options;
104
108
  constructor(data, options = {}) {
@@ -112,22 +116,22 @@ class ReactiveObjectArray extends ReactiveArray {
112
116
  return unsupported('reverse');
113
117
  }
114
118
  pop() {
115
- return dispose(super.pop());
119
+ return (0, signal_1.dispose)(super.pop());
116
120
  }
117
121
  push(...values) {
118
122
  return super.push(...factory(values, this.#options));
119
123
  }
120
124
  shift() {
121
- return dispose(super.shift());
125
+ return (0, signal_1.dispose)(super.shift());
122
126
  }
123
127
  sort() {
124
128
  return unsupported('sort');
125
129
  }
126
130
  splice(start, deleteCount = super.length, ...values) {
127
- return dispose(super.splice(start, deleteCount, ...factory(values, this.#options)));
131
+ return (0, signal_1.dispose)(super.splice(start, deleteCount, ...factory(values, this.#options)));
128
132
  }
129
133
  unshift(...values) {
130
134
  return super.unshift(...factory(values, this.#options));
131
135
  }
132
136
  }
133
- export { ReactiveArray, ReactiveObjectArray };
137
+ exports.ReactiveObjectArray = ReactiveObjectArray;
@@ -1,4 +1,6 @@
1
- import { ReactiveObject } from './object';
2
- export default (data, options) => {
3
- return new ReactiveObject(data, options);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const object_1 = require("./object");
4
+ exports.default = (data, options) => {
5
+ return new object_1.ReactiveObject(data, options);
4
6
  };
@@ -1,6 +1,9 @@
1
- import { computed, signal } from '../signal';
2
- import { defineProperty, isArray } from '../utilities';
3
- import { ReactiveArray, ReactiveObjectArray } from './array';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReactiveObject = void 0;
4
+ const signal_1 = require("../signal");
5
+ const utilities_1 = require("../utilities");
6
+ const array_1 = require("./array");
4
7
  class ReactiveObject {
5
8
  nodes = {};
6
9
  constructor(data, options = {}) {
@@ -8,23 +11,23 @@ class ReactiveObject {
8
11
  for (let key in data) {
9
12
  let input = data[key];
10
13
  if (typeof input === 'function') {
11
- let node = nodes[key] = computed(input, options);
12
- defineProperty(this, key, {
14
+ let node = nodes[key] = (0, signal_1.computed)(input, options);
15
+ (0, utilities_1.defineProperty)(this, key, {
13
16
  enumerable: true,
14
17
  get() {
15
18
  return node.get();
16
19
  }
17
20
  });
18
21
  }
19
- else if (isArray(input)) {
22
+ else if ((0, utilities_1.isArray)(input)) {
20
23
  let node, test = input[0];
21
24
  if (typeof test === 'object' && test !== null && test?.constructor?.name === 'Object') {
22
- node = nodes[key] = new ReactiveObjectArray(input, options);
25
+ node = nodes[key] = new array_1.ReactiveObjectArray(input, options);
23
26
  }
24
27
  else {
25
- node = nodes[key] = new ReactiveArray(input);
28
+ node = nodes[key] = new array_1.ReactiveArray(input);
26
29
  }
27
- defineProperty(this, key, {
30
+ (0, utilities_1.defineProperty)(this, key, {
28
31
  enumerable: true,
29
32
  get() {
30
33
  node.track();
@@ -33,8 +36,8 @@ class ReactiveObject {
33
36
  });
34
37
  }
35
38
  else {
36
- let node = nodes[key] = signal(input, options);
37
- defineProperty(this, key, {
39
+ let node = nodes[key] = (0, signal_1.signal)(input, options);
40
+ (0, utilities_1.defineProperty)(this, key, {
38
41
  enumerable: true,
39
42
  get() {
40
43
  return node.get();
@@ -53,4 +56,4 @@ class ReactiveObject {
53
56
  }
54
57
  }
55
58
  }
56
- export { ReactiveObject };
59
+ exports.ReactiveObject = ReactiveObject;
package/build/resource.js CHANGED
@@ -1,6 +1,11 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { signal } from './signal';
3
- class Resource extends CustomFunction {
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const custom_function_1 = __importDefault(require("@esportsplus/custom-function"));
7
+ const signal_1 = require("./signal");
8
+ class Resource extends custom_function_1.default {
4
9
  #data;
5
10
  #input;
6
11
  #ok;
@@ -26,9 +31,9 @@ class Resource extends CustomFunction {
26
31
  this.#ok.set(false);
27
32
  });
28
33
  });
29
- this.#data = signal(undefined, options);
30
- this.#input = signal(null, options);
31
- this.#ok = signal(null, options);
34
+ this.#data = (0, signal_1.signal)(undefined, options);
35
+ this.#input = (0, signal_1.signal)(null, options);
36
+ this.#ok = (0, signal_1.signal)(null, options);
32
37
  }
33
38
  get data() {
34
39
  return this.#data.get();
@@ -45,6 +50,6 @@ class Resource extends CustomFunction {
45
50
  this.#ok.dispose();
46
51
  }
47
52
  }
48
- export default (fn, options = {}) => {
53
+ exports.default = (fn, options = {}) => {
49
54
  return new Resource(fn, options);
50
55
  };
package/build/signal.js CHANGED
@@ -1,5 +1,8 @@
1
- import { CHECK, CLEAN, COMPUTED, DIRTY, DISPOSED, EFFECT, ROOT, SIGNAL } from './constants';
2
- import { isArray } from './utilities';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Reactive = exports.signal = exports.root = exports.effect = exports.dispose = exports.computed = void 0;
4
+ const constants_1 = require("./constants");
5
+ const utilities_1 = require("./utilities");
3
6
  let index = 0, observer = null, observers = null, scope = null;
4
7
  class Reactive {
5
8
  changed = null;
@@ -16,7 +19,7 @@ class Reactive {
16
19
  value;
17
20
  constructor(state, type, value) {
18
21
  let root = null;
19
- if (type !== ROOT) {
22
+ if (type !== constants_1.ROOT) {
20
23
  if (scope !== null) {
21
24
  root = scope;
22
25
  }
@@ -24,7 +27,7 @@ class Reactive {
24
27
  root = observer.root;
25
28
  }
26
29
  if (root == null) {
27
- if (type === EFFECT) {
30
+ if (type === constants_1.EFFECT) {
28
31
  throw new Error(`Reactivity: 'effect' cannot be created without a reactive root`);
29
32
  }
30
33
  }
@@ -62,7 +65,7 @@ class Reactive {
62
65
  }
63
66
  }
64
67
  dispose() {
65
- if (this.state === DISPOSED) {
68
+ if (this.state === constants_1.DISPOSED) {
66
69
  return;
67
70
  }
68
71
  this.dispatch('cleanup', this);
@@ -71,10 +74,10 @@ class Reactive {
71
74
  this.listeners = null;
72
75
  this.observers = null;
73
76
  this.sources = null;
74
- this.state = DISPOSED;
77
+ this.state = constants_1.DISPOSED;
75
78
  }
76
79
  get() {
77
- if (this.state === DISPOSED) {
80
+ if (this.state === constants_1.DISPOSED) {
78
81
  return this.value;
79
82
  }
80
83
  if (observer !== null) {
@@ -90,13 +93,13 @@ class Reactive {
90
93
  observers.push(this);
91
94
  }
92
95
  }
93
- if (this.type === COMPUTED || this.type === EFFECT) {
96
+ if (this.type === constants_1.COMPUTED || this.type === constants_1.EFFECT) {
94
97
  sync(this);
95
98
  }
96
99
  return this.value;
97
100
  }
98
101
  on(event, listener) {
99
- if (this.state === DIRTY) {
102
+ if (this.state === constants_1.DIRTY) {
100
103
  if (event !== 'cleanup') {
101
104
  throw new Error(`Reactivity: events set within computed or effects must use the 'cleanup' event name`);
102
105
  }
@@ -126,16 +129,17 @@ class Reactive {
126
129
  this.on(event, listener);
127
130
  }
128
131
  set(value) {
129
- if (this.type !== SIGNAL && observer !== this) {
132
+ if (this.type !== constants_1.SIGNAL && observer !== this) {
130
133
  throw new Error(`Reactivity: 'set' method is only available on signals`);
131
134
  }
132
135
  if (this.changed(this.value, value)) {
133
136
  this.value = value;
134
- notify(this.observers, DIRTY);
137
+ notify(this.observers, constants_1.DIRTY);
135
138
  }
136
139
  return this.value;
137
140
  }
138
141
  }
142
+ exports.Reactive = Reactive;
139
143
  function changed(a, b) {
140
144
  return a !== b;
141
145
  }
@@ -146,11 +150,11 @@ function notify(nodes, state) {
146
150
  for (let i = 0, n = nodes.length; i < n; i++) {
147
151
  let node = nodes[i];
148
152
  if (node.state < state) {
149
- if (node.type === EFFECT && node.state === CLEAN) {
153
+ if (node.type === constants_1.EFFECT && node.state === constants_1.CLEAN) {
150
154
  node.root.scheduler(node.task);
151
155
  }
152
156
  node.state = state;
153
- notify(node.observers, CHECK);
157
+ notify(node.observers, constants_1.CHECK);
154
158
  }
155
159
  }
156
160
  }
@@ -168,19 +172,19 @@ function removeSourceObservers(node, start) {
168
172
  }
169
173
  }
170
174
  function sync(node) {
171
- if (node.state === CHECK && node.sources !== null) {
175
+ if (node.state === constants_1.CHECK && node.sources !== null) {
172
176
  for (let i = 0, n = node.sources.length; i < n; i++) {
173
177
  sync(node.sources[i]);
174
- if (node.state === DIRTY) {
178
+ if (node.state === constants_1.DIRTY) {
175
179
  break;
176
180
  }
177
181
  }
178
182
  }
179
- if (node.state === DIRTY) {
183
+ if (node.state === constants_1.DIRTY) {
180
184
  update(node);
181
185
  }
182
186
  else {
183
- node.state = CLEAN;
187
+ node.state = constants_1.CLEAN;
184
188
  }
185
189
  }
186
190
  function update(node) {
@@ -217,7 +221,7 @@ function update(node) {
217
221
  removeSourceObservers(node, index);
218
222
  node.sources.length = index;
219
223
  }
220
- if (node.type === COMPUTED) {
224
+ if (node.type === constants_1.COMPUTED) {
221
225
  node.set(value);
222
226
  }
223
227
  }
@@ -226,18 +230,19 @@ function update(node) {
226
230
  observer = o;
227
231
  observers = os;
228
232
  }
229
- node.state = CLEAN;
233
+ node.state = constants_1.CLEAN;
230
234
  }
231
235
  const computed = (fn, options) => {
232
- let instance = new Reactive(DIRTY, COMPUTED, undefined);
236
+ let instance = new Reactive(constants_1.DIRTY, constants_1.COMPUTED, undefined);
233
237
  instance.changed = options?.changed || changed;
234
238
  instance.fn = fn;
235
239
  return instance;
236
240
  };
241
+ exports.computed = computed;
237
242
  const dispose = (dispose) => {
238
243
  if (dispose == null) {
239
244
  }
240
- else if (isArray(dispose)) {
245
+ else if ((0, utilities_1.isArray)(dispose)) {
241
246
  for (let i = 0, n = dispose.length; i < n; i++) {
242
247
  dispose[i].dispose();
243
248
  }
@@ -247,23 +252,28 @@ const dispose = (dispose) => {
247
252
  }
248
253
  return dispose;
249
254
  };
255
+ exports.dispose = dispose;
250
256
  const effect = (fn) => {
251
- let instance = new Reactive(DIRTY, EFFECT, null);
257
+ let instance = new Reactive(constants_1.DIRTY, constants_1.EFFECT, null);
252
258
  instance.fn = fn;
253
259
  instance.task = () => instance.get();
254
260
  update(instance);
255
261
  return instance;
256
262
  };
263
+ exports.effect = effect;
257
264
  const root = (fn, scheduler) => {
258
265
  let o = observer, s = scope;
259
266
  if (scheduler === undefined) {
267
+ if (o?.type === constants_1.EFFECT) {
268
+ scope = o.root;
269
+ }
260
270
  if (scope === null) {
261
271
  throw new Error('Reactivity: `root` cannot be created without a task scheduler');
262
272
  }
263
273
  scheduler = scope.scheduler;
264
274
  }
265
275
  observer = null;
266
- scope = new Reactive(CLEAN, ROOT, null);
276
+ scope = new Reactive(constants_1.CLEAN, constants_1.ROOT, null);
267
277
  scope.scheduler = scheduler;
268
278
  scope.tracking = fn.length > 0;
269
279
  let result = fn.call(null, scope);
@@ -271,10 +281,10 @@ const root = (fn, scheduler) => {
271
281
  scope = s;
272
282
  return result;
273
283
  };
284
+ exports.root = root;
274
285
  const signal = (value, options) => {
275
- let instance = new Reactive(CLEAN, SIGNAL, value);
286
+ let instance = new Reactive(constants_1.CLEAN, constants_1.SIGNAL, value);
276
287
  instance.changed = options?.changed || changed;
277
288
  return instance;
278
289
  };
279
- export { computed, dispose, effect, root, signal };
280
- export { Reactive };
290
+ exports.signal = signal;
package/build/types.js CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isArray = exports.defineProperty = void 0;
1
4
  const { isArray } = Array;
5
+ exports.isArray = isArray;
2
6
  const { defineProperty } = Object;
3
- export { defineProperty, isArray };
7
+ exports.defineProperty = defineProperty;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "@esportsplus/custom-function": "^0.0.1"
5
5
  },
6
6
  "devDependencies": {
7
- "@esportsplus/typescript": "^0.0.15"
7
+ "@esportsplus/typescript": "^0.0.16"
8
8
  },
9
9
  "main": "build/index.js",
10
10
  "name": "@esportsplus/reactivity",
@@ -16,5 +16,5 @@
16
16
  "prepublishOnly": "npm run build"
17
17
  },
18
18
  "types": "build/index.d.ts",
19
- "version": "0.1.17"
19
+ "version": "0.1.19"
20
20
  }
package/src/index.ts CHANGED
@@ -2,4 +2,5 @@ export { default as macro } from './macro';
2
2
  export { default as resource } from './resource';
3
3
  export { default as reactive } from './reactive';
4
4
  export { computed, dispose, effect, root, signal } from './signal';
5
+ export * from './constants';
5
6
  export * from './types';
package/src/signal.ts CHANGED
@@ -338,6 +338,10 @@ const root = <T>(fn: NeverAsync<(instance: Root) => T>, scheduler?: Scheduler) =
338
338
  s = scope;
339
339
 
340
340
  if (scheduler === undefined) {
341
+ if (o?.type === EFFECT) {
342
+ scope = o.root;
343
+ }
344
+
341
345
  if (scope === null) {
342
346
  throw new Error('Reactivity: `root` cannot be created without a task scheduler');
343
347
  }
package/tsconfig.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "baseUrl": ".",
4
+ "declaration": true,
4
5
  "declarationDir": "build",
5
- "outDir": "build",
6
+ "outDir": "build"
6
7
  },
7
8
  "exclude": ["node_modules"],
8
- "extends": "@esportsplus/typescript/tsconfig.base.json",
9
+ "extends": "./node_modules/@esportsplus/typescript/tsconfig.base.json",
9
10
  "include": ["src"]
10
11
  }