Skip to content
Myr

Merge branch 'master' into patch_123

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Jason Miller · committed by GitHub · parents ec9122b, f0f36b0 · Browse files · 8 files+168 added−68 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+168 added−68 removed

  • tests:+72 added−26 removed
  • other:+96 added−42 removed

8 files+168 added−68 removed

modified:src/index.ts+54 added-28 removed
modified:test/index_test.ts+17 added-6 removed
1import mitt, { Emitter } from '..';1import mitt, { Emitter, EventHandlerMap } from '..';2import chai, { expect } from 'chai';3import { spy } from 'sinon';4import sinonChai from 'sinon-chai';5chai.use(sinonChai);
8 unmodified lines
14 const map = new Map();15 const a = spy();16 const b = spy();17 map.set('foo', [a, b]);18 const events = mitt(map);18 const events = mitt<{ foo: undefined }>(map);19 events.emit('foo');20 expect(a).to.have.been.calledOnce;21 expect(b).to.have.been.calledOnce;22 });23});2425describe('mitt#', () => {26 let events, inst: Emitter;26 const eventType = Symbol('eventType');27 type Events = {28 foo: unknown;29 constructor: unknown;30 FOO: unknown;31 bar: unknown;32 Bar: unknown;33 'baz:bat!': unknown;34 'baz:baT!': unknown;35 Foo: unknown;36 [eventType]: unknown;37 };38 let events: EventHandlerMap<Events>, inst: Emitter<Events>;3928 beforeEach( () => {40 beforeEach(() => {41 events = new Map();42 inst = mitt(events);43 });44
49 unmodified lines
94 });9596 it('can take symbols for event types', () => {97 const foo = () => {};86 const eventType = Symbol('eventType');98 inst.on(eventType, foo);99 expect(events.get(eventType)).to.deep.equal([foo]);100 });101
59 unmodified lines
161162 it('should invoke handler for type', () => {163 const event = { a: 'b' };164154 inst.on('foo', (one, two?) => {165 inst.on('foo', (one, two?: unknown) => {166 expect(one).to.deep.equal(event);167 expect(two).to.be.an('undefined');168 });169
32 unmodified lines
modified:test/test-types-compilation.ts+55 added-20 removed
modified:.gitignore+1 added
4 unmodified lines
5/index.d.ts6package-lock.json7.DS_Store8.idea9.vscode
added:LICENSE+21 added
1MIT License23Copyright (c) 2021 Jason Miller45Permission is hereby granted, free of charge, to any person obtaining a copy6of this software and associated documentation files (the "Software"), to deal7in the Software without restriction, including without limitation the rights8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9copies of the Software, and to permit persons to whom the Software is10furnished to do so, subject to the following conditions:1112The above copyright notice and this permission notice shall be included in all13copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21SOFTWARE.
modified:package.json+5 added-5 removed
10 unmodified lines
11 "typings": "index.d.ts",12 "scripts": {13 "test": "npm-run-all --silent typecheck lint mocha test-types",14 "mocha": "mocha test",15 "test-types": "tsc test/test-types-compilation.ts --noEmit",15 "test-types": "tsc test/test-types-compilation.ts --noEmit --strict",16 "lint": "eslint src test --ext ts --ext js",17 "typecheck": "tsc --noEmit",18 "bundle": "microbundle",19 "build": "npm-run-all --silent clean -p bundle -s docs",
13 unmodified lines
33 "Jason Miller <jason@developit.ca>"34 ],35 "license": "MIT",36 "files": [37 "src",37 "dist",38 "index.d.ts"39 ],40 "mocha": {
35 unmodified lines
76 "jest/valid-expect": 0,77 "@typescript-eslint/no-explicit-any": 0,78 "@typescript-eslint/explicit-function-return-type": 0,79 "@typescript-eslint/explicit-module-boundary-types": 0,81 "@typescript-eslint/no-empty-function": 080 "@typescript-eslint/no-empty-function": 0,81 "@typescript-eslint/no-non-null-assertion": 082 }83 },84 "eslintIgnore": [85 "dist",
17 unmodified lines
103 "rimraf": "^3.0.2",104 "sinon": "^9.0.2",105 "sinon-chai": "^3.5.0",106 "ts-node": "^8.10.2",107 "typescript": "^3.9.3"107 "typescript": "^3.9.7"108 }109}109}
modified:README.md+13 added-5 removed
78 unmodified lines
79```8081### Typescript8283Set `"strict": true` in your tsconfig.json to get improved type inference for `mitt` instance methods.8485```ts86import mitt from 'mitt';8788type Events = {89 foo: string90 bar?: number91}9285const emitter: mitt.Emitter = mitt();93const emitter: mitt.Emitter<Events> = mitt<Events>();94```9596## Examples & Demos97
35 unmodified lines
133Register an event handler for the given type.134135#### Parameters136129- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `"*"` for all events137- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `'*'` for all events138- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event139140### off141
1 unmodified line
143If omit the `handler`, all event handlers of the given type are deleted.144145#### Parameters146139- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `"*"`147- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `'*'`148- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove149150### emit151152Invoke all handlers for the given type.145If present, `"*"` handlers are invoked after type-matched handlers.153If present, `'*'` handlers are invoked after type-matched handlers.154147Note: Manually firing "\*" handlers is not supported.155Note: Manually firing '\*' handlers is not supported.156157#### Parameters158159- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** The event type to invoke
29 unmodified lines
modified:tsconfig.json+2 added-4 removed