@esportsplus/reactivity 0.0.20 → 0.0.21

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/build/index.js CHANGED
@@ -1,277 +1,4 @@
1
- /******/ (() => { // webpackBootstrap
2
- /******/ "use strict";
3
- var __webpack_exports__ = {};
4
-
5
- // UNUSED EXPORTS: default, effect, reactive, scheduler
6
-
7
- ;// CONCATENATED MODULE: ./src/symbols.ts
8
- const CLEAN = 0;
9
- const CHECK = 1;
10
- const DIRTY = 2;
11
-
12
-
13
- ;// CONCATENATED MODULE: ./src/reactive.ts
14
-
15
- let index = 0, reaction = null, queue = [], scheduled = false, schedulers = new Set, stack = null;
16
- function mark(instances, state) {
17
- if (!instances) {
18
- return;
19
- }
20
- for (let i = 0, n = instances.length; i < n; i++) {
21
- let instance = instances[i];
22
- if (instance.state < state) {
23
- if (instance.effect && instance.state === CLEAN) {
24
- queue.push(instance);
25
- if (!scheduled) {
26
- schedule();
27
- }
28
- }
29
- instance.state = state;
30
- if (instance.observers) {
31
- mark(instance.observers, CHECK);
32
- }
33
- }
34
- }
35
- }
36
- function schedule() {
37
- if (scheduled) {
38
- return;
39
- }
40
- for (let scheduler of schedulers) {
41
- scheduler.schedule();
42
- }
43
- scheduled = true;
44
- }
45
- async function task() {
46
- let n = queue.length;
47
- for (let i = 0; i < n; i++) {
48
- await queue[i].get();
49
- }
50
- queue.splice(0, n);
51
- scheduled = false;
52
- if (queue.length) {
53
- schedule();
54
- }
55
- }
56
- class Reactive {
57
- fn;
58
- value;
59
- effect;
60
- cleanup = null;
61
- observers = null;
62
- sources = null;
63
- state;
64
- constructor(data, effect = false) {
65
- this.effect = effect;
66
- if (typeof data === 'function') {
67
- this.fn = data;
68
- this.state = DIRTY;
69
- this.value = undefined;
70
- if (effect) {
71
- this.update();
72
- }
73
- }
74
- else {
75
- this.state = CLEAN;
76
- this.value = data;
77
- }
78
- }
79
- get() {
80
- if (reaction) {
81
- if (!stack && reaction.sources && reaction.sources[index] == this) {
82
- index++;
83
- }
84
- else {
85
- if (!stack) {
86
- stack = [this];
87
- }
88
- else {
89
- stack.push(this);
90
- }
91
- }
92
- }
93
- if (this.fn) {
94
- this.sync();
95
- }
96
- return this.value;
97
- }
98
- set(value) {
99
- if (this.value !== value) {
100
- mark(this.observers, DIRTY);
101
- }
102
- this.value = value;
103
- }
104
- removeParentObservers() {
105
- if (!this.sources) {
106
- return;
107
- }
108
- for (let i = index; i < this.sources.length; i++) {
109
- let source = this.sources[i];
110
- source.observers[source.observers.findIndex((v) => v === this)] = source.observers[source.observers.length - 1];
111
- source.observers.pop();
112
- }
113
- }
114
- sync() {
115
- if (this.state === CHECK && this.sources) {
116
- for (let i = 0, n = this.sources.length; i < n; i++) {
117
- this.sources[i].sync();
118
- if (this.state === DIRTY) {
119
- break;
120
- }
121
- }
122
- }
123
- if (this.state === DIRTY) {
124
- this.update();
125
- }
126
- this.state = CLEAN;
127
- }
128
- update() {
129
- let previous = {
130
- index: index,
131
- reaction: reaction,
132
- stack: stack,
133
- value: this.value
134
- };
135
- index = 0;
136
- reaction = this;
137
- stack = [];
138
- try {
139
- if (this.cleanup) {
140
- for (let i = 0, n = this.cleanup.length; i < n; i++) {
141
- this.cleanup[i](this.value);
142
- }
143
- this.cleanup.length = 0;
144
- }
145
- this.value = this.fn((fn) => {
146
- if (!this.cleanup) {
147
- this.cleanup = [fn];
148
- }
149
- else {
150
- this.cleanup.push(fn);
151
- }
152
- });
153
- if (stack.length) {
154
- this.removeParentObservers();
155
- if (this.sources && index > 0) {
156
- this.sources.length = index + stack.length;
157
- for (let i = 0; i < stack.length; i++) {
158
- this.sources[index + i] = stack[i];
159
- }
160
- }
161
- else {
162
- this.sources = stack;
163
- }
164
- for (let i = index; i < this.sources.length; i++) {
165
- let source = this.sources[i];
166
- if (!source.observers) {
167
- source.observers = [this];
168
- }
169
- else {
170
- source.observers.push(this);
171
- }
172
- }
173
- }
174
- else if (this.sources && index < this.sources.length) {
175
- this.removeParentObservers();
176
- this.sources.length = index;
177
- }
178
- }
179
- finally {
180
- index = previous.index;
181
- reaction = previous.reaction;
182
- stack = previous.stack;
183
- }
184
- if (this.observers && previous.value !== this.value) {
185
- for (let i = 0; i < this.observers.length; i++) {
186
- this.observers[i].state = DIRTY;
187
- }
188
- }
189
- this.state = CLEAN;
190
- }
191
- }
192
- const scheduler = {
193
- add: (scheduler) => {
194
- scheduler.tasks.add(task);
195
- schedulers.add(scheduler);
196
- },
197
- delete: (scheduler) => {
198
- scheduler.tasks.delete(task);
199
- schedulers.delete(scheduler);
200
- }
201
- };
202
- /* harmony default export */ const reactive = (Reactive);
203
-
204
-
205
- ;// CONCATENATED MODULE: ./src/methods/effect.ts
206
-
207
- /* harmony default export */ const effect = ((fn) => {
208
- new reactive(fn, true);
209
- });
210
-
211
- ;// CONCATENATED MODULE: ./src/methods/reactive.ts
212
-
213
- function factory(value) {
214
- if (typeof value === 'object' && value !== null && (value.constructor === Object)) {
215
- return obj(value);
216
- }
217
- return new reactive(value);
218
- }
219
- function fn(value) {
220
- let fn = new reactive(value);
221
- return (...args) => {
222
- let value = fn.get();
223
- if (args.length && typeof value === 'function') {
224
- value = value(...args);
225
- }
226
- return value;
227
- };
228
- }
229
- function obj(values) {
230
- let lazy = {}, properties = {};
231
- for (let key in values) {
232
- properties[key] = {
233
- get() {
234
- if (!lazy[key]) {
235
- lazy[key] = factory(values[key]);
236
- }
237
- if (lazy[key] instanceof reactive) {
238
- return lazy[key].get();
239
- }
240
- return lazy[key];
241
- },
242
- set(value) {
243
- if (!lazy[key]) {
244
- lazy[key] = factory(values[key]);
245
- }
246
- if (lazy[key] instanceof reactive) {
247
- lazy[key].set(value);
248
- }
249
- else {
250
- lazy[key] = factory(value);
251
- }
252
- }
253
- };
254
- }
255
- return Object.defineProperties({}, properties);
256
- }
257
- ;
258
- /* harmony default export */ const methods_reactive = ((value) => {
259
- if (typeof value === 'function') {
260
- return fn(value);
261
- }
262
- return factory(value);
263
- });
264
-
265
- ;// CONCATENATED MODULE: ./src/methods/index.ts
266
-
267
-
268
-
269
-
270
- ;// CONCATENATED MODULE: ./src/index.ts
271
-
272
-
273
- /* harmony default export */ const src = ({ effect: effect, reactive: methods_reactive, scheduler: scheduler });
274
-
275
-
276
- /******/ })()
277
- ;
1
+ import { scheduler } from './reactive';
2
+ import { effect, reactive } from './methods';
3
+ export default { effect, reactive, scheduler };
4
+ export { effect, reactive, scheduler };
@@ -0,0 +1,4 @@
1
+ import Reactive from '../reactive';
2
+ export default (fn) => {
3
+ new Reactive(fn, true);
4
+ };
@@ -0,0 +1,3 @@
1
+ import effect from './effect';
2
+ import reactive from './reactive';
3
+ export { effect, reactive };
@@ -1,3 +1,3 @@
1
- import { Infer } from '~/types';
1
+ import { Infer } from '../types';
2
2
  declare const _default: <T>(value: T) => Infer<T>;
3
3
  export default _default;
@@ -0,0 +1,52 @@
1
+ import Reactive from '../reactive';
2
+ function factory(value) {
3
+ if (typeof value === 'object' && value !== null && (value.constructor === Object)) {
4
+ return obj(value);
5
+ }
6
+ return new Reactive(value);
7
+ }
8
+ function fn(value) {
9
+ let fn = new Reactive(value);
10
+ return (...args) => {
11
+ let value = fn.get();
12
+ if (args.length && typeof value === 'function') {
13
+ value = value(...args);
14
+ }
15
+ return value;
16
+ };
17
+ }
18
+ function obj(values) {
19
+ let lazy = {}, properties = {};
20
+ for (let key in values) {
21
+ properties[key] = {
22
+ get() {
23
+ if (!lazy[key]) {
24
+ lazy[key] = factory(values[key]);
25
+ }
26
+ if (lazy[key] instanceof Reactive) {
27
+ return lazy[key].get();
28
+ }
29
+ return lazy[key];
30
+ },
31
+ set(value) {
32
+ if (!lazy[key]) {
33
+ lazy[key] = factory(values[key]);
34
+ }
35
+ if (lazy[key] instanceof Reactive) {
36
+ lazy[key].set(value);
37
+ }
38
+ else {
39
+ lazy[key] = factory(value);
40
+ }
41
+ }
42
+ };
43
+ }
44
+ return Object.defineProperties({}, properties);
45
+ }
46
+ ;
47
+ export default (value) => {
48
+ if (typeof value === 'function') {
49
+ return fn(value);
50
+ }
51
+ return factory(value);
52
+ };
@@ -0,0 +1,190 @@
1
+ import { CLEAN, CHECK, DIRTY } from './symbols';
2
+ let index = 0, reaction = null, queue = [], scheduled = false, schedulers = new Set, stack = null;
3
+ function mark(instances, state) {
4
+ if (!instances) {
5
+ return;
6
+ }
7
+ for (let i = 0, n = instances.length; i < n; i++) {
8
+ let instance = instances[i];
9
+ if (instance.state < state) {
10
+ if (instance.effect && instance.state === CLEAN) {
11
+ queue.push(instance);
12
+ if (!scheduled) {
13
+ schedule();
14
+ }
15
+ }
16
+ instance.state = state;
17
+ if (instance.observers) {
18
+ mark(instance.observers, CHECK);
19
+ }
20
+ }
21
+ }
22
+ }
23
+ function schedule() {
24
+ if (scheduled) {
25
+ return;
26
+ }
27
+ for (let scheduler of schedulers) {
28
+ scheduler.schedule();
29
+ }
30
+ scheduled = true;
31
+ }
32
+ async function task() {
33
+ let n = queue.length;
34
+ for (let i = 0; i < n; i++) {
35
+ await queue[i].get();
36
+ }
37
+ queue.splice(0, n);
38
+ scheduled = false;
39
+ if (queue.length) {
40
+ schedule();
41
+ }
42
+ }
43
+ class Reactive {
44
+ fn;
45
+ value;
46
+ effect;
47
+ cleanup = null;
48
+ observers = null;
49
+ sources = null;
50
+ state;
51
+ constructor(data, effect = false) {
52
+ this.effect = effect;
53
+ if (typeof data === 'function') {
54
+ this.fn = data;
55
+ this.state = DIRTY;
56
+ this.value = undefined;
57
+ if (effect) {
58
+ this.update();
59
+ }
60
+ }
61
+ else {
62
+ this.state = CLEAN;
63
+ this.value = data;
64
+ }
65
+ }
66
+ get() {
67
+ if (reaction) {
68
+ if (!stack && reaction.sources && reaction.sources[index] == this) {
69
+ index++;
70
+ }
71
+ else {
72
+ if (!stack) {
73
+ stack = [this];
74
+ }
75
+ else {
76
+ stack.push(this);
77
+ }
78
+ }
79
+ }
80
+ if (this.fn) {
81
+ this.sync();
82
+ }
83
+ return this.value;
84
+ }
85
+ set(value) {
86
+ if (this.value !== value) {
87
+ mark(this.observers, DIRTY);
88
+ }
89
+ this.value = value;
90
+ }
91
+ removeParentObservers() {
92
+ if (!this.sources) {
93
+ return;
94
+ }
95
+ for (let i = index; i < this.sources.length; i++) {
96
+ let source = this.sources[i];
97
+ source.observers[source.observers.findIndex((v) => v === this)] = source.observers[source.observers.length - 1];
98
+ source.observers.pop();
99
+ }
100
+ }
101
+ sync() {
102
+ if (this.state === CHECK && this.sources) {
103
+ for (let i = 0, n = this.sources.length; i < n; i++) {
104
+ this.sources[i].sync();
105
+ if (this.state === DIRTY) {
106
+ break;
107
+ }
108
+ }
109
+ }
110
+ if (this.state === DIRTY) {
111
+ this.update();
112
+ }
113
+ this.state = CLEAN;
114
+ }
115
+ update() {
116
+ let previous = {
117
+ index: index,
118
+ reaction: reaction,
119
+ stack: stack,
120
+ value: this.value
121
+ };
122
+ index = 0;
123
+ reaction = this;
124
+ stack = [];
125
+ try {
126
+ if (this.cleanup) {
127
+ for (let i = 0, n = this.cleanup.length; i < n; i++) {
128
+ this.cleanup[i](this.value);
129
+ }
130
+ this.cleanup.length = 0;
131
+ }
132
+ this.value = this.fn((fn) => {
133
+ if (!this.cleanup) {
134
+ this.cleanup = [fn];
135
+ }
136
+ else {
137
+ this.cleanup.push(fn);
138
+ }
139
+ });
140
+ if (stack.length) {
141
+ this.removeParentObservers();
142
+ if (this.sources && index > 0) {
143
+ this.sources.length = index + stack.length;
144
+ for (let i = 0; i < stack.length; i++) {
145
+ this.sources[index + i] = stack[i];
146
+ }
147
+ }
148
+ else {
149
+ this.sources = stack;
150
+ }
151
+ for (let i = index; i < this.sources.length; i++) {
152
+ let source = this.sources[i];
153
+ if (!source.observers) {
154
+ source.observers = [this];
155
+ }
156
+ else {
157
+ source.observers.push(this);
158
+ }
159
+ }
160
+ }
161
+ else if (this.sources && index < this.sources.length) {
162
+ this.removeParentObservers();
163
+ this.sources.length = index;
164
+ }
165
+ }
166
+ finally {
167
+ index = previous.index;
168
+ reaction = previous.reaction;
169
+ stack = previous.stack;
170
+ }
171
+ if (this.observers && previous.value !== this.value) {
172
+ for (let i = 0; i < this.observers.length; i++) {
173
+ this.observers[i].state = DIRTY;
174
+ }
175
+ }
176
+ this.state = CLEAN;
177
+ }
178
+ }
179
+ const scheduler = {
180
+ add: (scheduler) => {
181
+ scheduler.tasks.add(task);
182
+ schedulers.add(scheduler);
183
+ },
184
+ delete: (scheduler) => {
185
+ scheduler.tasks.delete(task);
186
+ schedulers.delete(scheduler);
187
+ }
188
+ };
189
+ export default Reactive;
190
+ export { scheduler };
@@ -0,0 +1,4 @@
1
+ const CLEAN = 0;
2
+ const CHECK = 1;
3
+ const DIRTY = 2;
4
+ export { CLEAN, CHECK, DIRTY };
package/build/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,19 +1,18 @@
1
1
  {
2
2
  "author": "ICJR",
3
- "browser": "build/index.browser.js",
4
3
  "description": "Reactivity",
5
4
  "devDependencies": {
6
- "@esportsplus/webpack": "^0.0.79"
5
+ "@esportsplus/webpack": "^0.0.80"
7
6
  },
8
7
  "main": "build/index.js",
9
8
  "name": "@esportsplus/reactivity",
10
9
  "private": false,
11
10
  "scripts": {
12
- "build": "webpack",
11
+ "build": "tsc && tsc-alias",
13
12
  "-": "-",
14
13
  "prepare": "npm run build",
15
14
  "prepublishOnly": "npm run build"
16
15
  },
17
16
  "types": "build/index.d.ts",
18
- "version": "0.0.20"
17
+ "version": "0.0.21"
19
18
  }
package/webpack.config.ts DELETED
@@ -1,6 +0,0 @@
1
- import { config, entry } from '@esportsplus/webpack';
2
-
3
-
4
- export default config.library({
5
- entry: entry.js('src/index.ts')
6
- });