/
update.ts
58 lines (55 loc) · 2.18 KB
/
update.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Update
*/
import { invariant, isModelSchema, GUARDED_NOOP } from "../utils/utils";
import getDefaultModelSchema from "../api/getDefaultModelSchema";
import Context from "./Context";
import { deserializePropsWithSchema } from "./deserialize";
import { ClazzOrModelSchema } from "../api/types";
/**
* Similar to deserialize, but updates an existing object instance.
* Properties will always updated entirely, but properties not present in the json will be kept as is.
* Further this method behaves similar to deserialize.
*
* @param modelSchema, optional if it can be inferred from the instance type
* @param target target instance to update
* @param json the json to deserialize
* @param callback the callback to invoke once deserialization has completed.
* @param customArgs custom arguments that are available as `context.args` during the deserialization process. This can be used as dependency injection mechanism to pass in, for example, stores.
* @returns deserialized object, possibly incomplete.
*/
export default function update<T>(
modelschema: ClazzOrModelSchema<T>,
instance: T,
json: any,
callback?: (err: any, result: T) => void,
customArgs?: any
): void;
export default function update<T>(
instance: T,
json: any,
callback?: (err: any, result: T) => void,
customArgs?: any
): void;
export default function update(...args: any[]) {
let modelSchema;
if (typeof args[0] === "function" || isModelSchema(args[0])) {
// First overload
modelSchema = getDefaultModelSchema(args[0]);
args.shift();
} else {
modelSchema = getDefaultModelSchema(args[0]);
}
const [target, json, callback, customArgs] = args;
invariant(isModelSchema(modelSchema), "update failed to determine schema");
invariant(
typeof target === "object" && target && !Array.isArray(target),
"update needs an object"
);
const context = new Context(undefined, modelSchema, json, callback || GUARDED_NOOP, customArgs);
context.setTarget(target);
const lock = context.createCallback(GUARDED_NOOP);
const result = deserializePropsWithSchema(context, modelSchema, json, target);
lock();
return result;
}