colyseus-events
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -9,3 +9,10 @@ import { ArraySchema, CollectionSchema, MapSchema, Schema, SetSchema } from '@colyseus/schema'; | ||
} | ||
/** | ||
* make every change in Colyseus state trigger an event in the EventEmitter using the provided namespace. | ||
* @param state Colyseus state to track | ||
* @param events EventsEmitter of you choice. has to support `emit()` and `on()` | ||
* @param namespace Prefix of events name to emit | ||
* @returns the provided events emitter | ||
*/ | ||
export declare function wireEvents<T extends Events>(state: Colyseus, events: T, namespace: string): T; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -10,2 +10,9 @@ "use strict"; | ||
} | ||
/** | ||
* make every change in Colyseus state trigger an event in the EventEmitter using the provided namespace. | ||
* @param state Colyseus state to track | ||
* @param events EventsEmitter of you choice. has to support `emit()` and `on()` | ||
* @param namespace Prefix of events name to emit | ||
* @returns the provided events emitter | ||
*/ | ||
function wireEvents(state, events, namespace) { | ||
@@ -24,5 +31,4 @@ if (isPrimitive(state)) { | ||
}; | ||
for (const f in state) { | ||
if (!schemaKeys.includes(f) && Object.prototype.hasOwnProperty.call(state, f)) { | ||
const field = f; | ||
for (const field in state) { | ||
if (!schemaKeys.includes(field) && Object.prototype.hasOwnProperty.call(state, field)) { | ||
const fieldNamespace = `${namespace}.${field}`; | ||
@@ -35,3 +41,3 @@ //@ts-ignore : the field is legal for the state | ||
else if (state instanceof schema_1.ArraySchema) { | ||
state.onAdd = (value, field) => { | ||
state.onAdd = state.onChange = (value, field) => { | ||
const fieldNamespace = `${namespace}[${field}]`; | ||
@@ -41,7 +47,2 @@ events.emit(fieldNamespace, value, fieldNamespace); | ||
}; | ||
state.onChange = (value, field) => { | ||
const fieldNamespace = `${namespace}[${field}]`; | ||
events.emit(fieldNamespace, value, fieldNamespace); | ||
wireEvents(value, events, fieldNamespace); | ||
}; | ||
state.onRemove = (_, field) => { | ||
@@ -57,3 +58,3 @@ const fieldNamespace = `${namespace}[${field}]`; | ||
else if (state instanceof schema_1.MapSchema) { | ||
state.onAdd = (value, field) => { | ||
state.onAdd = state.onChange = (value, field) => { | ||
const fieldNamespace = `${namespace}["${field}"]`; | ||
@@ -63,7 +64,2 @@ events.emit(fieldNamespace, value, fieldNamespace); | ||
}; | ||
state.onChange = (value, field) => { | ||
const fieldNamespace = `${namespace}["${field}"]`; | ||
events.emit(fieldNamespace, value, fieldNamespace); | ||
wireEvents(value, events, fieldNamespace); | ||
}; | ||
state.onRemove = (_, field) => { | ||
@@ -70,0 +66,0 @@ const fieldNamespace = `${namespace}["${field}"]`; |
{ | ||
"name": "colyseus-events", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "generate notification events from colyseus state", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:starwards/colyseus-events.git", |
@@ -21,8 +21,16 @@ # colyseus-events | ||
const room: Room<GameState> = await client.joinOrCreate("game"); | ||
const events = wireEvents(room.state, new EventEmitter()); | ||
const events = wireEvents(room.state, new EventEmitter(), "state"); | ||
``` | ||
then you can wire listeners to `events` and start triggering them. | ||
then you can wire listeners to `events` and start triggering them. | ||
## Developer instructions | ||
whenever something changes in the state, an event will be emitted immediately. the name of the event will be the path of the changed property (or element). The event value will be the new value of that property or element. for convenience, the second value will be the event name. this can be helpful for listeners thatregister for more that one property. | ||
examples: | ||
when the server executes: `state.foo.bar = 15` event `'state.foo.bar'` will be emitted with values `15` and `'state.foo.bar'`. | ||
when the server executes: `state.foo.bar.push(15)` event `'state.foo.bar[0]'` will be emitted with values `15` and `'state.foo.bar[0]'`. | ||
``` | ||
## Contributor instructions | ||
### Installing workspace | ||
@@ -29,0 +37,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12493
87
66