vsn 0.1.34 → 0.1.37
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/demo/vsn.js +1 -1
- package/dist/EventDispatcher.d.ts +4 -0
- package/dist/EventDispatcher.js +41 -2
- package/dist/EventDispatcher.js.map +1 -1
- package/dist/Scope/ScopeDataAbstract.d.ts +1 -1
- package/dist/Scope/ScopeDataAbstract.js +11 -2
- package/dist/Scope/ScopeDataAbstract.js.map +1 -1
- package/dist/Scope/WrappedArray.d.ts +2 -0
- package/dist/Scope/WrappedArray.js +18 -0
- package/dist/Scope/WrappedArray.js.map +1 -1
- package/dist/Scope/properties/ArrayProperty.d.ts +3 -0
- package/dist/Scope/properties/ArrayProperty.js +20 -2
- package/dist/Scope/properties/ArrayProperty.js.map +1 -1
- package/dist/Scope/properties/Property.d.ts +4 -0
- package/dist/Scope/properties/Property.js +21 -0
- package/dist/Scope/properties/Property.js.map +1 -1
- package/dist/Scope.js +1 -1
- package/dist/Scope.js.map +1 -1
- package/package.json +1 -1
- package/src/EventDispatcher.ts +42 -0
- package/src/Scope/ScopeDataAbstract.ts +7 -2
- package/src/Scope/WrappedArray.ts +20 -0
- package/src/Scope/properties/ArrayProperty.ts +14 -2
- package/src/Scope/properties/Property.ts +24 -0
- package/src/Scope.ts +1 -1
- package/test/EventDispatcher.spec.ts +191 -0
- package/test/Scope/ScopeData.spec.ts +58 -0
- package/test/Scope/properties/ArrayProperty.spec.ts +8 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { EventDispatcher } from "../src/EventDispatcher";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
describe('EventDispatcher', () => {
|
|
5
|
+
let dispatcher: EventDispatcher,
|
|
6
|
+
dummy: any = null,
|
|
7
|
+
dummy2: any = null;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
dispatcher = new EventDispatcher();
|
|
11
|
+
dummy = jasmine.createSpyObj('dummy', ['callback']);
|
|
12
|
+
dummy2 = jasmine.createSpyObj('dummy2', ['callback']);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should bind and be called properly", () => {
|
|
16
|
+
dispatcher.on('event', dummy.callback.bind(dummy));
|
|
17
|
+
expect(dummy.callback).not.toHaveBeenCalled();
|
|
18
|
+
dispatcher.dispatch('event', 1, 2, 'three');
|
|
19
|
+
expect(dummy.callback).toHaveBeenCalled();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should unbind and not be called", () => {
|
|
23
|
+
const key: number = dispatcher.on('event', dummy.callback.bind(dummy));
|
|
24
|
+
expect(dispatcher.off('event', key)).toEqual(true);
|
|
25
|
+
dispatcher.dispatch('event', 1, 2, 'three');
|
|
26
|
+
expect(dummy.callback).not.toHaveBeenCalled();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should not unbind events that don't exist", () => {
|
|
30
|
+
const key: number = dispatcher.on('event', dummy.callback.bind(dummy));
|
|
31
|
+
expect(dispatcher.off('fake_event', -1)).toEqual(false);
|
|
32
|
+
dispatcher.dispatch('event', 1, 2, 'three');
|
|
33
|
+
expect(dummy.callback).toHaveBeenCalled();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should bind and be called properly", () => {
|
|
37
|
+
dispatcher.on('event', dummy.callback.bind(dummy));
|
|
38
|
+
expect(dummy.callback).not.toHaveBeenCalled();
|
|
39
|
+
dispatcher.dispatch('event', 1, 2, 'three');
|
|
40
|
+
expect(dummy.callback).toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should unbind all", () => {
|
|
44
|
+
dispatcher.on('event', dummy.callback, dummy);
|
|
45
|
+
dispatcher.on('event', dummy.callback, dummy);
|
|
46
|
+
dispatcher.on('event', dummy2.callback, dummy2);
|
|
47
|
+
expect(dispatcher.off('event')).toEqual(true);
|
|
48
|
+
expect(dispatcher.offWithContext('event', dummy)).toBe(0);
|
|
49
|
+
expect(dispatcher.offWithContext('event', dummy2)).toBe(0);
|
|
50
|
+
dispatcher.dispatch('event');
|
|
51
|
+
expect(dummy.callback).not.toHaveBeenCalled();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should unbind all with matching context", () => {
|
|
55
|
+
dispatcher.on('event', dummy.callback, dummy);
|
|
56
|
+
dispatcher.on('event', dummy.callback, dummy);
|
|
57
|
+
dispatcher.on('event', dummy2.callback, dummy2);
|
|
58
|
+
expect(dispatcher.offWithContext('event', dummy)).toBe(2);
|
|
59
|
+
dispatcher.dispatch('event');
|
|
60
|
+
expect(dummy.callback).not.toHaveBeenCalled();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should unbind the correct listeners by key", () => {
|
|
64
|
+
let key1 = dispatcher.on('event', dummy.callback, dummy),
|
|
65
|
+
key2 = dispatcher.on('event', dummy.callback, dummy),
|
|
66
|
+
key3 = dispatcher.on('event', dummy.callback, dummy),
|
|
67
|
+
key4 = dispatcher.on('event', dummy.callback, dummy),
|
|
68
|
+
key5 = dispatcher.on('event', dummy.callback, dummy);
|
|
69
|
+
|
|
70
|
+
expect(dispatcher.off('event', key2)).toBe(true);
|
|
71
|
+
expect(dispatcher.off('event', key4)).toBe(true);
|
|
72
|
+
expect(dispatcher.off('event', key1)).toBe(true);
|
|
73
|
+
expect(dispatcher.off('event', key3)).toBe(true);
|
|
74
|
+
expect(dispatcher.off('event', key5)).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should fail to unbind the once listeners", () => {
|
|
78
|
+
let key1: number = dispatcher.once('event', dummy.callback, dummy),
|
|
79
|
+
cb1: any = dispatcher.getListener('event', key1),
|
|
80
|
+
key2: number = dispatcher.once('event', dummy.callback, dummy),
|
|
81
|
+
cb2: any = dispatcher.getListener('event', key2),
|
|
82
|
+
key3: number = dispatcher.once('event', dummy.callback, dummy),
|
|
83
|
+
cb3: any = dispatcher.getListener('event', key3),
|
|
84
|
+
key4: number = dispatcher.on('event', dummy.callback, dummy),
|
|
85
|
+
cb4: any = dispatcher.getListener('event', key4),
|
|
86
|
+
key5: number = dispatcher.once('event', dummy.callback, dummy),
|
|
87
|
+
cb5: any = dispatcher.getListener('event', key5);
|
|
88
|
+
|
|
89
|
+
dispatcher.dispatch('event');
|
|
90
|
+
dispatcher.dispatch('event');
|
|
91
|
+
dispatcher.dispatch('event');
|
|
92
|
+
dispatcher.dispatch('event');
|
|
93
|
+
expect(dispatcher.off('event', key2)).toBe(false);
|
|
94
|
+
expect(cb2.calls).toBe(1);
|
|
95
|
+
expect(dispatcher.off('event', key4)).toBe(true);
|
|
96
|
+
expect(cb4.calls).toBe(4);
|
|
97
|
+
expect(dispatcher.off('event', key1)).toBe(false);
|
|
98
|
+
expect(cb1.calls).toBe(1);
|
|
99
|
+
expect(dispatcher.off('event', key3)).toBe(false);
|
|
100
|
+
expect(cb3.calls).toBe(1);
|
|
101
|
+
expect(dispatcher.off('event', key5)).toBe(false);
|
|
102
|
+
expect(cb5.calls).toBe(1);
|
|
103
|
+
expect(dispatcher.off('event', key4)).toBe(false);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("should only be called once", () => {
|
|
107
|
+
let onceCalls = 0,
|
|
108
|
+
normalCalls = 0;
|
|
109
|
+
dispatcher.once('event', () => {
|
|
110
|
+
// empty
|
|
111
|
+
});
|
|
112
|
+
dispatcher.once('event', () => {
|
|
113
|
+
onceCalls += 1;
|
|
114
|
+
});
|
|
115
|
+
dispatcher.once('event', () => {
|
|
116
|
+
// empty
|
|
117
|
+
});
|
|
118
|
+
dispatcher.on('event', () => {
|
|
119
|
+
normalCalls += 1;
|
|
120
|
+
});
|
|
121
|
+
dispatcher.dispatch('event');
|
|
122
|
+
dispatcher.dispatch('event');
|
|
123
|
+
dispatcher.dispatch('event');
|
|
124
|
+
dispatcher.dispatch('event');
|
|
125
|
+
expect(onceCalls).toBe(1);
|
|
126
|
+
expect(normalCalls).toBe(4);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("should only be called once with nested event triggers", () => {
|
|
130
|
+
let onceCalls = 0,
|
|
131
|
+
normalCalls = 0;
|
|
132
|
+
const key: number = dispatcher.once('event', () => {
|
|
133
|
+
dispatcher.dispatch('event');
|
|
134
|
+
}),
|
|
135
|
+
cb: any = dispatcher.getListener('event', key);
|
|
136
|
+
dispatcher.dispatch('event');
|
|
137
|
+
expect(cb.calls).toBe(1);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should pass arguments", () => {
|
|
141
|
+
const key = dispatcher.once('event', (num, arr, obj) => {
|
|
142
|
+
expect(num).toBe(1);
|
|
143
|
+
expect(arr[0]).toBe(1);
|
|
144
|
+
expect(arr[1]).toBe(2);
|
|
145
|
+
expect(arr[2]).toBe(3);
|
|
146
|
+
expect(obj.foo).toBe('bar');
|
|
147
|
+
}),
|
|
148
|
+
cb = dispatcher.getListener('event', key);
|
|
149
|
+
dispatcher.dispatch('event', 1, [1,2,3], {foo:'bar'});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("cannot call a once event more than once", () => {
|
|
153
|
+
const key: number = dispatcher.once('event', () => {}),
|
|
154
|
+
cb: any = dispatcher.getListener('event', key);
|
|
155
|
+
dispatcher.dispatch('event');
|
|
156
|
+
expect(cb.call()).toBe(false);
|
|
157
|
+
expect(cb.calls).toBe(1);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("should not unbind anything and return 0", () => {
|
|
161
|
+
expect(dispatcher.offWithContext('event', null)).toBe(0);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("should not trigger events that don't exist", () => {
|
|
165
|
+
expect(dispatcher.dispatch('event')).toBe(undefined);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("should trigger events in the correct order", () => {
|
|
169
|
+
let check: number = 1;
|
|
170
|
+
dispatcher.on('event', () => {
|
|
171
|
+
expect(check).toBe(1);
|
|
172
|
+
check++;
|
|
173
|
+
});
|
|
174
|
+
dispatcher.on('event', () => {
|
|
175
|
+
expect(check).toBe(2);
|
|
176
|
+
check++;
|
|
177
|
+
});
|
|
178
|
+
dispatcher.on('event', () => {
|
|
179
|
+
expect(check).toBe(3);
|
|
180
|
+
check++;
|
|
181
|
+
});
|
|
182
|
+
dispatcher.on('event', () => {
|
|
183
|
+
expect(check).toBe(4);
|
|
184
|
+
check++;
|
|
185
|
+
});
|
|
186
|
+
dispatcher.on('event', () => {
|
|
187
|
+
expect(check).toBe(5);
|
|
188
|
+
});
|
|
189
|
+
dispatcher.dispatch('event');
|
|
190
|
+
});
|
|
191
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {ScopeData} from "../../src/Scope/ScopeData";
|
|
2
|
+
import {Property} from "../../src/Scope/properties/Property";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
describe('ScopeData', () => {
|
|
6
|
+
it("should set tags correctly", async () => {
|
|
7
|
+
const data = new ScopeData();
|
|
8
|
+
data.createProperty('test', Property, {
|
|
9
|
+
tags: ['test']
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
data.createProperty('test_int', Property, {
|
|
13
|
+
tags: ['test']
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
data.createProperty('not_test', Property, {
|
|
17
|
+
tags: ['not_test']
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
data['test'] = 'test';
|
|
21
|
+
data['test_int'] = 1;
|
|
22
|
+
data['not_test'] = 'not_test';
|
|
23
|
+
|
|
24
|
+
const taggedTestData = data.getData('test');
|
|
25
|
+
expect(taggedTestData).toEqual({
|
|
26
|
+
test: 'test',
|
|
27
|
+
test_int: 1
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const taggedNotTestData = data.getData('not_test');
|
|
31
|
+
expect(taggedNotTestData).toEqual({
|
|
32
|
+
not_test: 'not_test'
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const allData = data.getData();
|
|
36
|
+
expect(allData).toEqual({
|
|
37
|
+
test: 'test',
|
|
38
|
+
test_int: 1,
|
|
39
|
+
not_test: 'not_test'
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const notTestProp = data.getProperty('not_test');
|
|
43
|
+
notTestProp.addTag('test');
|
|
44
|
+
const taggedNotTestData2 = data.getData('test');
|
|
45
|
+
expect(taggedNotTestData2).toEqual({
|
|
46
|
+
test: 'test',
|
|
47
|
+
test_int: 1,
|
|
48
|
+
not_test: 'not_test'
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
notTestProp.removeTag('test');
|
|
52
|
+
const taggedNotTestData3 = data.getData('test');
|
|
53
|
+
expect(taggedNotTestData3).toEqual({
|
|
54
|
+
test: 'test',
|
|
55
|
+
test_int: 1
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -3,7 +3,7 @@ import {ArrayProperty} from "../../../src/Scope/properties/_imports";
|
|
|
3
3
|
import {WrappedArray} from "../../../src/vsn";
|
|
4
4
|
|
|
5
5
|
describe('ArrayProperty', () => {
|
|
6
|
-
it("
|
|
6
|
+
it("should do array things.", () => {
|
|
7
7
|
const d = new ScopeData();
|
|
8
8
|
d.createProperty('test', ArrayProperty);
|
|
9
9
|
d['test'] = [1, 2, 3];
|
|
@@ -12,5 +12,12 @@ describe('ArrayProperty', () => {
|
|
|
12
12
|
expect(d['test'].indexOf(2)).toBe(1);
|
|
13
13
|
expect(d['test'].indexOf(3)).toBe(2);
|
|
14
14
|
expect(d['test'].indexOf(4)).toBe(-1);
|
|
15
|
+
let calls = 0;
|
|
16
|
+
d.on('change:test', () => {
|
|
17
|
+
calls++;
|
|
18
|
+
});
|
|
19
|
+
d['test'].push(4);
|
|
20
|
+
expect(calls).toBeGreaterThanOrEqual(1); // push fires more than one event
|
|
21
|
+
expect(d['test'].indexOf(4)).toBe(3);
|
|
15
22
|
});
|
|
16
23
|
});
|