Skip to content

Commit 4315324

Browse files
committed
BREAKING: Remove event emitter
1 parent 664101c commit 4315324

File tree

8 files changed

+6
-531
lines changed

8 files changed

+6
-531
lines changed

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,12 @@ proxy objects that validate reading, assigning and deleting properties:
204204
- `error_code`: The `code` property to define on errors. Defaults to
205205
`E_SCHEMA`.
206206
- `writer = mySchema.write([data[, options]])`: Creates a writer with optional
207-
initial data and an event emitter. If the given data does not match the
208-
schema, an exception is thrown. The returned writer throws on undefined
209-
property modification, if an assigned value is invalid, or on an attempt to
210-
read an undefined property. These options are supported:
207+
initial data. If the given data does not match the schema, an exception is
208+
thrown. The returned writer throws on undefined property modification, if an
209+
assigned value is invalid, or on an attempt to read an undefined property.
210+
These options are supported:
211211
- `error_code`: The `code` property to define on errors. Defaults to
212212
`E_SCHEMA`.
213-
- `emitter`: If specified, these events will be emitted:
214-
- `set` when a property is assigned a new value
215-
- `delete` when a property is deleted
216-
- `push` when `push` is called on an array
217-
- `pop` when `pop` is called on an array
218-
- `unshift` when `unshift` is called on an array
219-
- `shift` when `shift` is called on an array
220-
- `splice` when `splice` is called on an array
221213
- `data = mySchema.verify(writer)`: Checks if any properties are missing in the
222214
given writer and returns the unwrapped data. Throws if the given object is
223215
not a schema writer.

index.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ module.exports.validator = validator;
7171

7272
module.exports.E_SCHEMA = E_SCHEMA;
7373

74-
/**
75-
* @typedef {Object} Emitter
76-
* @property {function(string, *): boolean | void} emit
77-
*/
7874
/**
7975
* @template {Value} V
8076
* @typedef WithToJSON
@@ -88,15 +84,11 @@ module.exports.E_SCHEMA = E_SCHEMA;
8884
* @template {Value} V
8985
* @typedef {V & WithToJSON<V>} SchemaWriter
9086
*/
91-
/**
92-
* @template {Value} V
93-
* @typedef WriterOptions
94-
* @property {Emitter} [emitter]
95-
*/
9687
/**
9788
* @template {Value} V
9889
* @callback SchemaRead
9990
* @param {V} value
91+
* @param {SchemaOptions} [options]
10092
* @returns {SchemaReader<V>}
10193
*/
10294
/* eslint-disable jsdoc/valid-types */
@@ -109,7 +101,7 @@ module.exports.E_SCHEMA = E_SCHEMA;
109101
* @template {Value} V
110102
* @callback SchemaWrite
111103
* @param {RecursivePartial<V>} value
112-
* @param {WriterOptions<V>} [options]
104+
* @param {SchemaOptions} [options]
113105
* @returns {SchemaWriter<V>}
114106
*/
115107

lib/array-verifyer.js

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
unwrap,
66
assertType,
77
lazy,
8-
stringify,
98
failSet,
109
failDelete,
1110
failSchemaValidation
@@ -72,10 +71,6 @@ function createArrayItemReader(itemTest, verify) {
7271
function createArrayItemWriter(itemTest) {
7372
return (array, options = {}, base = undefined) => {
7473
array = unwrap(array);
75-
const { emitter } = options;
76-
if (emitter) {
77-
emitArrayEvents(array, emitter, base, itemTest);
78-
}
7974
return new Proxy(array, {
8075
get: createItemGetter(itemTest, options, base, 'write', arrayPath),
8176
set(target, key, value) {
@@ -84,31 +79,10 @@ function createArrayItemWriter(itemTest) {
8479
const index = getArrayIndex(key, base);
8580
const path = arrayPath(base, index);
8681
itemTest.verify(value, {}, path);
87-
if (emitter) {
88-
emitter.emit('set', {
89-
type: 'array',
90-
array: target,
91-
base,
92-
index,
93-
path,
94-
value
95-
});
96-
}
9782
}
9883
return Reflect.set(target, key, value);
9984
},
10085
deleteProperty(target, key) {
101-
if (emitter) {
102-
const index = getArrayIndex(/** @type {string} */ (key), base);
103-
const path = arrayPath(base, index);
104-
emitter.emit('delete', {
105-
type: 'array',
106-
array: target,
107-
base,
108-
index,
109-
path
110-
});
111-
}
11286
return Reflect.deleteProperty(target, key);
11387
}
11488
});
@@ -131,94 +105,3 @@ function getArrayIndex(key, base) {
131105
}
132106
return index;
133107
}
134-
135-
/**
136-
* @template {Value} V
137-
* @param {Array<V>} array
138-
* @param {Object} emitter
139-
* @param {string | undefined} base
140-
* @param {Validator<V>} itemTest
141-
*/
142-
function emitArrayEvents(array, emitter, base, itemTest) {
143-
const proto = Object.create(Array.prototype);
144-
/**
145-
* @param {V[]} values
146-
* @returns {number}
147-
*/
148-
proto.push = (...values) => {
149-
const unwrapped = values.map(unwrap);
150-
verifyArrayMethodValues(base, 'push', 0, unwrapped, itemTest);
151-
emitter.emit('push', { array, base, values: unwrapped });
152-
return Array.prototype.push.call(array, ...unwrapped);
153-
};
154-
/**
155-
* @returns {V}
156-
*/
157-
proto.pop = () => {
158-
emitter.emit('pop', { array, base });
159-
return Array.prototype.pop.call(array);
160-
};
161-
/**
162-
* @returns {V}
163-
*/
164-
proto.shift = () => {
165-
emitter.emit('shift', { array, base });
166-
return Array.prototype.shift.call(array);
167-
};
168-
/**
169-
* @param {V[]} values
170-
* @returns {number}
171-
*/
172-
proto.unshift = (...values) => {
173-
const unwrapped = values.map(unwrap);
174-
verifyArrayMethodValues(base, 'unshift', 0, unwrapped, itemTest);
175-
emitter.emit('unshift', { array, base, values: unwrapped });
176-
return Array.prototype.unshift.call(array, ...unwrapped);
177-
};
178-
/**
179-
* @param {number} start
180-
* @param {number} delete_count
181-
* @param {V[]} values
182-
* @returns {V[]}
183-
*/
184-
proto.splice = (start, delete_count, ...values) => {
185-
const unwrapped = values.map(unwrap);
186-
verifyArrayMethodValues(base, 'splice', 2, unwrapped, itemTest);
187-
emitter.emit('splice', {
188-
array,
189-
base,
190-
start,
191-
delete_count,
192-
values: unwrapped
193-
});
194-
return Array.prototype.splice.call(
195-
array,
196-
start,
197-
delete_count,
198-
...unwrapped
199-
);
200-
};
201-
Object.setPrototypeOf(array, proto);
202-
}
203-
204-
/**
205-
* @template {Value} V
206-
* @param {string | undefined} base
207-
* @param {string} method
208-
* @param {number} offset
209-
* @param {Array<V>} values
210-
* @param {Validator<V>} itemTest
211-
*/
212-
function verifyArrayMethodValues(base, method, offset, values, itemTest) {
213-
values.forEach((value, i) => {
214-
if (itemTest(value) === false) {
215-
failSchemaValidation(
216-
new TypeError(
217-
`Expected argument ${
218-
i + offset + 1
219-
} of ${base}.${method} to be ${itemTest} but got ${stringify(value)}`
220-
)
221-
);
222-
}
223-
});
224-
}

lib/map-verifyer.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,6 @@ function createMapValueWriter(valueTest) {
7777
get: createItemGetter(valueTest, options, base, 'write', objectPath),
7878
set: createItemSetter(valueTest, options, base),
7979
deleteProperty(target, key) {
80-
if (options && options.emitter && typeof key === 'string') {
81-
const path = objectPath(base, key);
82-
options.emitter.emit('delete', {
83-
type: 'object',
84-
object: target,
85-
key,
86-
base,
87-
path
88-
});
89-
}
9080
return Reflect.deleteProperty(target, key);
9181
}
9282
});
@@ -99,16 +89,6 @@ function createItemSetter(valueTest, options, base) {
9989
value = unwrap(value);
10090
const path = objectPath(base, key);
10191
valueTest.verify(value, options, path);
102-
if (options && options.emitter) {
103-
options.emitter.emit('set', {
104-
type: 'object',
105-
object: target,
106-
key,
107-
value,
108-
base,
109-
path
110-
});
111-
}
11292
}
11393
return Reflect.set(target, key, value);
11494
};

lib/object-verifyer.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ function createWriter(tests, spec_options) {
8383
options.error_code
8484
);
8585
}
86-
if (options.emitter && typeof key === 'string') {
87-
const path = objectPath(base, key);
88-
options.emitter.emit('delete', {
89-
type: 'object',
90-
object: target,
91-
key,
92-
base,
93-
path
94-
});
95-
}
9686
return Reflect.deleteProperty(target, key);
9787
}
9888
});
@@ -137,16 +127,6 @@ function createPropertySetter(tests, options, base) {
137127
const path = objectPath(base, key);
138128
const test = getTest(tests, key, base, options);
139129
test.verify(value, options, path, false);
140-
if (options.emitter) {
141-
options.emitter.emit('set', {
142-
type: 'object',
143-
object: target,
144-
key,
145-
value,
146-
base,
147-
path
148-
});
149-
}
150130
}
151131
return Reflect.set(target, key, value);
152132
};

0 commit comments

Comments
 (0)