watch-state 3.4.2 → 3.4.5
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 +1 -1
- package/Event/Event.d.ts +1 -0
- package/Event/Event.es6.js +4 -3
- package/Event/Event.js +6 -4
- package/Event/index.es6.js +1 -1
- package/Event/index.js +1 -0
- package/constants.d.ts +0 -2
- package/constants.es6.js +1 -4
- package/constants.js +0 -4
- package/index.es6.js +3 -2
- package/index.js +3 -1
- package/package.json +1 -1
- package/utils/createEvent/createEvent.es6.js +1 -1
- package/utils/createEvent/createEvent.js +3 -3
- package/utils/index.d.ts +1 -0
- package/utils/index.es6.js +1 -0
- package/utils/index.js +2 -0
- package/utils/unwatch/index.d.ts +1 -0
- package/utils/unwatch/index.es6.js +1 -0
- package/utils/unwatch/index.js +9 -0
- package/utils/unwatch/unwatch.d.ts +15 -0
- package/utils/unwatch/unwatch.es6.js +25 -0
- package/utils/unwatch/unwatch.js +29 -0
- package/watch-state.min.js +1 -1
package/Cache/Cache.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ import { Watcher } from '../types';
|
|
|
26
26
|
export declare class Cache<V = any> extends Watch {
|
|
27
27
|
protected updated: boolean;
|
|
28
28
|
private _state;
|
|
29
|
-
constructor(watcher: Watcher
|
|
29
|
+
constructor(watcher: Watcher<V>, freeParent?: boolean, fireImmediately?: boolean);
|
|
30
30
|
destroy(): void;
|
|
31
31
|
run(): void;
|
|
32
32
|
get hasWatcher(): boolean;
|
package/Event/Event.d.ts
CHANGED
package/Event/Event.es6.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { scope
|
|
1
|
+
import { scope } from '../constants.es6.js';
|
|
2
2
|
|
|
3
3
|
class Event {
|
|
4
4
|
add(target) {
|
|
@@ -63,6 +63,7 @@ class Event {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
-
}
|
|
66
|
+
}
|
|
67
|
+
const globalEvent = new Event();
|
|
67
68
|
|
|
68
|
-
export { Event };
|
|
69
|
+
export { Event, globalEvent };
|
package/Event/Event.js
CHANGED
|
@@ -57,16 +57,18 @@ class Event {
|
|
|
57
57
|
update() {
|
|
58
58
|
var _a;
|
|
59
59
|
if ((_a = this.watchers) === null || _a === void 0 ? void 0 : _a.size) {
|
|
60
|
-
if (this ===
|
|
60
|
+
if (this === globalEvent) {
|
|
61
61
|
this.forceUpdate();
|
|
62
62
|
}
|
|
63
63
|
else {
|
|
64
|
-
|
|
64
|
+
globalEvent.start();
|
|
65
65
|
this.forceUpdate();
|
|
66
|
-
|
|
66
|
+
globalEvent.end();
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
}
|
|
70
|
+
}
|
|
71
|
+
const globalEvent = new Event();
|
|
71
72
|
|
|
72
73
|
exports.Event = Event;
|
|
74
|
+
exports.globalEvent = globalEvent;
|
package/Event/index.es6.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { Event } from './Event.es6.js';
|
|
1
|
+
export { Event, globalEvent } from './Event.es6.js';
|
package/Event/index.js
CHANGED
package/constants.d.ts
CHANGED
package/constants.es6.js
CHANGED
package/constants.js
CHANGED
|
@@ -2,14 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var Event = require('./Event/Event.js');
|
|
6
|
-
|
|
7
|
-
const globalEvent = new Event.Event();
|
|
8
5
|
const scope = {
|
|
9
6
|
activeWatcher: undefined,
|
|
10
7
|
activeEvent: undefined,
|
|
11
8
|
activeEventDeep: 0,
|
|
12
9
|
};
|
|
13
10
|
|
|
14
|
-
exports.globalEvent = globalEvent;
|
|
15
11
|
exports.scope = scope;
|
package/index.es6.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
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 } from './Event/Event.es6.js';
|
|
4
|
+
export { Event, globalEvent } from './Event/Event.es6.js';
|
|
5
5
|
export { onDestroy } from './utils/onDestroy/onDestroy.es6.js';
|
|
6
6
|
export { createEvent } from './utils/createEvent/createEvent.es6.js';
|
|
7
|
-
export {
|
|
7
|
+
export { unwatch } from './utils/unwatch/unwatch.es6.js';
|
|
8
|
+
export { scope } from './constants.es6.js';
|
package/index.js
CHANGED
|
@@ -8,6 +8,7 @@ var Cache = require('./Cache/Cache.js');
|
|
|
8
8
|
var Event = require('./Event/Event.js');
|
|
9
9
|
var onDestroy = require('./utils/onDestroy/onDestroy.js');
|
|
10
10
|
var createEvent = require('./utils/createEvent/createEvent.js');
|
|
11
|
+
var unwatch = require('./utils/unwatch/unwatch.js');
|
|
11
12
|
var constants = require('./constants.js');
|
|
12
13
|
|
|
13
14
|
|
|
@@ -16,7 +17,8 @@ exports.Watch = Watch.Watch;
|
|
|
16
17
|
exports.State = State.State;
|
|
17
18
|
exports.Cache = Cache.Cache;
|
|
18
19
|
exports.Event = Event.Event;
|
|
20
|
+
exports.globalEvent = Event.globalEvent;
|
|
19
21
|
exports.onDestroy = onDestroy.onDestroy;
|
|
20
22
|
exports.createEvent = createEvent.createEvent;
|
|
21
|
-
exports.
|
|
23
|
+
exports.unwatch = unwatch.unwatch;
|
|
22
24
|
exports.scope = constants.scope;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var Event = require('../../Event/Event.js');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* You can create event function with createEvent
|
|
@@ -19,9 +19,9 @@ var constants = require('../../constants.js');
|
|
|
19
19
|
* */
|
|
20
20
|
function createEvent(fn) {
|
|
21
21
|
return function () {
|
|
22
|
-
|
|
22
|
+
Event.globalEvent.start();
|
|
23
23
|
const result = fn.apply(this, arguments);
|
|
24
|
-
|
|
24
|
+
Event.globalEvent.end();
|
|
25
25
|
return result;
|
|
26
26
|
};
|
|
27
27
|
}
|
package/utils/index.d.ts
CHANGED
package/utils/index.es6.js
CHANGED
package/utils/index.js
CHANGED
|
@@ -4,8 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var onDestroy = require('./onDestroy/onDestroy.js');
|
|
6
6
|
var createEvent = require('./createEvent/createEvent.js');
|
|
7
|
+
var unwatch = require('./unwatch/unwatch.js');
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
exports.onDestroy = onDestroy.onDestroy;
|
|
11
12
|
exports.createEvent = createEvent.createEvent;
|
|
13
|
+
exports.unwatch = unwatch.unwatch;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './unwatch';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { unwatch } from './unwatch.es6.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* You can stop watching a piece of code
|
|
3
|
+
* ```typescript
|
|
4
|
+
* import { State, Watch, unwatch } from '../..'
|
|
5
|
+
*
|
|
6
|
+
* const count = new State(0)
|
|
7
|
+
*
|
|
8
|
+
* new Watch(() => {
|
|
9
|
+
* console.log(unwatch(() => count.value++))
|
|
10
|
+
* })
|
|
11
|
+
*
|
|
12
|
+
* count.value++
|
|
13
|
+
* ```
|
|
14
|
+
* */
|
|
15
|
+
export declare function unwatch<T>(fn: () => T): T;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { scope } from '../../constants.es6.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* You can stop watching a piece of code
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { State, Watch, unwatch } from '../..'
|
|
7
|
+
*
|
|
8
|
+
* const count = new State(0)
|
|
9
|
+
*
|
|
10
|
+
* new Watch(() => {
|
|
11
|
+
* console.log(unwatch(() => count.value++))
|
|
12
|
+
* })
|
|
13
|
+
*
|
|
14
|
+
* count.value++
|
|
15
|
+
* ```
|
|
16
|
+
* */
|
|
17
|
+
function unwatch(fn) {
|
|
18
|
+
const { activeWatcher } = scope;
|
|
19
|
+
scope.activeWatcher = undefined;
|
|
20
|
+
const result = fn();
|
|
21
|
+
scope.activeWatcher = activeWatcher;
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { unwatch };
|
|
@@ -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 stop watching a piece of code
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { State, Watch, unwatch } from '../..'
|
|
11
|
+
*
|
|
12
|
+
* const count = new State(0)
|
|
13
|
+
*
|
|
14
|
+
* new Watch(() => {
|
|
15
|
+
* console.log(unwatch(() => count.value++))
|
|
16
|
+
* })
|
|
17
|
+
*
|
|
18
|
+
* count.value++
|
|
19
|
+
* ```
|
|
20
|
+
* */
|
|
21
|
+
function unwatch(fn) {
|
|
22
|
+
const { activeWatcher } = constants.scope;
|
|
23
|
+
constants.scope.activeWatcher = undefined;
|
|
24
|
+
const result = fn();
|
|
25
|
+
constants.scope.activeWatcher = activeWatcher;
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.unwatch = unwatch;
|
package/watch-state.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var watchState=function(t){"use strict";
|
|
1
|
+
var watchState=function(t){"use strict";const e={activeWatcher:void 0,activeEvent:void 0,activeEventDeep:0};function s(t){e.activeWatcher&&e.activeWatcher.onClear(t)}class a{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(){e.activeEvent||(this.activeWatcher=e.activeWatcher,e.activeWatcher=void 0,e.activeEvent=this),e.activeEventDeep++}end(){--e.activeEventDeep||e.activeEvent!==this||(e.activeEvent=void 0,this.update(),e.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===i?this.forceUpdate():(i.start(),this.forceUpdate(),i.end()))}}const i=new a;class r{constructor(t,e,a){this.watcher=t,this.ran=!1,e||s((()=>this.destroy())),a||this.watchRun()}run(){const{ran:t}=this;return this.ran=!0,this.watcher(t)}watchRun(){const t=e.activeWatcher;e.activeWatcher=this,this.run(),e.activeWatcher=t}forceUpdate(){this.destroy(),this.watchRun()}update(){this.destroy(),e.activeEvent?e.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 c extends a{constructor(t){super(),this.state=t}get value(){return e.activeWatcher&&this.add(e.activeWatcher),this.state}set value(t){t!==this.state&&(this.state=t,this.update())}}class h 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 h)||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 c)}get value(){return this.updated||this.forceUpdate(),this.state.value}set value(t){this.state.value=t}}return t.Cache=h,t.Event=a,t.State=c,t.Watch=r,t.createEvent=function(t){return function(){i.start();const e=t.apply(this,arguments);return i.end(),e}},t.globalEvent=i,t.onDestroy=s,t.scope=e,t.unwatch=function(t){const{activeWatcher:s}=e;e.activeWatcher=void 0;const a=t();return e.activeWatcher=s,a},Object.defineProperty(t,"__esModule",{value:!0}),t}({});
|