@esportsplus/reactivity 0.4.6 → 0.4.8

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.
@@ -4,11 +4,13 @@
4
4
  # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5
5
 
6
6
  version: 2
7
+
7
8
  registries:
8
9
  npm-npmjs:
9
10
  token: ${{secrets.NPM_TOKEN}}
10
11
  type: npm-registry
11
12
  url: https://registry.npmjs.org
13
+
12
14
  updates:
13
15
  - package-ecosystem: "npm"
14
16
  directory: "/"
@@ -1,7 +1,9 @@
1
1
  name: bump
2
+
2
3
  on:
3
4
  push:
4
5
  branches: '**' # only trigger on branches, not on tags
6
+
5
7
  jobs:
6
8
  bump:
7
9
  uses: esportsplus/workflows/.github/workflows/bump.yml@main
@@ -0,0 +1,12 @@
1
+ name: dependabot automerge
2
+
3
+ on:
4
+ pull_request:
5
+ types: [opened, synchronize, labeled]
6
+ workflow_dispatch:
7
+
8
+ jobs:
9
+ automerge:
10
+ secrets:
11
+ NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
12
+ uses: esportsplus/workflows/.github/workflows/dependabot.yml@main
@@ -1,4 +1,5 @@
1
1
  name: publish to npm
2
+
2
3
  on:
3
4
  release:
4
5
  types: [published]
@@ -7,6 +8,7 @@ on:
7
8
  workflows: [bump]
8
9
  types:
9
10
  - completed
11
+
10
12
  jobs:
11
13
  publish:
12
14
  secrets:
package/build/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { default as macro } from './macro.js';
2
- export { default as resource } from './resource.js';
3
2
  export { default as reactive } from './reactive/index.js';
4
3
  export { computed, dispose, effect, root, signal } from './signal.js';
5
4
  export * from './constants.js';
package/build/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  export { default as macro } from './macro.js';
2
- export { default as resource } from './resource.js';
3
2
  export { default as reactive } from './reactive/index.js';
4
3
  export { computed, dispose, effect, root, signal } from './signal.js';
5
4
  export * from './constants.js';
@@ -10,7 +10,7 @@ export default (data, options = {}) => {
10
10
  value = object(data, options);
11
11
  }
12
12
  else {
13
- throw new Error(`Reactivity: 'reactive' received invalid input - ${JSON.stringify(data)}`);
13
+ throw new Error(`@esportsplus/reactivity: 'reactive' received invalid input - ${JSON.stringify(data)}`);
14
14
  }
15
15
  return value;
16
16
  };
@@ -4,6 +4,5 @@ type API<T> = Prettify<{
4
4
  } & {
5
5
  dispose: VoidFunction;
6
6
  }>;
7
- declare const _default: <T extends Record<PropertyKey, unknown>>(input: T, options?: Options) => API<T>;
8
- export default _default;
7
+ export default function object<T extends Record<PropertyKey, unknown>>(input: T, options?: Options): API<T>;
9
8
  export type { API as ReactiveObject };
@@ -1,4 +1,4 @@
1
- import { defineProperty, isArray, isFunction } from '@esportsplus/utilities';
1
+ import { defineProperty, isArray, isFunction, isObject } from '@esportsplus/utilities';
2
2
  import { computed, signal } from '../signal.js';
3
3
  import { default as array } from './array.js';
4
4
  class ReactiveObject {
@@ -6,9 +6,9 @@ class ReactiveObject {
6
6
  constructor(data, options = {}) {
7
7
  let signals = this.signals;
8
8
  for (let key in data) {
9
- let input = data[key];
10
- if (isArray(input)) {
11
- let s = signals[key] = array(input, options);
9
+ let value = data[key];
10
+ if (isArray(value)) {
11
+ let s = signals[key] = array(value, options);
12
12
  defineProperty(this, key, {
13
13
  enumerable: true,
14
14
  get() {
@@ -16,8 +16,8 @@ class ReactiveObject {
16
16
  }
17
17
  });
18
18
  }
19
- else if (isFunction(input)) {
20
- let s = signals[key] = computed(input, options);
19
+ else if (isFunction(value)) {
20
+ let s = signals[key] = computed(value, options);
21
21
  defineProperty(this, key, {
22
22
  enumerable: true,
23
23
  get() {
@@ -25,8 +25,17 @@ class ReactiveObject {
25
25
  }
26
26
  });
27
27
  }
28
+ else if (isObject(value)) {
29
+ let s = signals[key] = new ReactiveObject(value, options);
30
+ defineProperty(this, key, {
31
+ enumerable: true,
32
+ get() {
33
+ return s;
34
+ }
35
+ });
36
+ }
28
37
  else {
29
- let s = signals[key] = signal(input, options);
38
+ let s = signals[key] = signal(value, options);
30
39
  defineProperty(this, key, {
31
40
  enumerable: true,
32
41
  get() {
@@ -46,6 +55,7 @@ class ReactiveObject {
46
55
  }
47
56
  }
48
57
  }
49
- export default (input, options = {}) => {
58
+ export default function object(input, options = {}) {
50
59
  return new ReactiveObject(input, options);
51
- };
60
+ }
61
+ ;
package/build/signal.js CHANGED
@@ -25,7 +25,7 @@ class Reactive {
25
25
  }
26
26
  if (root == null) {
27
27
  if (type === EFFECT) {
28
- throw new Error(`Reactivity: 'effect' cannot be created without a reactive root`);
28
+ throw new Error(`@esportsplus/reactivity: 'effect' cannot be created without a reactive root`);
29
29
  }
30
30
  }
31
31
  else if (root.tracking) {
@@ -95,7 +95,7 @@ class Reactive {
95
95
  on(event, listener) {
96
96
  if (this.state === DIRTY) {
97
97
  if (event !== 'cleanup') {
98
- throw new Error(`Reactivity: events set within computed or effects must use the 'cleanup' event name`);
98
+ throw new Error(`@esportsplus/reactivity: events set within computed or effects must use the 'cleanup' event name`);
99
99
  }
100
100
  listener.once = true;
101
101
  }
@@ -124,7 +124,7 @@ class Reactive {
124
124
  }
125
125
  set(value) {
126
126
  if (this.type !== SIGNAL && observer !== this) {
127
- throw new Error(`Reactivity: 'set' method is only available on signals`);
127
+ throw new Error(`@esportsplus/reactivity: 'set' method is only available on signals`);
128
128
  }
129
129
  if (this.changed(this.value, value)) {
130
130
  this.value = value;
@@ -258,7 +258,7 @@ const root = (fn, scheduler) => {
258
258
  scope = o.root;
259
259
  }
260
260
  if (scope === null) {
261
- throw new Error('Reactivity: `root` cannot be created without a task scheduler');
261
+ throw new Error('@esportsplus/reactivity: `root` cannot be created without a task scheduler');
262
262
  }
263
263
  scheduler = scope.scheduler;
264
264
  }
package/build/types.d.ts CHANGED
@@ -18,7 +18,7 @@ type Effect = {
18
18
  type Infer<T> = T extends (...args: unknown[]) => unknown ? ReturnType<T> : T extends (infer U)[] ? ReactiveArray<U> : T extends ReactiveObject<T> ? ReactiveObject<T> : T extends Record<PropertyKey, unknown> ? {
19
19
  [K in keyof T]: T[K];
20
20
  } : T;
21
- type Event = string;
21
+ type Event = 'cleanup' | 'dispose' | 'update' | string;
22
22
  type Listener<D> = {
23
23
  once?: boolean;
24
24
  <V>(data: D, value: V): void;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
4
  "@esportsplus/custom-function": "^0.0.12",
5
- "@esportsplus/utilities": "^0.15.0"
5
+ "@esportsplus/utilities": "^0.19.0"
6
6
  },
7
7
  "devDependencies": {
8
8
  "@esportsplus/typescript": "^0.9.1"
@@ -12,7 +12,7 @@
12
12
  "private": false,
13
13
  "type": "module",
14
14
  "types": "build/index.d.ts",
15
- "version": "0.4.6",
15
+ "version": "0.4.8",
16
16
  "scripts": {
17
17
  "build": "tsc && tsc-alias",
18
18
  "-": "-"
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { default as macro } from './macro';
2
- export { default as resource } from './resource';
3
2
  export { default as reactive } from './reactive';
4
3
  export { computed, dispose, effect, root, signal } from './signal';
5
4
  export * from './constants';
@@ -22,7 +22,7 @@ export default <T>(data: Guard<T>, options: Options = {}) => {
22
22
  value = object(data as { [K in keyof T]: T[K] }, options);
23
23
  }
24
24
  else {
25
- throw new Error(`Reactivity: 'reactive' received invalid input - ${JSON.stringify(data)}`);
25
+ throw new Error(`@esportsplus/reactivity: 'reactive' received invalid input - ${JSON.stringify(data)}`);
26
26
  }
27
27
 
28
28
  return value as Reactive<T>;
@@ -1,4 +1,4 @@
1
- import { defineProperty, isArray, isFunction } from '@esportsplus/utilities';
1
+ import { defineProperty, isArray, isFunction, isObject } from '@esportsplus/utilities';
2
2
  import { computed, signal } from '~/signal';
3
3
  import { Computed, Infer, Options, Prettify, ReactiveArray, Signal } from '~/types';
4
4
  import { default as array } from './array';
@@ -8,17 +8,17 @@ type API<T> = Prettify< { [K in keyof T]: Infer<T[K]> } & { dispose: VoidFunctio
8
8
 
9
9
 
10
10
  class ReactiveObject<T extends Record<PropertyKey, unknown>> {
11
- signals: Record<PropertyKey, Computed<any> | ReactiveArray<any> | Signal<any>> = {};
11
+ signals: Record<PropertyKey, Computed<any> | ReactiveArray<any> | ReactiveObject<any> | Signal<any>> = {};
12
12
 
13
13
 
14
14
  constructor(data: T, options: Options = {}) {
15
15
  let signals = this.signals;
16
16
 
17
17
  for (let key in data) {
18
- let input = data[key];
18
+ let value = data[key];
19
19
 
20
- if (isArray(input)) {
21
- let s = signals[key] = array(input, options);
20
+ if (isArray(value)) {
21
+ let s = signals[key] = array(value, options);
22
22
 
23
23
  defineProperty(this, key, {
24
24
  enumerable: true,
@@ -27,8 +27,8 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
27
27
  }
28
28
  });
29
29
  }
30
- else if (isFunction(input)) {
31
- let s = signals[key] = computed(input as Computed<T>['fn'], options);
30
+ else if (isFunction(value)) {
31
+ let s = signals[key] = computed(value as Computed<T>['fn'], options);
32
32
 
33
33
  defineProperty(this, key, {
34
34
  enumerable: true,
@@ -37,8 +37,19 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
37
37
  }
38
38
  });
39
39
  }
40
+ else if (isObject(value)) {
41
+ // Type issue with factory function below, fix after testing, if this is kept
42
+ let s = signals[key] = new ReactiveObject(value, options);
43
+
44
+ defineProperty(this, key, {
45
+ enumerable: true,
46
+ get() {
47
+ return s;
48
+ }
49
+ });
50
+ }
40
51
  else {
41
- let s = signals[key] = signal(input, options);
52
+ let s = signals[key] = signal(value, options);
42
53
 
43
54
  defineProperty(this, key, {
44
55
  enumerable: true,
@@ -64,7 +75,7 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
64
75
  }
65
76
 
66
77
 
67
- export default <T extends Record<PropertyKey, unknown>>(input: T, options: Options = {}) => {
78
+ export default function object<T extends Record<PropertyKey, unknown>>(input: T, options: Options = {}) {
68
79
  return new ReactiveObject(input, options) as API<T>;
69
80
  };
70
81
  export type { API as ReactiveObject };
package/src/signal.ts CHANGED
@@ -37,7 +37,7 @@ class Reactive<T> {
37
37
 
38
38
  if (root == null) {
39
39
  if (type === EFFECT) {
40
- throw new Error(`Reactivity: 'effect' cannot be created without a reactive root`);
40
+ throw new Error(`@esportsplus/reactivity: 'effect' cannot be created without a reactive root`);
41
41
  }
42
42
  }
43
43
  else if (root.tracking) {
@@ -124,7 +124,7 @@ class Reactive<T> {
124
124
  on<T>(event: Event, listener: Listener<T>) {
125
125
  if (this.state === DIRTY) {
126
126
  if (event !== 'cleanup') {
127
- throw new Error(`Reactivity: events set within computed or effects must use the 'cleanup' event name`);
127
+ throw new Error(`@esportsplus/reactivity: events set within computed or effects must use the 'cleanup' event name`);
128
128
  }
129
129
 
130
130
  listener.once = true;
@@ -159,7 +159,7 @@ class Reactive<T> {
159
159
 
160
160
  set(value: T): T {
161
161
  if (this.type !== SIGNAL && observer !== this) {
162
- throw new Error(`Reactivity: 'set' method is only available on signals`);
162
+ throw new Error(`@esportsplus/reactivity: 'set' method is only available on signals`);
163
163
  }
164
164
 
165
165
  if (this.changed!(this.value, value)) {
@@ -339,7 +339,7 @@ const root = <T>(fn: NeverAsync<(instance: Root) => T>, scheduler?: Scheduler) =
339
339
  }
340
340
 
341
341
  if (scope === null) {
342
- throw new Error('Reactivity: `root` cannot be created without a task scheduler');
342
+ throw new Error('@esportsplus/reactivity: `root` cannot be created without a task scheduler');
343
343
  }
344
344
 
345
345
  scheduler = scope.scheduler;
package/src/types.ts CHANGED
@@ -32,7 +32,7 @@ type Infer<T> =
32
32
  ? { [K in keyof T]: T[K] }
33
33
  : T;
34
34
 
35
- type Event = string;
35
+ type Event = 'cleanup' | 'dispose' | 'update' | string;
36
36
 
37
37
  type Listener<D> = {
38
38
  once?: boolean;
@@ -1,15 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { Options } from './types.js';
3
- declare class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
4
- private arguments;
5
- private okay;
6
- private response;
7
- stop: boolean | null;
8
- constructor(fn: (...args: A) => R, options?: Options);
9
- get data(): Awaited<R>;
10
- get input(): A | null;
11
- get ok(): boolean | null;
12
- dispose(): void;
13
- }
14
- declare const _default: <A extends unknown[], R extends Promise<unknown>>(fn: (...args: A) => R, options?: Options) => Resource<A, R>;
15
- export default _default;
package/build/resource.js DELETED
@@ -1,50 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { signal } from './signal.js';
3
- class Resource extends CustomFunction {
4
- arguments;
5
- okay;
6
- response;
7
- stop = null;
8
- constructor(fn, options = {}) {
9
- super((...args) => {
10
- this.stop = null;
11
- this.arguments.set(args);
12
- this.okay.set(null);
13
- fn(...args)
14
- .then((value) => {
15
- if (this.stop === true) {
16
- return;
17
- }
18
- this.response.set(value);
19
- this.okay.set(true);
20
- })
21
- .catch(() => {
22
- if (this.stop === true) {
23
- return;
24
- }
25
- this.response.set(undefined);
26
- this.okay.set(false);
27
- });
28
- });
29
- this.response = signal(undefined, options);
30
- this.arguments = signal(null, options);
31
- this.okay = signal(null, options);
32
- }
33
- get data() {
34
- return this.response.get();
35
- }
36
- get input() {
37
- return this.arguments.get();
38
- }
39
- get ok() {
40
- return this.okay.get();
41
- }
42
- dispose() {
43
- this.arguments.dispose();
44
- this.okay.dispose();
45
- this.response.dispose();
46
- }
47
- }
48
- export default (fn, options = {}) => {
49
- return new Resource(fn, options);
50
- };
package/src/resource.ts DELETED
@@ -1,68 +0,0 @@
1
- import CustomFunction from '@esportsplus/custom-function';
2
- import { signal } from './signal';
3
- import { Options, Signal } from './types';
4
-
5
-
6
- class Resource<A extends unknown[], R extends Promise<unknown>> extends CustomFunction {
7
- private arguments: Signal<A | null>;
8
- private okay: Signal<boolean | null>;
9
- private response: Signal<Awaited<R>>;
10
-
11
- stop: boolean | null = null;
12
-
13
-
14
- constructor(fn: (...args: A) => R, options: Options = {}) {
15
- super((...args: A) => {
16
- this.stop = null;
17
-
18
- this.arguments.set(args);
19
- this.okay.set(null);
20
-
21
- fn(...args)
22
- .then((value) => {
23
- if (this.stop === true) {
24
- return;
25
- }
26
-
27
- this.response.set(value as Awaited<R>);
28
- this.okay.set(true);
29
- })
30
- .catch(() => {
31
- if (this.stop === true) {
32
- return;
33
- }
34
-
35
- this.response.set(undefined as Awaited<R>);
36
- this.okay.set(false);
37
- });
38
- });
39
- this.response = signal(undefined as Awaited<R>, options);
40
- this.arguments = signal<A | null>(null, options);
41
- this.okay = signal<boolean | null>(null, options);
42
- }
43
-
44
-
45
- get data() {
46
- return this.response.get();
47
- }
48
-
49
- get input() {
50
- return this.arguments.get();
51
- }
52
-
53
- get ok() {
54
- return this.okay.get();
55
- }
56
-
57
-
58
- dispose() {
59
- this.arguments.dispose();
60
- this.okay.dispose();
61
- this.response.dispose();
62
- }
63
- }
64
-
65
-
66
- export default <A extends unknown[], R extends Promise<unknown>>(fn: (...args: A) => R, options: Options = {}) => {
67
- return new Resource(fn, options);
68
- };