watch-state 3.3.3 → 3.4.2
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/Cache/Cache.d.ts +27 -3
- package/Cache/Cache.es6.js +29 -5
- package/Cache/Cache.js +29 -5
- package/Cache/index.d.ts +0 -1
- package/Cache/index.es6.js +1 -1
- package/Cache/index.js +0 -1
- package/Event/Event.d.ts +16 -5
- package/Event/Event.es6.js +21 -10
- package/Event/Event.js +31 -21
- package/Event/index.d.ts +0 -1
- package/Event/index.es6.js +1 -1
- package/Event/index.js +0 -2
- package/README.md +48 -20
- package/State/State.d.ts +1 -2
- package/State/State.es6.js +1 -2
- package/State/State.js +3 -4
- package/State/index.d.ts +0 -1
- package/State/index.es6.js +1 -1
- package/State/index.js +0 -1
- package/Watch/Watch.d.ts +36 -7
- package/Watch/Watch.es6.js +42 -5
- package/Watch/Watch.js +47 -10
- package/Watch/index.d.ts +0 -1
- package/Watch/index.es6.js +1 -1
- package/Watch/index.js +0 -1
- package/constants.d.ts +4 -0
- package/constants.es6.js +10 -0
- package/constants.js +15 -0
- package/index.d.ts +3 -1
- package/index.es6.js +4 -2
- package/index.js +7 -3
- package/package.json +1 -1
- package/types.d.ts +13 -0
- package/utils/createEvent/createEvent.d.ts +14 -0
- package/utils/createEvent/createEvent.es6.js +25 -0
- package/utils/createEvent/createEvent.js +29 -0
- package/utils/createEvent/index.d.ts +1 -0
- package/utils/createEvent/index.es6.js +1 -0
- package/utils/createEvent/index.js +9 -0
- package/utils/index.d.ts +2 -0
- package/utils/index.es6.js +2 -0
- package/utils/index.js +11 -0
- package/utils/onDestroy/index.d.ts +1 -0
- package/utils/onDestroy/index.es6.js +1 -0
- package/utils/onDestroy/index.js +9 -0
- package/utils/onDestroy/onDestroy.d.ts +25 -0
- package/utils/onDestroy/onDestroy.es6.js +32 -0
- package/utils/onDestroy/onDestroy.js +36 -0
- package/watch-state.min.js +1 -1
- package/scope/index.d.ts +0 -2
- package/scope/index.es6.js +0 -1
- package/scope/index.js +0 -7
- package/scope/scope.d.ts +0 -6
- package/scope/scope.es6.js +0 -7
- package/scope/scope.js +0 -9
package/Cache/Cache.d.ts
CHANGED
|
@@ -1,15 +1,39 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Watch } from '../Watch';
|
|
2
|
+
import { Watcher } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* You can cache computed state.
|
|
5
|
+
* The watcher will not be triggered while new result is the same.
|
|
6
|
+
* ```javascript
|
|
7
|
+
* const name = new State('Foo')
|
|
8
|
+
* const surname = new State('Bar')
|
|
9
|
+
*
|
|
10
|
+
* const fullName = new Cache(() => (
|
|
11
|
+
* `${name.value} ${surname.value[0]}`
|
|
12
|
+
* ))
|
|
13
|
+
*
|
|
14
|
+
* new Watch(() => {
|
|
15
|
+
* console.log(fullName.value)
|
|
16
|
+
* })
|
|
17
|
+
* // console.log('Foo B')
|
|
18
|
+
*
|
|
19
|
+
* surname.value = 'Baz'
|
|
20
|
+
* // nothing happens
|
|
21
|
+
*
|
|
22
|
+
* surname.value = 'Quux'
|
|
23
|
+
* // console.log('Foo Q')
|
|
24
|
+
* ```
|
|
25
|
+
* */
|
|
2
26
|
export declare class Cache<V = any> extends Watch {
|
|
3
|
-
|
|
27
|
+
protected updated: boolean;
|
|
4
28
|
private _state;
|
|
5
29
|
constructor(watcher: Watcher, freeParent?: boolean, fireImmediately?: boolean);
|
|
6
30
|
destroy(): void;
|
|
7
31
|
run(): void;
|
|
8
32
|
get hasWatcher(): boolean;
|
|
33
|
+
get size(): number;
|
|
9
34
|
deepUpdate(): void;
|
|
10
35
|
update(): void;
|
|
11
36
|
private get state();
|
|
12
37
|
get value(): V;
|
|
13
38
|
set value(value: V);
|
|
14
39
|
}
|
|
15
|
-
export default Cache;
|
package/Cache/Cache.es6.js
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
import { Watch } from '../Watch/Watch.es6.js';
|
|
2
2
|
import { State } from '../State/State.es6.js';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* You can cache computed state.
|
|
6
|
+
* The watcher will not be triggered while new result is the same.
|
|
7
|
+
* ```javascript
|
|
8
|
+
* const name = new State('Foo')
|
|
9
|
+
* const surname = new State('Bar')
|
|
10
|
+
*
|
|
11
|
+
* const fullName = new Cache(() => (
|
|
12
|
+
* `${name.value} ${surname.value[0]}`
|
|
13
|
+
* ))
|
|
14
|
+
*
|
|
15
|
+
* new Watch(() => {
|
|
16
|
+
* console.log(fullName.value)
|
|
17
|
+
* })
|
|
18
|
+
* // console.log('Foo B')
|
|
19
|
+
*
|
|
20
|
+
* surname.value = 'Baz'
|
|
21
|
+
* // nothing happens
|
|
22
|
+
*
|
|
23
|
+
* surname.value = 'Quux'
|
|
24
|
+
* // console.log('Foo Q')
|
|
25
|
+
* ```
|
|
26
|
+
* */
|
|
4
27
|
class Cache extends Watch {
|
|
5
28
|
constructor(watcher, freeParent, fireImmediately) {
|
|
6
29
|
super(watcher, freeParent, !fireImmediately);
|
|
@@ -13,8 +36,7 @@ class Cache extends Watch {
|
|
|
13
36
|
this.value = super.run();
|
|
14
37
|
}
|
|
15
38
|
get hasWatcher() {
|
|
16
|
-
|
|
17
|
-
if (this.updated && ((_b = (_a = this._state) === null || _a === void 0 ? void 0 : _a.watchers) === null || _b === void 0 ? void 0 : _b.size)) {
|
|
39
|
+
if (this.updated && this.size) {
|
|
18
40
|
for (const watcher of this._state.watchers) {
|
|
19
41
|
if (!(watcher instanceof Cache) || watcher.hasWatcher) {
|
|
20
42
|
return true;
|
|
@@ -22,11 +44,14 @@ class Cache extends Watch {
|
|
|
22
44
|
}
|
|
23
45
|
}
|
|
24
46
|
}
|
|
25
|
-
|
|
47
|
+
get size() {
|
|
26
48
|
var _a, _b;
|
|
49
|
+
return (_b = (_a = this._state) === null || _a === void 0 ? void 0 : _a.watchers) === null || _b === void 0 ? void 0 : _b.size;
|
|
50
|
+
}
|
|
51
|
+
deepUpdate() {
|
|
27
52
|
this.updated = false;
|
|
28
53
|
this.destroy();
|
|
29
|
-
if (
|
|
54
|
+
if (this.size) {
|
|
30
55
|
for (const watcher of this._state.watchers) {
|
|
31
56
|
watcher.deepUpdate();
|
|
32
57
|
}
|
|
@@ -56,5 +81,4 @@ class Cache extends Watch {
|
|
|
56
81
|
}
|
|
57
82
|
}
|
|
58
83
|
|
|
59
|
-
export default Cache;
|
|
60
84
|
export { Cache };
|
package/Cache/Cache.js
CHANGED
|
@@ -5,6 +5,29 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var Watch = require('../Watch/Watch.js');
|
|
6
6
|
var State = require('../State/State.js');
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* You can cache computed state.
|
|
10
|
+
* The watcher will not be triggered while new result is the same.
|
|
11
|
+
* ```javascript
|
|
12
|
+
* const name = new State('Foo')
|
|
13
|
+
* const surname = new State('Bar')
|
|
14
|
+
*
|
|
15
|
+
* const fullName = new Cache(() => (
|
|
16
|
+
* `${name.value} ${surname.value[0]}`
|
|
17
|
+
* ))
|
|
18
|
+
*
|
|
19
|
+
* new Watch(() => {
|
|
20
|
+
* console.log(fullName.value)
|
|
21
|
+
* })
|
|
22
|
+
* // console.log('Foo B')
|
|
23
|
+
*
|
|
24
|
+
* surname.value = 'Baz'
|
|
25
|
+
* // nothing happens
|
|
26
|
+
*
|
|
27
|
+
* surname.value = 'Quux'
|
|
28
|
+
* // console.log('Foo Q')
|
|
29
|
+
* ```
|
|
30
|
+
* */
|
|
8
31
|
class Cache extends Watch.Watch {
|
|
9
32
|
constructor(watcher, freeParent, fireImmediately) {
|
|
10
33
|
super(watcher, freeParent, !fireImmediately);
|
|
@@ -17,8 +40,7 @@ class Cache extends Watch.Watch {
|
|
|
17
40
|
this.value = super.run();
|
|
18
41
|
}
|
|
19
42
|
get hasWatcher() {
|
|
20
|
-
|
|
21
|
-
if (this.updated && ((_b = (_a = this._state) === null || _a === void 0 ? void 0 : _a.watchers) === null || _b === void 0 ? void 0 : _b.size)) {
|
|
43
|
+
if (this.updated && this.size) {
|
|
22
44
|
for (const watcher of this._state.watchers) {
|
|
23
45
|
if (!(watcher instanceof Cache) || watcher.hasWatcher) {
|
|
24
46
|
return true;
|
|
@@ -26,11 +48,14 @@ class Cache extends Watch.Watch {
|
|
|
26
48
|
}
|
|
27
49
|
}
|
|
28
50
|
}
|
|
29
|
-
|
|
51
|
+
get size() {
|
|
30
52
|
var _a, _b;
|
|
53
|
+
return (_b = (_a = this._state) === null || _a === void 0 ? void 0 : _a.watchers) === null || _b === void 0 ? void 0 : _b.size;
|
|
54
|
+
}
|
|
55
|
+
deepUpdate() {
|
|
31
56
|
this.updated = false;
|
|
32
57
|
this.destroy();
|
|
33
|
-
if (
|
|
58
|
+
if (this.size) {
|
|
34
59
|
for (const watcher of this._state.watchers) {
|
|
35
60
|
watcher.deepUpdate();
|
|
36
61
|
}
|
|
@@ -61,4 +86,3 @@ class Cache extends Watch.Watch {
|
|
|
61
86
|
}
|
|
62
87
|
|
|
63
88
|
exports.Cache = Cache;
|
|
64
|
-
exports.default = Cache;
|
package/Cache/index.d.ts
CHANGED
package/Cache/index.es6.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { Cache
|
|
1
|
+
export { Cache } from './Cache.es6.js';
|
package/Cache/index.js
CHANGED
package/Event/Event.d.ts
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
import Watch from '../Watch';
|
|
2
|
-
import Cache from '../Cache';
|
|
1
|
+
import { Watch } from '../Watch';
|
|
2
|
+
import { Cache } from '../Cache';
|
|
3
3
|
export declare class Event {
|
|
4
4
|
watchers: Set<Watch | Cache>;
|
|
5
|
-
activeWatchers: Set<Watch | Cache>;
|
|
6
5
|
activeWatcher: Watch;
|
|
7
6
|
add(target: Watch | Cache): void;
|
|
8
7
|
start(): void;
|
|
9
8
|
end(): void;
|
|
10
9
|
private forceUpdate;
|
|
10
|
+
/**
|
|
11
|
+
* You can run watchers of a state with `update` method.
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const count = new State(0)
|
|
14
|
+
*
|
|
15
|
+
* new Watch(() => {
|
|
16
|
+
* console.log(count.value)
|
|
17
|
+
* })
|
|
18
|
+
* // console.log(0)
|
|
19
|
+
*
|
|
20
|
+
* count.update()
|
|
21
|
+
* // console.log(0)
|
|
22
|
+
* ```
|
|
23
|
+
* */
|
|
11
24
|
update(): void;
|
|
12
25
|
}
|
|
13
|
-
export declare const globalEvent: Event;
|
|
14
|
-
export default Event;
|
package/Event/Event.es6.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import scope from '../
|
|
1
|
+
import { scope, globalEvent } from '../constants.es6.js';
|
|
2
2
|
|
|
3
3
|
class Event {
|
|
4
4
|
add(target) {
|
|
@@ -12,7 +12,7 @@ class Event {
|
|
|
12
12
|
else {
|
|
13
13
|
watchers = this.watchers = new Set([target]);
|
|
14
14
|
}
|
|
15
|
-
target.
|
|
15
|
+
target.onClear(() => watchers.delete(target));
|
|
16
16
|
}
|
|
17
17
|
start() {
|
|
18
18
|
if (!scope.activeEvent) {
|
|
@@ -30,13 +30,26 @@ class Event {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
forceUpdate() {
|
|
33
|
-
const {
|
|
34
|
-
this.
|
|
35
|
-
|
|
36
|
-
for (const watcher of this.activeWatchers) {
|
|
33
|
+
const { watchers } = this;
|
|
34
|
+
this.watchers = undefined;
|
|
35
|
+
for (const watcher of watchers) {
|
|
37
36
|
watcher.update();
|
|
38
37
|
}
|
|
39
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* You can run watchers of a state with `update` method.
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const count = new State(0)
|
|
43
|
+
*
|
|
44
|
+
* new Watch(() => {
|
|
45
|
+
* console.log(count.value)
|
|
46
|
+
* })
|
|
47
|
+
* // console.log(0)
|
|
48
|
+
*
|
|
49
|
+
* count.update()
|
|
50
|
+
* // console.log(0)
|
|
51
|
+
* ```
|
|
52
|
+
* */
|
|
40
53
|
update() {
|
|
41
54
|
var _a;
|
|
42
55
|
if ((_a = this.watchers) === null || _a === void 0 ? void 0 : _a.size) {
|
|
@@ -50,8 +63,6 @@ class Event {
|
|
|
50
63
|
}
|
|
51
64
|
}
|
|
52
65
|
}
|
|
53
|
-
}
|
|
54
|
-
const globalEvent = new Event();
|
|
66
|
+
}
|
|
55
67
|
|
|
56
|
-
export
|
|
57
|
-
export { Event, globalEvent };
|
|
68
|
+
export { Event };
|
package/Event/Event.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var constants = require('../constants.js');
|
|
6
6
|
|
|
7
7
|
class Event {
|
|
8
8
|
add(target) {
|
|
@@ -16,47 +16,57 @@ class Event {
|
|
|
16
16
|
else {
|
|
17
17
|
watchers = this.watchers = new Set([target]);
|
|
18
18
|
}
|
|
19
|
-
target.
|
|
19
|
+
target.onClear(() => watchers.delete(target));
|
|
20
20
|
}
|
|
21
21
|
start() {
|
|
22
|
-
if (!scope.activeEvent) {
|
|
23
|
-
this.activeWatcher = scope.activeWatcher;
|
|
24
|
-
scope.activeWatcher = undefined;
|
|
25
|
-
scope.activeEvent = this;
|
|
22
|
+
if (!constants.scope.activeEvent) {
|
|
23
|
+
this.activeWatcher = constants.scope.activeWatcher;
|
|
24
|
+
constants.scope.activeWatcher = undefined;
|
|
25
|
+
constants.scope.activeEvent = this;
|
|
26
26
|
}
|
|
27
|
-
scope.activeEventDeep++;
|
|
27
|
+
constants.scope.activeEventDeep++;
|
|
28
28
|
}
|
|
29
29
|
end() {
|
|
30
|
-
if (!--scope.activeEventDeep && scope.activeEvent === this) {
|
|
31
|
-
scope.activeEvent = undefined;
|
|
30
|
+
if (!--constants.scope.activeEventDeep && constants.scope.activeEvent === this) {
|
|
31
|
+
constants.scope.activeEvent = undefined;
|
|
32
32
|
this.update();
|
|
33
|
-
scope.activeWatcher = this.activeWatcher;
|
|
33
|
+
constants.scope.activeWatcher = this.activeWatcher;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
forceUpdate() {
|
|
37
|
-
const {
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
for (const watcher of this.activeWatchers) {
|
|
37
|
+
const { watchers } = this;
|
|
38
|
+
this.watchers = undefined;
|
|
39
|
+
for (const watcher of watchers) {
|
|
41
40
|
watcher.update();
|
|
42
41
|
}
|
|
43
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* You can run watchers of a state with `update` method.
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const count = new State(0)
|
|
47
|
+
*
|
|
48
|
+
* new Watch(() => {
|
|
49
|
+
* console.log(count.value)
|
|
50
|
+
* })
|
|
51
|
+
* // console.log(0)
|
|
52
|
+
*
|
|
53
|
+
* count.update()
|
|
54
|
+
* // console.log(0)
|
|
55
|
+
* ```
|
|
56
|
+
* */
|
|
44
57
|
update() {
|
|
45
58
|
var _a;
|
|
46
59
|
if ((_a = this.watchers) === null || _a === void 0 ? void 0 : _a.size) {
|
|
47
|
-
if (this === globalEvent) {
|
|
60
|
+
if (this === constants.globalEvent) {
|
|
48
61
|
this.forceUpdate();
|
|
49
62
|
}
|
|
50
63
|
else {
|
|
51
|
-
globalEvent.start();
|
|
64
|
+
constants.globalEvent.start();
|
|
52
65
|
this.forceUpdate();
|
|
53
|
-
globalEvent.end();
|
|
66
|
+
constants.globalEvent.end();
|
|
54
67
|
}
|
|
55
68
|
}
|
|
56
69
|
}
|
|
57
|
-
}
|
|
58
|
-
const globalEvent = new Event();
|
|
70
|
+
}
|
|
59
71
|
|
|
60
72
|
exports.Event = Event;
|
|
61
|
-
exports.default = Event;
|
|
62
|
-
exports.globalEvent = globalEvent;
|
package/Event/index.d.ts
CHANGED
package/Event/index.es6.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { Event
|
|
1
|
+
export { Event } from './Event.es6.js';
|
package/Event/index.js
CHANGED
package/README.md
CHANGED
|
@@ -54,16 +54,19 @@
|
|
|
54
54
|
<img src="https://img.shields.io/bundlephobia/minzip/watch-state" alt="watch-state minzipped size">
|
|
55
55
|
</a>
|
|
56
56
|
<a href="https://www.npmtrends.com/watch-state" target="_blank">
|
|
57
|
-
<img src="https://img.shields.io/npm/dm/watch-state.svg" alt="watch-state
|
|
57
|
+
<img src="https://img.shields.io/npm/dm/watch-state.svg" alt="watch-state downloads">
|
|
58
|
+
</a>
|
|
59
|
+
<a href="https://packagequality.com/#?package=watch-state" target="_blank">
|
|
60
|
+
<img src="https://packagequality.com/shield/watch-state.svg" alt="watch-state quality">
|
|
58
61
|
</a>
|
|
59
62
|
<a href="https://github.com/d8corp/watch-state/blob/master/LICENSE" target="_blank">
|
|
60
63
|
<img src="https://img.shields.io/npm/l/watch-state" alt="watch-state license">
|
|
61
64
|
</a>
|
|
62
65
|
<a href="https://changelogs.xyz/watch-state" target="_blank">
|
|
63
|
-
<img src="https://img.shields.io/badge/Changelog-⋮-brightgreen" alt="watch-state
|
|
66
|
+
<img src="https://img.shields.io/badge/Changelog-⋮-brightgreen" alt="watch-state changelog">
|
|
64
67
|
</a>
|
|
65
68
|
<a href="https://d8corp.github.io/watch-state/coverage/lcov-report" target="_blank">
|
|
66
|
-
<img src="https://github.com/d8corp/watch-state/workflows/tests/badge.svg" alt="watch-state
|
|
69
|
+
<img src="https://github.com/d8corp/watch-state/workflows/tests/badge.svg" alt="watch-state tests">
|
|
67
70
|
</a>
|
|
68
71
|
</div>
|
|
69
72
|
<br>
|
|
@@ -123,11 +126,12 @@ const {
|
|
|
123
126
|
Event
|
|
124
127
|
} = watchState
|
|
125
128
|
```
|
|
129
|
+
|
|
126
130
|
## Usage
|
|
127
131
|
### Simple example:
|
|
128
132
|
You can create an instance of `State` and **watch** it's **value**.
|
|
129
133
|
```javascript
|
|
130
|
-
import {Watch, State} from 'watch-state'
|
|
134
|
+
import { Watch, State } from 'watch-state'
|
|
131
135
|
|
|
132
136
|
const count = new State(0)
|
|
133
137
|
|
|
@@ -140,6 +144,7 @@ count.value++
|
|
|
140
144
|
count.value++
|
|
141
145
|
// console.log(2)
|
|
142
146
|
```
|
|
147
|
+
|
|
143
148
|
### Update argument:
|
|
144
149
|
You can check if the watching ran first by `update` argument.
|
|
145
150
|
```javascript
|
|
@@ -196,6 +201,7 @@ new Watch(() => {
|
|
|
196
201
|
count.update()
|
|
197
202
|
// console.log(0)
|
|
198
203
|
```
|
|
204
|
+
|
|
199
205
|
### Force update of Watch
|
|
200
206
|
You can run a watcher even when it's states are not updated.
|
|
201
207
|
```typescript
|
|
@@ -209,7 +215,8 @@ const watcher = new Watch(() => {
|
|
|
209
215
|
watcher.update()
|
|
210
216
|
// console.log(0)
|
|
211
217
|
```
|
|
212
|
-
|
|
218
|
+
|
|
219
|
+
### destroy
|
|
213
220
|
You can stop watching by `destroy` method of `Watch`.
|
|
214
221
|
```javascript
|
|
215
222
|
const count = new State(0)
|
|
@@ -227,26 +234,30 @@ watcher.destroy()
|
|
|
227
234
|
count.value++
|
|
228
235
|
// nothing happens
|
|
229
236
|
```
|
|
230
|
-
### Watch.onDestroy()
|
|
231
|
-
You can react on destruction of `Watch` by `onDestroy` method.
|
|
232
|
-
```javascript
|
|
233
|
-
const watcher = new Watch(() => {})
|
|
234
237
|
|
|
235
|
-
|
|
236
|
-
|
|
238
|
+
### onDestroy()
|
|
239
|
+
You can subscribe on destroy or update of watcher
|
|
240
|
+
```javascript
|
|
241
|
+
const count = new State(0)
|
|
242
|
+
const watcher = new Watch(() => {
|
|
243
|
+
console.log('count', count.value)
|
|
244
|
+
// the order does not matter
|
|
245
|
+
onDestroy(() => console.log('destructor'))
|
|
237
246
|
})
|
|
247
|
+
// console.log('count', 0)
|
|
238
248
|
|
|
239
|
-
|
|
249
|
+
count.value++
|
|
240
250
|
// console.log('destructor')
|
|
241
|
-
|
|
242
|
-
`onDestructor` returns `this` so you can use **fluent interface**.
|
|
243
|
-
```javascript
|
|
244
|
-
const watcher = new Watch(() => {})
|
|
245
|
-
.onDestroy(() => console.log('destructor'))
|
|
251
|
+
// console.log('count', 1)
|
|
246
252
|
|
|
247
253
|
watcher.destroy()
|
|
248
254
|
// console.log('destructor')
|
|
255
|
+
|
|
256
|
+
watcher.destroy()
|
|
257
|
+
count.value++
|
|
258
|
+
// nothing happens
|
|
249
259
|
```
|
|
260
|
+
|
|
250
261
|
### Deep watch:
|
|
251
262
|
You can use `Watch` inside a watcher.
|
|
252
263
|
Each watcher reacts on that states which used only inside it.
|
|
@@ -274,6 +285,7 @@ watching.value = false
|
|
|
274
285
|
state.value++
|
|
275
286
|
// nothing happens
|
|
276
287
|
```
|
|
288
|
+
|
|
277
289
|
### Cache:
|
|
278
290
|
You can cache computed state.
|
|
279
291
|
The watcher will not be triggered while new result is the same.
|
|
@@ -333,6 +345,7 @@ console.log(sortedList.value)
|
|
|
333
345
|
// console.log('computing')
|
|
334
346
|
// console.log(['a', 'b', 'c'])
|
|
335
347
|
```
|
|
348
|
+
|
|
336
349
|
### Event:
|
|
337
350
|
Use `Event` when you change several states to run their watchers after the event finished.
|
|
338
351
|
```javascript
|
|
@@ -366,7 +379,7 @@ new Watch(() => {
|
|
|
366
379
|
|
|
367
380
|
You can use `globalEvent` every time if you do not want to extend the Event functionality.
|
|
368
381
|
```typescript
|
|
369
|
-
import {State, globalEvent} from 'watch-state'
|
|
382
|
+
import { State, globalEvent } from 'watch-state'
|
|
370
383
|
const count = new State(0)
|
|
371
384
|
|
|
372
385
|
new Watch(() => {
|
|
@@ -375,6 +388,20 @@ new Watch(() => {
|
|
|
375
388
|
globalEvent.end()
|
|
376
389
|
})
|
|
377
390
|
```
|
|
391
|
+
|
|
392
|
+
### createEvent
|
|
393
|
+
You can create event function with createEvent
|
|
394
|
+
```typescript
|
|
395
|
+
import { State, createEvent } from 'watch-state'
|
|
396
|
+
|
|
397
|
+
const count = new State(0)
|
|
398
|
+
const increase = createEvent(() => {
|
|
399
|
+
console.log(count.value++)
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
new Watch(increase)
|
|
403
|
+
```
|
|
404
|
+
|
|
378
405
|
### Typescript:
|
|
379
406
|
Generic of `State`
|
|
380
407
|
```typescript
|
|
@@ -388,16 +415,17 @@ Generic of `Cache`
|
|
|
388
415
|
new Cache<string>(() => false)
|
|
389
416
|
// error, target of cache should return string
|
|
390
417
|
```
|
|
418
|
+
|
|
391
419
|
## Performance
|
|
392
420
|
You can check the performance test with **[MobX](https://www.npmjs.com/package/mobx)**, **[Effector](https://www.npmjs.com/package/effector)**, **[Storeon](https://www.npmjs.com/package/storeon)**, **[Mazzard](https://www.npmjs.com/package/mazzard)** and **[Redux](https://www.npmjs.com/package/redux)**.
|
|
393
421
|
Clone the repo, install packages and run this command
|
|
394
422
|
```shell
|
|
395
423
|
npm run speed
|
|
396
424
|
```
|
|
397
|
-
|
|
398
|
-

|
|
425
|
+
|
|
399
426
|
## Links
|
|
400
427
|
You can find more tools [here](https://www.npmjs.com/search?q=%40watch-state)
|
|
428
|
+
|
|
401
429
|
## Issues
|
|
402
430
|
If you find a bug or have a suggestion, please file an issue on [GitHub](https://github.com/d8corp/watch-state/issues)
|
|
403
431
|
|
package/State/State.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Event from '../Event';
|
|
1
|
+
import { Event } from '../Event';
|
|
2
2
|
export declare class State<T = any> extends Event {
|
|
3
3
|
state?: T;
|
|
4
4
|
constructor(state?: T);
|
|
@@ -22,4 +22,3 @@ export declare class State<T = any> extends Event {
|
|
|
22
22
|
* */
|
|
23
23
|
set value(value: T);
|
|
24
24
|
}
|
|
25
|
-
export default State;
|
package/State/State.es6.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Event } from '../Event/Event.es6.js';
|
|
2
|
-
import scope from '../
|
|
2
|
+
import { scope } from '../constants.es6.js';
|
|
3
3
|
|
|
4
4
|
class State extends Event {
|
|
5
5
|
constructor(state) {
|
|
@@ -37,5 +37,4 @@ class State extends Event {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
export default State;
|
|
41
40
|
export { State };
|
package/State/State.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var Event = require('../Event/Event.js');
|
|
6
|
-
var
|
|
6
|
+
var constants = require('../constants.js');
|
|
7
7
|
|
|
8
8
|
class State extends Event.Event {
|
|
9
9
|
constructor(state) {
|
|
@@ -18,8 +18,8 @@ class State extends Event.Event {
|
|
|
18
18
|
* ```
|
|
19
19
|
* */
|
|
20
20
|
get value() {
|
|
21
|
-
if (scope.activeWatcher) {
|
|
22
|
-
this.add(scope.activeWatcher);
|
|
21
|
+
if (constants.scope.activeWatcher) {
|
|
22
|
+
this.add(constants.scope.activeWatcher);
|
|
23
23
|
}
|
|
24
24
|
return this.state;
|
|
25
25
|
}
|
|
@@ -42,4 +42,3 @@ class State extends Event.Event {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
exports.State = State;
|
|
45
|
-
exports.default = State;
|
package/State/index.d.ts
CHANGED
package/State/index.es6.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { State
|
|
1
|
+
export { State } from './State.es6.js';
|
package/State/index.js
CHANGED
package/Watch/Watch.d.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
(update?: boolean): R;
|
|
3
|
-
}
|
|
4
|
-
export interface Destructor<R = any> {
|
|
5
|
-
(): R;
|
|
6
|
-
}
|
|
1
|
+
import { Destructor, Watcher } from '../types';
|
|
7
2
|
export declare class Watch {
|
|
8
3
|
private readonly watcher;
|
|
9
4
|
destructors: Destructor[];
|
|
@@ -12,8 +7,42 @@ export declare class Watch {
|
|
|
12
7
|
protected run(): any;
|
|
13
8
|
protected watchRun(): void;
|
|
14
9
|
protected forceUpdate(): void;
|
|
10
|
+
/**
|
|
11
|
+
* You can run a watcher even when it's states are not updated.
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const count = new State(0)
|
|
14
|
+
*
|
|
15
|
+
* const watcher = new Watch(() => {
|
|
16
|
+
* console.log(count.value)
|
|
17
|
+
* })
|
|
18
|
+
* // console.log(0)
|
|
19
|
+
*
|
|
20
|
+
* watcher.update()
|
|
21
|
+
* // console.log(0)
|
|
22
|
+
* ```
|
|
23
|
+
* */
|
|
15
24
|
update(): void;
|
|
25
|
+
/**
|
|
26
|
+
* You can stop watching by `destroy` method of `Watch`.
|
|
27
|
+
* ```javascript
|
|
28
|
+
* const count = new State(0)
|
|
29
|
+
*
|
|
30
|
+
* const watcher = new Watch(() => {
|
|
31
|
+
* console.log(count.value)
|
|
32
|
+
* })
|
|
33
|
+
* // console.log(0)
|
|
34
|
+
*
|
|
35
|
+
* count.value++
|
|
36
|
+
* // console.log(1)
|
|
37
|
+
*
|
|
38
|
+
* watcher.destroy()
|
|
39
|
+
*
|
|
40
|
+
* count.value++
|
|
41
|
+
* // nothing happens
|
|
42
|
+
* ```
|
|
43
|
+
* */
|
|
16
44
|
destroy(): void;
|
|
45
|
+
onClear(callback: Destructor): this;
|
|
46
|
+
/** @deprecated use onClear */
|
|
17
47
|
onDestroy(callback: Destructor): this;
|
|
18
48
|
}
|
|
19
|
-
export default Watch;
|
package/Watch/Watch.es6.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import scope from '../
|
|
1
|
+
import { scope } from '../constants.es6.js';
|
|
2
|
+
import { onDestroy } from '../utils/onDestroy/onDestroy.es6.js';
|
|
2
3
|
|
|
3
4
|
class Watch {
|
|
4
5
|
constructor(watcher, freeParent, freeUpdate) {
|
|
5
6
|
this.watcher = watcher;
|
|
6
7
|
this.ran = false;
|
|
7
|
-
if (!freeParent
|
|
8
|
-
|
|
8
|
+
if (!freeParent) {
|
|
9
|
+
onDestroy(() => this.destroy());
|
|
9
10
|
}
|
|
10
11
|
if (!freeUpdate) {
|
|
11
12
|
this.watchRun();
|
|
@@ -26,6 +27,20 @@ class Watch {
|
|
|
26
27
|
this.destroy();
|
|
27
28
|
this.watchRun();
|
|
28
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* You can run a watcher even when it's states are not updated.
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const count = new State(0)
|
|
34
|
+
*
|
|
35
|
+
* const watcher = new Watch(() => {
|
|
36
|
+
* console.log(count.value)
|
|
37
|
+
* })
|
|
38
|
+
* // console.log(0)
|
|
39
|
+
*
|
|
40
|
+
* watcher.update()
|
|
41
|
+
* // console.log(0)
|
|
42
|
+
* ```
|
|
43
|
+
* */
|
|
29
44
|
update() {
|
|
30
45
|
this.destroy();
|
|
31
46
|
if (scope.activeEvent) {
|
|
@@ -35,6 +50,25 @@ class Watch {
|
|
|
35
50
|
this.watchRun();
|
|
36
51
|
}
|
|
37
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* You can stop watching by `destroy` method of `Watch`.
|
|
55
|
+
* ```javascript
|
|
56
|
+
* const count = new State(0)
|
|
57
|
+
*
|
|
58
|
+
* const watcher = new Watch(() => {
|
|
59
|
+
* console.log(count.value)
|
|
60
|
+
* })
|
|
61
|
+
* // console.log(0)
|
|
62
|
+
*
|
|
63
|
+
* count.value++
|
|
64
|
+
* // console.log(1)
|
|
65
|
+
*
|
|
66
|
+
* watcher.destroy()
|
|
67
|
+
*
|
|
68
|
+
* count.value++
|
|
69
|
+
* // nothing happens
|
|
70
|
+
* ```
|
|
71
|
+
* */
|
|
38
72
|
destroy() {
|
|
39
73
|
const { destructors } = this;
|
|
40
74
|
if (destructors) {
|
|
@@ -42,7 +76,7 @@ class Watch {
|
|
|
42
76
|
destructors.forEach(e => e());
|
|
43
77
|
}
|
|
44
78
|
}
|
|
45
|
-
|
|
79
|
+
onClear(callback) {
|
|
46
80
|
if (this.destructors) {
|
|
47
81
|
this.destructors.push(callback);
|
|
48
82
|
}
|
|
@@ -51,7 +85,10 @@ class Watch {
|
|
|
51
85
|
}
|
|
52
86
|
return this;
|
|
53
87
|
}
|
|
88
|
+
/** @deprecated use onClear */
|
|
89
|
+
onDestroy(callback) {
|
|
90
|
+
return this.onClear(callback);
|
|
91
|
+
}
|
|
54
92
|
}
|
|
55
93
|
|
|
56
|
-
export default Watch;
|
|
57
94
|
export { Watch };
|
package/Watch/Watch.js
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var constants = require('../constants.js');
|
|
6
|
+
var onDestroy = require('../utils/onDestroy/onDestroy.js');
|
|
6
7
|
|
|
7
8
|
class Watch {
|
|
8
9
|
constructor(watcher, freeParent, freeUpdate) {
|
|
9
10
|
this.watcher = watcher;
|
|
10
11
|
this.ran = false;
|
|
11
|
-
if (!freeParent
|
|
12
|
-
|
|
12
|
+
if (!freeParent) {
|
|
13
|
+
onDestroy.onDestroy(() => this.destroy());
|
|
13
14
|
}
|
|
14
15
|
if (!freeUpdate) {
|
|
15
16
|
this.watchRun();
|
|
@@ -21,24 +22,57 @@ class Watch {
|
|
|
21
22
|
return this.watcher(ran);
|
|
22
23
|
}
|
|
23
24
|
watchRun() {
|
|
24
|
-
const prevWatcher = scope.activeWatcher;
|
|
25
|
-
scope.activeWatcher = this;
|
|
25
|
+
const prevWatcher = constants.scope.activeWatcher;
|
|
26
|
+
constants.scope.activeWatcher = this;
|
|
26
27
|
this.run();
|
|
27
|
-
scope.activeWatcher = prevWatcher;
|
|
28
|
+
constants.scope.activeWatcher = prevWatcher;
|
|
28
29
|
}
|
|
29
30
|
forceUpdate() {
|
|
30
31
|
this.destroy();
|
|
31
32
|
this.watchRun();
|
|
32
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* You can run a watcher even when it's states are not updated.
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const count = new State(0)
|
|
38
|
+
*
|
|
39
|
+
* const watcher = new Watch(() => {
|
|
40
|
+
* console.log(count.value)
|
|
41
|
+
* })
|
|
42
|
+
* // console.log(0)
|
|
43
|
+
*
|
|
44
|
+
* watcher.update()
|
|
45
|
+
* // console.log(0)
|
|
46
|
+
* ```
|
|
47
|
+
* */
|
|
33
48
|
update() {
|
|
34
49
|
this.destroy();
|
|
35
|
-
if (scope.activeEvent) {
|
|
36
|
-
scope.activeEvent.add(this);
|
|
50
|
+
if (constants.scope.activeEvent) {
|
|
51
|
+
constants.scope.activeEvent.add(this);
|
|
37
52
|
}
|
|
38
53
|
else {
|
|
39
54
|
this.watchRun();
|
|
40
55
|
}
|
|
41
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* You can stop watching by `destroy` method of `Watch`.
|
|
59
|
+
* ```javascript
|
|
60
|
+
* const count = new State(0)
|
|
61
|
+
*
|
|
62
|
+
* const watcher = new Watch(() => {
|
|
63
|
+
* console.log(count.value)
|
|
64
|
+
* })
|
|
65
|
+
* // console.log(0)
|
|
66
|
+
*
|
|
67
|
+
* count.value++
|
|
68
|
+
* // console.log(1)
|
|
69
|
+
*
|
|
70
|
+
* watcher.destroy()
|
|
71
|
+
*
|
|
72
|
+
* count.value++
|
|
73
|
+
* // nothing happens
|
|
74
|
+
* ```
|
|
75
|
+
* */
|
|
42
76
|
destroy() {
|
|
43
77
|
const { destructors } = this;
|
|
44
78
|
if (destructors) {
|
|
@@ -46,7 +80,7 @@ class Watch {
|
|
|
46
80
|
destructors.forEach(e => e());
|
|
47
81
|
}
|
|
48
82
|
}
|
|
49
|
-
|
|
83
|
+
onClear(callback) {
|
|
50
84
|
if (this.destructors) {
|
|
51
85
|
this.destructors.push(callback);
|
|
52
86
|
}
|
|
@@ -55,7 +89,10 @@ class Watch {
|
|
|
55
89
|
}
|
|
56
90
|
return this;
|
|
57
91
|
}
|
|
92
|
+
/** @deprecated use onClear */
|
|
93
|
+
onDestroy(callback) {
|
|
94
|
+
return this.onClear(callback);
|
|
95
|
+
}
|
|
58
96
|
}
|
|
59
97
|
|
|
60
98
|
exports.Watch = Watch;
|
|
61
|
-
exports.default = Watch;
|
package/Watch/index.d.ts
CHANGED
package/Watch/index.es6.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { Watch
|
|
1
|
+
export { Watch } from './Watch.es6.js';
|
package/Watch/index.js
CHANGED
package/constants.d.ts
ADDED
package/constants.es6.js
ADDED
package/constants.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var Event = require('./Event/Event.js');
|
|
6
|
+
|
|
7
|
+
const globalEvent = new Event.Event();
|
|
8
|
+
const scope = {
|
|
9
|
+
activeWatcher: undefined,
|
|
10
|
+
activeEvent: undefined,
|
|
11
|
+
activeEventDeep: 0,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
exports.globalEvent = globalEvent;
|
|
15
|
+
exports.scope = scope;
|
package/index.d.ts
CHANGED
package/index.es6.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { Watch } from './Watch/Watch.es6.js';
|
|
2
2
|
export { State } from './State/State.es6.js';
|
|
3
3
|
export { Cache } from './Cache/Cache.es6.js';
|
|
4
|
-
export { Event
|
|
5
|
-
export {
|
|
4
|
+
export { Event } from './Event/Event.es6.js';
|
|
5
|
+
export { onDestroy } from './utils/onDestroy/onDestroy.es6.js';
|
|
6
|
+
export { createEvent } from './utils/createEvent/createEvent.es6.js';
|
|
7
|
+
export { globalEvent, scope } from './constants.es6.js';
|
package/index.js
CHANGED
|
@@ -6,7 +6,9 @@ var Watch = require('./Watch/Watch.js');
|
|
|
6
6
|
var State = require('./State/State.js');
|
|
7
7
|
var Cache = require('./Cache/Cache.js');
|
|
8
8
|
var Event = require('./Event/Event.js');
|
|
9
|
-
var
|
|
9
|
+
var onDestroy = require('./utils/onDestroy/onDestroy.js');
|
|
10
|
+
var createEvent = require('./utils/createEvent/createEvent.js');
|
|
11
|
+
var constants = require('./constants.js');
|
|
10
12
|
|
|
11
13
|
|
|
12
14
|
|
|
@@ -14,5 +16,7 @@ exports.Watch = Watch.Watch;
|
|
|
14
16
|
exports.State = State.State;
|
|
15
17
|
exports.Cache = Cache.Cache;
|
|
16
18
|
exports.Event = Event.Event;
|
|
17
|
-
exports.
|
|
18
|
-
exports.
|
|
19
|
+
exports.onDestroy = onDestroy.onDestroy;
|
|
20
|
+
exports.createEvent = createEvent.createEvent;
|
|
21
|
+
exports.globalEvent = constants.globalEvent;
|
|
22
|
+
exports.scope = constants.scope;
|
package/package.json
CHANGED
package/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Watch } from './Watch';
|
|
2
|
+
import { Event } from './Event';
|
|
3
|
+
export interface Watcher<R = any> {
|
|
4
|
+
(update?: boolean): R;
|
|
5
|
+
}
|
|
6
|
+
export interface Destructor<R = any> {
|
|
7
|
+
(): R;
|
|
8
|
+
}
|
|
9
|
+
export interface Scope {
|
|
10
|
+
activeWatcher?: Watch;
|
|
11
|
+
activeEvent?: Event;
|
|
12
|
+
activeEventDeep: number;
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* You can create event function with createEvent
|
|
3
|
+
* ```typescript
|
|
4
|
+
* import { State, createEvent } from 'watch-state'
|
|
5
|
+
*
|
|
6
|
+
* const count = new State(0)
|
|
7
|
+
* const increase = createEvent(() => {
|
|
8
|
+
* console.log(count.value++)
|
|
9
|
+
* })
|
|
10
|
+
*
|
|
11
|
+
* new Watch(increase)
|
|
12
|
+
* ```
|
|
13
|
+
* */
|
|
14
|
+
export declare function createEvent<F extends Function>(fn: F): F;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { globalEvent } from '../../constants.es6.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* You can create event function with createEvent
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { State, createEvent } from 'watch-state'
|
|
7
|
+
*
|
|
8
|
+
* const count = new State(0)
|
|
9
|
+
* const increase = createEvent(() => {
|
|
10
|
+
* console.log(count.value++)
|
|
11
|
+
* })
|
|
12
|
+
*
|
|
13
|
+
* new Watch(increase)
|
|
14
|
+
* ```
|
|
15
|
+
* */
|
|
16
|
+
function createEvent(fn) {
|
|
17
|
+
return function () {
|
|
18
|
+
globalEvent.start();
|
|
19
|
+
const result = fn.apply(this, arguments);
|
|
20
|
+
globalEvent.end();
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { createEvent };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var constants = require('../../constants.js');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* You can create event function with createEvent
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { State, createEvent } from 'watch-state'
|
|
11
|
+
*
|
|
12
|
+
* const count = new State(0)
|
|
13
|
+
* const increase = createEvent(() => {
|
|
14
|
+
* console.log(count.value++)
|
|
15
|
+
* })
|
|
16
|
+
*
|
|
17
|
+
* new Watch(increase)
|
|
18
|
+
* ```
|
|
19
|
+
* */
|
|
20
|
+
function createEvent(fn) {
|
|
21
|
+
return function () {
|
|
22
|
+
constants.globalEvent.start();
|
|
23
|
+
const result = fn.apply(this, arguments);
|
|
24
|
+
constants.globalEvent.end();
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.createEvent = createEvent;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './createEvent';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createEvent } from './createEvent.es6.js';
|
package/utils/index.d.ts
ADDED
package/utils/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var onDestroy = require('./onDestroy/onDestroy.js');
|
|
6
|
+
var createEvent = require('./createEvent/createEvent.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
exports.onDestroy = onDestroy.onDestroy;
|
|
11
|
+
exports.createEvent = createEvent.createEvent;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './onDestroy';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { onDestroy } from './onDestroy.es6.js';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Destructor } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* You can subscribe on destroy or update of watcher
|
|
4
|
+
* ```javascript
|
|
5
|
+
* const count = new State(0)
|
|
6
|
+
* const watcher = new Watch(() => {
|
|
7
|
+
* console.log('count', count.value)
|
|
8
|
+
* // the order does not matter
|
|
9
|
+
* onDestroy(() => console.log('destructor'))
|
|
10
|
+
* })
|
|
11
|
+
* // console.log('count', 0)
|
|
12
|
+
*
|
|
13
|
+
* count.value++
|
|
14
|
+
* // console.log('destructor')
|
|
15
|
+
* // console.log('count', 1)
|
|
16
|
+
*
|
|
17
|
+
* watcher.destroy()
|
|
18
|
+
* // console.log('destructor')
|
|
19
|
+
*
|
|
20
|
+
* watcher.destroy()
|
|
21
|
+
* count.value++
|
|
22
|
+
* // nothing happens
|
|
23
|
+
* ```
|
|
24
|
+
* */
|
|
25
|
+
export declare function onDestroy(destructor: Destructor): void;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { scope } from '../../constants.es6.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* You can subscribe on destroy or update of watcher
|
|
5
|
+
* ```javascript
|
|
6
|
+
* const count = new State(0)
|
|
7
|
+
* const watcher = new Watch(() => {
|
|
8
|
+
* console.log('count', count.value)
|
|
9
|
+
* // the order does not matter
|
|
10
|
+
* onDestroy(() => console.log('destructor'))
|
|
11
|
+
* })
|
|
12
|
+
* // console.log('count', 0)
|
|
13
|
+
*
|
|
14
|
+
* count.value++
|
|
15
|
+
* // console.log('destructor')
|
|
16
|
+
* // console.log('count', 1)
|
|
17
|
+
*
|
|
18
|
+
* watcher.destroy()
|
|
19
|
+
* // console.log('destructor')
|
|
20
|
+
*
|
|
21
|
+
* watcher.destroy()
|
|
22
|
+
* count.value++
|
|
23
|
+
* // nothing happens
|
|
24
|
+
* ```
|
|
25
|
+
* */
|
|
26
|
+
function onDestroy(destructor) {
|
|
27
|
+
if (scope.activeWatcher) {
|
|
28
|
+
scope.activeWatcher.onClear(destructor);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { onDestroy };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var constants = require('../../constants.js');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* You can subscribe on destroy or update of watcher
|
|
9
|
+
* ```javascript
|
|
10
|
+
* const count = new State(0)
|
|
11
|
+
* const watcher = new Watch(() => {
|
|
12
|
+
* console.log('count', count.value)
|
|
13
|
+
* // the order does not matter
|
|
14
|
+
* onDestroy(() => console.log('destructor'))
|
|
15
|
+
* })
|
|
16
|
+
* // console.log('count', 0)
|
|
17
|
+
*
|
|
18
|
+
* count.value++
|
|
19
|
+
* // console.log('destructor')
|
|
20
|
+
* // console.log('count', 1)
|
|
21
|
+
*
|
|
22
|
+
* watcher.destroy()
|
|
23
|
+
* // console.log('destructor')
|
|
24
|
+
*
|
|
25
|
+
* watcher.destroy()
|
|
26
|
+
* count.value++
|
|
27
|
+
* // nothing happens
|
|
28
|
+
* ```
|
|
29
|
+
* */
|
|
30
|
+
function onDestroy(destructor) {
|
|
31
|
+
if (constants.scope.activeWatcher) {
|
|
32
|
+
constants.scope.activeWatcher.onClear(destructor);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
exports.onDestroy = onDestroy;
|
package/watch-state.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var watchState=function(t){"use strict";
|
|
1
|
+
var watchState=function(t){"use strict";class e{add(t){let{watchers:e}=this;if(e){if(e.has(t))return;e.add(t)}else e=this.watchers=new Set([t]);t.onClear((()=>e.delete(t)))}start(){a.activeEvent||(this.activeWatcher=a.activeWatcher,a.activeWatcher=void 0,a.activeEvent=this),a.activeEventDeep++}end(){--a.activeEventDeep||a.activeEvent!==this||(a.activeEvent=void 0,this.update(),a.activeWatcher=this.activeWatcher)}forceUpdate(){const{watchers:t}=this;this.watchers=void 0;for(const e of t)e.update()}update(){var t;(null===(t=this.watchers)||void 0===t?void 0:t.size)&&(this===s?this.forceUpdate():(s.start(),this.forceUpdate(),s.end()))}}const s=new e,a={activeWatcher:void 0,activeEvent:void 0,activeEventDeep:0};function i(t){a.activeWatcher&&a.activeWatcher.onClear(t)}class r{constructor(t,e,s){this.watcher=t,this.ran=!1,e||i((()=>this.destroy())),s||this.watchRun()}run(){const{ran:t}=this;return this.ran=!0,this.watcher(t)}watchRun(){const t=a.activeWatcher;a.activeWatcher=this,this.run(),a.activeWatcher=t}forceUpdate(){this.destroy(),this.watchRun()}update(){this.destroy(),a.activeEvent?a.activeEvent.add(this):this.watchRun()}destroy(){const{destructors:t}=this;t&&(this.destructors=void 0,t.forEach((t=>t())))}onClear(t){return this.destructors?this.destructors.push(t):this.destructors=[t],this}onDestroy(t){return this.onClear(t)}}class h extends e{constructor(t){super(),this.state=t}get value(){return a.activeWatcher&&this.add(a.activeWatcher),this.state}set value(t){t!==this.state&&(this.state=t,this.update())}}class c extends r{constructor(t,e,s){super(t,e,!s)}destroy(){return super.destroy()}run(){this.updated=!0,this.value=super.run()}get hasWatcher(){if(this.updated&&this.size)for(const t of this._state.watchers)if(!(t instanceof c)||t.hasWatcher)return!0}get size(){var t,e;return null===(e=null===(t=this._state)||void 0===t?void 0:t.watchers)||void 0===e?void 0:e.size}deepUpdate(){if(this.updated=!1,this.destroy(),this.size)for(const t of this._state.watchers)t.deepUpdate()}update(){this.updated&&(this.hasWatcher?this.forceUpdate():this.deepUpdate())}get state(){return this._state||(this._state=new h)}get value(){return this.updated||this.forceUpdate(),this.state.value}set value(t){this.state.value=t}}return t.Cache=c,t.Event=e,t.State=h,t.Watch=r,t.createEvent=function(t){return function(){s.start();const e=t.apply(this,arguments);return s.end(),e}},t.globalEvent=s,t.onDestroy=i,t.scope=a,Object.defineProperty(t,"__esModule",{value:!0}),t}({});
|
package/scope/index.d.ts
DELETED
package/scope/index.es6.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from './scope.es6.js';
|
package/scope/index.js
DELETED
package/scope/scope.d.ts
DELETED
package/scope/scope.es6.js
DELETED