@colyseus/schema
Advanced tools
Comparing version 3.0.0-alpha.13 to 3.0.0-alpha.14
@@ -6,4 +6,24 @@ import { Collection, NonFunctionNonPrimitivePropNames, NonFunctionPropNames } from "../../types/HelperTypes"; | ||
type InstanceCallback<T> = { | ||
/** | ||
* Trigger callback when value of a property changes. | ||
* | ||
* @param prop name of the property | ||
* @param callback callback to be triggered on property change | ||
* @param immediate trigger immediatelly if property has been already set. | ||
*/ | ||
listen<K extends NonFunctionPropNames<T>>(prop: K, callback: (value: T[K], previousValue: T[K]) => void, immediate?: boolean): any; | ||
/** | ||
* Trigger callback whenever any property changed within this instance. | ||
* | ||
* @param prop name of the property | ||
* @param callback callback to be triggered on property change | ||
* @param immediate trigger immediatelly if property has been already set. | ||
*/ | ||
onChange(callback: () => void): void; | ||
/** | ||
* Bind properties to another object. Changes on the properties will be reflected on the target object. | ||
* | ||
* @param targetObject object to bind properties to | ||
* @param properties list of properties to bind. If not provided, all properties will be bound. | ||
*/ | ||
bindTo(targetObject: any, properties?: Array<NonFunctionPropNames<T>>): void; | ||
@@ -14,6 +34,20 @@ } & { | ||
type CollectionCallback<K, V> = { | ||
/** | ||
* Trigger callback when an item has been added to the collection. | ||
* | ||
* @param callback | ||
* @param immediate | ||
*/ | ||
onAdd(callback: (item: V, index: K) => void, immediate?: boolean): void; | ||
/** | ||
* Trigger callback when an item has been removed to the collection. | ||
* | ||
* @param callback | ||
*/ | ||
onRemove(callback: (item: V, index: K) => void): void; | ||
}; | ||
export declare function getStateCallbacks<T extends Schema>(decoder: Decoder<T>): CallbackProxy<T>; | ||
export declare function getStateCallbacks<T extends Schema>(decoder: Decoder<T>): { | ||
$: (<F extends Schema>(instance: F) => CallbackProxy<F>); | ||
$state: CallbackProxy<T>; | ||
}; | ||
export {}; |
@@ -35,25 +35,22 @@ "use strict"; | ||
if (!uniqueRefIds.has(refId)) { | ||
try { | ||
// trigger onChange | ||
const replaceCallbacks = $callbacks?.[spec_1.OPERATION.REPLACE]; | ||
for (let i = replaceCallbacks?.length - 1; i >= 0; i--) { | ||
replaceCallbacks[i](); | ||
} | ||
// trigger onChange | ||
const replaceCallbacks = $callbacks?.[spec_1.OPERATION.REPLACE]; | ||
for (let i = replaceCallbacks?.length - 1; i >= 0; i--) { | ||
replaceCallbacks[i](); | ||
// try { | ||
// } catch (e) { | ||
// console.error(e); | ||
// } | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
try { | ||
if ($callbacks.hasOwnProperty(change.field)) { | ||
const fieldCallbacks = $callbacks[change.field]; | ||
for (let i = fieldCallbacks?.length - 1; i >= 0; i--) { | ||
fieldCallbacks[i](change.value, change.previousValue); | ||
} | ||
if ($callbacks.hasOwnProperty(change.field)) { | ||
const fieldCallbacks = $callbacks[change.field]; | ||
for (let i = fieldCallbacks?.length - 1; i >= 0; i--) { | ||
fieldCallbacks[i](change.value, change.previousValue); | ||
// try { | ||
// } catch (e) { | ||
// console.error(e); | ||
// } | ||
} | ||
} | ||
catch (e) { | ||
// | ||
console.error(e); | ||
} | ||
} | ||
@@ -136,8 +133,11 @@ else { | ||
bindTo: function bindTo(targetObject, properties) { | ||
// return $root.addCallback( | ||
// $root.refIds.get(context.instance), | ||
// OPERATION.BIND, | ||
// callback | ||
// ); | ||
console.log("bindTo", targetObject, properties); | ||
// | ||
// TODO: refactor this implementation. There is room for improvement here. | ||
// | ||
if (!properties) { | ||
properties = Object.keys(metadata); | ||
} | ||
return $root.addCallback($root.refIds.get(context.instance), spec_1.OPERATION.REPLACE, () => { | ||
properties.forEach((prop) => targetObject[prop] = context.instance[prop]); | ||
}); | ||
} | ||
@@ -233,5 +233,8 @@ }, { | ||
} | ||
return $(decoder.state); | ||
return { | ||
$, | ||
$state: $(decoder.state), | ||
}; | ||
} | ||
exports.getStateCallbacks = getStateCallbacks; | ||
//# sourceMappingURL=StateCallbacks.js.map |
{ | ||
"name": "@colyseus/schema", | ||
"version": "3.0.0-alpha.13", | ||
"version": "3.0.0-alpha.14", | ||
"description": "Binary state serializer with delta encoding for games", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -24,5 +24,12 @@ import { Metadata } from "../../Metadata"; | ||
? CollectionCallback<K, V> | ||
: InstanceCallback<T> | ||
: InstanceCallback<T>; | ||
type InstanceCallback<T> = { | ||
/** | ||
* Trigger callback when value of a property changes. | ||
* | ||
* @param prop name of the property | ||
* @param callback callback to be triggered on property change | ||
* @param immediate trigger immediatelly if property has been already set. | ||
*/ | ||
listen<K extends NonFunctionPropNames<T>>( | ||
@@ -33,3 +40,17 @@ prop: K, | ||
) | ||
/** | ||
* Trigger callback whenever any property changed within this instance. | ||
* | ||
* @param prop name of the property | ||
* @param callback callback to be triggered on property change | ||
* @param immediate trigger immediatelly if property has been already set. | ||
*/ | ||
onChange(callback: () => void): void; | ||
/** | ||
* Bind properties to another object. Changes on the properties will be reflected on the target object. | ||
* | ||
* @param targetObject object to bind properties to | ||
* @param properties list of properties to bind. If not provided, all properties will be bound. | ||
*/ | ||
bindTo(targetObject: any, properties?: Array<NonFunctionPropNames<T>>): void; | ||
@@ -41,4 +62,23 @@ } & { | ||
type CollectionCallback<K, V> = { | ||
/** | ||
* Trigger callback when an item has been added to the collection. | ||
* | ||
* @param callback | ||
* @param immediate | ||
*/ | ||
onAdd(callback: (item: V, index: K) => void, immediate?: boolean): void; | ||
/** | ||
* Trigger callback when an item has been removed to the collection. | ||
* | ||
* @param callback | ||
*/ | ||
onRemove(callback: (item: V, index: K) => void): void; | ||
// /** | ||
// * Trigger callback when an item has been removed to the collection. | ||
// * | ||
// * @param callback | ||
// */ | ||
// onChange(callback: (item: V, index: K) => void): void; | ||
}; | ||
@@ -54,3 +94,8 @@ | ||
export function getStateCallbacks<T extends Schema>(decoder: Decoder<T>): CallbackProxy<T> { | ||
export function getStateCallbacks<T extends Schema>( | ||
decoder: Decoder<T> | ||
): { | ||
$: (<F extends Schema>(instance: F) => CallbackProxy<F>), | ||
$state: CallbackProxy<T>, | ||
} { | ||
const $root = decoder.root; | ||
@@ -91,27 +136,25 @@ const callbacks = $root.callbacks; | ||
if (!uniqueRefIds.has(refId)) { | ||
try { | ||
// trigger onChange | ||
const replaceCallbacks = $callbacks?.[OPERATION.REPLACE]; | ||
for (let i = replaceCallbacks?.length - 1; i >= 0; i--) { | ||
replaceCallbacks[i](); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
// trigger onChange | ||
const replaceCallbacks = $callbacks?.[OPERATION.REPLACE]; | ||
for (let i = replaceCallbacks?.length - 1; i >= 0; i--) { | ||
replaceCallbacks[i](); | ||
// try { | ||
// } catch (e) { | ||
// console.error(e); | ||
// } | ||
} | ||
} | ||
try { | ||
if ($callbacks.hasOwnProperty(change.field)) { | ||
const fieldCallbacks = $callbacks[change.field]; | ||
for (let i = fieldCallbacks?.length - 1; i >= 0; i--) { | ||
fieldCallbacks[i](change.value, change.previousValue); | ||
} | ||
if ($callbacks.hasOwnProperty(change.field)) { | ||
const fieldCallbacks = $callbacks[change.field]; | ||
for (let i = fieldCallbacks?.length - 1; i >= 0; i--) { | ||
fieldCallbacks[i](change.value, change.previousValue); | ||
// try { | ||
// } catch (e) { | ||
// console.error(e); | ||
// } | ||
} | ||
} catch (e) { | ||
// | ||
console.error(e); | ||
} | ||
} else { | ||
@@ -214,8 +257,16 @@ // | ||
bindTo: function bindTo(targetObject: any, properties?: string[]) { | ||
// return $root.addCallback( | ||
// $root.refIds.get(context.instance), | ||
// OPERATION.BIND, | ||
// callback | ||
// ); | ||
console.log("bindTo", targetObject, properties); | ||
// | ||
// TODO: refactor this implementation. There is room for improvement here. | ||
// | ||
if (!properties) { | ||
properties = Object.keys(metadata); | ||
} | ||
return $root.addCallback( | ||
$root.refIds.get(context.instance), | ||
OPERATION.REPLACE, | ||
() => { | ||
properties.forEach((prop) => | ||
targetObject[prop] = context.instance[prop]) | ||
} | ||
); | ||
} | ||
@@ -322,3 +373,6 @@ }, { | ||
return $(decoder.state); | ||
return { | ||
$, | ||
$state: $(decoder.state), | ||
}; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
2566639
30885